blob: 3b822e8c7fa0b2f92bc48dcf66f7dadb62021baf [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 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
Rob Landley3071e2f2005-04-29 22:13:04 +000011/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000012/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
13
Pere Orga34425382011-03-31 14:43:25 +020014//usage:#define ln_trivial_usage
15//usage: "[OPTIONS] TARGET... LINK|DIR"
16//usage:#define ln_full_usage "\n\n"
17//usage: "Create a link LINK or DIR/TARGET to the specified TARGET(s)\n"
Pere Orga34425382011-03-31 14:43:25 +020018//usage: "\n -s Make symlinks instead of hardlinks"
19//usage: "\n -f Remove existing destinations"
20//usage: "\n -n Don't dereference symlinks - treat like normal file"
21//usage: "\n -b Make a backup of the target (if exists) before link operation"
22//usage: "\n -S suf Use suffix instead of ~ when making backup files"
Simon B44642d12012-05-06 13:18:35 +020023//usage: "\n -T 2nd arg must be a DIR"
24//usage: "\n -v Verbose"
Pere Orga34425382011-03-31 14:43:25 +020025//usage:
26//usage:#define ln_example_usage
27//usage: "$ ln -s BusyBox /tmp/ls\n"
28//usage: "$ ls -l /tmp/ls\n"
29//usage: "lrwxrwxrwx 1 root root 7 Apr 12 18:39 ls -> BusyBox*\n"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000032
Denis Vlasenko99912ca2007-04-10 15:43:37 +000033/* This is a NOEXEC applet. Be very careful! */
34
35
Simon B44642d12012-05-06 13:18:35 +020036#define LN_SYMLINK (1 << 0)
37#define LN_FORCE (1 << 1)
38#define LN_NODEREFERENCE (1 << 2)
39#define LN_BACKUP (1 << 3)
40#define LN_SUFFIX (1 << 4)
41#define LN_VERBOSE (1 << 5)
42#define LN_LINKFILE (1 << 6)
Eric Andersenf6be9441999-10-13 21:12:06 +000043
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000044int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000045int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000046{
Eric Andersen13241df2000-10-04 16:02:53 +000047 int status = EXIT_SUCCESS;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010048 int opts;
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 char *last;
50 char *src_name;
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000051 char *src;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000052 char *suffix = (char*)"~";
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000053 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 int (*link_func)(const char *, const char *);
55
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010056 opt_complementary = "-1"; /* min one arg */
Simon B44642d12012-05-06 13:18:35 +020057 opts = getopt32(argv, "sfnbS:vT", &suffix);
Manuel Novoa III cad53642003-03-19 09:13:01 +000058
59 last = argv[argc - 1];
60 argv += optind;
Simon B44642d12012-05-06 13:18:35 +020061 argc -= optind;
62
63 if ((opts & LN_LINKFILE) && argc > 2) {
64 bb_error_msg_and_die("-T accepts 2 args max");
65 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000066
Denys Vlasenkob0e8ced2011-03-21 12:36:35 +010067 if (!argv[1]) {
68 /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */
Manuel Novoa III cad53642003-03-19 09:13:01 +000069 *--argv = last;
Denys Vlasenkob0e8ced2011-03-21 12:36:35 +010070 /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to
71 * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE"
72 */
Denis Vlasenko818322b2007-09-24 18:27:04 +000073 last = bb_get_last_path_component_strip(xstrdup(last));
Manuel Novoa III cad53642003-03-19 09:13:01 +000074 }
75
76 do {
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000077 src_name = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 src = last;
79
80 if (is_directory(src,
Denys Vlasenkof282c6b2011-12-18 03:27:46 +010081 (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE
82 )
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000083 ) {
Simon B44642d12012-05-06 13:18:35 +020084 if (opts & LN_LINKFILE) {
85 bb_error_msg_and_die("'%s' is a directory", src);
86 }
Rob Landleyd921b2e2006-08-03 15:41:12 +000087 src_name = xstrdup(*argv);
Denis Vlasenko818322b2007-09-24 18:27:04 +000088 src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 free(src_name);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000090 src_name = src;
91 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010092 if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000093 // coreutils: "ln dangling_symlink new_hardlink" works
94 if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000095 bb_simple_perror_msg(*argv);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000096 status = EXIT_FAILURE;
97 free(src_name);
98 continue;
99 }
Eric Andersen815e9042000-06-06 16:15:23 +0000100 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100102 if (opts & LN_BACKUP) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000103 char *backup;
104 backup = xasprintf("%s%s", src, suffix);
105 if (rename(src, backup) < 0 && errno != ENOENT) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000106 bb_simple_perror_msg(src);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000107 status = EXIT_FAILURE;
Rob Landley3071e2f2005-04-29 22:13:04 +0000108 free(backup);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000109 continue;
110 }
111 free(backup);
112 /*
113 * When the source and dest are both hard links to the same
114 * inode, a rename may succeed even though nothing happened.
115 * Therefore, always unlink().
116 */
117 unlink(src);
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100118 } else if (opts & LN_FORCE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 unlink(src);
120 }
121
122 link_func = link;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +0100123 if (opts & LN_SYMLINK) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000124 link_func = symlink;
125 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000126
Simon B44642d12012-05-06 13:18:35 +0200127 if (opts & LN_VERBOSE) {
128 printf("'%s' -> '%s'\n", src, *argv);
129 }
130
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 if (link_func(*argv, src) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000132 bb_simple_perror_msg(src);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +0000133 status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 }
135
136 free(src_name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000137
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 } while ((++argv)[1]);
139
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000140 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000141}