From 90a4acfc6eb4c321e76fb408408923836afe170a Mon Sep 17 00:00:00 2001 From: Alexander Butenko Date: Tue, 20 Oct 2009 18:24:24 +0200 Subject: [PATCH] Check the non-existence of folders before creating them --- katze/katze-net.c | 2 +- katze/katze-utils.c | 76 ++++++++++++++++++++++++++++++++++++- katze/katze-utils.h | 4 ++ midori/main.c | 2 +- midori/midori-extension.c | 8 ++-- midori/midori-websettings.c | 2 +- 6 files changed, 86 insertions(+), 8 deletions(-) diff --git a/katze/katze-net.c b/katze/katze-net.c index e445aa8d..83ef0519 100644 --- a/katze/katze-net.c +++ b/katze/katze-net.c @@ -157,7 +157,7 @@ katze_net_get_cached_path (KatzeNet* net, cache_path = g_build_filename (net->cache_path, subfolder, NULL); else cache_path = net->cache_path; - g_mkdir_with_parents (cache_path, 0700); + katze_mkdir_with_parents (cache_path, 0700); checksum = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); extension = g_strrstr (uri, "."); diff --git a/katze/katze-utils.c b/katze/katze-utils.c index d5b55aea..7188dbf8 100644 --- a/katze/katze-utils.c +++ b/katze/katze-utils.c @@ -1,5 +1,5 @@ /* - Copyright (C) 2007-2008 Christian Dywan + Copyright (C) 2007-2009 Christian Dywan This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -11,6 +11,7 @@ #include "katze-utils.h" +#include #include #include @@ -980,3 +981,76 @@ katze_object_get_object (gpointer object, g_object_get (object, property, &value, NULL); return value; } + +/** + * katze_mkdir_with_parents: + * @pathname: a pathname in the GLib file name encoding + * @mode: permissions to use for newly created directories + * + * Create a directory if it doesn't already exist. Create intermediate + * parent directories as needed, too. + * + * Similar to g_mkdir_with_parents() but returning early if the + * @pathname refers to an existing directory. + * + * Returns: 0 if the directory already exists, or was successfully + * created. Returns -1 if an error occurred, with errno set. + * + * Since: 0.2.1 + */ +/* Creating directories recursively + Copyright 2000 Red Hat, Inc. + Originally copied from Glib 2.20, coding style adjusted + Modified to determine file existence early and pathname must be != NULL */ +int +katze_mkdir_with_parents (const gchar* pathname, + int mode) +{ + gchar* fn, *p; + + if (g_file_test (pathname, G_FILE_TEST_EXISTS)) + return 0; + + fn = g_strdup (pathname); + + if (g_path_is_absolute (fn)) + p = (gchar *) g_path_skip_root (fn); + else + p = fn; + + do + { + while (*p && !G_IS_DIR_SEPARATOR (*p)) + p++; + + if (!*p) + p = NULL; + else + *p = '\0'; + + if (!g_file_test (fn, G_FILE_TEST_EXISTS)) + { + if (g_mkdir (fn, mode) == -1) + { + g_free (fn); + return -1; + } + } + else if (!g_file_test (fn, G_FILE_TEST_IS_DIR)) + { + g_free (fn); + return -1; + } + if (p) + { + *p++ = G_DIR_SEPARATOR; + while (*p && G_IS_DIR_SEPARATOR (*p)) + p++; + } + } + while (p); + + g_free (fn); + + return 0; +} diff --git a/katze/katze-utils.h b/katze/katze-utils.h index 3cd418d6..8e675207 100644 --- a/katze/katze-utils.h +++ b/katze/katze-utils.h @@ -140,6 +140,10 @@ gpointer katze_object_get_object (gpointer object, const gchar* property); +int +katze_mkdir_with_parents (const gchar* pathname, + int mode); + G_END_DECLS #endif /* __KATZE_UTILS_H__ */ diff --git a/midori/main.c b/midori/main.c index b4ed755b..ac0cece3 100644 --- a/midori/main.c +++ b/midori/main.c @@ -67,7 +67,7 @@ static gchar* build_config_filename (const gchar* filename) { const gchar* path = sokoke_set_config_dir (NULL); - g_mkdir_with_parents (path, 0700); + katze_mkdir_with_parents (path, 0700); return g_build_filename (path, filename, NULL); } diff --git a/midori/midori-extension.c b/midori/midori-extension.c index 9eceea8b..f04ff2af 100644 --- a/midori/midori-extension.c +++ b/midori/midori-extension.c @@ -658,7 +658,7 @@ midori_extension_set_boolean (MidoriExtension* extension, /* FIXME: Handle readonly folder/ file */ gchar* config_file = g_build_filename (extension->priv->config_dir, "config", NULL); - g_mkdir_with_parents (extension->priv->config_dir, 0700); + katze_mkdir_with_parents (extension->priv->config_dir, 0700); g_key_file_set_boolean (extension->priv->key_file, "settings", name, value); sokoke_key_file_save_to_file (extension->priv->key_file, config_file, &error); @@ -755,7 +755,7 @@ midori_extension_set_integer (MidoriExtension* extension, /* FIXME: Handle readonly folder/ file */ gchar* config_file = g_build_filename (extension->priv->config_dir, "config", NULL); - g_mkdir_with_parents (extension->priv->config_dir, 0700); + katze_mkdir_with_parents (extension->priv->config_dir, 0700); g_key_file_set_integer (extension->priv->key_file, "settings", name, value); sokoke_key_file_save_to_file (extension->priv->key_file, config_file, &error); @@ -852,7 +852,7 @@ midori_extension_set_string (MidoriExtension* extension, /* FIXME: Handle readonly folder/ file */ gchar* config_file = g_build_filename (extension->priv->config_dir, "config", NULL); - g_mkdir_with_parents (extension->priv->config_dir, 0700); + katze_mkdir_with_parents (extension->priv->config_dir, 0700); g_key_file_set_string (extension->priv->key_file, "settings", name, value); sokoke_key_file_save_to_file (extension->priv->key_file, config_file, &error); @@ -964,7 +964,7 @@ midori_extension_set_string_list (MidoriExtension* extension, /* FIXME: Handle readonly folder/ file */ gchar* config_file = g_build_filename (extension->priv->config_dir, "config", NULL); - g_mkdir_with_parents (extension->priv->config_dir, 0700); + katze_mkdir_with_parents (extension->priv->config_dir, 0700); g_key_file_set_string_list (extension->priv->key_file, "settings", name, (const gchar**)value, length); sokoke_key_file_save_to_file (extension->priv->key_file, config_file, &error); diff --git a/midori/midori-websettings.c b/midori/midori-websettings.c index 336a14fb..4ac8fcc3 100644 --- a/midori/midori-websettings.c +++ b/midori/midori-websettings.c @@ -320,7 +320,7 @@ midori_get_download_dir (void) const gchar* dir = g_get_user_special_dir (G_USER_DIRECTORY_DOWNLOAD); if (dir) { - g_mkdir_with_parents (dir, 0700); + katze_mkdir_with_parents (dir, 0700); return dir; } return g_get_home_dir (); -- 2.39.5