Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini ln implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config LN |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 10 | //config: bool "ln (4.5 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: ln is used to create hard or soft links between files. |
| 14 | |
| 15 | //applet:IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN, BB_SUID_DROP, ln)) |
| 16 | |
| 17 | //kbuild:lib-$(CONFIG_LN) += ln.o |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | /* BB_AUDIT SUSv3 compliant */ |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 20 | /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 21 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ |
| 22 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 23 | //usage:#define ln_trivial_usage |
| 24 | //usage: "[OPTIONS] TARGET... LINK|DIR" |
| 25 | //usage:#define ln_full_usage "\n\n" |
| 26 | //usage: "Create a link LINK or DIR/TARGET to the specified TARGET(s)\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 27 | //usage: "\n -s Make symlinks instead of hardlinks" |
| 28 | //usage: "\n -f Remove existing destinations" |
| 29 | //usage: "\n -n Don't dereference symlinks - treat like normal file" |
| 30 | //usage: "\n -b Make a backup of the target (if exists) before link operation" |
| 31 | //usage: "\n -S suf Use suffix instead of ~ when making backup files" |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 32 | //usage: "\n -T 2nd arg must be a DIR" |
| 33 | //usage: "\n -v Verbose" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 34 | //usage: |
| 35 | //usage:#define ln_example_usage |
| 36 | //usage: "$ ln -s BusyBox /tmp/ls\n" |
| 37 | //usage: "$ ls -l /tmp/ls\n" |
| 38 | //usage: "lrwxrwxrwx 1 root root 7 Apr 12 18:39 ls -> BusyBox*\n" |
| 39 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 40 | #include "libbb.h" |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 42 | /* This is a NOEXEC applet. Be very careful! */ |
| 43 | |
| 44 | |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 45 | #define LN_SYMLINK (1 << 0) |
| 46 | #define LN_FORCE (1 << 1) |
| 47 | #define LN_NODEREFERENCE (1 << 2) |
| 48 | #define LN_BACKUP (1 << 3) |
| 49 | #define LN_SUFFIX (1 << 4) |
| 50 | #define LN_VERBOSE (1 << 5) |
| 51 | #define LN_LINKFILE (1 << 6) |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 53 | int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 54 | int ln_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 55 | { |
Eric Andersen | 13241df | 2000-10-04 16:02:53 +0000 | [diff] [blame] | 56 | int status = EXIT_SUCCESS; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 57 | int opts; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | char *last; |
| 59 | char *src_name; |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 60 | char *src; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 61 | char *suffix = (char*)"~"; |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 62 | struct stat statbuf; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | int (*link_func)(const char *, const char *); |
| 64 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 65 | opt_complementary = "-1"; /* min one arg */ |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 66 | opts = getopt32(argv, "sfnbS:vT", &suffix); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | |
| 68 | last = argv[argc - 1]; |
| 69 | argv += optind; |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 70 | argc -= optind; |
| 71 | |
| 72 | if ((opts & LN_LINKFILE) && argc > 2) { |
| 73 | bb_error_msg_and_die("-T accepts 2 args max"); |
| 74 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | |
Denys Vlasenko | b0e8ced | 2011-03-21 12:36:35 +0100 | [diff] [blame] | 76 | if (!argv[1]) { |
| 77 | /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | *--argv = last; |
Denys Vlasenko | b0e8ced | 2011-03-21 12:36:35 +0100 | [diff] [blame] | 79 | /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to |
| 80 | * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE" |
| 81 | */ |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 82 | last = bb_get_last_path_component_strip(xstrdup(last)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | do { |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 86 | src_name = NULL; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | src = last; |
| 88 | |
| 89 | if (is_directory(src, |
Denys Vlasenko | f282c6b | 2011-12-18 03:27:46 +0100 | [diff] [blame] | 90 | (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE |
| 91 | ) |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 92 | ) { |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 93 | if (opts & LN_LINKFILE) { |
| 94 | bb_error_msg_and_die("'%s' is a directory", src); |
| 95 | } |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 96 | src_name = xstrdup(*argv); |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 97 | src = concat_path_file(src, bb_get_last_path_component_strip(src_name)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | free(src_name); |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 99 | src_name = src; |
| 100 | } |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 101 | if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 102 | // coreutils: "ln dangling_symlink new_hardlink" works |
| 103 | if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 104 | bb_simple_perror_msg(*argv); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 105 | status = EXIT_FAILURE; |
| 106 | free(src_name); |
| 107 | continue; |
| 108 | } |
Eric Andersen | 815e904 | 2000-06-06 16:15:23 +0000 | [diff] [blame] | 109 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 110 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 111 | if (opts & LN_BACKUP) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 112 | char *backup; |
| 113 | backup = xasprintf("%s%s", src, suffix); |
| 114 | if (rename(src, backup) < 0 && errno != ENOENT) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 115 | bb_simple_perror_msg(src); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 116 | status = EXIT_FAILURE; |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 117 | free(backup); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 118 | continue; |
| 119 | } |
| 120 | free(backup); |
| 121 | /* |
| 122 | * When the source and dest are both hard links to the same |
| 123 | * inode, a rename may succeed even though nothing happened. |
| 124 | * Therefore, always unlink(). |
| 125 | */ |
| 126 | unlink(src); |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 127 | } else if (opts & LN_FORCE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 128 | unlink(src); |
| 129 | } |
| 130 | |
| 131 | link_func = link; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 132 | if (opts & LN_SYMLINK) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 133 | link_func = symlink; |
| 134 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 135 | |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 136 | if (opts & LN_VERBOSE) { |
| 137 | printf("'%s' -> '%s'\n", src, *argv); |
| 138 | } |
| 139 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 140 | if (link_func(*argv, src) != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 141 | bb_simple_perror_msg(src); |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 142 | status = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | free(src_name); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 146 | } while ((++argv)[1]); |
| 147 | |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 148 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 149 | } |