3
0

ln.c 3.9 KB

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