fixes
Some checks failed
BlockStorage/repertory/pipeline/head There was a failure building this commit

This commit is contained in:
2024-08-07 19:58:51 -05:00
parent 33fa52f5a4
commit efb2be5839
5 changed files with 68 additions and 21 deletions

View File

@ -42,13 +42,13 @@ auto file::attach_file(native_handle handle, bool read_only) -> file {
static_cast<DWORD>(path.size()),
FILE_NAME_NORMALIZED | VOLUME_NAME_DOS);
#else // !defined(_WIN32)
source_path.resize(PATH_MAX + 1);
path.resize(PATH_MAX + 1);
#if defined(__APPLE__)
fcntl(handle, F_GETPATH, source_path.data());
#else // !defined(__APPLE__)
readlink(("/proc/self/fd/" + std::to_string(handle)).c_str(),
source_path.data(), source_path.size());
readlink(("/proc/self/fd/" + std::to_string(handle)).c_str(), path.data(),
path.size());
#endif // defined(__APPLE__)
#endif // defined(_WIN32)
@ -87,14 +87,6 @@ auto file::open_file(std::filesystem::path path, bool read_only) -> file {
throw std::runtime_error("file not found: " + path.string());
}
if (not read_only) {
#if defined(_WIN32)
_chmod(path.string().c_str(), 0600U);
#else // !defined(_WIN32)
chmod(path.string().c_str(), 0600U);
#endif // defined(_WIN32)
}
#if defined(_WIN32)
auto *ptr =
_fsopen(path.string().c_str(), read_only ? "rb" : "rb+", _SH_DENYNO);
@ -121,9 +113,9 @@ auto file::open_or_create_file(std::filesystem::path path,
#if defined(_WIN32)
int old_mode{};
_umask_s(0600U, &old_mode);
_umask_s(077, &old_mode);
#else // !defined(_WIN32)
auto old_mode = umask(0600U);
auto old_mode = umask(077);
#endif // defined(_WIN32)
#if defined(_WIN32)
@ -361,7 +353,7 @@ auto file::write(const unsigned char *data, std::size_t to_write,
auto bytes_written =
fwrite(reinterpret_cast<const char *>(data), 1U, to_write, file_.get());
if (not feof(file_.get()) && ferror(file_.get())) {
throw std::runtime_error("failed to read file bytes");
throw std::runtime_error("failed to write file bytes");
}
flush();

View File

@ -22,6 +22,58 @@
#include "test.hpp"
namespace repertory {
TEST(utils_file, can_create_file) {
auto path = test::generate_test_file_name("utils_file");
EXPECT_FALSE(utils::file::is_file(path) || utils::file::is_directory(path));
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(file);
EXPECT_TRUE(utils::file::is_file(path));
}
TEST(utils_file, can_open_file) {
auto path = test::generate_test_file_name("utils_file");
{
auto file = utils::file::file::open_or_create_file(path);
EXPECT_TRUE(file);
}
{
auto file = utils::file::file::open_file(path);
EXPECT_TRUE(file);
}
}
TEST(utils_file, open_file_fails_if_not_found) {
auto path = test::generate_test_file_name("utils_file");
auto file = utils::file::file::open_file(path);
EXPECT_FALSE(file);
}
TEST(utils_file, write_fails_for_read_only_file) {
auto path = test::generate_test_file_name("utils_file");
auto file = utils::file::file::open_or_create_file(path, true);
EXPECT_TRUE(utils::file::is_file(path));
EXPECT_TRUE(file);
std::size_t bytes_written{};
EXPECT_FALSE(file.write(reinterpret_cast<const unsigned char *>("0"), 1U, 0U,
&bytes_written));
EXPECT_EQ(0U, bytes_written);
}
TEST(utils_file, can_attach_file) {
auto path = test::generate_test_file_name("utils_file");
auto file = utils::file::file::open_or_create_file(path);
auto file2 = utils::file::file::attach_file(file.get_handle());
EXPECT_TRUE(file);
EXPECT_TRUE(file2);
EXPECT_EQ(file.get_path(), file2.get_path());
}
#if defined(PROJECT_ENABLE_JSON)
TEST(utils_file, read_and_write_json_file) {
auto path = test::generate_test_file_name("utils_file");