diff --git a/src/common/fs/FileUtils.cpp b/src/common/fs/FileUtils.cpp index fc04eb40a97..44eac382224 100644 --- a/src/common/fs/FileUtils.cpp +++ b/src/common/fs/FileUtils.cpp @@ -364,6 +364,14 @@ bool FileUtils::exist(const std::string& path) { return access(path.c_str(), F_OK) == 0; } +// static +bool FileUtils::rename(const std::string& src, const std::string& dst) { + auto status = ::rename(src.c_str(), dst.c_str()); + LOG_IF(WARNING, status != 0) << "Rename " << src << " to " << dst << " failed, the errno: " + << ::strerror(errno); + return status == 0; +} + std::vector FileUtils::listAllTypedEntitiesInDir( const char* dirpath, FileType type, diff --git a/src/common/fs/FileUtils.h b/src/common/fs/FileUtils.h index 2e459f0e2c4..c322501f49f 100644 --- a/src/common/fs/FileUtils.h +++ b/src/common/fs/FileUtils.h @@ -110,6 +110,10 @@ class FileUtils final { static bool makeDir(const std::string& dir, uint32_t mode = 0775); // Check the path is exist static bool exist(const std::string& path); + // Like the command `mv', apply to file and directory + // Refer to `man 3 rename' + // return false when rename failed + static bool rename(const std::string& src, const std::string& dst); /** * List all entities in the given directory, whose type matches diff --git a/src/kvstore/test/NebulaStoreTest.cpp b/src/kvstore/test/NebulaStoreTest.cpp index 5ce38e613c3..fa4788fbeae 100644 --- a/src/kvstore/test/NebulaStoreTest.cpp +++ b/src/kvstore/test/NebulaStoreTest.cpp @@ -9,6 +9,7 @@ #include #include #include "fs/TempDir.h" +#include "fs/FileUtils.h" #include "kvstore/NebulaStore.h" #include "kvstore/PartManager.h" #include "kvstore/RocksEngine.h" @@ -759,18 +760,21 @@ TEST(NebulaStoreTest, ThreeCopiesCheckpointTest) { std::string rm = folly::stringPrintf("%s/disk%d/nebula/0", rootPath.path(), i); fs::FileUtils::remove(folly::stringPrintf("%s/data", rm.data()).c_str(), true); fs::FileUtils::remove(folly::stringPrintf("%s/wal", rm.data()).c_str(), true); - std::string mv = folly::stringPrintf( - "/usr/bin/mv %s/disk%d/nebula/0/checkpoints/snapshot/data %s/disk%d/nebula/0/data", - rootPath.path(), i , rootPath.path(), i); - sleep(1); - auto ret = system(mv.c_str()); - ASSERT_EQ(0, ret); - mv = folly::stringPrintf( - "/usr/bin/mv %s/disk%d/nebula/0/checkpoints/snapshot/wal %s/disk%d/nebula/0/wal", - rootPath.path(), i , rootPath.path(), i); - sleep(1); - ret = system(mv.c_str()); - ASSERT_EQ(0, ret); + std::string src = folly::stringPrintf( + "%s/disk%d/nebula/0/checkpoints/snapshot/data", + rootPath.path(), i); + std::string dst = folly::stringPrintf( + "%s/disk%d/nebula/0/data", + rootPath.path(), i); + ASSERT_TRUE(fs::FileUtils::rename(src, dst)); + + src = folly::stringPrintf( + "%s/disk%d/nebula/0/checkpoints/snapshot/wal", + rootPath.path(), i); + dst = folly::stringPrintf( + "%s/disk%d/nebula/0/wal", + rootPath.path(), i); + ASSERT_TRUE(fs::FileUtils::rename(src, dst)); } LOG(INFO) << "Let's start the engine via checkpoint";