3
0

ln.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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"
  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: "[OPTIONS] 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 2nd arg must be a 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. opt_complementary = "-1"; /* min one arg */
  56. opts = getopt32(argv, "sfnbS:vT", &suffix);
  57. last = argv[argc - 1];
  58. argv += optind;
  59. argc -= optind;
  60. if ((opts & LN_LINKFILE) && argc > 2) {
  61. bb_error_msg_and_die("-T accepts 2 args max");
  62. }
  63. if (!argv[1]) {
  64. /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */
  65. *--argv = last;
  66. /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to
  67. * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE"
  68. */
  69. last = bb_get_last_path_component_strip(xstrdup(last));
  70. }
  71. do {
  72. src_name = NULL;
  73. src = last;
  74. if (is_directory(src,
  75. (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE
  76. )
  77. ) {
  78. if (opts & LN_LINKFILE) {
  79. bb_error_msg_and_die("'%s' is a directory", src);
  80. }
  81. src_name = xstrdup(*argv);
  82. src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
  83. free(src_name);
  84. src_name = src;
  85. }
  86. if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
  87. // coreutils: "ln dangling_symlink new_hardlink" works
  88. if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
  89. bb_simple_perror_msg(*argv);
  90. status = EXIT_FAILURE;
  91. free(src_name);
  92. continue;
  93. }
  94. }
  95. if (opts & LN_BACKUP) {
  96. char *backup;
  97. backup = xasprintf("%s%s", src, suffix);
  98. if (rename(src, backup) < 0 && errno != ENOENT) {
  99. bb_simple_perror_msg(src);
  100. status = EXIT_FAILURE;
  101. free(backup);
  102. continue;
  103. }
  104. free(backup);
  105. /*
  106. * When the source and dest are both hard links to the same
  107. * inode, a rename may succeed even though nothing happened.
  108. * Therefore, always unlink().
  109. */
  110. unlink(src);
  111. } else if (opts & LN_FORCE) {
  112. unlink(src);
  113. }
  114. link_func = link;
  115. if (opts & LN_SYMLINK) {
  116. link_func = symlink;
  117. }
  118. if (opts & LN_VERBOSE) {
  119. printf("'%s' -> '%s'\n", src, *argv);
  120. }
  121. if (link_func(*argv, src) != 0) {
  122. bb_simple_perror_msg(src);
  123. status = EXIT_FAILURE;
  124. }
  125. free(src_name);
  126. } while ((++argv)[1]);
  127. return status;
  128. }