mirror of
https://github.com/Ed94/metadesk.git
synced 2026-06-13 07:52:22 -07:00
Linux-specific directory listing.
This commit is contained in:
+2
-1
@@ -7,9 +7,10 @@
|
||||
#if MD_OS_WINDOWS
|
||||
# include "md_win32.c"
|
||||
#elif MD_OS_LINUX
|
||||
# include "md_posix.c"
|
||||
# include "md_linux.c"
|
||||
#else
|
||||
# error No default implementation for this OS
|
||||
// NOTE(mal): Consider md_posix.c
|
||||
#endif
|
||||
|
||||
#include "md_malloc.c"
|
||||
|
||||
+2
-1
@@ -594,11 +594,12 @@ typedef struct MD_FileIter MD_FileIter;
|
||||
struct MD_FileIter
|
||||
{
|
||||
// This is opaque state to store OS-specific file-system iteration data.
|
||||
MD_u64 state;
|
||||
MD_u64 state[2];
|
||||
};
|
||||
|
||||
//~ Basic Utilities
|
||||
#define MD_Assert(c) if (!(c)) { *(volatile MD_u64 *)0 = 0; }
|
||||
#define MD_StaticAssert(c,label) MD_u8 MD_static_assert_##label[(c)?(1):(-1)]
|
||||
#define MD_ArrayCount(a) (sizeof(a) / sizeof((a)[0]))
|
||||
MD_FUNCTION MD_b32 MD_CharIsAlpha(MD_u8 c);
|
||||
MD_FUNCTION MD_b32 MD_CharIsAlphaUpper(MD_u8 c);
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
// LICENSE AT END OF FILE (MIT).
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
// NOTE(mal): To get these constants I need to #define _GNU_SOURCE, which invites non-POSIX behavior I'd rather avoid
|
||||
#ifndef O_PATH
|
||||
#define O_PATH 010000000
|
||||
#endif
|
||||
#define AT_NO_AUTOMOUNT 0x800
|
||||
#define AT_SYMLINK_NOFOLLOW 0x100
|
||||
|
||||
#define MD_IMPL_FileIterIncrement MD_LINUX_FileIterIncrement
|
||||
typedef struct MD_LINUX_FileIter MD_LINUX_FileIter;
|
||||
struct MD_LINUX_FileIter
|
||||
{
|
||||
int dir_fd;
|
||||
DIR *dir;
|
||||
};
|
||||
MD_StaticAssert(sizeof(MD_LINUX_FileIter) <= sizeof(MD_FileIter), file_iter_size_check);
|
||||
|
||||
static MD_b32
|
||||
MD_LINUX_FileIterIncrement(MD_FileIter *opaque_it, MD_String8 path, MD_FileInfo *out_info)
|
||||
{
|
||||
MD_b32 result = 0;
|
||||
|
||||
MD_LINUX_FileIter *it = (MD_LINUX_FileIter *)opaque_it;
|
||||
if(it->dir == 0)
|
||||
{
|
||||
it->dir = opendir((char*)path.str);
|
||||
it->dir_fd = open((char *)path.str, O_PATH|O_CLOEXEC);
|
||||
}
|
||||
|
||||
if(it->dir != 0 && it->dir_fd != -1)
|
||||
{
|
||||
struct dirent *dir_entry = readdir(it->dir);
|
||||
if(dir_entry)
|
||||
{
|
||||
out_info->filename = MD_PushStringF("%s", dir_entry->d_name);
|
||||
out_info->flags = 0;
|
||||
|
||||
struct stat st;
|
||||
if(fstatat(it->dir_fd, dir_entry->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) == 0)
|
||||
{
|
||||
if((st.st_mode & S_IFMT) == S_IFDIR)
|
||||
{
|
||||
out_info->flags |= MD_FileFlag_Directory;
|
||||
}
|
||||
out_info->file_size = st.st_size;
|
||||
}
|
||||
result = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if(result == 0)
|
||||
{
|
||||
if(it->dir != 0)
|
||||
{
|
||||
closedir(it->dir);
|
||||
it->dir = 0;
|
||||
}
|
||||
if(it->dir_fd != -1)
|
||||
{
|
||||
close(it->dir_fd);
|
||||
it->dir_fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
Copyright 2021 Dion Systems LLC
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
+36
-33
@@ -4,52 +4,55 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define MD_IMPL_FileIterIncrement MD_POSIX_FileIterIncrement
|
||||
typedef struct MD_POSIX_FileIter MD_POSIX_FileIter;
|
||||
struct MD_POSIX_FileIter
|
||||
{
|
||||
DIR *dir;
|
||||
};
|
||||
MD_StaticAssert(sizeof(MD_POSIX_FileIter) <= sizeof(MD_FileIter), file_iter_size_check);
|
||||
|
||||
static MD_b32
|
||||
MD_POSIX_FileIterIncrement(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_info)
|
||||
MD_POSIX_FileIterIncrement(MD_FileIter *opaque_it, MD_String8 path, MD_FileInfo *out_info)
|
||||
{
|
||||
MD_b32 result = 0;
|
||||
|
||||
DIR *dir = (DIR *)(it->state);
|
||||
if(dir == 0)
|
||||
|
||||
MD_POSIX_FileIter *it = (MD_POSIX_FileIter *)opaque_it;
|
||||
if(it->dir == 0)
|
||||
{
|
||||
dir = opendir((char*)path.str);
|
||||
it->dir = opendir((char*)path.str);
|
||||
}
|
||||
|
||||
struct dirent *dir_entry = 0;
|
||||
if(dir && (dir_entry = readdir(dir)))
|
||||
if(it->dir)
|
||||
{
|
||||
out_info->filename = MD_PushStringF("%s", dir_entry->d_name);
|
||||
out_info->flags = 0;
|
||||
|
||||
if(path.size > 1 && path.str[path.size-1] == '/')
|
||||
struct dirent *dir_entry = readdir(it->dir);
|
||||
if(dir_entry)
|
||||
{
|
||||
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->filename = MD_PushStringF("%s", dir_entry->d_name);
|
||||
out_info->flags = 0;
|
||||
|
||||
if(path.size > 1 && path.str[path.size-1] == '/')
|
||||
{
|
||||
out_info->flags |= MD_FileFlag_Directory;
|
||||
path.size -= 1;
|
||||
}
|
||||
out_info->file_size = st.st_size;
|
||||
|
||||
struct stat st;
|
||||
MD_String8 cfile_path = MD_PushStringF("%.*s/%s", MD_StringExpand(path), dir_entry->d_name);
|
||||
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(it->dir);
|
||||
it->dir = 0;
|
||||
}
|
||||
result = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
closedir(dir);
|
||||
dir = 0;
|
||||
result = 0;
|
||||
}
|
||||
|
||||
it->state = *(MD_u64 *)(&dir);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
+2
-2
@@ -62,7 +62,7 @@ MD_WIN32_FileIterIncrement(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_in
|
||||
MD_b32 result = 0;
|
||||
|
||||
WIN32_FIND_DATAA find_data = {0};
|
||||
HANDLE state = *(HANDLE *)(&it->state);
|
||||
HANDLE state = *(HANDLE *)(&it->state[0]);
|
||||
if(state == 0)
|
||||
{
|
||||
MD_b32 need_star = 0;
|
||||
@@ -80,7 +80,7 @@ MD_WIN32_FileIterIncrement(MD_FileIter *it, MD_String8 path, MD_FileInfo *out_in
|
||||
result = !!FindNextFileA(state, &find_data);
|
||||
}
|
||||
|
||||
it->state = *(MD_u64 *)(&state);
|
||||
it->state[0] = *(MD_u64 *)(&state);
|
||||
if(result)
|
||||
{
|
||||
out_info->flags = 0;
|
||||
|
||||
Reference in New Issue
Block a user