ln.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini ln implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //config:config LN
  10. //config: bool "ln (4.9 kb)"
  11. //config: default y
  12. //config: help
  13. //config: ln is used to create hard or soft links between files.
  14. //applet:IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN, BB_SUID_DROP, ln))
  15. //kbuild:lib-$(CONFIG_LN) += ln.o
  16. /* BB_AUDIT SUSv3 compliant */
  17. /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
  18. /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
  19. //usage:#define ln_trivial_usage
  20. //usage: "[-sfnbtv] [-S SUF] TARGET... LINK|DIR"
  21. //usage:#define ln_full_usage "\n\n"
  22. //usage: "Create a link LINK or DIR/TARGET to the specified TARGET(s)\n"
  23. //usage: "\n -s Make symlinks instead of hardlinks"
  24. //usage: "\n -f Remove existing destinations"
  25. //usage: "\n -n Don't dereference symlinks - treat like normal file"
  26. //usage: "\n -b Make a backup of the target (if exists) before link operation"
  27. //usage: "\n -S SUF Use suffix instead of ~ when making backup files"
  28. //usage: "\n -T Treat LINK as a file, not DIR"
  29. //usage: "\n -v Verbose"
  30. //usage:
  31. //usage:#define ln_example_usage
  32. //usage: "$ ln -s BusyBox /tmp/ls\n"
  33. //usage: "$ ls -l /tmp/ls\n"
  34. //usage: "lrwxrwxrwx 1 root root 7 Apr 12 18:39 ls -> BusyBox*\n"
  35. #include "libbb.h"
  36. /* This is a NOEXEC applet. Be very careful! */
  37. #define LN_SYMLINK (1 << 0)
  38. #define LN_FORCE (1 << 1)
  39. #define LN_NODEREFERENCE (1 << 2)
  40. #define LN_BACKUP (1 << 3)
  41. #define LN_SUFFIX (1 << 4)
  42. #define LN_VERBOSE (1 << 5)
  43. #define LN_LINKFILE (1 << 6)
  44. int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  45. int ln_main(int argc, char **argv)
  46. {
  47. int status = EXIT_SUCCESS;
  48. int opts;
  49. char *last;
  50. char *src_name;
  51. char *src;
  52. char *suffix = (char*)"~";
  53. struct stat statbuf;
  54. int (*link_func)(const char *, const char *);
  55. opts = getopt32(argv, "^" "sfnbS:vT" "\0" "-1", &suffix);
  56. /*
  57. -s, --symbolic make symbolic links instead of hard links
  58. -f, --force remove existing destination files
  59. -n, --no-dereference treat LINK_NAME as a normal file if it is a symbolic link to a directory
  60. -b like --backup but does not accept an argument
  61. --backup[=CONTROL] make a backup of each existing destination file
  62. -S, --suffix=SUFFIX override the usual backup suffix
  63. -v, --verbose
  64. -T, --no-target-directory
  65. -d, -F, --directory allow the superuser to attempt to hard link directories
  66. -i, --interactive prompt whether to remove destinations
  67. -L, --logical dereference TARGETs that are symbolic links
  68. -P, --physical make hard links directly to symbolic links
  69. -r, --relative create symbolic links relative to link location
  70. -t, --target-directory=DIRECTORY specify the DIRECTORY in which to create the links
  71. */
  72. last = argv[argc - 1];
  73. argv += optind;
  74. argc -= optind;
  75. if ((opts & LN_LINKFILE) && argc > 2) {
  76. bb_simple_error_msg_and_die("-T accepts 2 args max");
  77. }
  78. if (!argv[1]) {
  79. /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */
  80. *--argv = last;
  81. /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to
  82. * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE"
  83. */
  84. last = bb_get_last_path_component_strip(xstrdup(last));
  85. }
  86. do {
  87. src_name = NULL;
  88. src = last;
  89. if (is_directory(src,
  90. /*followlinks:*/ !(opts & (LN_NODEREFERENCE|LN_LINKFILE))
  91. /* Why LN_LINKFILE does not follow links:
  92. * -T/--no-target-directory implies -n/--no-dereference
  93. */
  94. )
  95. ) {
  96. if (opts & LN_LINKFILE) {
  97. bb_error_msg_and_die("'%s' is a directory", src);
  98. }
  99. src_name = xstrdup(*argv);
  100. src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
  101. free(src_name);
  102. src_name = src;
  103. }
  104. if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
  105. // coreutils: "ln dangling_symlink new_hardlink" works
  106. if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
  107. bb_simple_perror_msg(*argv);
  108. status = EXIT_FAILURE;
  109. free(src_name);
  110. continue;
  111. }
  112. }
  113. if (opts & LN_BACKUP) {
  114. char *backup;
  115. backup = xasprintf("%s%s", src, suffix);
  116. if (rename(src, backup) < 0 && errno != ENOENT) {
  117. bb_simple_perror_msg(src);
  118. status = EXIT_FAILURE;
  119. free(backup);
  120. continue;
  121. }
  122. free(backup);
  123. /*
  124. * When the source and dest are both hard links to the same
  125. * inode, a rename may succeed even though nothing happened.
  126. * Therefore, always unlink().
  127. */
  128. unlink(src);
  129. } else if (opts & LN_FORCE) {
  130. unlink(src);
  131. }
  132. link_func = link;
  133. if (opts & LN_SYMLINK) {
  134. link_func = symlink;
  135. }
  136. if (opts & LN_VERBOSE) {
  137. printf("'%s' -> '%s'\n", src, *argv);
  138. }
  139. if (link_func(*argv, src) != 0) {
  140. bb_simple_perror_msg(src);
  141. status = EXIT_FAILURE;
  142. }
  143. free(src_name);
  144. } while ((++argv)[1]);
  145. return status;
  146. }