From f726a2a5d842b04614ff19aa22d7d9a6c3bd1295 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Thu, 22 Aug 2002 16:10:47 +0000 Subject: Remove an e_path directory, and its parent "subfolders" dir if it's now * e-path.c (e_path_rmdir): Remove an e_path directory, and its parent "subfolders" dir if it's now empty. svn path=/trunk/; revision=17837 --- e-util/ChangeLog | 5 +++++ e-util/e-path.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ e-util/e-path.h | 1 + 3 files changed, 54 insertions(+) diff --git a/e-util/ChangeLog b/e-util/ChangeLog index 53cdc63750..c1b916be6f 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,8 @@ +2002-08-22 Dan Winship + + * e-path.c (e_path_rmdir): Remove an e_path directory, and its + parent "subfolders" dir if it's now empty. + 2002-08-22 JP Rosevear * e-time-utils.c (e_time_parse_date): if the year was two digits, 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 #include #include +#include #include #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; +} diff --git a/e-util/e-path.h b/e-util/e-path.h index a40cf9fb16..2295889778 100644 --- a/e-util/e-path.h +++ b/e-util/e-path.h @@ -32,4 +32,5 @@ gboolean e_path_find_folders (const char *prefix, EPathFindFoldersCallback callback, gpointer data); +int e_path_rmdir (const char *prefix, const char *vpath); #endif /* __E_PATH__ */ -- cgit