diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-08-24 23:09:01 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-08-24 23:14:38 +0300 |
commit | c5018c65b97c93810c7c90f5db11ae1cc10900f3 (patch) | |
tree | 0a08f22e31270f19324254497036d9c45e6a45a9 /source/blender/blenlib/intern | |
parent | e8a0478c63c5f611d086920976815f7d78e915e3 (diff) |
BLI_fileops: Some fixes and cleanup.
* Fix BLI_file_touch, used to add one dummy byte ((unsigned char)EOF) to empty files!
* Get rid of static global temp string in WIN32 area (very bad, and useless!).
* Get rid of paranoid NULL checks in WIN32's BLI_gzopen().
* Add non-relative filename asserts to WIN32 file operations too.
* ifdef-out BLI_move and BLI_create_symlink, unused (and the later is not even implemented for windows).
Partly based on patch and points raised by Jason Wilkins (jwilkins) in T32870, thanks!
Diffstat (limited to 'source/blender/blenlib/intern')
-rw-r--r-- | source/blender/blenlib/intern/fileops.c | 66 |
1 files changed, 43 insertions, 23 deletions
diff --git a/source/blender/blenlib/intern/fileops.c b/source/blender/blenlib/intern/fileops.c index bb75db7f11e..bc952165b61 100644 --- a/source/blender/blenlib/intern/fileops.c +++ b/source/blender/blenlib/intern/fileops.c @@ -193,15 +193,25 @@ bool BLI_file_is_writable(const char *filename) /** * Creates the file with nothing in it, or updates its last-modified date if it already exists. - * Returns true if successful. (like the unix touch command) + * Returns true if successful (like the unix touch command). */ bool BLI_file_touch(const char *file) { FILE *f = BLI_fopen(file, "r+b"); + if (f != NULL) { int c = getc(f); - rewind(f); - putc(c, f); + + if (c == EOF) { + /* Empty file, reopen in truncate write mode... */ + fclose(f); + f = BLI_fopen(file, "w+b"); + } + else { + /* Otherwise, rewrite first byte. */ + rewind(f); + putc(c, f); + } } else { f = BLI_fopen(file, "wb"); @@ -220,10 +230,10 @@ static void callLocalErrorCallBack(const char *err) printf("%s\n", err); } -static char str[MAXPATHLEN + 12]; - FILE *BLI_fopen(const char *filename, const char *mode) { + BLI_assert(!BLI_path_is_rel(filename)); + return ufopen(filename, mode); } @@ -247,41 +257,42 @@ void *BLI_gzopen(const char *filename, const char *mode) { gzFile gzfile; - if (!filename || !mode) { - return 0; - } - else { - /* xxx Creates file before transcribing the path */ - if (mode[0] == 'w') - fclose(ufopen(filename, "a")); + BLI_assert(!BLI_path_is_rel(filename)); - /* temporary #if until we update all libraries to 1.2.7 - * for correct wide char path handling */ + /* xxx Creates file before transcribing the path */ + if (mode[0] == 'w') + fclose(ufopen(filename, "a")); + + /* temporary #if until we update all libraries to 1.2.7 + * for correct wide char path handling */ #if ZLIB_VERNUM >= 0x1270 && !defined(FREE_WINDOWS) - UTF16_ENCODE(filename); + UTF16_ENCODE(filename); - gzfile = gzopen_w(filename_16, mode); + gzfile = gzopen_w(filename_16, mode); - UTF16_UN_ENCODE(filename); + UTF16_UN_ENCODE(filename); #else - { - char short_name[256]; - BLI_get_short_name(short_name, filename); - gzfile = gzopen(short_name, mode); - } -#endif + { + char short_name[256]; + BLI_get_short_name(short_name, filename); + gzfile = gzopen(short_name, mode); } +#endif return gzfile; } int BLI_open(const char *filename, int oflag, int pmode) { + BLI_assert(!BLI_path_is_rel(filename)); + return uopen(filename, oflag, pmode); } int BLI_access(const char *filename, int mode) { + BLI_assert(!BLI_path_is_rel(filename)); + return uaccess(filename, mode); } @@ -351,6 +362,8 @@ int BLI_delete(const char *file, bool dir, bool recursive) { int err; + BLI_assert(!BLI_path_is_rel(file)); + if (recursive) { err = delete_recursive(file); } @@ -365,6 +378,7 @@ int BLI_delete(const char *file, bool dir, bool recursive) #if 0 int BLI_move(const char *file, const char *to) { + char str[MAXPATHLEN + 12]; int err; /* windows doesn't support moving to a directory @@ -396,6 +410,7 @@ int BLI_move(const char *file, const char *to) int BLI_copy(const char *file, const char *to) { + char str[MAXPATHLEN + 12]; int err; /* windows doesn't support copying to a directory @@ -424,13 +439,16 @@ int BLI_copy(const char *file, const char *to) return err; } +#if 0 int BLI_create_symlink(const char *file, const char *to) { + /* See patch from T30870, should this ever become needed. */ callLocalErrorCallBack("Linking files is unsupported on Windows"); (void)file; (void)to; return 1; } +#endif /** \return true on success (i.e. given path now exists on FS), false otherwise. */ bool BLI_dir_create_recursive(const char *dirname) @@ -968,10 +986,12 @@ int BLI_copy(const char *file, const char *to) return ret; } +#if 0 int BLI_create_symlink(const char *file, const char *to) { return symlink(to, file); } +#endif /** \return true on success (i.e. given path now exists on FS), false otherwise. */ bool BLI_dir_create_recursive(const char *dirname) |