From 1231cbba5d3b211625fa093f4d292adbee827d27 Mon Sep 17 00:00:00 2001 From: liquidaty Date: Thu, 22 Feb 2024 12:24:47 -0800 Subject: [PATCH] use zsv_replace_file(), which in turn uses MoveFileW on win, instead of rename() (#149) * use zsv_replace_file(), which in turn uses MoveFileW on win, instead of rename() --- app/2db.c | 4 +++- app/mv.c | 13 +++++++------ app/prop.c | 5 +++-- app/utils/os.c | 2 +- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/app/2db.c b/app/2db.c index 51c2d20f..1640b6d7 100644 --- a/app/2db.c +++ b/app/2db.c @@ -18,6 +18,7 @@ #include #include +#include #include @@ -651,9 +652,10 @@ static int zsv_2db_finish(zsv_2db_handle data) { // rename tmp to target unlink(data->opts.db_fn); - if(rename(data->db_fn_tmp, data->opts.db_fn)) { + if(zsv_replace_file(data->db_fn_tmp, data->opts.db_fn)) { fprintf(stderr, "Unable to rename %s to %s\n", data->db_fn_tmp, data->opts.db_fn); + zsv_perror(NULL); err = 1; } else fprintf(stderr, "Database %s created\n", data->opts.db_fn); diff --git a/app/mv.c b/app/mv.c index 80bcfe7e..9ef0014e 100644 --- a/app/mv.c +++ b/app/mv.c @@ -25,6 +25,7 @@ #include #include #include +#include const char *zsv_mv_usage_msg[] = { APPNAME ": move a file and its related cache", @@ -88,18 +89,18 @@ int ZSV_MAIN_NO_OPTIONS_FUNC(ZSV_COMMAND)(int argc, const char *argv[]) { fprintf(stderr, "Use `mv --cache %s ` to move or `rm --cache %s` to remove, then try again\n", dest, dest); } else if(move_file && (verbose ? fprintf(stderr, "Renaming files\n") : 1) - && rename(source, dest)) { + && zsv_replace_file(source, dest)) { err = errno; fprintf(stderr, "%s -> %s: ", source, dest); - perror(NULL); + zsv_perror(NULL); } else if(zsv_dir_exists((const char *)source_cache_dir) && (verbose ? fprintf(stderr, "Moving caches\n") : 1) - && rename((char*)source_cache_dir, (char*)dest_cache_dir)) { + && rename(// rename(): not sure will work on Win with NFS dirs... + (char*)source_cache_dir, (char*)dest_cache_dir) + ) { err = errno; fprintf(stderr, "%s -> %s: ", source_cache_dir, dest_cache_dir); perror(NULL); - - // try to revert the prior rename - if(rename(dest, source)) { + if(rename(dest, source)) { // try to revert the prior rename. see above re Win + NFS fprintf(stderr, "%s -> %s: ", dest, source); perror(NULL); } diff --git a/app/prop.c b/app/prop.c index 5c29155c..6e8c3576 100644 --- a/app/prop.c +++ b/app/prop.c @@ -733,9 +733,10 @@ static int zsv_prop_foreach_copy(struct zsv_foreach_dirent_handle *h, size_t dep else { if(h->verbose) fprintf(stderr, "Renaming: %s => %s\n", dest_prop_filepath_tmp, dest_prop_filepath); - if(rename(dest_prop_filepath_tmp, dest_prop_filepath)) { + if(zsv_replace_file(dest_prop_filepath_tmp, dest_prop_filepath)) { const char *msg = strerror(errno); - fprintf(stderr, "Unable to rename %s -> %s: %s\n", dest_prop_filepath_tmp, dest_prop_filepath, msg ? msg : "Unknown error"); + fprintf(stderr, "Unable to rename %s -> %s: ", dest_prop_filepath_tmp, dest_prop_filepath); + zsv_perror(NULL); ctx->err = errno; } } diff --git a/app/utils/os.c b/app/utils/os.c index 72c1c66d..928c7974 100644 --- a/app/utils/os.c +++ b/app/utils/os.c @@ -64,7 +64,7 @@ int zsv_replace_file(const void *src, const void *dest) { zsv_win_to_unicode(dest, wdest, ARRAY_SIZE(wdest)); zsv_win_to_unicode(src, wsrc, ARRAY_SIZE(wsrc)); - if(ReplaceFileW(wdest, wsrc, NULL, REPLACEFILE_IGNORE_MERGE_ERRORS, 0, 0)) // success + if(MoveFileExW(wsrc, wdest, MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH)) // success return 0; if(GetLastError() == 2) // file not found, could be target. use simple rename