mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] readlink: Improve -f handling
@ 2017-07-03 13:17 Ian Abbott
  2017-07-05 15:50 ` Lucas Stach
  0 siblings, 1 reply; 2+ messages in thread
From: Ian Abbott @ 2017-07-03 13:17 UTC (permalink / raw)
  To: barebox; +Cc: Ian Abbott

For `readlink -f FILE VARIABLE`, set VARIABLE to the absolute, canonical
file name of FILE by following symbolic links.  All but the final
component of FILE must exist.

Prior to commit a602bebcf7e4 ("fs: Implement links to directories") the
above worked in the limited case where FILE was an absolute path, the
final component was a symbolic link, and all preceding components were
directories.  If FILE was a relative path with the final component being
a symbolic link, and all preceding paths being directories, the command
sort of worked, but produced a relative path as the result.  Both of
these cases were broken by the above commit.

Signed-off-by: Ian Abbott <abbotti@mev.co.uk>
---
 commands/readlink.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/commands/readlink.c b/commands/readlink.c
index a19c8e004..2dfa56667 100644
--- a/commands/readlink.c
+++ b/commands/readlink.c
@@ -44,17 +44,21 @@ static int do_readlink(int argc, char *argv[])
 	if (argc < optind + 2)
 		return COMMAND_ERROR_USAGE;
 
-	if (readlink(argv[optind], realname, PATH_MAX - 1) < 0)
-		goto err;
-
 	if (canonicalize) {
-		char *buf = canonicalize_path(realname);
+		char *buf = canonicalize_path(argv[optind]);
+		struct stat s;
 
 		if (!buf)
 			goto err;
+		if (stat(dirname(argv[optind]), &s) || !S_ISDIR(s.st_mode)) {
+			free(buf);
+			goto err;
+		}
 		setenv(argv[optind + 1], buf);
 		free(buf);
 	} else {
+		if (readlink(argv[optind], realname, PATH_MAX - 1) < 0)
+			goto err;
 		setenv(argv[optind + 1], realname);
 	}
 
@@ -65,15 +69,16 @@ err:
 }
 
 BAREBOX_CMD_HELP_START(readlink)
-BAREBOX_CMD_HELP_TEXT("Read value of a symbolic link and store it into VARIABLE.")
+BAREBOX_CMD_HELP_TEXT("Read value of a symbolic link or canonical file name and store it into VARIABLE.")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
-BAREBOX_CMD_HELP_OPT ("-f", "canonicalize by following first symlink");
+BAREBOX_CMD_HELP_OPT("-f", "canonicalize by following symlinks;")
+BAREBOX_CMD_HELP_OPT("",   "final component need not exist");
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(readlink)
 	.cmd		= do_readlink,
-	BAREBOX_CMD_DESC("read value of a symbolic link")
+	BAREBOX_CMD_DESC("read value of a symbolic link or canonical file name")
 	BAREBOX_CMD_OPTS("[-f] FILE VARIABLE")
 	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
 	BAREBOX_CMD_HELP(cmd_readlink_help)
-- 
2.11.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] readlink: Improve -f handling
  2017-07-03 13:17 [PATCH] readlink: Improve -f handling Ian Abbott
@ 2017-07-05 15:50 ` Lucas Stach
  0 siblings, 0 replies; 2+ messages in thread
From: Lucas Stach @ 2017-07-05 15:50 UTC (permalink / raw)
  To: Ian Abbott; +Cc: barebox

Am Montag, den 03.07.2017, 14:17 +0100 schrieb Ian Abbott:
> For `readlink -f FILE VARIABLE`, set VARIABLE to the absolute, canonical
> file name of FILE by following symbolic links.  All but the final
> component of FILE must exist.
> 
> Prior to commit a602bebcf7e4 ("fs: Implement links to directories") the
> above worked in the limited case where FILE was an absolute path, the
> final component was a symbolic link, and all preceding components were
> directories.  If FILE was a relative path with the final component being
> a symbolic link, and all preceding paths being directories, the command
> sort of worked, but produced a relative path as the result.  Both of
> these cases were broken by the above commit.
> 
> Signed-off-by: Ian Abbott <abbotti@mev.co.uk>

Applied, thanks.

> ---
>  commands/readlink.c | 19 ++++++++++++-------
>  1 file changed, 12 insertions(+), 7 deletions(-)
> 
> diff --git a/commands/readlink.c b/commands/readlink.c
> index a19c8e004..2dfa56667 100644
> --- a/commands/readlink.c
> +++ b/commands/readlink.c
> @@ -44,17 +44,21 @@ static int do_readlink(int argc, char *argv[])
>  	if (argc < optind + 2)
>  		return COMMAND_ERROR_USAGE;
>  
> -	if (readlink(argv[optind], realname, PATH_MAX - 1) < 0)
> -		goto err;
> -
>  	if (canonicalize) {
> -		char *buf = canonicalize_path(realname);
> +		char *buf = canonicalize_path(argv[optind]);
> +		struct stat s;
>  
>  		if (!buf)
>  			goto err;
> +		if (stat(dirname(argv[optind]), &s) || !S_ISDIR(s.st_mode)) {
> +			free(buf);
> +			goto err;
> +		}
>  		setenv(argv[optind + 1], buf);
>  		free(buf);
>  	} else {
> +		if (readlink(argv[optind], realname, PATH_MAX - 1) < 0)
> +			goto err;
>  		setenv(argv[optind + 1], realname);
>  	}
>  
> @@ -65,15 +69,16 @@ err:
>  }
>  
>  BAREBOX_CMD_HELP_START(readlink)
> -BAREBOX_CMD_HELP_TEXT("Read value of a symbolic link and store it into VARIABLE.")
> +BAREBOX_CMD_HELP_TEXT("Read value of a symbolic link or canonical file name and store it into VARIABLE.")
>  BAREBOX_CMD_HELP_TEXT("")
>  BAREBOX_CMD_HELP_TEXT("Options:")
> -BAREBOX_CMD_HELP_OPT ("-f", "canonicalize by following first symlink");
> +BAREBOX_CMD_HELP_OPT("-f", "canonicalize by following symlinks;")
> +BAREBOX_CMD_HELP_OPT("",   "final component need not exist");
>  BAREBOX_CMD_HELP_END
>  
>  BAREBOX_CMD_START(readlink)
>  	.cmd		= do_readlink,
> -	BAREBOX_CMD_DESC("read value of a symbolic link")
> +	BAREBOX_CMD_DESC("read value of a symbolic link or canonical file name")
>  	BAREBOX_CMD_OPTS("[-f] FILE VARIABLE")
>  	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
>  	BAREBOX_CMD_HELP(cmd_readlink_help)



_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-07-05 15:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-03 13:17 [PATCH] readlink: Improve -f handling Ian Abbott
2017-07-05 15:50 ` Lucas Stach

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox