From 26cd90e100d5542a04eeb594b84e7121fd8a69d5 Mon Sep 17 00:00:00 2001 From: Alexander Larsson Date: Thu, 25 Feb 2016 16:51:45 +0100 Subject: [PATCH] Allow specifying subdir of xdg dir, like: --filesytem=xdg-download/subdir --- common/xdg-app-run.c | 45 +++++++++++++++++++++++++++--------- doc/xdg-app-build-finish.xml | 3 ++- doc/xdg-app-override.xml | 3 ++- doc/xdg-app-run.xml | 3 ++- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/common/xdg-app-run.c b/common/xdg-app-run.c index 703cb95c..b17cdefb 100644 --- a/common/xdg-app-run.c +++ b/common/xdg-app-run.c @@ -372,13 +372,34 @@ get_user_dir_config_key (GUserDirectory dir) } static int -get_user_dir_from_string (const char *filesystem) +get_user_dir_from_string (const char *filesystem, const char **suffix) { - if (strcmp (filesystem, "xdg-desktop") == 0) + char *slash; + g_autofree char *prefix; + gsize len; + + slash = strchr (filesystem, '/'); + + if (slash) + len = slash - filesystem; + else + len = strlen (filesystem); + + if (suffix) + { + const char *rest = filesystem + len; + while (*rest == '/') + rest ++; + *suffix = rest; + } + + prefix = g_strndup (filesystem, len); + + if (strcmp (prefix, "xdg-desktop") == 0) return G_USER_DIRECTORY_DESKTOP; - if (strcmp (filesystem, "xdg-documents") == 0) + if (strcmp (prefix, "xdg-documents") == 0) return G_USER_DIRECTORY_DOCUMENTS; - if (strcmp (filesystem, "xdg-download") == 0) + if (strcmp (prefix, "xdg-download") == 0) return G_USER_DIRECTORY_DOWNLOAD; if (strcmp (filesystem, "xdg-music") == 0) return G_USER_DIRECTORY_MUSIC; @@ -422,13 +443,13 @@ static gboolean xdg_app_context_verify_filesystem (const char *filesystem_and_mode, GError **error) { - char *filesystem = parse_filesystem_flags (filesystem_and_mode, NULL); + g_autofree char *filesystem = parse_filesystem_flags (filesystem_and_mode, NULL); if (strcmp (filesystem, "host") == 0) return TRUE; if (strcmp (filesystem, "home") == 0) return TRUE; - if (get_user_dir_from_string (filesystem) >= 0) + if (get_user_dir_from_string (filesystem, NULL) >= 0) return TRUE; if (g_str_has_prefix (filesystem, "~/")) return TRUE; @@ -436,7 +457,7 @@ xdg_app_context_verify_filesystem (const char *filesystem_and_mode, return TRUE; g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_FAILED, - "Unknown filesystem location %s, valid types are: host,home,xdg-*,~/dir,/dir,\n", filesystem); + "Unknown filesystem location %s, valid types are: host,home,xdg-*[/...],~/dir,/dir,\n", filesystem); return FALSE; } @@ -1469,8 +1490,9 @@ xdg_app_run_add_environment_args (GPtrArray *argv_array, if (g_str_has_prefix (filesystem, "xdg-")) { - const char *path; - int dir = get_user_dir_from_string (filesystem); + const char *path, *rest = NULL; + g_autofree char *subpath = NULL; + int dir = get_user_dir_from_string (filesystem, &rest); if (dir < 0) { @@ -1488,7 +1510,8 @@ xdg_app_run_add_environment_args (GPtrArray *argv_array, continue; } - if (g_file_test (path, G_FILE_TEST_EXISTS)) + subpath = g_build_filename (path, rest, NULL); + if (g_file_test (subpath, G_FILE_TEST_EXISTS)) { if (xdg_dirs_conf == NULL) xdg_dirs_conf = g_string_new (""); @@ -1496,7 +1519,7 @@ xdg_app_run_add_environment_args (GPtrArray *argv_array, g_string_append_printf (xdg_dirs_conf, "%s=\"%s\"\n", get_user_dir_config_key (dir), path); g_ptr_array_add (argv_array, g_strdup (mode_arg)); - g_ptr_array_add (argv_array, g_strdup_printf ("%s", path)); + g_ptr_array_add (argv_array, g_strdup_printf ("%s", subpath)); } } else if (g_str_has_prefix (filesystem, "~/")) diff --git a/doc/xdg-app-build-finish.xml b/doc/xdg-app-build-finish.xml index 8b323f95..1bacd85d 100644 --- a/doc/xdg-app-build-finish.xml +++ b/doc/xdg-app-build-finish.xml @@ -160,7 +160,8 @@ This updates the [Context] group in the metadata. FS can be one of: home, host, xdg-desktop, xdg-documents, xdg-download xdg-music, xdg-pictures, xdg-public-share, xdg-templates, xdg-videos, - an absolute path, or a homedir-relative path like ~/dir. + an absolute path, or a homedir-relative path like ~/dir or paths + relative to the xdg dirs, like xdg-download/subdir. This option can be used multiple times. diff --git a/doc/xdg-app-override.xml b/doc/xdg-app-override.xml index 4371c12d..a2ef31ca 100644 --- a/doc/xdg-app-override.xml +++ b/doc/xdg-app-override.xml @@ -141,7 +141,8 @@ This overrides to the Context section from the application metadata. FS can be one of: home, host, xdg-desktop, xdg-documents, xdg-download xdg-music, xdg-pictures, xdg-public-share, xdg-templates, xdg-videos, - an absolute path, or a homedir-relative path like ~/dir. + an absolute path, or a homedir-relative path like ~/dir or paths + relative to the xdg dirs, like xdg-download/subdir. This option can be used multiple times. diff --git a/doc/xdg-app-run.xml b/doc/xdg-app-run.xml index 4730ec50..44c3ddc9 100644 --- a/doc/xdg-app-run.xml +++ b/doc/xdg-app-run.xml @@ -202,7 +202,8 @@ This overrides to the Context section from the application metadata. FS can be one of: home, host, xdg-desktop, xdg-documents, xdg-download xdg-music, xdg-pictures, xdg-public-share, xdg-templates, xdg-videos, - an absolute path, or a homedir-relative path like ~/dir. + an absolute path, or a homedir-relative path like ~/dir or paths + relative to the xdg dirs, like xdg-download/subdir. This option can be used multiple times.