Skip to content

Commit

Permalink
use zsv_replace_file(), which in turn uses MoveFileW on win, instead …
Browse files Browse the repository at this point in the history
…of rename() (#149)

* use zsv_replace_file(), which in turn uses MoveFileW on win, instead of rename()
  • Loading branch information
liquidaty authored Feb 22, 2024
1 parent 7790279 commit 1231cbb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion app/2db.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <zsv/utils/mem.h>
#include <zsv/utils/string.h>
#include <zsv/utils/os.h>

#include <yajl_helper/yajl_helper.h>

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 7 additions & 6 deletions app/mv.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <zsv/utils/dirs.h>
#include <zsv/utils/file.h>
#include <zsv/utils/cache.h>
#include <zsv/utils/os.h>

const char *zsv_mv_usage_msg[] = {
APPNAME ": move a file and its related cache",
Expand Down Expand Up @@ -88,18 +89,18 @@ int ZSV_MAIN_NO_OPTIONS_FUNC(ZSV_COMMAND)(int argc, const char *argv[]) {
fprintf(stderr, "Use `mv --cache %s <destination>` 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);
}
Expand Down
5 changes: 3 additions & 2 deletions app/prop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion app/utils/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1231cbb

Please sign in to comment.