Skip to content

Commit

Permalink
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exist…
Browse files Browse the repository at this point in the history
…s().
  • Loading branch information
Laurent Monin committed Nov 15, 2008
1 parent 8b10477 commit 6419843
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/bar_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static gint comment_legacy_write(FileData *fd, GList *keywords, const gchar *com
mode_t mode = 0755;

comment_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
if (cache_ensure_dir_exists(comment_dir, mode))
if (recursive_mkdir_if_not_exists(comment_dir, mode))
{
gchar *filename = g_strconcat(fd->name, GQ_CACHE_EXT_METADATA, NULL);

Expand Down
2 changes: 1 addition & 1 deletion src/cache-loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ static gboolean cache_loader_process(CacheLoader *cl)
mode_t mode = 0755;

base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
if (cache_ensure_dir_exists(base, mode))
if (recursive_mkdir_if_not_exists(base, mode))
{
g_free(cl->cd->path);
cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, TRUE, NULL);
Expand Down
34 changes: 0 additions & 34 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,40 +582,6 @@ gint cache_sim_data_filled(ImageSimilarityData *sd)
*-------------------------------------------------------------------
*/

/* warning: this func modifies path string contents!, on fail it is set to fail point */
gint cache_ensure_dir_exists(gchar *path, mode_t mode)
{
if (!path) return FALSE;

if (!isdir(path))
{
gchar *p = path;
while (p[0] != '\0')
{
p++;
if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
{
gint end = TRUE;
if (p[0] != '\0')
{
p[0] = '\0';
end = FALSE;
}
if (!isdir(path))
{
DEBUG_1("creating sub dir:%s", path);
if (!mkdir_utf8(path, mode))
{
log_printf("create dir failed: %s\n", path);
return FALSE;
}
}
if (!end) p[0] = G_DIR_SEPARATOR;
}
}
}
return TRUE;
}

static void cache_path_parts(CacheType type,
const gchar **cache_rc, const gchar **cache_local, const gchar **cache_ext)
Expand Down
2 changes: 0 additions & 2 deletions src/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ void cache_sim_data_set_md5sum(CacheData *cd, guchar digest[16]);
void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd);
gint cache_sim_data_filled(ImageSimilarityData *sd);


gint cache_ensure_dir_exists(gchar *path, mode_t mode);
gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode);
gchar *cache_find_location(CacheType type, const gchar *source);

Expand Down
6 changes: 3 additions & 3 deletions src/cache_maint.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ static void cache_maint_moved(FileData *fd)
if (!src || !dest) return;

