Check the non-existence of folders before creating them

This commit is contained in:
Alexander Butenko 2009-10-20 18:24:24 +02:00 committed by Christian Dywan
parent 10b6cbb50a
commit 90a4acfc6e
6 changed files with 86 additions and 8 deletions

View file

@ -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, ".");

View file

@ -1,5 +1,5 @@
/*
Copyright (C) 2007-2008 Christian Dywan <christian@twotoasts.de>
Copyright (C) 2007-2009 Christian Dywan <christian@twotoasts.de>
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 <glib/gstdio.h>
#include <glib/gi18n.h>
#include <string.h>
@ -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;
}

View file

@ -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__ */

View file

@ -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);
}

View file

@ -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);

View file

@ -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 ();