/* `ln' program to create links between files.
- Copyright (C) 86, 89, 90, 91, 1995-2005 Free Software Foundation, Inc.
+ Copyright (C) 1986, 1989-1991, 1995-2006 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include "backupfile.h"
#include "dirname.h"
#include "error.h"
+#include "filenamecat.h"
#include "quote.h"
#include "yesno.h"
lstat (File, Stat_buf)
#endif
-/* Construct a string NEW_DEST by concatenating DEST, a slash, and
- basename(SOURCE) in alloca'd memory. Don't modify DEST or SOURCE. */
-
-#define FILE_BASENAME_CONCAT(new_dest, dest, source) \
- do \
- { \
- const char *source_base; \
- char *tmp_source; \
- size_t buf_len = strlen (source) + 1; \
- \
- tmp_source = alloca (buf_len); \
- memcpy (tmp_source, (source), buf_len); \
- strip_trailing_slashes (tmp_source); \
- source_base = base_name (tmp_source); \
- \
- (new_dest) = alloca (strlen ((dest)) + 1 \
- + strlen (source_base) + 1); \
- stpcpy (stpcpy (stpcpy ((new_dest), (dest)), "/"), source_base);\
- } \
- while (0)
-
/* The name by which the program was run, for error messages. */
char *program_name;
/* FIXME: document */
static enum backup_type backup_type;
-/* A pointer to the function used to make links. This will point to either
- `link' or `symlink'. */
-static int (*linkfunc) ();
-
/* If true, make symbolic links; otherwise, make hard links. */
static bool symbolic_link;
{"target-directory", required_argument, NULL, 't'},
{"symbolic", no_argument, NULL, 's'},
{"verbose", no_argument, NULL, 'v'},
- {"version-control", required_argument, NULL, 'V'}, /* Deprecated. FIXME. */
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
static bool
target_directory_operand (char const *file)
{
- char const *b = base_name (file);
+ char const *b = last_component (file);
size_t blen = strlen (b);
bool looks_like_a_dir = (blen == 0 || ISSLASH (b[blen - 1]));
struct stat st;
/* Make a link DEST to the (usually) existing file SOURCE.
Symbolic links to nonexistent files are allowed.
- If DEST_IS_DIR, put the link to SOURCE in the DEST directory.
Return true if successful. */
static bool
-do_link (const char *source, const char *dest, bool dest_is_dir)
+do_link (const char *source, const char *dest)
{
struct stat source_stats;
struct stat dest_stats;
char *dest_backup = NULL;
bool lstat_ok = false;
+ bool source_is_dir = false;
+ bool ok;
/* Use stat here instead of lstat.
On SVR4, link does not follow symlinks, so this check disallows
quote (source));
}
- if (!hard_dir_link && S_ISDIR (source_stats.st_mode))
+ if (S_ISDIR (source_stats.st_mode))
{
- error (0, 0, _("%s: hard link not allowed for directory"),
- quote (source));
- return false;
+ source_is_dir = true;
+ if (! hard_dir_link)
+ {
+ error (0, 0, _("%s: hard link not allowed for directory"),
+ quote (source));
+ return false;
+ }
}
}
- if (dest_is_dir)
- {
- /* Treat DEST as a directory; build the full filename. */
- char *new_dest;
- FILE_BASENAME_CONCAT (new_dest, dest, source);
- dest = new_dest;
- }
-
if (remove_existing_files || interactive || backup_type != no_backups)
{
lstat_ok = (lstat (dest, &dest_stats) == 0);
fprintf (stderr, _("%s: replace %s? "), program_name, quote (dest));
if (!yesno ())
return true;
+ remove_existing_files = true;
}
if (backup_type != no_backups)
{
- char *tmp_backup = find_backup_file_name (dest, backup_type);
- size_t buf_len = strlen (tmp_backup) + 1;
- dest_backup = alloca (buf_len);
- memcpy (dest_backup, tmp_backup, buf_len);
- free (tmp_backup);
- if (rename (dest, dest_backup))
+ dest_backup = find_backup_file_name (dest, backup_type);
+ if (rename (dest, dest_backup) != 0)
{
- if (errno != ENOENT)
+ int rename_errno = errno;
+ free (dest_backup);
+ dest_backup = NULL;
+ if (rename_errno != ENOENT)
{
- error (0, errno, _("cannot backup %s"), quote (dest));
+ error (0, rename_errno, _("cannot backup %s"), quote (dest));
return false;
}
- else
- dest_backup = NULL;
}
}
}
- if (verbose)
- {
- printf ((symbolic_link
- ? _("create symbolic link %s to %s")
- : _("create hard link %s to %s")),
- quote_n (0, dest), quote_n (1, source));
- if (dest_backup)
- printf (_(" (backup: %s)"), quote (dest_backup));
- putchar ('\n');
- }
-
- if ((*linkfunc) (source, dest) == 0)
- return true;
+ ok = ((symbolic_link ? symlink (source, dest) : link (source, dest))
+ == 0);
/* If the attempt to create a link failed and we are removing or
backing up destinations, unlink the destination and try again.
POSIX 1003.1-2004 requires that ln -f A B must unlink B even on
failure (e.g., when A does not exist). This is counterintuitive,
and we submitted a defect report
- <http://www.opengroup.org/sophocles/show_mail.tpl?source=L&listname=austin-review-l&id=1795>
+ <http://www.opengroup.org/austin/mailarchives/ag-review/msg01794.html>
(2004-06-24). If the committee does not fix the standard we'll
have to change the behavior of ln -f, at least if POSIXLY_CORRECT
is set. In the meantime ln -f A B will not unlink B unless the
If we didn't remove DEST in that case, the subsequent LINKFUNC
call would fail. */
- if (errno == EEXIST && (remove_existing_files || dest_backup))
+ if (!ok && errno == EEXIST && (remove_existing_files || dest_backup))
{
if (unlink (dest) != 0)
{
error (0, errno, _("cannot remove %s"), quote (dest));
+ free (dest_backup);
return false;
}
- if (linkfunc (source, dest) == 0)
- return true;
+ ok = ((symbolic_link ? symlink (source, dest) : link (source, dest))
+ == 0);
}
- error (0, errno,
- (symbolic_link
- ? _("creating symbolic link %s to %s")
- : _("creating hard link %s to %s")),
- quote_n (0, dest), quote_n (1, source));
-
- if (dest_backup)
+ if (ok)
{
- if (rename (dest_backup, dest))
- error (0, errno, _("cannot un-backup %s"), quote (dest));
+ if (verbose)
+ {
+ if (dest_backup)
+ printf ("%s ~ ", quote (dest_backup));
+ printf ("%s %c> %s\n", quote_n (0, dest), (symbolic_link ? '-' : '='),
+ quote_n (1, source));
+ }
}
- return false;
+ else
+ {
+ error (0, errno,
+ (symbolic_link
+ ? (errno != ENAMETOOLONG && *source
+ ? _("creating symbolic link %s")
+ : _("creating symbolic link %s -> %s"))
+ : (errno == EMLINK && !source_is_dir
+ ? _("creating hard link to %.0s%s")
+ : (errno == EDQUOT || errno == EEXIST || errno == ENOSPC
+ || errno == EROFS)
+ ? _("creating hard link %s")
+ : _("creating hard link %s => %s"))),
+ quote_n (0, dest), quote_n (1, source));
+
+ if (dest_backup)
+ {
+ if (rename (dest_backup, dest) != 0)
+ error (0, errno, _("cannot un-backup %s"), quote (dest));
+ }
+ }
+
+ free (dest_backup);
+ return ok;
}
void
-t, --target-directory=DIRECTORY specify the DIRECTORY in which to create\n\
the links\n\
-T, --no-target-directory treat LINK_NAME as a normal file\n\
- -v, --verbose print name of each file before linking\n\
+ -v, --verbose print name of each linked file\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
bool make_backups = false;
char *backup_suffix_string;
char *version_control_string = NULL;
- char *target_directory = NULL;
+ char const *target_directory = NULL;
bool no_target_directory = false;
int n_files;
char **file;
symbolic_link = remove_existing_files = interactive = verbose
= hard_dir_link = false;
- while ((c = getopt_long (argc, argv, "bdfinst:vFS:TV:", long_options, NULL))
+ while ((c = getopt_long (argc, argv, "bdfinst:vFS:T", long_options, NULL))
!= -1)
{
switch (c)
{
- case 'V': /* FIXME: this is deprecated. Remove it in 2001. */
- error (0, 0,
- _("warning: --version-control (-V) is obsolete; support for\
- it\nwill be removed in some future release. Use --backup=%s instead."
- ), optarg);
- /* Fall through. */
-
case 'b':
make_backups = true;
if (optarg)
quote (file[n_files - 1]));
}
- if (symbolic_link)
- linkfunc = symlink;
- else
- linkfunc = link;
-
if (backup_suffix_string)
simple_backup_suffix = xstrdup (backup_suffix_string);
int i;
ok = true;
for (i = 0; i < n_files; ++i)
- ok &= do_link (file[i], target_directory, true);
+ {
+ char *dest_base;
+ char *dest = file_name_concat (target_directory,
+ last_component (file[i]),
+ &dest_base);
+ strip_trailing_slashes (dest_base);
+ ok &= do_link (file[i], dest);
+ free (dest);
+ }
}
else
- ok = do_link (file[0], file[1], false);
+ ok = do_link (file[0], file[1]);
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}