mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: BAREBOX <barebox@lists.infradead.org>
Subject: [PATCH 9/9] commands: ln: create link inside DEST if it is a directory
Date: Tue, 20 Oct 2026 09:09:34 +0200	[thread overview]
Message-ID: <20261020-fs-symlink-fixes-v1-9-e7ac8c29b08a@pengutronix.de> (raw)
In-Reply-To: <20261020-fs-symlink-fixes-v1-0-e7ac8c29b08a@pengutronix.de>

With Linux ln, when the link name is an existing directory, the link is
created inside that directory and named after the last component of the
target, so that 'ln -s /etc/foo .' creates ./foo. barebox ln instead
tries to create the link at DEST itself and fails with -EEXIST.

Do the same as Linux. Trailing slashes in the target are ignored for the
link name, but are kept in the target the link points to.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 commands/ln.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)

diff --git a/commands/ln.c b/commands/ln.c
index c907b306c9..1b18dfcf63 100644
--- a/commands/ln.c
+++ b/commands/ln.c
@@ -10,10 +10,14 @@
 #include <fs.h>
 #include <errno.h>
 #include <getopt.h>
+#include <libbb.h>
 
 static int do_ln(int argc, char *argv[])
 {
-	int opt;
+	const char *target, *dest;
+	char *linkname = NULL;
+	struct stat s;
+	int opt, ret;
 
 	while ((opt = getopt(argc, argv, "s")) > 0) {
 		switch (opt) {
@@ -31,7 +35,22 @@ static int do_ln(int argc, char *argv[])
 	if (argc != 2)
 		return COMMAND_ERROR_USAGE;
 
-	if (symlink(argv[0], argv[1]) < 0) {
+	target = argv[0];
+	dest = argv[1];
+
+	/* like Linux, create the link inside DEST when it is a directory */
+	if (!stat(dest, &s) && S_ISDIR(s.st_mode)) {
+		char *tmp = xstrdup(target);
+
+		linkname = concat_path_file(dest, posix_basename(tmp));
+		free(tmp);
+		dest = linkname;
+	}
+
+	ret = symlink(target, dest);
+	free(linkname);
+
+	if (ret < 0) {
 		perror("ln");
 		return 1;
 	}
@@ -39,7 +58,9 @@ static int do_ln(int argc, char *argv[])
 }
 
 BAREBOX_CMD_HELP_START(ln)
-BAREBOX_CMD_HELP_TEXT("Create a symbolic link DEST pointing to SRC.")
+BAREBOX_CMD_HELP_TEXT("Create a symbolic link DEST pointing to SRC. If DEST is an existing")
+BAREBOX_CMD_HELP_TEXT("directory, the link is created in it, named after the last component")
+BAREBOX_CMD_HELP_TEXT("of SRC.")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT("-s", "ignored, all links are symbolic links")

-- 
2.47.3




      parent reply	other threads:[~2026-10-20  7:10 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  7:09 [PATCH 0/9] symlink fixes and improvements Sascha Hauer
2026-10-20  7:09 ` [PATCH 1/9] fs: ramfs: report symlink target length as st_size Sascha Hauer
2026-10-20  7:09 ` [PATCH 2/9] fs: qemu_fw_cfg: " Sascha Hauer
2026-10-20  7:09 ` [PATCH 3/9] fs: ramfs: create symlinks with mode 0777 Sascha Hauer
2026-10-20  7:09 ` [PATCH 4/9] fs: follow absolute symlinks from the root directory Sascha Hauer
2026-10-20  7:09 ` [PATCH 5/9] fs: allow trailing symlinks pointing to " Sascha Hauer
2026-10-20  7:09 ` [PATCH 6/9] fs: symlink: drop references taken by filename_create() Sascha Hauer
2026-10-20  7:09 ` [PATCH 7/9] test: self: dirfd: add tests for absolute symlinks Sascha Hauer
2026-10-20  7:09 ` [PATCH 8/9] commands: ln: accept -s option Sascha Hauer
2026-10-20  7:09 ` Sascha Hauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261020-fs-symlink-fixes-v1-9-e7ac8c29b08a@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox