[Bf-blender-cvs] [d0c5eac] master: BLI_fileops: Make BLI_dir_create_recursive() return success/error status.

Bastien Montagne noreply at git.blender.org
Tue Jul 14 18:58:07 CEST 2015


Commit: d0c5eac4b7e5dc3173ee354f6f0afdd0d1e078dc
Author: Bastien Montagne
Date:   Tue Jul 14 18:36:48 2015 +0200
Branches: master
https://developer.blender.org/rBd0c5eac4b7e5dc3173ee354f6f0afdd0d1e078dc

BLI_fileops: Make BLI_dir_create_recursive() return success/error status.

Handy to know directly whether a file creation succeeded or not.

Also, made a few more changes in this func, and harmonized a bit win32/*nix versions.

===================================================================

M	source/blender/blenlib/BLI_fileops.h
M	source/blender/blenlib/intern/fileops.c

===================================================================

diff --git a/source/blender/blenlib/BLI_fileops.h b/source/blender/blenlib/BLI_fileops.h
index 01aa5d3..fa5a4ac 100644
--- a/source/blender/blenlib/BLI_fileops.h
+++ b/source/blender/blenlib/BLI_fileops.h
@@ -85,7 +85,7 @@ struct direntry;
 
 bool   BLI_is_dir(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 bool   BLI_is_file(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
-void   BLI_dir_create_recursive(const char *dir) ATTR_NONNULL();
+bool   BLI_dir_create_recursive(const char *dir) ATTR_NONNULL();
 double BLI_dir_free_space(const char *dir) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
 char  *BLI_current_working_dir(char *dir, const size_t maxlen) ATTR_NONNULL();
 
diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c
index 0bef763..5232c99 100644
--- a/source/blender/blenlib/intern/fileops.c
+++ b/source/blender/blenlib/intern/fileops.c
@@ -432,10 +432,12 @@ int BLI_create_symlink(const char *file, const char *to)
 	return 1;
 }
 
-void BLI_dir_create_recursive(const char *dirname)
+/** \return true on success (i.e. given path now exists on FS), false otherwise. */
+bool BLI_dir_create_recursive(const char *dirname)
 {
 	char *lslash;
 	char tmp[MAXPATHLEN];
+	bool ret = true;
 
 	/* First remove possible slash at the end of the dirname.
 	 * This routine otherwise tries to create
@@ -443,30 +445,35 @@ void BLI_dir_create_recursive(const char *dirname)
 	 * blah1/blah2 (without slash) */
 
 	BLI_strncpy(tmp, dirname, sizeof(tmp));
-	lslash = (char *)BLI_last_slash(tmp);
-
-	if (lslash && (*(lslash + 1) == '\0')) {
-		*lslash = '\0';
-	}
+	BLI_del_slash(tmp);
 
 	/* check special case "c:\foo", don't try create "c:", harmless but prints an error below */
 	if (isalpha(tmp[0]) && (tmp[1] == ':') && tmp[2] == '\0') return;
 
-	if (BLI_exists(tmp)) return;
+	if (BLI_is_dir(tmp)) {
+		return true;
+	}
+	else if (BLI_exists(tmp)) {
+		return false;
+	}
 
 	lslash = (char *)BLI_last_slash(tmp);
 
 	if (lslash) {
 		/* Split about the last slash and recurse */
 		*lslash = 0;
-		BLI_dir_create_recursive(tmp);
+		if (!BLI_dir_create_recursive(tmp)) {
+			ret = false;
+		}
 	}
 
-	if (dirname[0]) {  /* patch, this recursive loop tries to create a nameless directory */
+	if (ret && dirname[0]) {  /* patch, this recursive loop tries to create a nameless directory */
 		if (umkdir(dirname) == -1) {
 			printf("Unable to create directory %s\n", dirname);
+			ret = false;
 		}
 	}
+	return ret;
 }
 
 int BLI_rename(const char *from, const char *to)
@@ -964,7 +971,8 @@ int BLI_create_symlink(const char *file, const char *to)
 	return symlink(to, file);
 }
 
-void BLI_dir_create_recursive(const char *dirname)
+/** \return true on success (i.e. given path now exists on FS), false otherwise. */
+bool BLI_dir_create_recursive(const char *dirname)
 {
 	char *lslash;
 	size_t size;
@@ -972,8 +980,14 @@ void BLI_dir_create_recursive(const char *dirname)
 	char static_buf[MAXPATHLEN];
 #endif
 	char *tmp;
+	bool ret = true;
 
-	if (BLI_exists(dirname)) return;
+	if (BLI_is_dir(dirname)) {
+		return true;
+	}
+	else if (BLI_exists(dirname)) {
+		return false;
+	}
 
 #ifdef MAXPATHLEN
 	size = MAXPATHLEN;
@@ -985,18 +999,26 @@ void BLI_dir_create_recursive(const char *dirname)
 
 	BLI_strncpy(tmp, dirname, size);
 		
+	/* Avoids one useless recursion in case of '/foo/bar/' path... */
+	BLI_del_slash(tmp);
+
 	lslash = (char *)BLI_last_slash(tmp);
 	if (lslash) {
 		/* Split about the last slash and recurse */
 		*lslash = 0;
-		BLI_dir_create_recursive(tmp);
+		if (!BLI_dir_create_recursive(tmp)) {
+			ret = false;
+		}
 	}
 
 #ifndef MAXPATHLEN
 	MEM_freeN(tmp);
 #endif
 
-	mkdir(dirname, 0777);
+	if (ret) {
+		ret = (mkdir(dirname, 0777) == 0);
+	}
+	return ret;
 }
 
 int BLI_rename(const char *from, const char *to)




More information about the Bf-blender-cvs mailing list