Merge pull request #22695 from taosdata/fix/xsren/renamefile_on_windows
atomic rename file on windows
This commit is contained in:
commit
f00efbdf11
|
@ -43,7 +43,7 @@ target_link_libraries(
|
|||
)
|
||||
if(TD_WINDOWS)
|
||||
target_link_libraries(
|
||||
os PUBLIC ws2_32 iconv msvcregex wcwidth winmm crashdump dbghelp version
|
||||
os PUBLIC ws2_32 iconv msvcregex wcwidth winmm crashdump dbghelp version KtmW32
|
||||
)
|
||||
elseif(TD_DARWIN_64)
|
||||
find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation)
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
|
||||
#ifdef WINDOWS
|
||||
#include <io.h>
|
||||
#include <WinBase.h>
|
||||
#include <ktmw32.h>
|
||||
#define F_OK 0
|
||||
#define W_OK 2
|
||||
#define R_OK 4
|
||||
|
@ -175,12 +177,32 @@ int32_t taosRemoveFile(const char *path) { return remove(path); }
|
|||
|
||||
int32_t taosRenameFile(const char *oldName, const char *newName) {
|
||||
#ifdef WINDOWS
|
||||
bool code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
|
||||
if (!code) {
|
||||
printf("failed to rename file %s to %s, reason:%s\n", oldName, newName, strerror(errno));
|
||||
bool finished = false;
|
||||
|
||||
HANDLE transactionHandle = CreateTransaction(NULL, NULL, 0, 0, 0, INFINITE, NULL);
|
||||
if (transactionHandle == INVALID_HANDLE_VALUE) {
|
||||
printf("failed to rename file %s to %s, reason: CreateTransaction failed.\n", oldName, newName);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return code ? 0 : -1;
|
||||
BOOL result = MoveFileTransacted(oldName, newName, NULL, NULL, MOVEFILE_REPLACE_EXISTING, transactionHandle);
|
||||
|
||||
if (result) {
|
||||
finished = CommitTransaction(transactionHandle);
|
||||
if (!finished) {
|
||||
DWORD error = GetLastError();
|
||||
printf("failed to rename file %s to %s, reason: CommitTransaction errcode %d.\n", oldName, newName, error);
|
||||
}
|
||||
} else {
|
||||
RollbackTransaction(transactionHandle);
|
||||
DWORD error = GetLastError();
|
||||
finished = false;
|
||||
printf("failed to rename file %s to %s, reason: MoveFileTransacted errcode %d.\n", oldName, newName, error);
|
||||
}
|
||||
|
||||
CloseHandle(transactionHandle);
|
||||
|
||||
return finished ? 0 : -1;
|
||||
#else
|
||||
int32_t code = rename(oldName, newName);
|
||||
if (code < 0) {
|
||||
|
|
Loading…
Reference in New Issue