diff src/util.c @ 7612:3ae88e96dde2

[gaim-migrate @ 8236] This is an attempt to both fix the directory creation permissions and simplify the creation of nested directories from within gaim at the same time. If you need nested directories, simply call gaim_build_dir with the same arguments you would have given to mkdir and it will recursively create the tree for you. I spent way too much time on this. committer: Tailor Script <tailor@pidgin.im>
author Ethan Blanton <elb@pidgin.im>
date Sun, 23 Nov 2003 18:41:11 +0000
parents 54b370f7d9bf
children 48d7eea88598
line wrap: on
line diff
--- a/src/util.c	Sun Nov 23 18:21:03 2003 +0000
+++ b/src/util.c	Sun Nov 23 18:41:11 2003 +0000
@@ -22,6 +22,10 @@
  */
 #include "internal.h"
 
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
 #include "conversation.h"
 #include "debug.h"
 #include "prpl.h"
@@ -1183,6 +1187,51 @@
 	return NULL;
 }
 
+int gaim_build_dir (char *path, int mode)
+{
+	struct stat st;
+	char *dir, **components, delim[] = { G_DIR_SEPARATOR, '\0' };
+	int cur, len;
+
+	if (path == NULL || path[0] != G_DIR_SEPARATOR)
+		return -1;
+
+	dir = g_new0(char, strlen(path) + 1);
+	components = g_strsplit(path, delim, -1);
+	len = 0;
+	for (cur = 0; components[cur] != NULL; cur++) {
+		dir[len++] = G_DIR_SEPARATOR;
+		strcpy(dir + len, components[cur]);
+		len += strlen(components[cur]);
+		if (stat(dir, &st) == 0) {
+			if ((st.st_mode & S_IFMT) == S_IFDIR)
+				continue;
+			else {
+				gaim_debug(GAIM_DEBUG_WARNING, "build_dir", "bad path: %s\n", path);
+				g_strfreev(components);
+				g_free(dir);
+				return -1;
+			}
+		} else if (errno != ENOENT) {
+			gaim_debug(GAIM_DEBUG_WARNING, "build_dir", "stat: %s\n", strerror(errno));
+			g_strfreev(components);
+			g_free(dir);
+			return -1;
+		}
+
+		if (mkdir(dir, mode) < 0) {
+			gaim_debug(GAIM_DEBUG_WARNING, "build_dir", "mkdir: %s\n", strerror(errno));
+			g_strfreev(components);
+			g_free(dir);
+			return -1;
+		}
+	}
+
+	g_strfreev(components);
+	g_free(dir);
+	return 0;
+}
+
 /*
  * Like mkstemp() but returns a file pointer, uses a pre-set template,
  * uses the semantics of tempnam() for the directory to use and allocates