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