From cb49cb30fe65d4ee317572f232f7017a8c1fe6a2 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 21 Apr 2017 09:13:00 +0000 Subject: Add -n to s6-ln; document its nonposixness --- doc/s6-ln.html | 25 ++++++++++++++++++++++--- src/skaembutils/s6-ln.c | 8 +++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/doc/s6-ln.html b/doc/s6-ln.html index 3ca0245..1417c35 100644 --- a/doc/s6-ln.html +++ b/doc/s6-ln.html @@ -25,7 +25,7 @@

Interface

-     s6-ln [ -s ] [ -f ] [ -L ] [ -P ] old new
+     s6-ln [ -n ] [ -s ] [ -f ] [ -L ] [ -P ] source... target
 

@@ -33,12 +33,31 @@ ln utility.

+

Options

+ + +

Posixness

- s6-ln is suitable as a Single Unix + s6-ln would be suitable as a Single Unix ln -program. +program, except that POSIX mandates that a preexisting target must first +be unlink()ed and then (sym)link()ed, which prevents +atomic replacements. + s6-ln aims to be reliable and allow for atomic replacements, so it +deviates from POSIX on that point: when target exists and needs to +be replaced, s6-ln first creates a (sym)link to a unique temporary +name, then rename()s the temporary name to target. This +behaviour makes sure that target is atomically replaced - there's +no point in time where it does not exist.

diff --git a/src/skaembutils/s6-ln.c b/src/skaembutils/s6-ln.c index 59b85a4..65a650f 100644 --- a/src/skaembutils/s6-ln.c +++ b/src/skaembutils/s6-ln.c @@ -19,7 +19,7 @@ #include #include -#define USAGE "s6-ln [ -s ] [ -f ] [ -L ] [ -P ] src... dest" +#define USAGE "s6-ln [ -n ] [ -s ] [ -f ] [ -L ] [ -P ] src... dest" typedef int linkfunc_t (char const *, char const *) ; typedef linkfunc_t *linkfunc_t_ref ; @@ -78,15 +78,17 @@ int main (int argc, char const *const *argv) { linkfunc_t_ref mylink = &link ; /* default to system behaviour */ ln_t_ref f = &noforce ; + int nodir = 0 ; PROG = "s6-ln" ; { subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "sfLP", &l) ; + int opt = subgetopt_r(argc, argv, "nsfLP", &l) ; if (opt == -1) break ; switch (opt) { + case 'n' : nodir = 1 ; break ; case 's': mylink = &symlink ; break ; case 'f': f = &force ; break ; case 'L': if (mylink != &symlink) mylink = &linkderef ; break ; @@ -120,7 +122,7 @@ int main (int argc, char const *const *argv) { struct stat st ; - if (stat(argv[1], &st) < 0) + if (nodir ? lstat(argv[1], &st) : stat(argv[1], &st) < 0) { if (errno != ENOENT) strerr_diefu2sys(111, "stat ", argv[1]) ; (*f)(argv[0], argv[1], mylink) ; -- cgit v1.2.3