POSIX directory listing.

This commit is contained in:
Miguel Lechon
2021-01-21 20:09:11 +01:00
parent 5ff1c5e0a5
commit 4d2565b3f3
3 changed files with 54 additions and 5 deletions
+1 -1
View File
@@ -12,7 +12,7 @@ popd () {
echo ~~~ Metadesk Build ~~~ echo ~~~ Metadesk Build ~~~
# TODO(mal): Review these warnings # TODO(mal): Review these warnings
accepted_clang_warnings="-Wno-return-type -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option" accepted_clang_warnings="-Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option"
compile_flags="-I../source/ $accepted_clang_warnings" compile_flags="-I../source/ $accepted_clang_warnings"
mkdir -p build mkdir -p build
Executable → Regular
+2 -1
View File
@@ -1,7 +1,8 @@
@echo off @echo off
echo ~~~ Metadesk Build ~~~ echo ~~~ Metadesk Build ~~~
set accepted_clang_warnings=-Wno-return-type -Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option rem TODO(mal): Review these warnings
set accepted_clang_warnings=-Wno-deprecated-declarations -Wno-pointer-sign -Wno-writable-strings -Wno-unknown-warning-option
set compile_flags=-I../source/ %accepted_clang_warnings% set compile_flags=-I../source/ %accepted_clang_warnings%
if not exist build mkdir build if not exist build mkdir build
+51 -3
View File
@@ -1,5 +1,53 @@
static MD_b32 #include <dirent.h>
_MD_OS_IMPL_FileIter_Increment(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info) #include <sys/stat.h>
{
#define MD_IMPL_FileIterIncrement MD_POSIX_FileIterIncrement
static MD_b32
MD_POSIX_FileIterIncrement(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info)
{
MD_b32 result = 0;
DIR *dir = (DIR *)(it->state);
if(dir == 0)
{
dir = opendir((char*)path.str);
}
struct dirent *dir_entry = 0;
if(dir && (dir_entry = readdir(dir)))
{
out_info->filename = MD_PushStringF("%s", dir_entry->d_name);
out_info->flags = 0;
if(path.size > 1 && path.str[path.size-1] == '/')
{
path.size -= 1;
}
MD_String8 cfile_path = MD_PushStringF("%.*s/%s", MD_StringExpand(path), dir_entry->d_name);
// NOTE(mal): On Linux, fstatat(2) would save us from doing path manipulation and avoid some race
// conditions but we would need to make space for an extra directory file descriptor
// inside MD_FileIter struct. We would also stop being POSIX-compliant.
struct stat st;
if(stat((char *)cfile_path.str, &st) == 0)
{
if((st.st_mode & S_IFMT) == S_IFDIR)
{
out_info->flags |= MD_FileFlag_Directory;
}
out_info->file_size = st.st_size;
}
result = 1;
}
else
{
closedir(dir);
dir = 0;
result = 0;
}
it->state = *(MD_u64 *)(&dir);
return result;
} }