atomic rename file on windows
This commit is contained in:
parent
3014e4c5cc
commit
549ee9f2e0
|
@ -40,6 +40,7 @@ target_link_libraries(
|
||||||
os
|
os
|
||||||
PUBLIC pthread
|
PUBLIC pthread
|
||||||
PUBLIC zlibstatic
|
PUBLIC zlibstatic
|
||||||
|
PUBLIC KtmW32
|
||||||
)
|
)
|
||||||
if(TD_WINDOWS)
|
if(TD_WINDOWS)
|
||||||
target_link_libraries(
|
target_link_libraries(
|
||||||
|
|
|
@ -19,6 +19,8 @@
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
|
#include <WinBase.h>
|
||||||
|
#include <ktmw32.h>
|
||||||
#define F_OK 0
|
#define F_OK 0
|
||||||
#define W_OK 2
|
#define W_OK 2
|
||||||
#define R_OK 4
|
#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) {
|
int32_t taosRenameFile(const char *oldName, const char *newName) {
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
bool code = MoveFileEx(oldName, newName, MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);
|
bool finished = false;
|
||||||
if (!code) {
|
|
||||||
printf("failed to rename file %s to %s, reason:%s\n", oldName, newName, strerror(errno));
|
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
|
#else
|
||||||
int32_t code = rename(oldName, newName);
|
int32_t code = rename(oldName, newName);
|
||||||
if (code < 0) {
|
if (code < 0) {
|
||||||
|
|
Loading…
Reference in New Issue