Skip to content

Commit

Permalink
Make --symlink idempotent
Browse files Browse the repository at this point in the history
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.

[Anders Blomdell: use readlink_malloc()]
[smcv: Squash multiple changes into one; coding style fixes]
Resolves: #549
Signed-off-by: Jakob Kaivo <jkk@ung.org>
Co-authored-by: Anders Blomdell
Co-authored-by: Simon McVittie <smcv@collabora.com>
Signed-off-by: Simon McVittie <smcv@collabora.com>
  • Loading branch information
jkaivo authored and smcv committed Jan 31, 2024
1 parent 4872aef commit 381c636
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion bubblewrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,25 @@ setup_newroot (bool unshare_pid,
case SETUP_MAKE_SYMLINK:
assert (op->source != NULL); /* guaranteed by the constructor */
if (symlink (op->source, dest) != 0)
die_with_error ("Can't make symlink at %s", op->dest);
{
if (errno == EEXIST)
{
cleanup_free char *existing = readlink_malloc (dest);
if (existing == NULL)
{
if (errno == EINVAL)
die ("Can't make symlink at %s: destination exists and is not a symlink", op->dest);
else
die_with_error ("Can't make symlink at %s: destination exists, and cannot read symlink target", op->dest);
}

if (strcmp (existing, op->source) == 0)
break;

die ("Can't make symlink at %s: existing destination is %s", op->dest, existing);
}
die_with_error ("Can't make symlink at %s", op->dest);
}
break;

case SETUP_SET_HOSTNAME:
Expand Down

0 comments on commit 381c636

Please sign in to comment.