From: <dg...@su...> - 2009-01-15 09:54:49
|
Author: friedrich.beckmann Date: Thu Jan 15 10:53:52 2009 New Revision: 5152 URL: http://www.opensync.org/changeset/5152 Log: windows port - g_unlink cannot remove a non-empty directory on windows Modified: trunk/opensync/common/opensync_file.c trunk/opensync/common/opensync_file.h trunk/opensync/group/opensync_updater.c Modified: trunk/opensync/common/opensync_file.c ============================================================================== --- trunk/opensync/common/opensync_file.c Thu Jan 15 10:51:55 2009 (r5151) +++ trunk/opensync/common/opensync_file.c Thu Jan 15 10:53:52 2009 (r5152) @@ -90,3 +90,47 @@ return ret; } +int osync_remove_directory_recursively(const char *dirname) +{ + GDir *gdir = NULL; + GError *gerror = NULL; + const char *gdir_entry = NULL; + char *gdir_entry_path = NULL; + + gdir = g_dir_open(dirname, 0, &gerror); + if (!gdir) + goto error; + while ((gdir_entry = g_dir_read_name(gdir))) { + gdir_entry_path = g_strdup_printf("%s%c%s", dirname, G_DIR_SEPARATOR, gdir_entry); + if (g_file_test(gdir_entry_path, G_FILE_TEST_IS_DIR)) { + if (osync_remove_directory_recursively(gdir_entry_path) < 0){ + g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", gdir_entry_path); + g_free(gdir_entry_path); + goto error; + } + }else{ + if (g_unlink(gdir_entry_path) < 0){ + g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", gdir_entry_path); + g_free(gdir_entry_path); + goto error; + } + } + g_free(gdir_entry_path); + } /* While */ + g_dir_close(gdir); + gdir = NULL; + if (g_rmdir(dirname) < 0){ + g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", dirname); + goto error; + } + + return 0; + +error: + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, gerror->message); + g_error_free(gerror); + if (gdir) + g_dir_close(gdir); + return -1; +} + Modified: trunk/opensync/common/opensync_file.h ============================================================================== --- trunk/opensync/common/opensync_file.h Thu Jan 15 10:51:55 2009 (r5151) +++ trunk/opensync/common/opensync_file.h Thu Jan 15 10:53:52 2009 (r5152) @@ -56,6 +56,18 @@ */ OSYNC_EXPORT osync_bool osync_file_read(const char *filename, char **data, unsigned int *size, OSyncError **error); +/*! @brief Removes a directory recursively + * + * Removes a directory recursively. This is an + * internal function for portability. + * + * @param dirname Directory which will be deleted + * @returns 0 if successful, -1 otherwise + * + */ + +int osync_remove_directory_recursively(const char *dirname); + /*@} */ #endif /* _OPENSYNC_FILE_H */ Modified: trunk/opensync/group/opensync_updater.c ============================================================================== --- trunk/opensync/group/opensync_updater.c Thu Jan 15 10:51:55 2009 (r5151) +++ trunk/opensync/group/opensync_updater.c Thu Jan 15 10:53:52 2009 (r5152) @@ -37,6 +37,7 @@ #include "opensync-group.h" #include "common/opensync_xml_internals.h" +#include "common/opensync_file.h" #include "opensync_updater.h" #include "opensync_updater_private.h" @@ -555,7 +556,7 @@ backup_groupdir = osync_strdup_printf("%s.bak", tmp); osync_free(tmp); } - if (g_unlink(groupdir) < 0) { + if (osync_remove_directory_recursively(groupdir) < 0) { g_set_error(&gerror, G_FILE_ERROR, g_file_error_from_errno(errno), "%s", groupdir); osync_error_set(&error, OSYNC_ERROR_GENERIC, "Could not remove current group directory: %s", gerror->message); g_error_free(gerror); |