From 83a1ae525fcf38b35635aa0849ede0ca38b8c438 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Fri, 21 Apr 2023 18:29:37 -0400 Subject: [PATCH] Make --symlink idempotent When creating symlinks specified with --symlink, check errno if creation fails. If the reason was EEXIST, check the path of the existing symlink. If it matches what was desired, continue as normal. --- bubblewrap.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bubblewrap.c b/bubblewrap.c index 8322ea03..800c8303 100644 --- a/bubblewrap.c +++ b/bubblewrap.c @@ -1474,7 +1474,20 @@ setup_newroot (bool unshare_pid, case SETUP_MAKE_SYMLINK: assert (op->source != NULL); /* guaranteed by the constructor */ if (symlink (op->source, dest) != 0) + { + if (errno == EEXIST) + { + char existing[PATH_MAX] = ""; + readlink (dest, existing, sizeof (existing)); + if (strcmp(existing, op->source) == 0) + { + break; + } + die_with_error ("Can't make symlink at %s (existing symlink points to %s)", + op->dest, existing); + } die_with_error ("Can't make symlink at %s", op->dest); + } break; case SETUP_SET_HOSTNAME: