Can you change what a symlink points to after it is created?

前端 未结 7 1738
陌清茗
陌清茗 2020-12-12 10:52

Does any operating system provide a mechanism (system call — not command line program) to change the pathname referenced by a symbolic link (symlink) — other than by unlinki

7条回答
  •  执念已碎
    2020-12-12 11:36

    AFAIK, no, you can't. You have to remove it and recreate it. Actually, you can overwrite a symlink and thus update the pathname referenced by it:

    $ ln -s .bashrc test
    $ ls -al test
    lrwxrwxrwx 1 pascal pascal 7 2009-09-23 17:12 test -> .bashrc
    $ ln -s .profile test
    ln: creating symbolic link `test': File exists
    $ ln -s -f .profile test
    $ ls -al test
    lrwxrwxrwx 1 pascal pascal 8 2009-09-23 17:12 test -> .profile
    

    EDIT: As the OP pointed out in a comment, using the --force option will make ln perform a system call to unlink() before symlink(). Below, the output of strace on my linux box proving it:

    $ strace -o /tmp/output.txt ln -s -f .bash_aliases test
    $ grep -C3 ^unlink /tmp/output.txt 
    lstat64("test", {st_mode=S_IFLNK|0777, st_size=7, ...}) = 0
    stat64(".bash_aliases", {st_mode=S_IFREG|0644, st_size=2043, ...}) = 0
    symlink(".bash_aliases", "test")        = -1 EEXIST (File exists)
    unlink("test")                          = 0
    symlink(".bash_aliases", "test")        = 0
    close(0)                                = 0
    close(1)                                = 0
    

    So I guess the final answer is "no".

    EDIT: The following is copied from Arto Bendiken's answer over on unix.stackexchange.com, circa 2016.

    This can indeed be done atomically with rename(2), by first creating the new symlink under a temporary name and then cleanly overwriting the old symlink in one go. As the man page states:

    If newpath refers to a symbolic link the link will be overwritten.

    In the shell, you would do this with mv -T as follows:

    $ mkdir a b
    $ ln -s a z
    $ ln -s b z.new
    $ mv -T z.new z
    

    You can strace that last command to make sure it is indeed using rename(2) under the hood:

    $ strace mv -T z.new z
    lstat64("z.new", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
    lstat64("z", {st_mode=S_IFLNK|0777, st_size=1, ...}) = 0
    rename("z.new", "z")                    = 0
    

    Note that in the above, both mv -T and strace are Linux-specific.

    On FreeBSD, use mv -h alternately.

    Editor's note: This is how Capistrano has done it for years now, ever since ~2.15. See this pull request.

提交回复
热议问题