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!
This commit is contained in:
Bastien Montagne 2015-08-24 22:09:01 +02:00
parent e8a0478c63
commit c5018c65b9
2 changed files with 45 additions and 23 deletions

@ -58,8 +58,10 @@ int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
int BLI_copy(const char *path, const char *to) ATTR_NONNULL();
int BLI_rename(const char *from, const char *to) ATTR_NONNULL();
int BLI_delete(const char *path, bool dir, bool recursive) ATTR_NONNULL();
#if 0 /* Unused */
int BLI_move(const char *path, const char *to) ATTR_NONNULL();
int BLI_create_symlink(const char *path, const char *to) ATTR_NONNULL();
#endif
/* keep in sync with the definition of struct direntry in BLI_fileops_types.h */
#ifdef WIN32

@ -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)