在不扩展符号链接的情况下协调相对路径

时间:2015-02-18 00:50:28

标签: c linux symlink

在Linux上使用C语言,我有两个包含相对于cwd的路径的字符数组。我们说它们是"foo/txt""bar/txt"。我想创建一个从第一个到第二个指向的符号链接。问题是,如果我转到symlink("bar/txt", "foo/txt")foo/txt会查找不存在的foo/bar/txt。我可以将".."添加到目标路径的开头,但我不知道源字符串中包含了多少目录级别

复杂的问题是目标我自己是一个符号链接,我不想遍历它,如果我使用realpath()可能会发生。一旦按照我的意图创建链接,目录结构可能如下所示(对于我的目的,如果生成的符号链接是相对的或绝对的,则无关紧要):

/somecwd/foo/txt -> ../bar/txt
/somecwd/bar/txt -> ../baz/txt
/somecww/baz/txt

有谁知道我怎么会这样做?非常感谢任何帮助。

编辑:理想情况下,即使我给出的路径是绝对路径

,这也会起作用

2 个答案:

答案 0 :(得分:1)

使用symlink()函数,BSD(Mac OS X)手册页说:

 int symlink(const char *path1, const char *path2);
     

说明

     

path2创建符号链接path1path2是创建的文件的名称,path1是用于创建符号链接的字符串)。两个名称都可以是任意路径名;这些文件不必在同一个文件系统上。

请注意,您指定为path1的内容将逐字地用作符号链接的内容。因此,要准确地制作符号链接,必须使相对路径正确。也就是说,作为path1传递的名称必须是相对于path2的正确名称。

换句话说,你不能完全避免映射名字的过程,以及所有伴随的困难。

我有一个Perl脚本,relpath,我从Convert absolute path into relative path given a current directory的答案和来自comp.unix.shell的相当旧的新闻组消息拼凑而成。将其中的一部分转换为C并不是人类的智慧。事实上,它必然会做很多次。难点在于找到代码。

relpath

#!/usr/bin/env perl
#
# @(#)$Id: relpath.pl,v 1.4 2014/12/08 18:23:17 jleffler Exp $
#
# Usage:    relpath source target [...]
#
# Purpose:  Print relative path of target w.r.t. source
#
# Based loosely on code from:
# http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-10/1256.html
# Via: https://stackoverflow.com/questions/2564634

use strict;
use warnings;
use File::Basename;
use Cwd qw(realpath getcwd);

if (scalar @ARGV < 2)
{
    my $arg0 = basename($0, ".pl");
    die "Usage: $arg0 from to [...]\n"
}

my $pwd;
my $verbose = 0;

# Fettle filename so it is absolute.
# Deals with '//', '/./' and '/../' notations, plus symlinks.
# The realpath() function does the hard work if the path exists.
# For non-existent paths, the code does a purely textual hack.
sub resolve
{
    my($name) = @_;
    my($path) = realpath($name);
    if (!defined $path)
    {
        # Path does not exist - do the best we can with lexical analysis
        # Assume Unix - not dealing with Windows.
        $path = $name;
        if ($name !~ m%^/%)
        {
            $pwd = getcwd if !defined $pwd;
            $path = "$pwd/$path";
        }
        $path =~ s%//+%/%g;     # Not UNC paths.
        $path =~ s%/$%%;        # No trailing /
        $path =~ s%/\./%/%g;    # No embedded /./
        # Try to eliminate /../abc/
        $path =~ s%/\.\./(?:[^/]+)(/|$)%$1%g;
        $path =~ s%/\.$%%;      # No trailing /.
        $path =~ s%^\./%%;      # No leading ./
        # What happens with . and / as inputs?
    }
    return($path);
}

sub print_result
{
    my($source, $target, $relpath) = @_;
    if ($verbose)
    {
        print "source  = $ARGV[0]\n";
        print "target  = $ARGV[1]\n";
        print "relpath = $relpath\n";
    }
    else
    {
        print "$relpath\n";
    }
}

# Nasty!
my($source) = resolve($ARGV[0]);
my(@source) = split '/', $source;
shift @ARGV;

sub relpath
{
    my($name) = @_;
    my($target) = resolve($name);
    print_result($source, $target, ".") if ($source eq $target);

    # Split!
    my(@target) = split '/', $target;

    my $count = scalar(@source);
       $count = scalar(@target) if (scalar(@target) < $count);
    my $relpath = "";
    my $i;

    # Both paths are absolute; Perl splits an empty field 0.
    for ($i = 1; $i < $count; $i++)
    {
        last if $source[$i] ne $target[$i];
    }

    for (my $s = $i; $s < scalar(@source); $s++)
    {
        $relpath = "$relpath/" if ($s > $i);
        $relpath = "$relpath..";
    }
    for (my $t = $i; $t < scalar(@target); $t++)
    {
        $relpath = "$relpath/" if ($relpath ne "");
        $relpath = "$relpath$target[$t]";
    }

    print_result($source, $target, $relpath);
}

