doc: fix a numfmt help section typo
[platform/upstream/coreutils.git] / src / ln.c
1 /* 'ln' program to create links between files.
2    Copyright (C) 1986-2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16 \f
17 /* Written by Mike Parker and David MacKenzie. */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <getopt.h>
23
24 #include "system.h"
25 #include "backupfile.h"
26 #include "error.h"
27 #include "filenamecat.h"
28 #include "file-set.h"
29 #include "hash.h"
30 #include "hash-triple.h"
31 #include "quote.h"
32 #include "relpath.h"
33 #include "same.h"
34 #include "yesno.h"
35 #include "canonicalize.h"
36
37 /* The official name of this program (e.g., no 'g' prefix).  */
38 #define PROGRAM_NAME "ln"
39
40 #define AUTHORS \
41   proper_name ("Mike Parker"), \
42   proper_name ("David MacKenzie")
43
44 /* FIXME: document */
45 static enum backup_type backup_type;
46
47 /* If true, make symbolic links; otherwise, make hard links.  */
48 static bool symbolic_link;
49
50 /* If true, make symbolic links relative  */
51 static bool relative;
52
53 /* If true, hard links are logical rather than physical.  */
54 static bool logical = !!LINK_FOLLOWS_SYMLINKS;
55
56 /* If true, ask the user before removing existing files.  */
57 static bool interactive;
58
59 /* If true, remove existing files unconditionally.  */
60 static bool remove_existing_files;
61
62 /* If true, list each file as it is moved. */
63 static bool verbose;
64
65 /* If true, allow the superuser to *attempt* to make hard links
66    to directories.  However, it appears that this option is not useful
67    in practice, since even the superuser is prohibited from hard-linking
68    directories on most existing systems (Solaris being an exception).  */
69 static bool hard_dir_link;
70
71 /* If nonzero, and the specified destination is a symbolic link to a
72    directory, treat it just as if it were a directory.  Otherwise, the
73    command 'ln --force --no-dereference file symlink-to-dir' deletes
74    symlink-to-dir before creating the new link.  */
75 static bool dereference_dest_dir_symlinks = true;
76
77 /* This is a set of destination name/inode/dev triples for hard links
78    created by ln.  Use this data structure to avoid data loss via a
79    sequence of commands like this:
80    rm -rf a b c; mkdir a b c; touch a/f b/f; ln -f a/f b/f c && rm -r a b */
81 static Hash_table *dest_set;
82
83 /* Initial size of the dest_set hash table.  */
84 enum { DEST_INFO_INITIAL_CAPACITY = 61 };
85
86 static struct option const long_options[] =
87 {
88   {"backup", optional_argument, NULL, 'b'},
89   {"directory", no_argument, NULL, 'F'},
90   {"no-dereference", no_argument, NULL, 'n'},
91   {"no-target-directory", no_argument, NULL, 'T'},
92   {"force", no_argument, NULL, 'f'},
93   {"interactive", no_argument, NULL, 'i'},
94   {"suffix", required_argument, NULL, 'S'},
95   {"target-directory", required_argument, NULL, 't'},
96   {"logical", no_argument, NULL, 'L'},
97   {"physical", no_argument, NULL, 'P'},
98   {"relative", no_argument, NULL, 'r'},
99   {"symbolic", no_argument, NULL, 's'},
100   {"verbose", no_argument, NULL, 'v'},
101   {GETOPT_HELP_OPTION_DECL},
102   {GETOPT_VERSION_OPTION_DECL},
103   {NULL, 0, NULL, 0}
104 };
105
106 /* FILE is the last operand of this command.  Return true if FILE is a
107    directory.  But report an error there is a problem accessing FILE,
108    or if FILE does not exist but would have to refer to an existing
109    directory if it referred to anything at all.  */
110
111 static bool
112 target_directory_operand (char const *file)
113 {
114   char const *b = last_component (file);
115   size_t blen = strlen (b);
116   bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
117   struct stat st;
118   int stat_result =
119     (dereference_dest_dir_symlinks ? stat (file, &st) : lstat (file, &st));
120   int err = (stat_result == 0 ? 0 : errno);
121   bool is_a_dir = !err && S_ISDIR (st.st_mode);
122   if (err && err != ENOENT)
123     error (EXIT_FAILURE, err, _("accessing %s"), quote (file));
124   if (is_a_dir < looks_like_a_dir)
125     error (EXIT_FAILURE, err, _("target %s is not a directory"), quote (file));
126   return is_a_dir;
127 }
128
129 /* Return FROM represented as relative to the dir of TARGET.
130    The result is malloced.  */
131
132 static char *
133 convert_abs_rel (const char *from, const char *target)
134 {
135   char *realtarget = canonicalize_filename_mode (target, CAN_MISSING);
136   char *realfrom = canonicalize_filename_mode (from, CAN_MISSING);
137
138   /* Write to a PATH_MAX buffer.  */
139   char *relative_from = xmalloc (PATH_MAX);
140
141   /* Get dirname to generate paths relative to.  */
142   realtarget[dir_len (realtarget)] = '\0';
143
144   if (!relpath (realfrom, realtarget, relative_from, PATH_MAX))
145     {
146       free (relative_from);
147       relative_from = NULL;
148     }
149
150   free (realtarget);
151   free (realfrom);
152
153   return relative_from ? relative_from : xstrdup (from);
154 }
155
156 /* Make a link DEST to the (usually) existing file SOURCE.
157    Symbolic links to nonexistent files are allowed.
158    Return true if successful.  */
159
160 static bool
161 do_link (const char *source, const char *dest)
162 {
163   struct stat source_stats;
164   struct stat dest_stats;
165   char *dest_backup = NULL;
166   char *rel_source = NULL;
167   bool dest_lstat_ok = false;
168   bool source_is_dir = false;
169   bool ok;
170
171   if (!symbolic_link)
172     {
173        /* Which stat to use depends on whether linkat will follow the
174           symlink.  We can't use the shorter
175           (logical?stat:lstat) (source, &source_stats)
176           since stat might be a function-like macro.  */
177       if ((logical ? stat (source, &source_stats)
178            : lstat (source, &source_stats))
179           != 0)
180         {
181           error (0, errno, _("accessing %s"), quote (source));
182           return false;
183         }
184
185       if (S_ISDIR (source_stats.st_mode))
186         {
187           source_is_dir = true;
188           if (! hard_dir_link)
189             {
190               error (0, 0, _("%s: hard link not allowed for directory"),
191                      quote (source));
192               return false;
193             }
194         }
195     }
196
197   if (remove_existing_files || interactive || backup_type != no_backups)
198     {
199       dest_lstat_ok = (lstat (dest, &dest_stats) == 0);
200       if (!dest_lstat_ok && errno != ENOENT)
201         {
202           error (0, errno, _("accessing %s"), quote (dest));
203           return false;
204         }
205     }
206
207   /* If the current target was created as a hard link to another
208      source file, then refuse to unlink it.  */
209   if (dest_lstat_ok
210       && dest_set != NULL
211       && seen_file (dest_set, dest, &dest_stats))
212     {
213       error (0, 0,
214              _("will not overwrite just-created %s with %s"),
215              quote_n (0, dest), quote_n (1, source));
216       return false;
217     }
218
219   /* If --force (-f) has been specified without --backup, then before
220      making a link ln must remove the destination file if it exists.
221      (with --backup, it just renames any existing destination file)
222      But if the source and destination are the same, don't remove
223      anything and fail right here.  */
224   if ((remove_existing_files
225        /* Ensure that "ln --backup f f" fails here, with the
226           "... same file" diagnostic, below.  Otherwise, subsequent
227           code would give a misleading "file not found" diagnostic.
228           This case is different than the others handled here, since
229           the command in question doesn't use --force.  */
230        || (!symbolic_link && backup_type != no_backups))
231       && dest_lstat_ok
232       /* Allow 'ln -sf --backup k k' to succeed in creating the
233          self-referential symlink, but don't allow the hard-linking
234          equivalent: 'ln -f k k' (with or without --backup) to get
235          beyond this point, because the error message you'd get is
236          misleading.  */
237       && (backup_type == no_backups || !symbolic_link)
238       && (!symbolic_link || stat (source, &source_stats) == 0)
239       && SAME_INODE (source_stats, dest_stats)
240       /* The following detects whether removing DEST will also remove
241          SOURCE.  If the file has only one link then both are surely
242          the same link.  Otherwise check whether they point to the same
243          name in the same directory.  */
244       && (source_stats.st_nlink == 1 || same_name (source, dest)))
245     {
246       error (0, 0, _("%s and %s are the same file"),
247              quote_n (0, source), quote_n (1, dest));
248       return false;
249     }
250
251   if (dest_lstat_ok)
252     {
253       if (S_ISDIR (dest_stats.st_mode))
254         {
255           error (0, 0, _("%s: cannot overwrite directory"), quote (dest));
256           return false;
257         }
258       if (interactive)
259         {
260           fprintf (stderr, _("%s: replace %s? "), program_name, quote (dest));
261           if (!yesno ())
262             return true;
263           remove_existing_files = true;
264         }
265
266       if (backup_type != no_backups)
267         {
268           dest_backup = find_backup_file_name (dest, backup_type);
269           if (rename (dest, dest_backup) != 0)
270             {
271               int rename_errno = errno;
272               free (dest_backup);
273               dest_backup = NULL;
274               if (rename_errno != ENOENT)
275                 {
276                   error (0, rename_errno, _("cannot backup %s"), quote (dest));
277                   return false;
278                 }
279             }
280         }
281     }
282
283   if (relative)
284     source = rel_source = convert_abs_rel (source, dest);
285
286   ok = ((symbolic_link ? symlink (source, dest)
287          : linkat (AT_FDCWD, source, AT_FDCWD, dest,
288                    logical ? AT_SYMLINK_FOLLOW : 0))
289         == 0);
290
291   /* If the attempt to create a link failed and we are removing or
292      backing up destinations, unlink the destination and try again.
293
294      On the surface, POSIX describes an algorithm that states that
295      'ln -f A B' will call unlink() on B before ever attempting
296      link() on A.  But strictly following this has the counterintuitive
297      effect of losing the contents of B, if A does not exist.
298      Fortunately, POSIX 2008 clarified that an application is free
299      to fail early if it can prove that continuing onwards cannot
300      succeed, so we are justified in trying link() before blindly
301      removing B, thus sometimes calling link() a second time during
302      a successful 'ln -f A B'.
303
304      Try to unlink DEST even if we may have backed it up successfully.
305      In some unusual cases (when DEST and DEST_BACKUP are hard-links
306      that refer to the same file), rename succeeds and DEST remains.
307      If we didn't remove DEST in that case, the subsequent symlink or link
308      call would fail.  */
309
310   if (!ok && errno == EEXIST && (remove_existing_files || dest_backup))
311     {
312       if (unlink (dest) != 0)
313         {
314           error (0, errno, _("cannot remove %s"), quote (dest));
315           free (dest_backup);
316           free (rel_source);
317           return false;
318         }
319
320       ok = ((symbolic_link ? symlink (source, dest)
321              : linkat (AT_FDCWD, source, AT_FDCWD, dest,
322                        logical ? AT_SYMLINK_FOLLOW : 0))
323             == 0);
324     }
325
326   if (ok)
327     {
328       /* Right after creating a hard link, do this: (note dest name and
329          source_stats, which are also the just-linked-destinations stats) */
330       record_file (dest_set, dest, &source_stats);
331
332       if (verbose)
333         {
334           if (dest_backup)
335             printf ("%s ~ ", quote (dest_backup));
336           printf ("%s %c> %s\n", quote_n (0, dest), (symbolic_link ? '-' : '='),
337                   quote_n (1, source));
338         }
339     }
340   else
341     {
342       error (0, errno,
343              (symbolic_link
344               ? (errno != ENAMETOOLONG && *source
345                  ? _("failed to create symbolic link %s")
346                  : _("failed to create symbolic link %s -> %s"))
347               : (errno == EMLINK && !source_is_dir
348                  ? _("failed to create hard link to %.0s%s")
349                  : (errno == EDQUOT || errno == EEXIST || errno == ENOSPC
350                     || errno == EROFS)
351                  ? _("failed to create hard link %s")
352                  : _("failed to create hard link %s => %s"))),
353              quote_n (0, dest), quote_n (1, source));
354
355       if (dest_backup)
356         {
357           if (rename (dest_backup, dest) != 0)
358             error (0, errno, _("cannot un-backup %s"), quote (dest));
359         }
360     }
361
362   free (dest_backup);
363   free (rel_source);
364   return ok;
365 }
366
367 void
368 usage (int status)
369 {
370   if (status != EXIT_SUCCESS)
371     emit_try_help ();
372   else
373     {
374       printf (_("\
375 Usage: %s [OPTION]... [-T] TARGET LINK_NAME   (1st form)\n\
376   or:  %s [OPTION]... TARGET                  (2nd form)\n\
377   or:  %s [OPTION]... TARGET... DIRECTORY     (3rd form)\n\
378   or:  %s [OPTION]... -t DIRECTORY TARGET...  (4th form)\n\
379 "),
380               program_name, program_name, program_name, program_name);
381       fputs (_("\
382 In the 1st form, create a link to TARGET with the name LINK_NAME.\n\
383 In the 2nd form, create a link to TARGET in the current directory.\n\
384 In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.\n\
385 Create hard links by default, symbolic links with --symbolic.\n\
386 By default, each destination (name of new link) should not already exist.\n\
387 When creating hard links, each TARGET must exist.  Symbolic links\n\
388 can hold arbitrary text; if later resolved, a relative link is\n\
389 interpreted in relation to its parent directory.\n\
390 "), stdout);
391
392       emit_mandatory_arg_note ();
393
394       fputs (_("\
395       --backup[=CONTROL]      make a backup of each existing destination file\n\
396   -b                          like --backup but does not accept an argument\n\
397   -d, -F, --directory         allow the superuser to attempt to hard link\n\
398                                 directories (note: will probably fail due to\n\
399                                 system restrictions, even for the superuser)\n\
400   -f, --force                 remove existing destination files\n\
401 "), stdout);
402       fputs (_("\
403   -i, --interactive           prompt whether to remove destinations\n\
404   -L, --logical               dereference TARGETs that are symbolic links\n\
405   -n, --no-dereference        treat LINK_NAME as a normal file if\n\
406                                 it is a symbolic link to a directory\n\
407   -P, --physical              make hard links directly to symbolic links\n\
408   -r, --relative              create symbolic links relative to link location\n\
409   -s, --symbolic              make symbolic links instead of hard links\n\
410 "), stdout);
411       fputs (_("\
412   -S, --suffix=SUFFIX         override the usual backup suffix\n\
413   -t, --target-directory=DIRECTORY  specify the DIRECTORY in which to create\n\
414                                 the links\n\
415   -T, --no-target-directory   treat LINK_NAME as a normal file always\n\
416   -v, --verbose               print name of each linked file\n\
417 "), stdout);
418       fputs (HELP_OPTION_DESCRIPTION, stdout);
419       fputs (VERSION_OPTION_DESCRIPTION, stdout);
420       fputs (_("\
421 \n\
422 The backup suffix is '~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
423 The version control method may be selected via the --backup option or through\n\
424 the VERSION_CONTROL environment variable.  Here are the values:\n\
425 \n\
426 "), stdout);
427       fputs (_("\
428   none, off       never make backups (even if --backup is given)\n\
429   numbered, t     make numbered backups\n\
430   existing, nil   numbered if numbered backups exist, simple otherwise\n\
431   simple, never   always make simple backups\n\
432 "), stdout);
433       printf (_("\
434 \n\
435 Using -s ignores -L and -P.  Otherwise, the last option specified controls\n\
436 behavior when a TARGET is a symbolic link, defaulting to %s.\n\
437 "), LINK_FOLLOWS_SYMLINKS ? "-L" : "-P");
438       emit_ancillary_info ();
439     }
440   exit (status);
441 }
442
443 int
444 main (int argc, char **argv)
445 {
446   int c;
447   bool ok;
448   bool make_backups = false;
449   char *backup_suffix_string;
450   char *version_control_string = NULL;
451   char const *target_directory = NULL;
452   bool no_target_directory = false;
453   int n_files;
454   char **file;
455
456   initialize_main (&argc, &argv);
457   set_program_name (argv[0]);
458   setlocale (LC_ALL, "");
459   bindtextdomain (PACKAGE, LOCALEDIR);
460   textdomain (PACKAGE);
461
462   atexit (close_stdin);
463
464   /* FIXME: consider not calling getenv for SIMPLE_BACKUP_SUFFIX unless
465      we'll actually use backup_suffix_string.  */
466   backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
467
468   symbolic_link = remove_existing_files = interactive = verbose
469     = hard_dir_link = false;
470
471   while ((c = getopt_long (argc, argv, "bdfinrst:vFLPS:T", long_options, NULL))
472          != -1)
473     {
474       switch (c)
475         {
476         case 'b':
477           make_backups = true;
478           if (optarg)
479             version_control_string = optarg;
480           break;
481         case 'd':
482         case 'F':
483           hard_dir_link = true;
484           break;
485         case 'f':
486           remove_existing_files = true;
487           interactive = false;
488           break;
489         case 'i':
490           remove_existing_files = false;
491           interactive = true;
492           break;
493         case 'L':
494           logical = true;
495           break;
496         case 'n':
497           dereference_dest_dir_symlinks = false;
498           break;
499         case 'P':
500           logical = false;
501           break;
502         case 'r':
503           relative = true;
504           break;
505         case 's':
506           symbolic_link = true;
507           break;
508         case 't':
509           if (target_directory)
510             error (EXIT_FAILURE, 0, _("multiple target directories specified"));
511           else
512             {
513               struct stat st;
514               if (stat (optarg, &st) != 0)
515                 error (EXIT_FAILURE, errno, _("accessing %s"), quote (optarg));
516               if (! S_ISDIR (st.st_mode))
517                 error (EXIT_FAILURE, 0, _("target %s is not a directory"),
518                        quote (optarg));
519             }
520           target_directory = optarg;
521           break;
522         case 'T':
523           no_target_directory = true;
524           break;
525         case 'v':
526           verbose = true;
527           break;
528         case 'S':
529           make_backups = true;
530           backup_suffix_string = optarg;
531           break;
532         case_GETOPT_HELP_CHAR;
533         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
534         default:
535           usage (EXIT_FAILURE);
536           break;
537         }
538     }
539
540   n_files = argc - optind;
541   file = argv + optind;
542
543   if (n_files <= 0)
544     {
545       error (0, 0, _("missing file operand"));
546       usage (EXIT_FAILURE);
547     }
548
549   if (no_target_directory)
550     {
551       if (target_directory)
552         error (EXIT_FAILURE, 0,
553                _("cannot combine --target-directory "
554                  "and --no-target-directory"));
555       if (n_files != 2)
556         {
557           if (n_files < 2)
558             error (0, 0,
559                    _("missing destination file operand after %s"),
560                    quote (file[0]));
561           else
562             error (0, 0, _("extra operand %s"), quote (file[2]));
563           usage (EXIT_FAILURE);
564         }
565     }
566   else if (!target_directory)
567     {
568       if (n_files < 2)
569         target_directory = ".";
570       else if (2 <= n_files && target_directory_operand (file[n_files - 1]))
571         target_directory = file[--n_files];
572       else if (2 < n_files)
573         error (EXIT_FAILURE, 0, _("target %s is not a directory"),
574                quote (file[n_files - 1]));
575     }
576
577   if (backup_suffix_string)
578     simple_backup_suffix = xstrdup (backup_suffix_string);
579
580   backup_type = (make_backups
581                  ? xget_version (_("backup type"), version_control_string)
582                  : no_backups);
583
584   if (relative && !symbolic_link)
585     {
586         error (EXIT_FAILURE, 0,
587                _("cannot do --relative without --symbolic"));
588     }
589
590
591   if (target_directory)
592     {
593       int i;
594
595       /* Create the data structure we'll use to record which hard links we
596          create.  Used to ensure that ln detects an obscure corner case that
597          might result in user data loss.  Create it only if needed.  */
598       if (2 <= n_files
599           && remove_existing_files
600           /* Don't bother trying to protect symlinks, since ln clobbering
601              a just-created symlink won't ever lead to real data loss.  */
602           && ! symbolic_link
603           /* No destination hard link can be clobbered when making
604              numbered backups.  */
605           && backup_type != numbered_backups)
606
607         {
608           dest_set = hash_initialize (DEST_INFO_INITIAL_CAPACITY,
609                                       NULL,
610                                       triple_hash,
611                                       triple_compare,
612                                       triple_free);
613           if (dest_set == NULL)
614             xalloc_die ();
615         }
616
617       ok = true;
618       for (i = 0; i < n_files; ++i)
619         {
620           char *dest_base;
621           char *dest = file_name_concat (target_directory,
622                                          last_component (file[i]),
623                                          &dest_base);
624           strip_trailing_slashes (dest_base);
625           ok &= do_link (file[i], dest);
626           free (dest);
627         }
628     }
629   else
630     ok = do_link (file[0], file[1]);
631
632   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
633 }