blob: 2dcf79c07e44203fdd6ece85c13da6186afd799d [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6be9441999-10-13 21:12:06 +00002/*
3 * Mini ln implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf6be9441999-10-13 21:12:06 +00008 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +01009//config:config LN
Denys Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "ln (4.9 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010011//config: default y
12//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: ln is used to create hard or soft links between files.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014
15//applet:IF_LN(APPLET_NOEXEC(ln, ln, BB_DIR_BIN, BB_SUID_DROP, ln))
16
17//kbuild:lib-$(CONFIG_LN) += ln.o
Eric Andersenf6be9441999-10-13 21:12:06 +000018
Manuel Novoa III cad53642003-03-19 09:13:01 +000019/* BB_AUDIT SUSv3 compliant */
Rob Landley3071e2f2005-04-29 22:13:04 +000020/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000021/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
22
Pere Orga34425382011-03-31 14:43:25 +020023//usage:#define ln_trivial_usage
Denys Vlasenko84d5edd2020-12-13 22:34:05 +010024//usage: "[-sfnbtv] [-S SUF] TARGET... LINK|DIR"
Pere Orga34425382011-03-31 14:43:25 +020025//usage:#define ln_full_usage "\n\n"
26//usage: "Create a link LINK or DIR/TARGET to the specified TARGET(s)\n"
Pere Orga34425382011-03-31 14:43:25 +020027//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"
Denys Vlasenko84d5edd2020-12-13 22:34:05 +010031//usage: "\n -S SUF Use suffix instead of ~ when making backup files"
John L. Hammond94f607a2019-05-09 09:22:09 -050032//usage: "\n -T Treat LINK as a file, not DIR"
Simon B44642d12012-05-06 13:18:35 +020033//usage: "\n -v Verbose"
Pere Orga34425382011-03-31 14:43:25 +020034//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 Vlasenkob6adbf12007-05-26 19:00:18 +000040#include "libbb.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000041
Denis Vlasenko99912ca2007-04-10 15:43:37 +000042/* This is a NOEXEC applet. Be very careful! */
43
Simon B44642d12012-05-06 13:18:35 +020044#define LN_SYMLINK (1 << 0)
45#define LN_FORCE (1 << 1)
46#define LN_NODEREFERENCE (1 << 2)
47#define LN_BACKUP (1 << 3)
48#define LN_SUFFIX (1 << 4)
49#define LN_VERBOSE (1 << 5)
50#define LN_LINKFILE (1 << 6)
Eric Andersenf6be9441999-10-13 21:12:06 +000051
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000052int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000053int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000054{
Eric Andersen13241df2000-10-04 16:02:53 +000055 int status = EXIT_SUCCESS;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010056 int opts;
Manuel Novoa III cad53642003-03-19 09:13:01 +000057 char *last;
58 char *src_name;
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000059 char *src;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000060 char *suffix = (char*)"~";
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000061 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 int (*link_func)(const char *, const char *);
63
Denys Vlasenko22542ec2017-08-08 21:55:02 +020064 opts = getopt32(argv, "^" "sfnbS:vT" "\0" "-1", &suffix);
Kaarle Ritvanen71b268c2019-10-08 14:07:50 +020065/*
66 -s, --symbolic make symbolic links instead of hard links
67 -f, --force remove existing destination files
68 -n, --no-dereference treat LINK_NAME as a normal file if it is a symbolic link to a directory
69 -b like --backup but does not accept an argument
70 --backup[=CONTROL] make a backup of each existing destination file
71 -S, --suffix=SUFFIX override the usual backup suffix
72 -v, --verbose
73 -T, --no-target-directory
74 -d, -F, --directory allow the superuser to attempt to hard link directories
75 -i, --interactive prompt whether to remove destinations
76 -L, --logical dereference TARGETs that are symbolic links
77 -P, --physical make hard links directly to symbolic links
78 -r, --relative create symbolic links relative to link location
79 -t, --target-directory=DIRECTORY specify the DIRECTORY in which to create the links
80 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 last = argv[argc - 1];
82 argv += optind;
Simon B44642d12012-05-06 13:18:35 +020083 argc -= optind;
84
85 if ((opts & LN_LINKFILE) && argc > 2) {
James Byrne69374872019-07-02 11:35:03 +020086 bb_simple_error_msg_and_die("-T accepts 2 args max");
Simon B44642d12012-05-06 13:18:35 +020087 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000088
Denys Vlasenkob0e8ced2011-03-21 12:36:35 +010089 if (!argv[1]) {
90 /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 *--argv = last;
Denys Vlasenkob0e8ced2011-03-21 12:36:35 +010092 /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to
93 * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE"
94 */
Denis Vlasenko818322b2007-09-24 18:27:04 +000095 last = bb_get_last_path_component_strip(xstrdup(last));
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 }
97
98 do {
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000099 src_name = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 src = last;
101
102 if (is_directory(src,
Kaarle Ritvanen71b268c2019-10-08 14:07:50 +0200103 /*followlinks:*/ !(opts & (LN_NODEREFERENCE|LN_LINKFILE))
104 /* Why LN_LINKFILE does not follow links:
105 * -T/--no-target-directory implies -n/--no-dereference
106 */
107 )
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000108 ) {
Simon B44642d12012-05-06 13:18:35 +0200109 if (opts & LN_LINKFILE) {
110 bb_error_msg_and_die("'%s' is a directory", src);
111 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000112 src_name = xstrdup(*argv);
Denis Vlasenko818322b2007-09-24 18:27:04 +0000113 src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
Manuel Novoa III cad53642003-03-19 09:13:01 +0000114 free(src_name);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +0000115 src_name = src;
116 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100117 if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000118 // coreutils: "ln dangling_symlink new_hardlink" works
119 if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000120 bb_simple_perror_msg(*argv);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000121 status = EXIT_FAILURE;
122 free(src_name);
123 continue;
124 }
Eric Andersen815e9042000-06-06 16:15:23 +0000125 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000126
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100127 if (opts & LN_BACKUP) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000128 char *backup;
129 backup = xasprintf("%s%s", src, suffix);
130 if (rename(src, backup) < 0 && errno != ENOENT) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000131 bb_simple_perror_msg(src);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000132 status = EXIT_FAILURE;
Rob Landley3071e2f2005-04-29 22:13:04 +0000133 free(backup);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000134 continue;
135 }
136 free(backup);
137 /*
138 * When the source and dest are both hard links to the same
139 * inode, a rename may succeed even though nothing happened.
140 * Therefore, always unlink().
141 */
142 unlink(src);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100143 } else if (opts & LN_FORCE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000144 unlink(src);
145 }
146
147 link_func = link;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100148 if (opts & LN_SYMLINK) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 link_func = symlink;
150 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000151
Simon B44642d12012-05-06 13:18:35 +0200152 if (opts & LN_VERBOSE) {
153 printf("'%s' -> '%s'\n", src, *argv);
154 }
155
Manuel Novoa III cad53642003-03-19 09:13:01 +0000156 if (link_func(*argv, src) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000157 bb_simple_perror_msg(src);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +0000158 status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000159 }
160
161 free(src_name);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000162 } while ((++argv)[1]);
163
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000164 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000165}