diff mbox series

[v4,1/4] cache.h: Introduce a generic "xdg_config_home_for(…)" function

Message ID 20210524071538.46862-2-lenaic@lhuard.fr (mailing list archive)
State Superseded
Headers show
Series add support for systemd timers on Linux | expand

Commit Message

Lénaïc Huard May 24, 2021, 7:15 a.m. UTC
Current implementation of `xdg_config_home(filename)` returns
`$XDG_CONFIG_HOME/git/$filename`, with the `git` subdirectory inserted
between the `XDG_CONFIG_HOME` environment variable and the parameter.

This patch introduces a `xdg_config_home_for(prog, filename)` function
which is more generic. It only concatenates "$XDG_CONFIG_HOME", or
"$HOME/.config" if the former isn’t defined, with the parameters,
without adding `git` in between.

`xdg_config_home(filename)` is now implemented by calling
`xdg_config_home_for("git", filename)` but this new generic function can
be used to compute the configuration directory of other programs.

Signed-off-by: Lénaïc Huard <lenaic@lhuard.fr>
---
 cache.h |  7 +++++++
 path.c  | 13 ++++++++++---
 2 files changed, 17 insertions(+), 3 deletions(-)

Comments

Phillip Wood May 24, 2021, 9:33 a.m. UTC | #1
Hi Lénaïc

This looks fine to me. I'm not 100% sold on calling the parameter prog 
as our program name later in the series ends up being "systemd/user" so 
something like "subdir" might have been better but that is not worth 
rerolling for.

Best Wishes

Phillip

On 24/05/2021 08:15, Lénaïc Huard wrote:
> Current implementation of `xdg_config_home(filename)` returns
> `$XDG_CONFIG_HOME/git/$filename`, with the `git` subdirectory inserted
> between the `XDG_CONFIG_HOME` environment variable and the parameter.
> 
> This patch introduces a `xdg_config_home_for(prog, filename)` function
> which is more generic. It only concatenates "$XDG_CONFIG_HOME", or
> "$HOME/.config" if the former isn’t defined, with the parameters,
> without adding `git` in between.
> 
> `xdg_config_home(filename)` is now implemented by calling
> `xdg_config_home_for("git", filename)` but this new generic function can
> be used to compute the configuration directory of other programs.
> 
> Signed-off-by: Lénaïc Huard <lenaic@lhuard.fr>
> ---
>   cache.h |  7 +++++++
>   path.c  | 13 ++++++++++---
>   2 files changed, 17 insertions(+), 3 deletions(-)
> 
> diff --git a/cache.h b/cache.h
> index 148d9ab5f1..8a2969414a 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1263,6 +1263,13 @@ int is_ntfs_dotgitattributes(const char *name);
>    */
>   int looks_like_command_line_option(const char *str);
>   
> +/**
> + * Return a newly allocated string with the evaluation of
> + * "$XDG_CONFIG_HOME/$prog/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
> + * "$HOME/.config/$prog/$filename". Return NULL upon error.
> + */
> +char *xdg_config_home_for(const char *prog, const char *filename);
> +
>   /**
>    * Return a newly allocated string with the evaluation of
>    * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
> diff --git a/path.c b/path.c
> index 7b385e5eb2..3641d4c456 100644
> --- a/path.c
> +++ b/path.c
> @@ -1498,21 +1498,28 @@ int looks_like_command_line_option(const char *str)
>   	return str && str[0] == '-';
>   }
>   
> -char *xdg_config_home(const char *filename)
> +char *xdg_config_home_for(const char *prog, const char *filename)
>   {
>   	const char *home, *config_home;
>   
> +	assert(prog);
>   	assert(filename);
>   	config_home = getenv("XDG_CONFIG_HOME");
>   	if (config_home && *config_home)
> -		return mkpathdup("%s/git/%s", config_home, filename);
> +		return mkpathdup("%s/%s/%s", config_home, prog, filename);
>   
>   	home = getenv("HOME");
>   	if (home)
> -		return mkpathdup("%s/.config/git/%s", home, filename);
> +		return mkpathdup("%s/.config/%s/%s", home, prog, filename);
> +
>   	return NULL;
>   }
>   
> +char *xdg_config_home(const char *filename)
> +{
> +	return xdg_config_home_for("git", filename);
> +}
> +
>   char *xdg_cache_home(const char *filename)
>   {
>   	const char *home, *cache_home;
>
Đoàn Trần Công Danh May 24, 2021, 12:23 p.m. UTC | #2
On 2021-05-24 10:33:30+0100, Phillip Wood <phillip.wood123@gmail.com> wrote:
> Hi Lénaïc
> 
> This looks fine to me. I'm not 100% sold on calling the parameter prog as
> our program name later in the series ends up being "systemd/user" so
> something like "subdir" might have been better but that is not worth
> rerolling for.

I'll take the blame for that "prog".
I didn't think very hard at the time of writing :(

Yes, "subdir" is definitely better.
And it's aligned with the XDG Base Directory specifications:

	A user-specific version of the configuration file may be
	created in $XDG_CONFIG_HOME/subdir/filename

> On 24/05/2021 08:15, Lénaïc Huard wrote:
> > Current implementation of `xdg_config_home(filename)` returns
> > `$XDG_CONFIG_HOME/git/$filename`, with the `git` subdirectory inserted
> > between the `XDG_CONFIG_HOME` environment variable and the parameter.
> > 
> > This patch introduces a `xdg_config_home_for(prog, filename)` function
> > which is more generic. It only concatenates "$XDG_CONFIG_HOME", or
> > "$HOME/.config" if the former isn’t defined, with the parameters,
> > without adding `git` in between.
> > 
> > `xdg_config_home(filename)` is now implemented by calling
> > `xdg_config_home_for("git", filename)` but this new generic function can
> > be used to compute the configuration directory of other programs.
diff mbox series

Patch

diff --git a/cache.h b/cache.h
index 148d9ab5f1..8a2969414a 100644
--- a/cache.h
+++ b/cache.h
@@ -1263,6 +1263,13 @@  int is_ntfs_dotgitattributes(const char *name);
  */
 int looks_like_command_line_option(const char *str);
 
+/**
+ * Return a newly allocated string with the evaluation of
+ * "$XDG_CONFIG_HOME/$prog/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
+ * "$HOME/.config/$prog/$filename". Return NULL upon error.
+ */
+char *xdg_config_home_for(const char *prog, const char *filename);
+
 /**
  * Return a newly allocated string with the evaluation of
  * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
diff --git a/path.c b/path.c
index 7b385e5eb2..3641d4c456 100644
--- a/path.c
+++ b/path.c
@@ -1498,21 +1498,28 @@  int looks_like_command_line_option(const char *str)
 	return str && str[0] == '-';
 }
 
-char *xdg_config_home(const char *filename)
+char *xdg_config_home_for(const char *prog, const char *filename)
 {
 	const char *home, *config_home;
 
+	assert(prog);
 	assert(filename);
 	config_home = getenv("XDG_CONFIG_HOME");
 	if (config_home && *config_home)
-		return mkpathdup("%s/git/%s", config_home, filename);
+		return mkpathdup("%s/%s/%s", config_home, prog, filename);
 
 	home = getenv("HOME");
 	if (home)
-		return mkpathdup("%s/.config/git/%s", home, filename);
+		return mkpathdup("%s/.config/%s/%s", home, prog, filename);
+
 	return NULL;
 }
 
+char *xdg_config_home(const char *filename)
+{
+	return xdg_config_home_for("git", filename);
+}
+
 char *xdg_cache_home(const char *filename)
 {
 	const char *home, *cache_home;