Fix ln(1) symbolic link handling
This was broken in multiple ways. For instance, the overlay- check of identical files (name and target) was omitted for symbolic links for some reason. While at it, I fixed the stat-handling, improved the error- messages so the right paths were shown and removed the illegimite bail-out when the target-fstatat failed (we want only a warning here as well).
This commit is contained in:
parent
d23cc72490
commit
78b285deb6
61
ln.c
61
ln.c
|
@ -4,6 +4,7 @@
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <libgen.h>
|
#include <libgen.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
@ -18,10 +19,10 @@ usage(void)
|
||||||
int
|
int
|
||||||
main(int argc, char *argv[])
|
main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
char *fname, *to;
|
char *targetdir = ".", *target = NULL;
|
||||||
int sflag = 0, fflag = 0, hasto = 0, dirfd = AT_FDCWD, ret = 0,
|
int ret = 0, sflag = 0, fflag = 0, dirfd = AT_FDCWD,
|
||||||
flags = AT_SYMLINK_FOLLOW;
|
hastarget = 0, flags = AT_SYMLINK_FOLLOW;
|
||||||
struct stat st, tost;
|
struct stat st, tst;
|
||||||
|
|
||||||
ARGBEGIN {
|
ARGBEGIN {
|
||||||
case 'f':
|
case 'f':
|
||||||
|
@ -43,15 +44,16 @@ main(int argc, char *argv[])
|
||||||
if (!argc)
|
if (!argc)
|
||||||
usage();
|
usage();
|
||||||
|
|
||||||
fname = sflag ? "symlink" : "link";
|
if (argc > 1) {
|
||||||
|
|
||||||
if (argc >= 2) {
|
|
||||||
if (!stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) {
|
if (!stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) {
|
||||||
if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0)
|
if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0)
|
||||||
eprintf("open %s:", argv[argc - 1]);
|
eprintf("open %s:", argv[argc - 1]);
|
||||||
|
targetdir = argv[argc - 1];
|
||||||
|
if (targetdir[strlen(targetdir) - 1] == '/')
|
||||||
|
targetdir[strlen(targetdir) - 1] = '\0';
|
||||||
} else if (argc == 2) {
|
} else if (argc == 2) {
|
||||||
to = argv[argc - 1];
|
hastarget = 1;
|
||||||
hasto = 1;
|
target = argv[argc - 1];
|
||||||
} else {
|
} else {
|
||||||
eprintf("%s: not a directory\n", argv[argc - 1]);
|
eprintf("%s: not a directory\n", argv[argc - 1]);
|
||||||
}
|
}
|
||||||
|
@ -60,27 +62,34 @@ main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
for (; *argv; argc--, argv++) {
|
for (; *argv; argc--, argv++) {
|
||||||
if (!hasto)
|
if (!hastarget)
|
||||||
to = basename(*argv);
|
target = basename(*argv);
|
||||||
if (!sflag) {
|
|
||||||
if (stat(*argv, &st) < 0) {
|
if (stat(*argv, &st) < 0) {
|
||||||
weprintf("stat %s:", *argv);
|
weprintf("stat %s:", *argv);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
continue;
|
continue;
|
||||||
} else if (fstatat(dirfd, to, &tost, AT_SYMLINK_NOFOLLOW) < 0) {
|
} else if (fstatat(dirfd, target, &tst, AT_SYMLINK_NOFOLLOW) < 0) {
|
||||||
if (errno != ENOENT)
|
if (errno != ENOENT) {
|
||||||
eprintf("stat %s:", to);
|
weprintf("fstatat %s %s:", targetdir, target);
|
||||||
} else if (st.st_dev == tost.st_dev && st.st_ino == tost.st_ino) {
|
|
||||||
weprintf("%s and %s are the same file\n", *argv, *argv);
|
|
||||||
ret = 1;
|
ret = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
} else if (st.st_dev == tst.st_dev && st.st_ino == tst.st_ino) {
|
||||||
|
weprintf("%s and %s/%s are the same file\n", *argv, targetdir, target);
|
||||||
|
ret = 1;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (fflag)
|
|
||||||
unlinkat(dirfd, to, 0);
|
if (fflag && unlinkat(dirfd, target, 0) < 0 && errno != ENOENT) {
|
||||||
if ((!sflag ? linkat(AT_FDCWD, *argv, dirfd, to, flags)
|
weprintf("unlinkat %s %s:", targetdir, target);
|
||||||
: symlinkat(*argv, dirfd, to)) < 0) {
|
ret = 1;
|
||||||
weprintf("%s %s <- %s:", fname, *argv, to);
|
continue;
|
||||||
|
}
|
||||||
|
if ((sflag ? symlinkat(*argv, dirfd, target) :
|
||||||
|
linkat(AT_FDCWD, *argv, dirfd, target, flags)) < 0) {
|
||||||
|
weprintf("%s %s <- %s/%s:", sflag ? "symlinkat" : "linkat",
|
||||||
|
*argv, targetdir, target);
|
||||||
ret = 1;
|
ret = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user