Skip to content

Commit

Permalink
Attempt to support /etc/goxel/scripts path
Browse files Browse the repository at this point in the history
Untested for now, since I did this on a Mac.  This works by iterating
/etc/goxel from sys_iter_paths with SYS_LOCATION_CONFIG.

Would probably need to add some options to this function to specify if
we want to skip non present or non readable directories.
  • Loading branch information
guillaumechereau committed Mar 26, 2024
1 parent aca3bad commit 2f4e4f9
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,21 @@ void sys_on_saved(const char *path)
LOG_I("Saved %s", path);
}

static int on_path(void *arg, const char *path)
static int sys_get_path_(void *arg, const char *path)
{
char *buf = USER_GET(arg, 0);
size_t size = *(size_t*)USER_GET(arg, 1);
assert(buf[0] == '\0');
snprintf(buf, size, "%s", path);
return 1;
}

int sys_get_path(int location, int options, const char *name,
char *buf, size_t size)
{
sys_iter_paths(location, options, name, USER_PASS(buf, &size), on_path);
buf[0] = '\0';
sys_iter_paths(location, options, name, USER_PASS(buf, &size),
sys_get_path_);
return 0;
}

Expand All @@ -326,11 +329,18 @@ static void path_join(char *buf, size_t size, const char *a, const char *b)
snprintf(buf, size, "%s%s%s", a, need_sep ? "/" : "", b);
}

static bool path_exists(const char *path)
{
struct stat path_stat;
return stat(path, &path_stat) == 0;
}

int sys_iter_paths(int location, int options, const char *name,
void *arg, int (*f)(void *arg, const char *path))
{
char base_dir[PATH_MAX];
char path[PATH_MAX];
int r;

if (sys_callbacks.iter_paths) {
return sys_callbacks.iter_paths(
Expand All @@ -341,7 +351,16 @@ int sys_iter_paths(int location, int options, const char *name,
if (location == SYS_LOCATION_CONFIG) {
snprintf(base_dir, sizeof(base_dir), "%s", get_user_dir());
path_join(path, sizeof(path), base_dir, name);
f(arg, path);
r = f(arg, path);
if (r != 0) return r < 0 ? r : 0;

// Also consider /etc/goxel.
if (path_exists("/etc/goxel")) {
path_join(path, sizeof(path), "/etc/goxel", name);
r = f(arg, path);
if (r != 0) return r < 0 ? r : 0;
}

return 0;
}

Expand Down

0 comments on commit 2f4e4f9

Please sign in to comment.