diff options
Diffstat (limited to 'source/fud_directory.cpp')
-rw-r--r-- | source/fud_directory.cpp | 98 |
1 files changed, 38 insertions, 60 deletions
diff --git a/source/fud_directory.cpp b/source/fud_directory.cpp index 1697692..318d7e6 100644 --- a/source/fud_directory.cpp +++ b/source/fud_directory.cpp @@ -72,48 +72,53 @@ Result<DirectoryEntry, FudStatus> DirectoryEntry::fromStat(const String& name, c break; } - return RetType::okay(DirectoryEntry{ - name, - statBuffer.st_ino, - size, - static_cast<size_t>(statBuffer.st_nlink), - statBuffer.st_mtime, - entryType}); + DirectoryEntry entry{}; + auto copyStatus = entry.name.copy(name); + if (copyStatus != FudStatus::Success) { + return RetType::error(copyStatus); + } + entry.inode = statBuffer.st_ino; + entry.size = size; + entry.links = static_cast<size_t>(statBuffer.st_nlink); + entry.modificationTime = statBuffer.st_mtime; + entry.entryType = entryType; + return RetType::okay(std::move(entry)); } -Directory::Directory(const String& name) : m_name{name} +Result<Directory, FudStatus> Directory::make(const String& name) { - if (!m_name.valid()) { - return; + using RetType = Result<Directory, FudStatus>; + Directory directory{}; + if (!name.valid()) { + return RetType::error(FudStatus::InvalidInput); } - if (!m_name.utf8Valid()) { - m_status = FudStatus::Utf8Invalid; - return; + if (!name.utf8Valid()) { + return RetType::error(FudStatus::Utf8Invalid); } - m_directory = opendir(m_name.c_str()); - if (m_directory == nullptr) { - m_status = FudStatus::Failure; - m_errorCode = errno; - return; + auto copyResult = directory.m_name.copy(name); + if (copyResult != FudStatus::Success) { + return RetType::error(copyResult); } - m_dirFd = dirfd(m_directory); - if (m_dirFd == -1) { - m_status = FudStatus::Failure; - m_errorCode = errno; - closedir(m_directory); - m_directory = nullptr; - return; + directory.m_directory = opendir(name.c_str()); + if (directory.m_directory == nullptr) { + return RetType::error(FudStatus::Failure); } - m_errorCode = 0; - m_status = FudStatus::Success; + directory.m_dirFd = dirfd(directory.m_directory); + if (directory.m_dirFd == -1) { + closedir(directory.m_directory); + return RetType::error(FudStatus::Failure); + } + + directory.m_errorCode = 0; + return RetType::okay(std::move(directory)); } Directory::Directory(Directory&& rhs) noexcept : - m_name{std::move(rhs.m_name)}, m_directory{rhs.m_directory}, m_dirFd{rhs.m_dirFd} + m_name{std::move(rhs.m_name)}, m_directory{rhs.m_directory}, m_errorCode{rhs.m_errorCode}, m_dirFd{rhs.m_dirFd} { rhs.m_directory = nullptr; rhs.m_dirFd = -1; @@ -127,35 +132,21 @@ Directory::~Directory() } } -bool Directory::valid() const -{ - return m_directory != nullptr && m_dirFd != -1; -} - Result<DirectoryEntry, FudStatus> Directory::info() { using RetType = Result<DirectoryEntry, FudStatus>; - if (!valid()) { - m_status = FudStatus::ObjectInvalid; - return RetType::error(m_status); - } - Stat sBuffer{}; auto fStatus = fstat(m_dirFd, &sBuffer); if (fStatus == -1) { m_errorCode = errno; - m_status = FudStatus::Failure; - return RetType::error(m_status); + return RetType::error(FudStatus::Failure); } auto retValue = DirectoryEntry::fromStat(m_name, sBuffer); if (retValue.isOkay()) { m_errorCode = 0; - m_status = FudStatus::Success; - } else { - m_status = retValue.getError(); } return retValue; @@ -165,18 +156,12 @@ Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry() { using RetType = Result<std::optional<DirectoryEntry>, FudStatus>; - if (!valid()) { - m_status = FudStatus::ObjectInvalid; - return RetType::error(m_status); - } - errno = 0; auto* dirEntry = readdir(m_directory); if (dirEntry == nullptr) { if (errno != 0) { m_errorCode = errno; - m_status = FudStatus::Failure; - return RetType::error(m_status); + return RetType::error(FudStatus::Failure); } return RetType::okay(std::nullopt); } @@ -184,8 +169,7 @@ Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry() const char* entryNameCString = dirEntry->d_name; if (entryNameCString == nullptr) { m_errorCode = -1; - m_status = FudStatus::NullPointer; - return RetType::error(m_status); + return RetType::error(FudStatus::NullPointer); } m_errorCode = 0; @@ -195,8 +179,7 @@ Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry() auto fStatus = fstatat(m_dirFd, entryNameCString, &sBuffer, flags); if (fStatus == -1) { m_errorCode = errno; - m_status = FudStatus::Failure; - return RetType::error(m_status); + return RetType::error(FudStatus::Failure); } auto entryNameResult = String::makeFromCString(entryNameCString); @@ -207,23 +190,18 @@ Result<std::optional<DirectoryEntry>, FudStatus> Directory::getNextEntry() if (retValue.isOkay()) { m_errorCode = 0; - m_status = FudStatus::Success; return RetType::okay(retValue.takeOkay()); } - m_status = retValue.getError(); return RetType::error(retValue.getError()); } FudStatus Directory::reset() { - if (!valid()) { - } if (m_directory != nullptr) { rewinddir(m_directory); } else { - m_status = FudStatus::ObjectInvalid; - return m_status; + return FudStatus::ObjectInvalid; } return FudStatus::Success; } |