[common/client] platform: make filesystem access portable

This commit is contained in:
Geoffrey McRae
2026-09-12 09:27:14 +10:00
parent a60da36420
commit 6d367d4ab2
17 changed files with 780 additions and 118 deletions

View File

@@ -24,6 +24,7 @@
#include "app.h"
#include "common/debug.h"
#include "common/filesystem.h"
#include "common/option.h"
#include "common/paths.h"
#include "common/stringlist.h"
@@ -35,7 +36,6 @@
#include "basic.vert.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
@@ -46,7 +46,6 @@
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#define GLSL_MAX_TEXTURES 16
#define GLSL_EXPR_STACK 128
@@ -1133,27 +1132,24 @@ static bool scanDirectory(const char * root, const char * relative,
if (!path)
return false;
DIR * directory = opendir(path);
if (!directory)
StringList entries = lgListDirectory(path);
if (!entries)
{
DEBUG_ERROR("Unable to open GLSL shader directory '%s': %s",
path, strerror(errno));
DEBUG_ERROR("Unable to open GLSL shader directory '%s'", path);
free(path);
return false;
}
bool result = true;
struct dirent * entry;
while ((entry = readdir(directory)))
for (unsigned int i = 0; i < stringlist_count(entries); ++i)
{
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
const char * entry = stringlist_at(entries, i);
char * childRelative;
if (relative && *relative)
alloc_sprintf(&childRelative, "%s/%s", relative, entry->d_name);
alloc_sprintf(&childRelative, "%s/%s", relative, entry);
else
childRelative = lg_strdup(entry->d_name);
childRelative = lg_strdup(entry);
if (!childRelative)
{
result = false;
@@ -1169,23 +1165,18 @@ static bool scanDirectory(const char * root, const char * relative,
break;
}
struct stat statbuf;
if (lstat(childPath, &statbuf) == 0)
const LGFileType type = lgPathTypeNoFollow(childPath);
if (type == LG_FILE_TYPE_DIRECTORY)
{
if (S_ISDIR(statbuf.st_mode))
{
result = scanDirectory(root, childRelative, files);
free(childRelative);
}
else if (S_ISREG(statbuf.st_mode) && hasGLSLExtension(entry->d_name))
{
const unsigned int count = stringlist_count(files);
stringlist_push(files, childRelative);
result = stringlist_count(files) == count + 1;
if (!result)
free(childRelative);
}
else
result = scanDirectory(root, childRelative, files);
free(childRelative);
}
else if (type == LG_FILE_TYPE_REGULAR && hasGLSLExtension(entry))
{
const unsigned int count = stringlist_count(files);
stringlist_push(files, childRelative);
result = stringlist_count(files) == count + 1;
if (!result)
free(childRelative);
}
else
@@ -1196,7 +1187,7 @@ static bool scanDirectory(const char * root, const char * relative,
break;
}
closedir(directory);
stringlist_free(&entries);
free(path);
return result;
}
@@ -1219,13 +1210,13 @@ static bool glslCreate(EGL_FilterAddFn add, void * opaque)
return false;
DEBUG_INFO("Scanning runtime GLSL filters in: %s", g_shaderPath);
struct stat statbuf;
if (stat(g_shaderPath, &statbuf) < 0 && (!configured || !*configured))
if (lgPathType(g_shaderPath) == LG_FILE_TYPE_MISSING &&
(!configured || !*configured))
{
if (mkdir(g_shaderPath, S_IRWXU) < 0 && errno != EEXIST)
if (!lgCreateDirectory(g_shaderPath))
{
DEBUG_ERROR("Unable to create GLSL shader directory '%s': %s",
g_shaderPath, strerror(errno));
DEBUG_ERROR("Unable to create GLSL shader directory '%s'",
g_shaderPath);
return false;
}
}

View File

@@ -24,15 +24,13 @@
#include "app.h"
#include "cimgui.h"
#include <dirent.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <stdatomic.h>
#include <sys/stat.h>
#include "common/debug.h"
#include "common/array.h"
#include "common/filesystem.h"
#include "common/option.h"
#include "common/paths.h"
#include "common/stringlist.h"
@@ -113,8 +111,6 @@ static void loadPreset(struct EGL_PostProcess * this, const char * name);
static void loadPresetList(struct EGL_PostProcess * this)
{
DIR * dir = NULL;
alloc_sprintf(&this->presetDir, "%s/presets", lgConfigDir());
if (!this->presetDir)
{
@@ -122,14 +118,14 @@ static void loadPresetList(struct EGL_PostProcess * this)
return;
}
if (mkdir(this->presetDir, S_IRWXU) < 0 && errno != EEXIST)
if (!lgCreateDirectory(this->presetDir))
{
DEBUG_ERROR("Failed to create presets directory: %s", this->presetDir);
goto fail;
}
dir = opendir(this->presetDir);
if (!dir)
StringList entries = lgListDirectory(this->presetDir);
if (!entries)
{
DEBUG_ERROR("Failed to open presets directory: %s", this->presetDir);
goto fail;
@@ -139,33 +135,45 @@ static void loadPresetList(struct EGL_PostProcess * this)
if (!this->presets)
{
DEBUG_ERROR("Failed to allocate memory for preset list");
goto fail;
goto fail_entries;
}
struct dirent * entry;
const char * preset = option_get_string("egl", "preset");
this->activePreset = -1;
while ((entry = readdir(dir)) != NULL)
for (unsigned int i = 0; i < stringlist_count(entries); ++i)
{
if (entry->d_type != DT_REG)
const char * entry = stringlist_at(entries, i);
char * path;
alloc_sprintf(&path, "%s/%s", this->presetDir, entry);
if (!path)
goto fail_entries;
const LGFileType type = lgPathTypeNoFollow(path);
free(path);
if (type != LG_FILE_TYPE_REGULAR)
continue;
DEBUG_INFO("Found preset: %s", entry->d_name);
char * name = strdup(entry->d_name);
DEBUG_INFO("Found preset: %s", entry);
char * name = strdup(entry);
if (!name)
{
DEBUG_ERROR("Failed to allocate memory");
goto fail;
goto fail_entries;
}
const unsigned int count = stringlist_count(this->presets);
stringlist_push(this->presets, name);
if (stringlist_count(this->presets) != count + 1)
{
free(name);
goto fail_entries;
}
app_registerImGuiText(name);
if (preset && strcmp(preset, name) == 0)
this->activePreset = stringlist_count(this->presets) - 1;
}
closedir(dir);
stringlist_free(&entries);
if (preset)
{
@@ -177,11 +185,12 @@ static void loadPresetList(struct EGL_PostProcess * this)
return;
fail_entries:
stringlist_free(&entries);
fail:
free(this->presetDir);
this->presetDir = NULL;
if (dir)
closedir(dir);
if (this->presets)
stringlist_free(&this->presets);
}
@@ -222,7 +231,7 @@ static bool savePreset(struct EGL_PostProcess * this, const char * name)
return false;
}
FILE * file = fopen(path, "w");
FILE * file = lgOpenFile(path, "w");
if (!file)
{
const char * strError = strerror(errno);
@@ -362,7 +371,7 @@ static void deletePreset(struct EGL_PostProcess * this)
return;
}
unlink(path);
lgDeleteFile(path);
free(path);
stringlist_remove(this->presets, this->activePreset);
if (this->activePreset >= stringlist_count(this->presets))