Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gulp.src() doesn't follow symlinks #1273

Closed
OnkelTem opened this issue Sep 24, 2015 · 12 comments
Closed

Gulp.src() doesn't follow symlinks #1273

OnkelTem opened this issue Sep 24, 2015 · 12 comments

Comments

@OnkelTem
Copy link

Original issue title: Incorrect permissions while copying symlinked files

If a file being copied by gulp is a symlink then the target gets 777 permissions.
I created a repo to demonstrate this behavior: https://github.com/OnkelTem/gulp-copy-symlink-demo

So how to make it to follow symlinks to get correct stats?

@OnkelTem
Copy link
Author

Here is a console example of stat'ing src/file1:

$ stat src/file1
  File: `src/file1' -> `./file1source'
  Size: 13              Blocks: 0          IO Block: 4096   symbolic link
Device: 802h/2050d      Inode: 429120      Links: 1
Access: (0777/lrwxrwxrwx)  Uid: ( 1000/user)   Gid: ( 1000/user)
Access: 2015-09-24 13:31:27.676082282 +0300
Modify: 2015-09-24 13:31:27.676082282 +0300
Change: 2015-09-24 13:31:27.676082282 +0300
 Birth: -

Note Access: (0777/lrwxrwxrwx) — this is symlink's properties.
And now with -L (follow symlinks) flag:

$ stat -L src/file1
  File: `src/file1'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d      Inode: 429121      Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/user)   Gid: ( 1000/user)
Access: 2015-09-24 13:24:54.000000000 +0300
Modify: 2015-09-24 13:24:54.000000000 +0300
Change: 2015-09-24 13:31:27.676082282 +0300
 Birth: -

— much better, showing the actual file's permissions.

So it's pretty clear that some underlying stuff doesn't follow symlinks.

@OnkelTem OnkelTem changed the title Incorrect permissions while copying symlinked files Gulp.src() doesn't follow symlinks Sep 25, 2015
@yocontra
Copy link
Member

Use gulp.symlink not gulp.dest

@OnkelTem
Copy link
Author

@contra

Eric, I'm not sure I understand you.

You are advising to change something in the control script. Well, It's like having different soaps to wash hands at odd and even days of month. Symlinks are there to substitute files, a level of abstraction, you can symlink one file or all files, and the behavior of a system should be the same still.

I have a feeling that you didn't read the issue. Closing the issue seems to me premature.

@yocontra
Copy link
Member

Okay, I will re-open the issue because I must be misunderstanding you. Can you tell me if using gulp.symlink solves this problem?

@yocontra yocontra reopened this Sep 27, 2015
@OnkelTem
Copy link
Author

Thank you.
Shouldn't gulp.symlink create a simlink as its name implies? If this is the case, then I should note that it's not what I'm talking about as I don't need any symlinks to be created. In my case I have a template file symlinked somewhere else. Consider this example:

_project_common_files/
  src/
    LICENSE.txt
project1/
  src/
   ~LICENSE.txt (-> ../_project_common_files/LICENSE.txt)
project2/
  src/
   ~LICENSE.txt (-> ../_project_common_files/LICENSE.txt)
...

Does it make sense now?

That said, I had no chance to test gulp.symlink as there is no symlink property or function in the gulp object.

@yocontra
Copy link
Member

@OnkelTem Can you try all of this out on the latest version of gulp (on the 4.0 branch)?

You can install it with npm install gulpjs/gulp#4.0

@OnkelTem
Copy link
Author

@contra

Thanks for the quick reply, that way we will nail it down soon :)

Just tried 4.0 branch, first with gulp.dest() and then with gulp.symlink().

With gulp.dest() the result is different (comparing to 3.x) but yet wrong — in the tmp/ dir I'm getting file1source file now, i.e. the symlink is followed "too much":

-rw-rw-r-- 1 user user    0 Sep 27 14:37 file1source

With gulp.symlink() I got even more weird results:

lrwxrwxrwx 1 user user   61 Sep 27 14:40 file1source -> /var/.../gulp-copy-symlink-demo/src/file1source

— the name is taken from the original file while created a symlink to original file.

@OnkelTem
Copy link
Author

Branch 4.0

It seems to me that confusion comes from vinyl-fs's resolveSymlink() which overwrites file name by recursively following symlinks. This quick patch demonstrates the expected behavior:

  • stats are read from the real file
  • name and path are preserved
--- gulp-copy-symlink-demo/vinyl-fs.orig/lib/src/resolveSymlinks.js
+++ gulp-copy-symlink-demo/vinyl-fs/lib/src/resolveSymlinks.js
@@ -24,11 +24,20 @@
           return cb(err);
         }

+        // Save for later
+        var _base = globFile.base,
+            _path = globFile.path;
+
         globFile.base = path.dirname(filePath);
         globFile.path = filePath;

         // recurse to get real file stat
         resolveFile(globFile, enc, cb);
+
+        // restore
+        globFile.base = _base;
+        globFile.path = _path;
+
       });
     });
   }

IMHO this is what link following actually means, how it's implemented right now - is reading links.

Branch 3.0

In previous versions of vinyl-fs was used getStats.js and getStats(). The related code:

function getStats() {
  return through2.obj(fetchStats);
}

function fetchStats(file, enc, cb) {
  fs.lstat(file.path, function (err, stat) {
    if (stat) {
      file.stat = stat;
    }
    cb(err, file);
  });
}

— i.e. no symlinks processing, stats are read directly from the file, and if the file is a link then 777 is what we'd get on Linux.

@yocontra
Copy link
Member

@OnkelTem Nice! Want to send a PR?

@OnkelTem
Copy link
Author

@contra sure, done. I hope this won't break something else.

UPDATE. Hehe, and it did.

@phated
Copy link
Member

phated commented Sep 28, 2015

I am pretty sure you are looking for the followSymlinks option on gulp.src - by setting that to false, we don't follow symlinks.

@phated
Copy link
Member

phated commented Nov 9, 2015

This issue has been opened on the vinyl-fs repo. Going to close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants