Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ucmnfsstore-ut.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ Status SpaceManager::NewBlock(const std::string& blockId) const
UC_ERROR("Failed({}) to new block({}).", status, blockId);
return status;
}
status = file->Open(IFile::OpenFlag::CREATE | IFile::OpenFlag::READ_WRITE);
if ((File::Access(this->_layout.DataFilePath(blockId, false), IFile::AccessMode::EXIST)).Success()) {
status = Status::DuplicateKey();
UC_ERROR("Failed({}) to new block({}).", status, blockId);
return status;
}
status = file->Open(IFile::OpenFlag::CREATE | IFile::OpenFlag::EXCL | IFile::OpenFlag::READ_WRITE);
if (status.Failure()) {
UC_ERROR("Failed({}) to new block({}).", status, blockId);
return status;
Expand Down
14 changes: 12 additions & 2 deletions unifiedcache/csrc/ucmnfsstore/cc/infra/file/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,24 @@

namespace UC {

using FileImpl = PosixFile;

std::unique_ptr<IFile> File::Make(const std::string& path)
{
try {
return std::make_unique<PosixFile>(path);
return std::make_unique<FileImpl>(path);
} catch (const std::exception& e) {
UC_ERROR("Failed({}) to make file({}) pointer.", e.what(), path);
return nullptr;
}
}

} // namespace UC
Status File::MkDir(const std::string& path) { return FileImpl{path}.MkDir(); }

Status File::RmDir(const std::string& path) { return FileImpl{path}.RmDir(); }

Status File::Rename(const std::string& path, const std::string& newName) { return FileImpl{path}.Rename(newName); }

Status File::Access(const std::string& path, const int32_t mode) { return FileImpl{path}.Access(mode); }

} // namespace UC
4 changes: 4 additions & 0 deletions unifiedcache/csrc/ucmnfsstore/cc/infra/file/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ namespace UC {
class File {
public:
static std::unique_ptr<IFile> Make(const std::string& path);
static Status MkDir(const std::string& path);
static Status RmDir(const std::string& path);
static Status Rename(const std::string& path, const std::string& newName);
static Status Access(const std::string& path, const int32_t mode);
};

} // namespace UC
Expand Down
1 change: 1 addition & 0 deletions unifiedcache/csrc/ucmnfsstore/cc/infra/file/ifile.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class IFile {
static constexpr uint32_t CREATE = O_CREAT;
static constexpr uint32_t DIRECT = O_DIRECT;
static constexpr uint32_t APPEND = O_APPEND;
static constexpr uint32_t EXCL = O_EXCL;
};

public:
Expand Down
3 changes: 2 additions & 1 deletion unifiedcache/csrc/ucmnfsstore/cc/infra/file/posix_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PosixFile::~PosixFile() { this->Close(); }

Status PosixFile::MkDir()
{
constexpr auto permission = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
constexpr auto permission = (S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH);
auto ret = mkdir(this->Path().c_str(), permission);
auto eno = errno;
if (ret != 0) {
Expand Down Expand Up @@ -97,6 +97,7 @@ Status PosixFile::Open(const uint32_t flags)
auto eno = errno;
auto status = this->_handle >= 0 ? Status::OK() : Status::OsApiError();
if (status.Failure()) {
if (eno == EEXIST) { status = Status::DuplicateKey(); }
UC_ERROR("Failed({},{}) to open file({}) with flags({}).", eno, status, this->Path(), flags);
}
return status;
Expand Down
8 changes: 3 additions & 5 deletions unifiedcache/csrc/ucmnfsstore/cpy/ucmnfsstore.py.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@ namespace py = pybind11;

namespace UC {

inline int32_t AllocBatch(const py::list& blockIds)
inline py::list AllocBatch(const py::list& blockIds)
{
int32_t ret = 0;
for (auto id : blockIds) {
if ((ret = Alloc(id.cast<std::string>())) != 0) { break; }
}
py::list ret;
for (auto id : blockIds) { ret.append(Alloc(id.cast<std::string>())); }
return ret;
}

Expand Down
42 changes: 42 additions & 0 deletions unifiedcache/csrc/ucmnfsstore/test/case/file_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* MIT License
*
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* */
#include "cmn/path_base.h"
#include "file/file.h"

class UCFileTest : public UC::PathBase {};

TEST_F(UCFileTest, DirCreateAndRemove)
{
auto path1 = this->Path() + "dir1";
ASSERT_EQ(UC::File::Access(path1, UC::IFile::AccessMode::EXIST), UC::Status::NotFound());
ASSERT_EQ(UC::File::MkDir(path1), UC::Status::OK());
ASSERT_EQ(UC::File::Access(path1, UC::IFile::AccessMode::EXIST), UC::Status::OK());
auto path2 = this->Path() + "dir2";
ASSERT_EQ(UC::File::Access(path2, UC::IFile::AccessMode::EXIST), UC::Status::NotFound());
ASSERT_EQ(UC::File::Rename(path1, path2), UC::Status::OK());
ASSERT_EQ(UC::File::Access(path1, UC::IFile::AccessMode::EXIST), UC::Status::NotFound());
ASSERT_EQ(UC::File::Access(path2, UC::IFile::AccessMode::EXIST), UC::Status::OK());
ASSERT_EQ(UC::File::RmDir(path2), UC::Status::OK());
ASSERT_EQ(UC::File::Access(path2, UC::IFile::AccessMode::EXIST), UC::Status::NotFound());
}
2 changes: 2 additions & 0 deletions unifiedcache/csrc/ucmnfsstore/test/case/posix_file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ TEST_F(UCPosixFileTest, FileCreateAndRemove)
ASSERT_EQ(file.Access(UC::IFile::AccessMode::EXIST), UC::Status::NotFound());
ASSERT_EQ(file.Open(UC::IFile::OpenFlag::WRITE_ONLY), UC::Status::OsApiError());
ASSERT_EQ(file.Open(UC::IFile::OpenFlag::WRITE_ONLY | UC::IFile::OpenFlag::CREATE), UC::Status::OK());
ASSERT_EQ(file.Open(UC::IFile::OpenFlag::WRITE_ONLY | UC::IFile::OpenFlag::CREATE | UC::IFile::OpenFlag::EXCL),
UC::Status::DuplicateKey());
ASSERT_EQ(file.Access(UC::IFile::AccessMode::EXIST), UC::Status::OK());
ASSERT_EQ(file.Access(UC::IFile::AccessMode::READ), UC::Status::OK());
ASSERT_EQ(file.Access(UC::IFile::AccessMode::WRITE), UC::Status::OK());
Expand Down
41 changes: 41 additions & 0 deletions unifiedcache/csrc/ucmnfsstore/test/case/space_manager_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* MIT License
*
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* */
#include "cmn/path_base.h"
#include "space/space_manager.h"

class UCSpaceManagerTest : public UC::PathBase {};

TEST_F(UCSpaceManagerTest, NewBlockTwice)
{
UC::SpaceManager spaceMgr;
ASSERT_EQ(spaceMgr.Setup({this->Path()}, 1024 * 1024), UC::Status::OK());
const std::string block1 = "block1";
ASSERT_FALSE(spaceMgr.LookupBlock(block1));
ASSERT_EQ(spaceMgr.NewBlock(block1), UC::Status::OK());
ASSERT_FALSE(spaceMgr.LookupBlock(block1));
ASSERT_EQ(spaceMgr.NewBlock(block1), UC::Status::DuplicateKey());
ASSERT_EQ(spaceMgr.CommitBlock(block1), UC::Status::OK());
ASSERT_TRUE(spaceMgr.LookupBlock(block1));
ASSERT_EQ(spaceMgr.NewBlock(block1), UC::Status::DuplicateKey());
}