changeset 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 92d05dd1047f
children 62d11301b8a6
files src/log.c src/util.c src/util.h
diffstat 3 files changed, 65 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/log.c	Sun Nov 23 18:21:03 2003 +0000
+++ b/src/log.c	Sun Nov 23 18:41:11 2003 +0000
@@ -339,20 +339,9 @@
 
 		strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.xml", localtime(&log->time));
 
-		dir = g_build_filename(ud, "logs", NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
-		dir = g_build_filename(ud, "logs",
-				       prpl, NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
-		dir = g_build_filename(ud, "logs",
-				       prpl, guy, NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
 		dir = g_build_filename(ud, "logs",
 				       prpl, guy, gaim_normalize(log->account, log->name), NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
+		gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
 		g_free(guy);
 
 		char *filename = g_build_filename(dir, date, NULL);
@@ -442,20 +431,9 @@
 
 		strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.html", localtime(&log->time));
 
-		dir = g_build_filename(ud, "logs", NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
-		dir = g_build_filename(ud, "logs",
-				       prpl, NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
-		dir = g_build_filename(ud, "logs",
-				       prpl, guy, NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
 		dir = g_build_filename(ud, "logs",
 				       prpl, guy, gaim_normalize(log->account, log->name), NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
+		gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
 		g_free(guy);
 
 		filename = g_build_filename(dir, date, NULL);
@@ -584,20 +562,9 @@
 		}
 		strftime(date, sizeof(date), "%Y-%m-%d.%H%M%S.txt", localtime(&log->time));
 
-		dir = g_build_filename(ud, "logs", NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
-		dir = g_build_filename(ud, "logs",
-				       prpl, NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
-		dir = g_build_filename(ud, "logs",
-				       prpl, guy, NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
-		g_free(dir);
 		dir = g_build_filename(ud, "logs",
 				       prpl, guy, gaim_normalize(log->account, log->name), NULL);
-		mkdir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
+		gaim_build_dir (dir, S_IRUSR | S_IWUSR | S_IXUSR);
 		g_free(guy);
 
 		filename = g_build_filename(dir, date, NULL);
--- 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
--- a/src/util.h	Sun Nov 23 18:21:03 2003 +0000
+++ b/src/util.h	Sun Nov 23 18:41:11 2003 +0000
@@ -252,6 +252,19 @@
 char *gaim_user_dir(void);
 
 /**
+ * Builds a complete path from the root, making any directories along
+ * the path which do not already exist.
+ * 
+ * @param path The path you wish to create.  Note that it must start
+ *        from the root or this function will fail.
+ * 
+ * @param mode Unix-style permissions for this directory.
+ * 
+ * @return 0 for success, nonzero on any error.
+ */
+int gaim_build_dir(char *path, int mode);
+
+/**
  * Creates a temporary file and returns a file pointer to it.
  *
  * This is like mkstemp(), but returns a file pointer and uses a