Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix --keep-foreign when the target is missing completely #109

Merged
merged 2 commits into from
May 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,16 @@ static int readConfig(struct alternativeSet *set, const char *title,

static int isLink(char *path) {
struct stat sbuf;
int rc = 0;

rc = lstat(path, &sbuf);
if (!rc) {
rc = S_ISLNK(sbuf.st_mode);
}
return rc;
if (lstat(path, &sbuf))
return 0;
return !!S_ISLNK(sbuf.st_mode);
}

static int fileExists(char *path) {
struct stat sbuf;

return !stat(path, &sbuf);
}

static int facilityBelongsToUs(char *facility, const char *altDir) {
Expand Down Expand Up @@ -536,7 +539,17 @@ static int makeLinks(struct linkSet *l, const char *altDir, int flags) {
sl = alloca(strlen(altDir) + strlen(l->title) + 2);
sprintf(sl, "%s/%s", altDir, l->title);

if (isLink(l->facility) && (!FL_KEEP_FOREIGN(flags) || facilityBelongsToUs(l->facility, altDir)) ) {
if (fileExists(l->facility) && !isLink(l->facility)) {
fprintf(
stderr,
_("failed to link %s -> %s: %s exists and it is not a symlink\n"),
l->facility, sl, l->facility);
} else if (FL_KEEP_FOREIGN(flags) && isLink(l->facility) && !facilityBelongsToUs(l->facility, altDir)) {
fprintf(
stderr,
_("failed to link %s -> %s: --keep-foreign was set and link %s points outside %s\n"),
l->facility, sl, l->facility, altDir);
} else {
if (FL_TEST(flags)) {
printf(_("would link %s -> %s\n"), l->facility, sl);
} else {
Expand All @@ -553,11 +566,7 @@ static int makeLinks(struct linkSet *l, const char *altDir, int flags) {
}
}
}
} else
fprintf(
stderr,
_("failed to link %s -> %s: %s exists and it is either not a symlink or --keep-foreign was set and link points outside %s\n"),
l->facility, sl, l->facility, altDir);
}

if (FL_TEST(flags)) {
printf(_("would link %s -> %s\n"), sl, l->target);
Expand Down
Loading