atomic rename file on windows

This commit is contained in:
xsren 2023-09-01 16:37:51 +08:00
parent 3014e4c5cc
commit 549ee9f2e0
2 changed files with 27 additions and 4 deletions

View File

@ -40,6 +40,7 @@ target_link_libraries(
os
PUBLIC pthread
PUBLIC zlibstatic
PUBLIC KtmW32
)
if(TD_WINDOWS)
target_link_libraries(

View File

@ -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) {