diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-07-14 19:36:48 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-07-14 19:57:38 +0300 |
commit | d0c5eac4b7e5dc3173ee354f6f0afdd0d1e078dc (patch) | |
tree | 4639ebcc1971495fb5e50f743bf52c4fdb173ec8 /source/blender/blenlib/intern | |
parent | 38940662e540743a6a8da287390a02a9f3f76f6d (diff) |
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.
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r-- | source/blender/blenlib/intern/fileops.c | 48 |
1 files changed, 35 insertions, 13 deletions
diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index 0bef763b254..5232c99944a 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) |