Node的fs.symlink()认为链接已经存在;绝对没有

时间:2014-11-09 15:54:36

标签: node.js fs

我有这段代码:

// ...

var symlinkPrecommit = function (callback) {

  console.log("\n > Creating pre-commit symlink in .git/hooks/pre-commit\n");

  source = path.resolve('.git/hooks/pre-commit');
  target = path.resolve('precommit.js');

  fs.symlink(source, target, 'file', function (err) {
    if (err) {
      console.log(
        err.code === 'EEXIST' ? "Link already created!\n" : "Error\n"
      );
      console.log(err);
    }
    if (callback) callback();
  });

}

// ...

当我运行它时,我明白了:

MaffBookPro: matt$ ./build.js

 > Creating pre-commit symlink in .git/hooks/pre-commit

Link already created!

{ [Error: EEXIST, symlink '/Users/matt/work/project/.git/hooks/pre-commit']
  errno: 47,
  code: 'EEXIST',
  path: '/Users/matt/work/project/.git/hooks/pre-commit' }

但是当我ls -lah尝试创建符号链接的目录时,该文件肯定不存在 - 并且权限看起来完全正常:

MaffBookPro: matt$ ls -lah .git/hooks/
total 80
drwxr-xr-x  11 matt  staff   374B  9 Nov 15:39 .
drwxr-xr-x  15 matt  staff   510B  9 Nov 15:45 ..
-rwxr-xr-x   1 matt  staff   452B  9 Oct 12:52 applypatch-msg.sample
-rwxr-xr-x   1 matt  staff   896B  9 Oct 12:52 commit-msg.sample
-rwxr-xr-x   1 matt  staff   189B  9 Oct 12:52 post-update.sample
-rwxr-xr-x   1 matt  staff   398B  9 Oct 12:52 pre-applypatch.sample
-rwxr-xr-x   1 matt  staff   1.6K  9 Oct 12:52 pre-commit.sample
-rwxr-xr-x   1 matt  staff   1.3K  9 Oct 12:52 pre-push.sample
-rwxr-xr-x   1 matt  staff   4.8K  9 Oct 12:52 pre-rebase.sample
-rwxr-xr-x   1 matt  staff   1.2K  9 Oct 12:52 prepare-commit-msg.sample
-rwxr-xr-x   1 matt  staff   3.5K  9 Oct 12:52 update.sample

有什么想法吗?提前谢谢!

1 个答案:

答案 0 :(得分:5)

这对我来说是违反直觉的,因为我想象src - > dst,但在: fs.symlink(srcpath, dstpath[, type], callback)

  • srcpath是指向
  • 的链接
  • dstpath是创建新链接的地方

它的命名方式与unix ln命令相同,如果您不考虑正在构建的链接,它会更容易记住,而是记住它与cp具有相同的接口,其中source是现有文件,目标是创建新文件的位置。