BLI_file: split BLI_rename into two functions

Include the term "overwrite" so users of this function are aware
this function will delete the destination file (unlike LIBC rename).

- Add BLI_rename_overwrite (previously called BLI_rename).
- BLI_rename is now a wrapper for rename.

Use BLI_rename when renaming in the file selector.
This commit is contained in:
Campbell Barton 2023-05-16 13:13:05 +10:00
parent d2a3689b4a
commit 802af4e04b
10 changed files with 31 additions and 20 deletions

View File

@ -840,12 +840,12 @@ bool AssetCatalogDefinitionFile::write_to_disk(const CatalogFilePath &dest_file_
return false;
}
if (BLI_exists(dest_file_path.c_str())) {
if (BLI_rename(dest_file_path.c_str(), backup_path.c_str())) {
if (BLI_rename_overwrite(dest_file_path.c_str(), backup_path.c_str())) {
/* TODO: communicate what went wrong. */
return false;
}
}
if (BLI_rename(writable_path.c_str(), dest_file_path.c_str())) {
if (BLI_rename_overwrite(writable_path.c_str(), dest_file_path.c_str())) {
/* TODO: communicate what went wrong. */
return false;
}

View File

@ -335,7 +335,7 @@ int BKE_packedfile_write_to_file(ReportList *reports,
if (remove_tmp) {
if (ret_value == RET_ERROR) {
if (BLI_rename(filepath_temp, filepath) != 0) {
if (BLI_rename_overwrite(filepath_temp, filepath) != 0) {
BKE_reportf(reports,
RPT_ERROR,
"Error restoring temp file (check files '%s' '%s')",

View File

@ -3531,7 +3531,7 @@ void BKE_ptcache_disk_cache_rename(PTCacheID *pid, const char *name_src, const c
if (frame != -1) {
BLI_path_join(old_path_full, sizeof(old_path_full), path, de->d_name);
ptcache_filepath(pid, new_path_full, frame, true, true);
BLI_rename(old_path_full, new_path_full);
BLI_rename_overwrite(old_path_full, new_path_full);
}
}
}

View File

@ -39,6 +39,14 @@ extern "C" {
*/
int BLI_exists(const char *path) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL();
int BLI_copy(const char *file, const char *to) ATTR_NONNULL();
/**
* Rename a file or directory.
*
* \return zero on success (matching 'rename' behavior).
*/
int BLI_rename(const char *from, const char *to);
/**
* Rename a file or directory.
*
@ -53,7 +61,7 @@ int BLI_copy(const char *file, const char *to) ATTR_NONNULL();
* This will be deleted if it already exists, unless it's a directory which will fail.
* \return zero on success (matching 'rename' behavior).
*/
int BLI_rename(const char *from, const char *to) ATTR_NONNULL();
int BLI_rename_overwrite(const char *from, const char *to) ATTR_NONNULL();
/**
* Deletes the specified file or directory (depending on dir), optionally
* doing recursive delete of directory contents.

View File

@ -380,6 +380,15 @@ bool BLI_file_ensure_parent_dir_exists(const char *filepath)
}
int BLI_rename(const char *from, const char *to)
{
#ifdef WIN32
return urename(from, to);
#else
return rename(from, to);
#endif
}
int BLI_rename_overwrite(const char *from, const char *to)
{
if (!BLI_exists(from)) {
return 1;
@ -404,13 +413,7 @@ int BLI_rename(const char *from, const char *to)
}
}
int ret;
#ifdef WIN32
ret = urename(from, to);
#else
ret = rename(from, to);
#endif
return ret;
return BLI_rename(from, to);
}
#ifdef WIN32

View File

@ -1383,7 +1383,7 @@ static bool do_history(const char *name, ReportList *reports)
if (BLI_exists(tempname1)) {
SNPRINTF(tempname2, "%s%d", name, hisnr);
if (BLI_rename(tempname1, tempname2)) {
if (BLI_rename_overwrite(tempname1, tempname2)) {
BKE_report(reports, RPT_ERROR, "Unable to make version backup");
return true;
}
@ -1395,7 +1395,7 @@ static bool do_history(const char *name, ReportList *reports)
if (BLI_exists(name)) {
SNPRINTF(tempname1, "%s%d", name, hisnr);
if (BLI_rename(name, tempname1)) {
if (BLI_rename_overwrite(name, tempname1)) {
BKE_report(reports, RPT_ERROR, "Unable to make version backup");
return true;
}
@ -1562,7 +1562,7 @@ bool BLO_write_file(Main *mainvar,
}
}
if (BLI_rename(tempname, filepath) != 0) {
if (BLI_rename_overwrite(tempname, filepath) != 0) {
BKE_report(reports, RPT_ERROR, "Cannot change old file (file saved with @)");
return false;
}

View File

@ -148,7 +148,7 @@ void IMB_index_builder_finish(anim_index_builder *fp, int rollback)
}
else {
unlink(fp->filepath);
BLI_rename(fp->filepath_temp, fp->filepath);
BLI_rename_overwrite(fp->filepath_temp, fp->filepath);
}
MEM_freeN(fp);
@ -784,7 +784,7 @@ static void free_proxy_output_ffmpeg(struct proxy_output_ctx *ctx, int rollback)
else {
get_proxy_filepath(ctx->anim, ctx->proxy_size, filepath, false);
unlink(filepath);
BLI_rename(filepath_tmp, filepath);
BLI_rename_overwrite(filepath_tmp, filepath);
}
MEM_freeN(ctx);

View File

@ -436,7 +436,7 @@ static ImBuf *thumb_create_ex(const char *file_path,
#endif
// printf("%s saving thumb: '%s'\n", __func__, tpath);
BLI_rename(temp, tpath);
BLI_rename_overwrite(temp, tpath);
}
}
return img;

View File

@ -302,7 +302,7 @@ int DocumentExporter::exportCurrentScene()
/* Finally move the created document into place */
fprintf(stdout, "Collada export to: %s\n", this->export_settings.get_filepath());
int status = BLI_rename(native_filename.c_str(), this->export_settings.get_filepath());
int status = BLI_rename_overwrite(native_filename.c_str(), this->export_settings.get_filepath());
if (status != 0) {
status = BLI_copy(native_filename.c_str(), this->export_settings.get_filepath());
BLI_delete(native_filename.c_str(), false, false);

View File

@ -2144,7 +2144,7 @@ void wm_autosave_delete(void)
BLI_delete(filepath, false, false);
}
else {
BLI_rename(filepath, str);
BLI_rename_overwrite(filepath, str);
}
}
}