[PATCH 19/65] separate get_filepath() from create_filepath()

From: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Thu, 17 Apr 2014 16:57:03 +0200

- get_filepath should only get the pathname, not create the directories.
- create_dirtree check if the topdir exists and is a directory, if so,
  don't try all the combinations top-down to create the dirs.

Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
---
 ii.c | 45 ++++++++++++++++++++++++---------------------
 1 file changed, 24 insertions(+), 21 deletions(-)
diff --git a/ii.c b/ii.c
index eda920c..c193940 100644
--- a/ii.c
+++ b/ii.c
_AT_@ -58,42 +58,45 @@ striplower(char *s) {
 	return s;
 }
 
-/* creates directories top-down, if necessary */
+/* creates directories bottom-up, if necessary */
 static void
 create_dirtree(const char *dir) {
 	char tmp[_POSIX_PATH_MAX];
 	char *p = NULL;
+	struct stat st = { 0 };
 	size_t len;
 
 	strlcpy(tmp, dir, sizeof(tmp));
 	len = strnlen(tmp, sizeof(tmp));
 	if(len > 0 && tmp[len - 1] == '/')
-		tmp[len - 1] = 0;
-	for(p = tmp + 1; *p; p++)
-		if(*p == '/') {
-			*p = 0;
-			mkdir(tmp, S_IRWXU);
-			*p = '/';
-		}
-	mkdir(tmp, S_IRWXU);
-}
+		tmp[len - 1] = '\0';
 
-/* TODO: dont call create_dirtree() in get_filepath() ? */
-static int
-get_filepath(char *filepath, size_t len, char *channel, char *file) {
-	if(channel) {
-		if(snprintf(filepath, len, "%s/%s", path, channel) <= 0)
-			return 0;
-		create_dirtree(filepath);
-		return snprintf(filepath, len, "%s/%s/%s", path, channel, file);
+	if((stat(tmp, &st) != -1) && S_ISDIR(st.st_mode))
+		return; /* dir exists */
+
+	for(p = tmp + 1; *p; p++) {
+		if(*p != '/')
+			continue;
+		*p = '\0';
+		mkdir(tmp, S_IRWXU);
+		*p = '/';
 	}
-	return snprintf(filepath, len, "%s/%s", path, file);
+	mkdir(tmp, S_IRWXU);
 }
 
 static void
 create_filepath(char *filepath, size_t len, char *channel, char *suffix) {
-	if(get_filepath(filepath, len, striplower(channel), suffix) <= 0)
-		eprintf("path to irc directory too long");
+	const char *e = "path to irc directory too long\n";
+
+	if(channel && channel[0]) {
+		striplower(channel);
+		if(snprintf(filepath, len, "%s/%s", path, channel) <= 0)
+			eprintf(e);
+		create_dirtree(filepath);
+		if(snprintf(filepath, len, "%s/%s/%s", path, channel, suffix) <= 0)
+			eprintf(e);
+	} else if(snprintf(filepath, len, "%s/%s", path, suffix) <= 0)
+		eprintf(e);
 }
 
 static int
-- 
2.4.10
--Multipart=_Mon__9_May_2016_17_21_10_+0200_I.6cpFVydhq75aaE
Content-Type: text/x-diff;
 name="0020-stylistic-changes.patch"
Content-Disposition: attachment;
 filename="0020-stylistic-changes.patch"
Content-Transfer-Encoding: 7bit
Received on Mon Sep 17 2001 - 00:00:00 CEST

This archive was generated by hypermail 2.3.0 : Mon May 09 2016 - 17:24:22 CEST