[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

@@ -20,19 +20,17 @@
#include "main.h"
#include "config.h"
#include "config_platform.h"
#include "kb.h"
#include "common/option.h"
#include "common/debug.h"
#include "common/paths.h"
#include "common/filesystem.h"
#include "common/option.h"
#include "common/stringutils.h"
#include <errno.h>
#include <limits.h>
#include <pwd.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
// forwards
static bool optRendererParse (struct Option * opt, const char * str);
@@ -565,62 +563,21 @@ void config_init(void)
option_register(options);
}
static bool loadDefaultConfig(const char * path, void * opaque)
{
(void)opaque;
if (lgPathType(path) != LG_FILE_TYPE_REGULAR)
return true;
DEBUG_INFO("Loading config from: %s", path);
return option_load(path);
}
bool config_load(int argc, char * argv[])
{
// load any global options first
struct stat st;
if (stat("/etc/looking-glass-client.ini", &st) >= 0 && S_ISREG(st.st_mode))
{
DEBUG_INFO("Loading config from: /etc/looking-glass-client.ini");
if (!option_load("/etc/looking-glass-client.ini"))
return false;
}
// load config from user's home directory
struct passwd * pw = getpwuid(getuid());
if (!pw)
DEBUG_WARN("getpwuid failed, unable to load user configuration");
else
{
char * localFile;
alloc_sprintf(&localFile, "%s/.looking-glass-client.ini", pw->pw_dir);
if (!localFile)
{
DEBUG_ERROR("out of memory");
return false;
}
if (stat(localFile, &st) >= 0 && S_ISREG(st.st_mode))
{
DEBUG_INFO("Loading config from: %s", localFile);
if (!option_load(localFile))
{
free(localFile);
return false;
}
}
free(localFile);
}
// load config from XDG_CONFIG_HOME
char * xdgFile;
alloc_sprintf(&xdgFile, "%s/client.ini", lgConfigDir());
if (!xdgFile)
{
DEBUG_ERROR("out of memory");
if (!config_platformVisitDefaultFiles(loadDefaultConfig, NULL))
return false;
}
if (xdgFile && stat(xdgFile, &st) >= 0 && S_ISREG(st.st_mode))
{
DEBUG_INFO("Loading config from: %s", xdgFile);
if (!option_load(xdgFile))
{
free(xdgFile);
return false;
}
}
free(xdgFile);
// parse the command line arguments
if (!option_parse(argc, argv))
@@ -630,7 +587,7 @@ bool config_load(int argc, char * argv[])
const char * configFile = option_get_string("app", "configFile");
if (configFile)
{
if (stat(configFile, &st) < 0 || !S_ISREG(st.st_mode))
if (lgPathType(configFile) != LG_FILE_TYPE_REGULAR)
{
DEBUG_ERROR("app:configFile set to invalid file: %s", configFile);
return false;

View File

@@ -0,0 +1,31 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef _H_LG_CLIENT_CONFIG_PLATFORM_
#define _H_LG_CLIENT_CONFIG_PLATFORM_
#include <stdbool.h>
typedef bool (*ConfigFileVisitor)(const char * path, void * opaque);
bool config_platformVisitDefaultFiles(
ConfigFileVisitor visitor, void * opaque);
#endif

View File

@@ -46,6 +46,7 @@
#include "common/thread.h"
#include "common/locking.h"
#include "common/event.h"
#include "common/filesystem.h"
#include "common/time.h"
#include "common/version.h"
#include "common/paths.h"
@@ -716,8 +717,8 @@ static void preSwapCallback(void * udata)
.dataSize = capture.dataSize,
};
FILE * file = fopen(l_testCapture.path, "wb");
bool written = false;
FILE * file = lgOpenFile(l_testCapture.path, "wb");
bool written = false;
if (file)
{
written =

View File

@@ -23,6 +23,7 @@
#include "font.h"
#include "common/debug.h"
#include "common/filesystem.h"
#include "common/stringutils.h"
#include <stdlib.h>
@@ -279,7 +280,7 @@ static ImFont * uiFontAddSize(ImFontAtlas * atlas, float size)
bool util_fileGetContents(const char * filename, char ** buffer, size_t * length)
{
FILE * fh = fopen(filename, "r");
FILE * fh = lgOpenFile(filename, "r");
if (!fh)
{
DEBUG_ERROR("Failed to open the file: %s", filename);