base = cache_get_location(CACHE_TYPE_THUMB, dest, FALSE, &mode);
if (cache_ensure_dir_exists(base, mode))
if (recursive_mkdir_if_not_exists(base, mode))
{
gchar *buf;
gchar *d;
Expand All @@ -548,7 +548,7 @@ static void cache_maint_moved(FileData *fd)
g_free(base);

base = cache_get_location(CACHE_TYPE_METADATA, dest, FALSE, &mode);
if (cache_ensure_dir_exists(base, mode))
if (recursive_mkdir_if_not_exists(base, mode))
{
gchar *buf;
gchar *d;
Expand Down Expand Up @@ -603,7 +603,7 @@ static void cache_maint_copied(FileData *fd)
if (!src_cache) return;

dest_base = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest, FALSE, &mode);
if (cache_ensure_dir_exists(dest_base, mode))
if (recursive_mkdir_if_not_exists(dest_base, mode))
{
gchar *path;

Expand Down
2 changes: 1 addition & 1 deletion src/dupe.c
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ static void dupe_item_write_cache(DupeItem *di)
if (!di) return;

base = cache_get_location(CACHE_TYPE_SIM, di->fd->path, FALSE, &mode);
if (cache_ensure_dir_exists(base, mode))
if (recursive_mkdir_if_not_exists(base, mode))
{
CacheData *cd;

Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ static void mkdir_if_not_exists(const gchar *path)

log_printf(_("Creating %s dir:%s\n"), GQ_APPNAME, path);

if (!mkdir_utf8(path, 0755))
if (!recursive_mkdir_if_not_exists(path, 0755))
{
log_printf(_("Could not create dir:%s\n"), path);
}
Expand Down
2 changes: 1 addition & 1 deletion src/pan-view.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ static gint pan_cache_step(PanWindow *pw)
mode_t mode = 0755;

base = cache_get_location(CACHE_TYPE_SIM, fd->path, FALSE, &mode);
if (cache_ensure_dir_exists(base, mode))
if (recursive_mkdir_if_not_exists(base, mode))
{
g_free(cd->path);
cd->path = cache_get_location(CACHE_TYPE_SIM, fd->path, TRUE, NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@ static void search_file_load_process(SearchData *sd, CacheData *cd)

path = image_loader_get_fd(sd->img_loader)->path;
base = cache_get_location(CACHE_TYPE_SIM, path, FALSE, &mode);
if (cache_ensure_dir_exists(base, mode))
if (recursive_mkdir_if_not_exists(base, mode))
{
g_free(cd->path);
cd->path = cache_get_location(CACHE_TYPE_SIM, path, TRUE, NULL);
Expand Down
2 changes: 1 addition & 1 deletion src/thumb.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static gboolean thumb_loader_save_thumbnail(ThumbLoader *tl, gboolean mark_failu

cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->fd->path, FALSE, &mode);

if (cache_ensure_dir_exists(cache_dir, mode))
if (recursive_mkdir_if_not_exists(cache_dir, mode))
{
gchar *cache_path;
gchar *pathl;
Expand Down
5 changes: 2 additions & 3 deletions src/thumb_standard.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "main.h"
#include "thumb_standard.h"

#include "cache.h" /* for cache_ensure_dir_exists */
#include "image-load.h"
#include "md5-util.h"
#include "pixbuf_util.h"
Expand Down Expand Up @@ -319,14 +318,14 @@ static void thumb_loader_std_save(ThumbLoaderStd *tl, GdkPixbuf *pixbuf)
source_base = remove_level_from_path(tl->fd->path);
if (stat_utf8(source_base, &st))
{
cache_ensure_dir_exists(base_path, st.st_mode);
recursive_mkdir_if_not_exists(base_path, st.st_mode);
}
g_free(source_base);
}
}
else
{
cache_ensure_dir_exists(base_path, THUMB_PERMS_FOLDER);
recursive_mkdir_if_not_exists(base_path, THUMB_PERMS_FOLDER);
}
g_free(base_path);

Expand Down
45 changes: 45 additions & 0 deletions src/ui_fileops.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,4 +762,49 @@ gint file_in_path(const gchar *name)

return ret;
}

gboolean recursive_mkdir_if_not_exists(gchar *path, mode_t mode)
{
if (!path) return FALSE;

if (!isdir(path))
{
gchar *npath = g_strdup(path);
gchar *p = npath;

while (p[0] != '\0')
{
p++;
if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
{
gint end = TRUE;

if (p[0] != '\0')
{
p[0] = '\0';
end = FALSE;
}

if (!isdir(npath))
{
DEBUG_1("creating sub dir:%s", npath);
if (!mkdir_utf8(npath, mode))
{
log_printf("create dir failed: %s\n", npath);
g_free(npath);
return FALSE;
}
}

if (!end) p[0] = G_DIR_SEPARATOR;
}
}
g_free(npath);
}

return TRUE;
}



/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
2 changes: 2 additions & 0 deletions src/ui_fileops.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ void parse_out_relatives(gchar *path);

gint file_in_path(const gchar *name);

gboolean recursive_mkdir_if_not_exists(gchar *path, mode_t mode);

#endif
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */

0 comments on commit 6419843

Please sign in to comment.