#include "hash.h"
#include "hash-triple.h"
#include "quote.h"
+#include "relpath.h"
#include "same.h"
#include "yesno.h"
+#include "canonicalize.h"
/* The official name of this program (e.g., no 'g' prefix). */
#define PROGRAM_NAME "ln"
/* If true, make symbolic links; otherwise, make hard links. */
static bool symbolic_link;
+/* If true, make symbolic links relative */
+static bool relative;
+
/* If true, hard links are logical rather than physical. */
static bool logical = !!LINK_FOLLOWS_SYMLINKS;
{"target-directory", required_argument, NULL, 't'},
{"logical", no_argument, NULL, 'L'},
{"physical", no_argument, NULL, 'P'},
+ {"relative", no_argument, NULL, 'r'},
{"symbolic", no_argument, NULL, 's'},
{"verbose", no_argument, NULL, 'v'},
{GETOPT_HELP_OPTION_DECL},
return is_a_dir;
}
+/* Return FROM represented as relative to the dir of TARGET.
+ The result is malloced. */
+
+static char *
+convert_abs_rel (const char *from, const char *target)
+{
+ char *realtarget = canonicalize_filename_mode (target, CAN_MISSING);
+ char *realfrom = canonicalize_filename_mode (from, CAN_MISSING);
+
+ /* Write to a PATH_MAX buffer. */
+ char *relative_from = xmalloc (PATH_MAX);
+
+ /* Get dirname to generate paths relative to. */
+ realtarget[dir_len (realtarget)] = '\0';
+
+ if (!relpath (realfrom, realtarget, relative_from, PATH_MAX))
+ {
+ free (relative_from);
+ relative_from = NULL;
+ }
+
+ free (realtarget);
+ free (realfrom);
+
+ return relative_from ? relative_from : xstrdup (from);
+}
+
/* Make a link DEST to the (usually) existing file SOURCE.
Symbolic links to nonexistent files are allowed.
Return true if successful. */
struct stat source_stats;
struct stat dest_stats;
char *dest_backup = NULL;
+ char *rel_source = NULL;
bool dest_lstat_ok = false;
bool source_is_dir = false;
bool ok;
}
}
+ if (relative)
+ source = rel_source = convert_abs_rel (source, dest);
+
ok = ((symbolic_link ? symlink (source, dest)
: linkat (AT_FDCWD, source, AT_FDCWD, dest,
logical ? AT_SYMLINK_FOLLOW : 0))
{
error (0, errno, _("cannot remove %s"), quote (dest));
free (dest_backup);
+ free (rel_source);
return false;
}
}
free (dest_backup);
+ free (rel_source);
return ok;
}
-n, --no-dereference treat LINK_NAME as a normal file if\n\
it is a symbolic link to a directory\n\
-P, --physical make hard links directly to symbolic links\n\
+ -r, --relative create symbolic links relative to link location\n\
-s, --symbolic make symbolic links instead of hard links\n\
"), stdout);
fputs (_("\
symbolic_link = remove_existing_files = interactive = verbose
= hard_dir_link = false;
- while ((c = getopt_long (argc, argv, "bdfinst:vFLPS:T", long_options, NULL))
+ while ((c = getopt_long (argc, argv, "bdfinrst:vFLPS:T", long_options, NULL))
!= -1)
{
switch (c)
case 'P':
logical = false;
break;
+ case 'r':
+ relative = true;
+ break;
case 's':
symbolic_link = true;
break;
? xget_version (_("backup type"), version_control_string)
: no_backups);
+ if (relative && !symbolic_link)
+ {
+ error (EXIT_FAILURE, 0,
+ _("cannot do --relative without --symbolic"));
+ }
+
+
if (target_directory)
{
int i;
--- /dev/null
+#!/bin/sh
+# Test "ln --relative".
+
+# Copyright (C) 2012 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
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/init.sh"; path_prepend_ ../src
+print_ver_ ln
+
+mkdir -p usr/bin || framework_failure_
+mkdir -p usr/lib/foo || framework_failure_
+touch usr/lib/foo/foo || framework_failure_
+
+ln -sr usr/lib/foo/foo usr/bin/foo
+test $(readlink usr/bin/foo) = '../lib/foo/foo' || fail=1
+
+ln -sr usr/bin/foo usr/lib/foo/link-to-foo
+test $(readlink usr/lib/foo/link-to-foo) = 'foo' || fail=1
+
+Exit $fail