kike: resolve the path to PID files better

This commit is contained in:
2015-07-02 01:02:06 +02:00
parent 51bf132c6b
commit b486965277
2 changed files with 63 additions and 15 deletions

View File

@@ -90,6 +90,49 @@ strncasecmp_ascii (const char *a, const char *b, size_t n)
return 0;
}
static char *
resolve_relative_runtime_filename (const char *filename)
{
struct str path;
str_init (&path);
const char *runtime_dir = getenv ("XDG_RUNTIME_DIR");
if (runtime_dir && *runtime_dir == '/')
str_append (&path, runtime_dir);
else
get_xdg_home_dir (&path, "XDG_DATA_HOME", ".local/share");
str_append_printf (&path, "/%s/%s", PROGRAM_NAME, filename);
// Try to create the file's ancestors
const char *last_slash = strrchr (path.str, '/');
if (last_slash && last_slash != path.str)
{
char *copy = xstrndup (path.str, last_slash - path.str);
(void) mkdir_with_parents (copy, NULL);
free (copy);
}
return str_steal (&path);
}
static char *
resolve_filename (const char *filename, char *(*relative_cb) (const char *))
{
// Absolute path is absolute
if (*filename == '/')
return xstrdup (filename);
// We don't want to use wordexp() for this as it may execute /bin/sh
if (*filename == '~')
{
// Paths to home directories ought to be absolute
char *expanded = try_expand_tilde (filename + 1);
if (expanded)
return expanded;
print_debug ("failed to expand the home directory in `%s'", filename);
}
return relative_cb (filename);
}
// --- Logging -----------------------------------------------------------------
static void