foreach my $target (@ARGV)
{
    relpath($target);
}

test.relpath

注意:这需要relpath.pl,而不仅仅是relpath

#!/bin/ksh
#
# @(#)$Id: test.relpath.sh,v 1.1 2010/04/25 15:19:20 jleffler Exp $
#
# Test relpath Perl script fairly exhaustively
# BUG: should include expected answers!

sed 's/#.*//;/^[    ]*$/d' <<! |

/home/part1/part2 /home/part1/part3
/home/part1/part2 /home/part4/part5
/home/part1/part2 /work/part6/part7
/home/part1       /work/part1/part2/part3/part4
/home             /work/part2/part3
/                 /work/part2/part3/part4

/home/part1/part2 /home/part1/part2/part3/part4
/home/part1/part2 /home/part1/part2/part3
/home/part1/part2 /home/part1/part2
/home/part1/part2 /home/part1
/home/part1/part2 /home
/home/part1/part2 /

/home/part1/part2 /work
/home/part1/part2 /work/part1
/home/part1/part2 /work/part1/part2
/home/part1/part2 /work/part1/part2/part3
/home/part1/part2 /work/part1/part2/part3/part4

home/part1/part2 home/part1/part3
home/part1/part2 home/part4/part5
home/part1/part2 work/part6/part7
home/part1       work/part1/part2/part3/part4
home             work/part2/part3
.                work/part2/part3

home/part1/part2 home/part1/part2/part3/part4
home/part1/part2 home/part1/part2/part3
home/part1/part2 home/part1/part2
home/part1/part2 home/part1
home/part1/part2 home
home/part1/part2 .

home/part1/part2 work
home/part1/part2 work/part1
home/part1/part2 work/part1/part2
home/part1/part2 work/part1/part2/part3
home/part1/part2 work/part1/part2/part3/part4

!

{
echo "Relative paths (source, target, relative path)"
while read source target
do
    echo "$source $target $(${PERL:-perl} relpath.pl $source $target)"
done |
awk '{ printf("%-20s   %-30s   %s\n", $1, $2, $3); }'
}

示例输出

请注意,如果../part3是目录,或者假定为目录,则第一个相对路径/home/part1/part2是正确的。此代码需要小心解释输出。请注意,symlink()系统调用不要求path1名称引用现有文件或目录(但相比之下,path2不得引用现有文件或目录,但只有叶元素不能存在;所有以前的目录必须存在。)

Relative paths (source, target, relative path)
/home/part1/part2      /home/part1/part3                ../part3
/home/part1/part2      /home/part4/part5                ../../part4/part5
/home/part1/part2      /work/part6/part7                ../../../work/part6/part7
/home/part1            /work/part1/part2/part3/part4    ../../work/part1/part2/part3/part4
/home                  /work/part2/part3                ../work/part2/part3
/                      /work/part2/part3/part4          work/part2/part3/part4
/home/part1/part2      /home/part1/part2/part3/part4    part3/part4
/home/part1/part2      /home/part1/part2/part3          part3
/home/part1/part2      /home/part1/part2                .
/home/part1/part2      /home/part1                      ..
/home/part1/part2      /home                            ../..
/home/part1/part2      /                                ../../..
/home/part1/part2      /work                            ../../../work
/home/part1/part2      /work/part1                      ../../../work/part1
/home/part1/part2      /work/part1/part2                ../../../work/part1/part2
/home/part1/part2      /work/part1/part2/part3          ../../../work/part1/part2/part3
/home/part1/part2      /work/part1/part2/part3/part4    ../../../work/part1/part2/part3/part4
home/part1/part2       home/part1/part3                 ../part3
home/part1/part2       home/part4/part5                 ../../part4/part5
home/part1/part2       work/part6/part7                 ../../../work/part6/part7
home/part1             work/part1/part2/part3/part4     ../../work/part1/part2/part3/part4
home                   work/part2/part3                 ../work/part2/part3
.                      work/part2/part3                 work/part2/part3
home/part1/part2       home/part1/part2/part3/part4     part3/part4
home/part1/part2       home/part1/part2/part3           part3
home/part1/part2       home/part1/part2                 .
home/part1/part2       home/part1                       ..
home/part1/part2       home                             ../..
home/part1/part2       .                                ../../..
home/part1/part2       work                             ../../../work
home/part1/part2       work/part1                       ../../../work/part1
home/part1/part2       work/part1/part2                 ../../../work/part1/part2
home/part1/part2       work/part1/part2/part3           ../../../work/part1/part2/part3
home/part1/part2       work/part1/part2/part3/part4     ../../../work/part1/part2/part3/part4

答案 1 :(得分:0)

作为一种快速解决方案(忽略双斜杠,.././),您可以计算源中的斜杠数,并在../之前添加相同数量。