Skip to content

Commit

Permalink
improve move file on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Feb 27, 2024
1 parent c6b0a56 commit be76595
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/tbox/platform/windows/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,21 @@ tb_bool_t tb_file_rename(tb_char_t const* path, tb_char_t const* dest)

// rename it
DWORD flags = MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH;
if (!MoveFileExW(full0, full1, flags))
{
// make directory
tb_file_mkdir(full1);
if (MoveFileExW(full0, full1, flags)) return tb_true;

// rename it again
return MoveFileExW(full0, full1, flags);
// try to create directories and rename it again
tb_file_mkdir(full1);
if (MoveFileExW(full0, full1, flags)) return tb_true;

// MoveFileExW will fail if it crosses drive, we can use copy/delete to rename file
if (CopyFileW(full0, full1, FALSE))
{
DWORD attrs = GetFileAttributesW(full0);
if (attrs & FILE_ATTRIBUTE_READONLY)
SetFileAttributesW(full0, attrs & ~FILE_ATTRIBUTE_READONLY);
return DeleteFileW(full0);
}
return tb_true;
return tb_false;
}
tb_bool_t tb_file_link(tb_char_t const* path, tb_char_t const* dest)
{
Expand Down

0 comments on commit be76595

Please sign in to comment.