Skip to content

Commit

Permalink
Update API & Fix Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
pegvin committed Feb 19, 2024
1 parent 9b73d81 commit 363c7f8
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
5 changes: 2 additions & 3 deletions include/fs/fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ namespace FileSystem {
bool GetFileSize(const String& filePath, u64* SizePtr); // get size of a file at `filePath` and store it in *SizePtr
bool ListDir(const String& dirPath, ListDirCallback cb);

// returns 1 if regular, 0 if not, -1 on error, with errno set
i32 IsRegularFile(const String& filePath);
i32 IsRegularDir(const String& dirPath);
bool IsDirectory(const String& dirPath);
bool IsFile(const String& filePath);
}

#endif // FILESYSTEM_FS_HPP_INCLUDED_
Expand Down
54 changes: 40 additions & 14 deletions src/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,19 @@ bool Fs::MakeDirRecursive(const String& _p) {
for (u32 i = 0; i < path.length(); ++i) {
if (path[i] == PATH_SEP_CHAR) {
String dirPath = path.substr(0, i + 1);
if (!Fs::IsRegularDir(dirPath)) {
if (!Fs::IsDirectory(dirPath)) {
if (!Fs::MakeDir(dirPath)) {
return false;
}
}
}
}

return Fs::MakeDir(path);
if (!Fs::IsDirectory(path)) {
return Fs::MakeDir(path);
}

return true;
}

bool Fs::GetFileSize(const String& filePath, u64* SizePtr) {
Expand Down Expand Up @@ -122,32 +126,54 @@ bool Fs::GetFileSize(const String& filePath, u64* SizePtr) {
return true;
}

#if !defined(S_IFMT)
#define S_IFMT _S_IFMT
#endif
#if !defined(S_ISDIR)
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#if !defined(S_ISREG)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#ifndef FS_TARGET_WINDOWS
#if !defined(S_IFMT)
#define S_IFMT _S_IFMT
#endif
#if !defined(S_ISDIR)
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#if !defined(S_ISREG)
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
#endif
#endif

i32 Fs::IsRegularFile(const String& filePath) {
bool Fs::IsFile(const String& filePath) {
#ifdef FS_TARGET_WINDOWS
std::wstring fPathWide = UTF8_To_WideString(filePath);
DWORD dwAttrib = GetFileAttributesW(fPathWide.c_str());

return (
dwAttrib != INVALID_FILE_ATTRIBUTES &&
!(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)
);
#else
struct stat st;

if (stat(filePath.c_str(), &st) < 0)
return -1;

return S_ISREG(st.st_mode);
return S_ISREG(st.st_mode) == 1;
#endif
}

i32 Fs::IsRegularDir(const String& dirPath) {
bool Fs::IsDirectory(const String& dirPath) {
#ifdef FS_TARGET_WINDOWS
std::wstring dPathWide = UTF8_To_WideString(dirPath);
DWORD dwAttrib = GetFileAttributesW(dPathWide.c_str());

return (
dwAttrib != INVALID_FILE_ATTRIBUTES &&
(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)
);
#else
struct stat st;

if (stat(dirPath.c_str(), &st) < 0)
return -1;

return S_ISDIR(st.st_mode);
return S_ISDIR(st.st_mode) == 1;
#endif
}

bool Fs::ListDir(const String& _dP, ListDirCallback cb) {
Expand Down
12 changes: 6 additions & 6 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ MU_TEST(Test_Fs_GetBaseName) {

MU_TEST(Test_Fs_MakeDir) {
mu_check(Fs::MakeDir("mainDir") == true);
mu_check(Fs::IsRegularDir("mainDir") == 1);
mu_check(Fs::IsDirectory("mainDir") == 1);
}

MU_TEST(Test_Fs_IsRegularX) {
mu_check(Fs::IsRegularDir(".github" PATH_SEP "workflows"));
mu_check(Fs::IsRegularFile(".github" PATH_SEP "workflows" PATH_SEP "ci.yml"));
mu_check(Fs::IsDirectory(".github" PATH_SEP "workflows"));
mu_check(Fs::IsFile(".github" PATH_SEP "workflows" PATH_SEP "ci.yml"));
}

MU_TEST(Test_Fs_MakeDirRecursive) {
Expand All @@ -43,9 +43,9 @@ MU_TEST(Test_Fs_MakeDirRecursive) {
strerror(errno)
#endif
);
mu_check(Fs::IsRegularDir("mainDir" PATH_SEP "first" PATH_SEP) == 1);
mu_check(Fs::IsRegularDir("mainDir" PATH_SEP "first" PATH_SEP "second" PATH_SEP) == 1);
mu_check(Fs::IsRegularDir("mainDir" PATH_SEP "first" PATH_SEP "second" PATH_SEP "third" PATH_SEP) == 1);
mu_check(Fs::IsDirectory("mainDir" PATH_SEP "first" PATH_SEP) == 1);
mu_check(Fs::IsDirectory("mainDir" PATH_SEP "first" PATH_SEP "second" PATH_SEP) == 1);
mu_check(Fs::IsDirectory("mainDir" PATH_SEP "first" PATH_SEP "second" PATH_SEP "third" PATH_SEP) == 1);
}

MU_TEST(Test_Fs_ListDir) {
Expand Down

0 comments on commit 363c7f8

Please sign in to comment.