git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 1/2] abspath: add a function to resolve paths with missing components

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Dec 2, 2020, 13:09 UTC
Message-ID
<nycvar.QRO.7.76.6.2012021406020.25979@tvgsbejvaqbjf.bet>
In-Reply-To
<20201127231916.609852-2-sandals@crustytoothpaste.net>
Hi brian,
On Fri, 27 Nov 2020, brian m. carlson wrote:
Show 14 quoted lines
> @@ -80,6 +97,16 @@ static void get_root_part(struct strbuf *resolved, struct strbuf *remaining)
>   */
>  char *strbuf_realpath(struct strbuf *resolved, const char *path,
>  		      int die_on_error)
> +{
> +	return strbuf_realpath_missing(resolved, path, 1, die_on_error);
> +}
> +
> +/*
> + * Just like strbuf_realpath, but allows specifying how many missing components
> + * are permitted.  -1 may be specified to allow an unlimited number.
> + */
> +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,
> +			      int missing_components, int die_on_error)

I am having a hard time with the name `missing_components`. Taking a step back, I think that we are interested in essentially two modes: allow one missing component, or an arbitrary amount of missing components.

If this assumption is correct, we may not even need to count components at all. We only need a flag indicating whether we allow only the last component to be missing, or any number of components. Maybe something like `error_on_missing_parent_directory` or some such?

Ciao, Dscho

Show 31 quoted lines
>  {
>  	struct strbuf remaining = STRBUF_INIT;
>  	struct strbuf next = STRBUF_INIT;
> @@ -128,8 +155,12 @@ char *strbuf_realpath(struct strbuf *resolved, const char *path,
>  		strbuf_addbuf(resolved, &next);
>
>  		if (lstat(resolved->buf, &st)) {
> +			int trailing_components = count_dir_separators(remaining.buf) +
> +						  (remaining.len != 0);
>  			/* error out unless this was the last component */
> -			if (errno != ENOENT || remaining.len) {
> +			if (errno != ENOENT ||
> +			    !(missing_components == -1 ||
> +			      trailing_components < missing_components)) {
>  				if (die_on_error)
>  					die_errno("Invalid path '%s'",
>  						  resolved->buf);
> diff --git a/cache.h b/cache.h
> index c0072d43b1..ee4bc5ec04 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -1320,6 +1320,8 @@ static inline int is_absolute_path(const char *path)
>  int is_directory(const char *);
>  char *strbuf_realpath(struct strbuf *resolved, const char *path,
>  		      int die_on_error);
> +char *strbuf_realpath_missing(struct strbuf *resolved, const char *path,
> +			      int missing_components, int die_on_error);
>  char *real_pathdup(const char *path, int die_on_error);
>  const char *absolute_path(const char *path);
>  char *absolute_pathdup(const char *path);
>
Previous: brian m. carlsonNext: brian m. carlson
Message 6 of 8 in “rev-parse options for absolute or relative paths”
  1. 0/2 rev-parse options for absolute or relative pathsbrian m. carlson, Nov 27, 2020
  2. 2/2 rev-parse: add option for absolute or relative path formattingbrian m. carlson, Nov 27, 2020
  3. 1/2 abspath: add a function to resolve paths with missing componentsbrian m. carlson, Nov 27, 2020
  4. René ScharfeNov 28, 2020
  5. brian m. carlsonNov 28, 2020
  6. Johannes SchindelinDec 2, 2020
  7. brian m. carlsonDec 2, 2020
  8. Johannes SchindelinDec 2, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.