aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/ln.c
blob: 306f0cac9a85d72901cc3c71642bd6745935d1d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* ln.c - Create filesystem links
 *
 * Copyright 2012 Andre Renaud <andre@bluewatersys.com>
 *
 * See http://opengroup.org/onlinepubs/9699919799/utilities/ln.html

USE_LN(NEWTOY(ln, "<1t:Tvnfs", TOYFLAG_BIN))

config LN
  bool "ln"
  default y
  help
    usage: ln [-sfnv] [-t DIR] [FROM...] TO

    Create a link between FROM and TO.
    One/two/many arguments work like "mv" or "cp".

    -s	Create a symbolic link
    -f	Force the creation of the link, even if TO already exists
    -n	Symlink at TO treated as file
    -t	Create links in DIR
    -T	TO always treated as file, max 2 arguments
    -v	Verbose
*/

#define FOR_ln
#include "toys.h"

GLOBALS(
  char *t;
)

void ln_main(void)
{
  char *dest = TT.t ? TT.t : toys.optargs[--toys.optc], *new;
  struct stat buf;
  int i;

  // With one argument, create link in current directory.
  if (!toys.optc) {
    toys.optc++;
    dest=".";
  }

  if (FLAG(T) && toys.optc>1) help_exit("Max 2 arguments");
  // Is destination a directory?
  if (!((FLAG(n)||FLAG(T)) ? lstat : stat)(dest, &buf)) {
    i = S_ISDIR(buf.st_mode);

    if ((FLAG(T) && i) || (!i && (toys.optc>1 || TT.t)))
      error_exit("'%s' %s a directory", dest, i ? "is" : "not");
  } else buf.st_mode = 0;

  for (i=0; i<toys.optc; i++) {
    int rc;
    char *oldnew, *try = toys.optargs[i];

    if (S_ISDIR(buf.st_mode)) new = xmprintf("%s/%s", dest, basename(try));
    else new = dest;

    // Force needs to unlink the existing target (if any). Do that by creating
    // a temp version and renaming it over the old one, so we can retain the
    // old file in cases we can't replace it (such as hardlink between mounts).
    oldnew = new;
    if (FLAG(f)) {
      new = xmprintf("%s_XXXXXX", new);
      rc = mkstemp(new);
      if (rc >= 0) {
        close(rc);
        if (unlink(new)) perror_msg("unlink '%s'", new);
      }
    }

    rc = FLAG(s) ? symlink(try, new) : link(try, new);
    if (FLAG(f)) {
      if (!rc) {
        int temp;

        rc = rename(new, oldnew);
        temp = errno;
        if (rc && unlink(new)) perror_msg("unlink '%s'", new);
        errno = temp;
      }
      free(new);
      new = oldnew;
    }
    if (rc) perror_msg("cannot create %s link from '%s' to '%s'",
                       FLAG(s) ? "symbolic" : "hard", try, new);
    else if (FLAG(v)) fprintf(stderr, "'%s' -> '%s'\n", new, try);

    if (new != dest) free(new);
  }
}