summaryrefslogtreecommitdiff
path: root/source/fud_file.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/fud_file.cpp')
-rw-r--r--source/fud_file.cpp59
1 files changed, 30 insertions, 29 deletions
diff --git a/source/fud_file.cpp b/source/fud_file.cpp
index 8dab031..8f84648 100644
--- a/source/fud_file.cpp
+++ b/source/fud_file.cpp
@@ -34,11 +34,11 @@ FileResult RegularFile::open(
Allocator* allocator)
{
if (allocator == nullptr) {
- return FudStatus::NullPointer;
+ return FileResult::error(FudStatus::NullPointer);
}
if (!filename.nullTerminated()) {
- return FudStatus::ArgumentInvalid;
+ return FileResult::error(FudStatus::ArgumentInvalid);
}
int dirFd = dirFdOption.valueOr(AT_FDCWD);
@@ -57,11 +57,11 @@ FileResult RegularFile::open(
openFlags = O_RDWR;
break;
default:
- return FudStatus::ArgumentInvalid;
+ return FileResult::error(FudStatus::ArgumentInvalid);
}
if (flags.hasFlag(OpenFlagEnum::Append) && flags.hasFlag(OpenFlagEnum::Truncate)) {
- return FudStatus::OperationInvalid;
+ return FileResult::error(FudStatus::OperationInvalid);
}
openFlags |= flags.flags();
@@ -74,31 +74,31 @@ FileResult RegularFile::open(
auto status = syscall(SYS_openat2, dirFd, filename.data(), &openHow, sizeof(openHow));
if (status == -1) {
if constexpr (EAGAIN != EWOULDBLOCK && status == EWOULDBLOCK) {
- return FudStatus::Partial;
+ return FileResult::error(FudStatus::Partial);
}
switch (errno) {
case ETXTBSY:
case EAGAIN:
- return FudStatus::Partial;
+ return FileResult::error(FudStatus::Partial);
case ENOENT:
- return FudStatus::NotFound;
+ return FileResult::error(FudStatus::NotFound);
case EBADF:
case EFBIG:
case EOVERFLOW:
case EINVAL:
case EISDIR:
case ENAMETOOLONG:
- return FudStatus::ArgumentInvalid;
+ return FileResult::error(FudStatus::ArgumentInvalid);
case EROFS:
case EACCES:
case EPERM:
- return FudStatus::PermissionDenied;
+ return FileResult::error(FudStatus::PermissionDenied);
case ELOOP:
case EXDEV:
case ENFILE:
case E2BIG:
default:
- return FudStatus::Failure;
+ return FileResult::error(FudStatus::Failure);
}
}
fudAssert(status <= std::numeric_limits<decltype(file.m_fd)>::max());
@@ -108,14 +108,14 @@ FileResult RegularFile::open(
Stat sBuffer{};
auto fStatus = fstat(file.m_fd, &sBuffer);
if (fStatus == -1) {
- return FudStatus::Failure;
+ return FileResult::error(FudStatus::Failure);
}
if ((sBuffer.st_mode & S_IFMT) != S_IFREG) {
- return FudStatus::ObjectInvalid;
+ return FileResult::error(FudStatus::ObjectInvalid);
}
- return file;
+ return FileResult::okay(std::move(file));
}
FileResult RegularFile::create(
@@ -128,11 +128,11 @@ FileResult RegularFile::create(
Allocator* allocator)
{
if (allocator == nullptr) {
- return FudStatus::NullPointer;
+ return FileResult::error(FudStatus::NullPointer);
}
if (!filename.nullTerminated()) {
- return FudStatus::ArgumentInvalid;
+ return FileResult::error(FudStatus::ArgumentInvalid);
}
int dirFd = dirFdOption.valueOr(AT_FDCWD);
@@ -151,11 +151,11 @@ FileResult RegularFile::create(
openFlags = O_RDWR;
break;
default:
- return FudStatus::ArgumentInvalid;
+ return FileResult::error(FudStatus::ArgumentInvalid);
}
if (flags.hasFlag(OpenFlagEnum::Append) && flags.hasFlag(OpenFlagEnum::Truncate)) {
- return FudStatus::OperationInvalid;
+ return FileResult::error(FudStatus::OperationInvalid);
}
openFlags |= flags.flags() | O_CREAT | (O_EXCL * static_cast<uint8_t>(exclusive));
@@ -169,23 +169,23 @@ FileResult RegularFile::create(
auto status = syscall(SYS_openat2, dirFd, filename.data(), &openHow, sizeof(openHow));
if (status == -1) {
if constexpr (EAGAIN != EWOULDBLOCK && status == EWOULDBLOCK) {
- return FudStatus::Partial;
+ return FileResult::error(FudStatus::Partial);
}
switch (errno) {
case ETXTBSY:
case EAGAIN:
- return FudStatus::Partial;
+ return FileResult::error(FudStatus::Partial);
case EBADF:
case EFBIG:
case EOVERFLOW:
case EINVAL:
case EISDIR:
case ENAMETOOLONG:
- return FudStatus::ArgumentInvalid;
+ return FileResult::error(FudStatus::ArgumentInvalid);
case EROFS:
case EACCES:
case EPERM:
- return FudStatus::PermissionDenied;
+ return FileResult::error(FudStatus::PermissionDenied);
case EDQUOT:
case ENOENT:
case ELOOP:
@@ -193,7 +193,7 @@ FileResult RegularFile::create(
case ENFILE:
case E2BIG:
default:
- return FudStatus::Failure;
+ return FileResult::error(FudStatus::Failure);
}
}
fudAssert(status <= std::numeric_limits<decltype(file.m_fd)>::max());
@@ -203,14 +203,14 @@ FileResult RegularFile::create(
Stat sBuffer{};
auto fStatus = fstat(file.m_fd, &sBuffer);
if (fStatus == -1) {
- return FudStatus::Failure;
+ return FileResult::error(FudStatus::Failure);
}
if ((sBuffer.st_mode & S_IFMT) != S_IFREG) {
- return FudStatus::ObjectInvalid;
+ return FileResult::error(FudStatus::ObjectInvalid);
}
- return file;
+ return FileResult::okay(std::move(file));
}
RegularFile::~RegularFile()
@@ -284,23 +284,24 @@ FudStatus RegularFile::close()
Result<size_t, FudStatus> RegularFile::size() const
{
+ using RetType = Result<size_t, FudStatus>;
auto fileSize = lseek(m_fd, 0, SEEK_END);
if (fileSize == -1) {
switch (errno) {
case EBADF:
case ESPIPE:
- return FudStatus::ObjectInvalid;
+ return RetType::error(FudStatus::ObjectInvalid);
default:
- return FudStatus::Failure;
+ return RetType::error(FudStatus::Failure);
}
}
auto seekBegin = lseek(m_fd, 0, SEEK_SET);
if (seekBegin == -1) {
- return FudStatus::Failure;
+ return RetType::error(FudStatus::Failure);
}
- return static_cast<size_t>(fileSize);
+ return RetType::okay(static_cast<size_t>(fileSize));
}
} // namespace fud