diff options
Diffstat (limited to 'e-util/e-path.c')
-rw-r--r-- | e-util/e-path.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/e-util/e-path.c b/e-util/e-path.c index 06ac05ef25..06b75686b1 100644 --- a/e-util/e-path.c +++ b/e-util/e-path.c @@ -23,6 +23,7 @@ #include <dirent.h> #include <string.h> #include <sys/stat.h> +#include <unistd.h> #include <glib.h> #include "e-path.h" @@ -204,3 +205,50 @@ e_path_find_folders (const char *prefix, { return find_folders_recursive (prefix, "", callback, data); } + + +/** + * e_path_rmdir: + * @prefix: a prefix to prepend to the path, or %NULL + * @path: the virtual path to convert to a filesystem path. + * + * This removes the directory pointed to by @prefix and @path + * and attempts to remove its parent "subfolders" directory too + * if it's empty. + * + * Return value: -1 (with errno set) if it failed to rmdir the + * specified directory. 0 otherwise, whether or not it removed + * the parent directory. + **/ +int +e_path_rmdir (const char *prefix, const char *vpath) +{ + char *physical_path, *p; + + /* Remove the directory itself */ + physical_path = e_path_to_physical (prefix, vpath); + if (rmdir (physical_path) == -1) { + g_free (physical_path); + return -1; + } + + /* Attempt to remove its parent "subfolders" directory, + * ignoring errors since it might not be empty. + */ + + p = strrchr (physical_path, '/'); + if (p[1] == '\0') { + g_free (physical_path); + return 0; + } + *p = '\0'; + p = strrchr (physical_path, '/'); + if (!p || strcmp (p + 1, SUBFOLDER_DIR_NAME) != 0) { + g_free (physical_path); + return 0; + } + + rmdir (physical_path); + g_free (physical_path); + return 0; +} |