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

Chown not actually altering files #5

Open
LexLythius opened this issue May 19, 2015 · 14 comments
Open

Chown not actually altering files #5

LexLythius opened this issue May 19, 2015 · 14 comments

Comments

@LexLythius
Copy link

I'm fairly new to Node.js and Gulp so I surely must have misunderstood the whole thing, but please bear with me.

I applied the very example in the readme but files don't get chown'ed (I'm running gulp as root of course).

The problem seems to be that the finish function doesn't have any side effects, since the file stat data is changed in memory but never persisted:

function finish() {
    file.stat.uid = finalUid != null ? finalUid : file.stat.uid;
    file.stat.gid = finalGid != null ? finalGid : file.stat.gid;
    cb(null, file);
}

my dirty fix was adding this code:

@@ -1,5 +1,6 @@
 'use strict';
 var gutil = require('gulp-util');
+var fs = require('fs');
 var through = require('through2');
 var uidNumber = require('uid-number');
 var defaultMode = 511 & (~process.umask()); // 511 = 0777
@@ -23,6 +24,17 @@ module.exports = function (user, group) {
        function finish() {
            file.stat.uid = finalUid != null ? finalUid : file.stat.uid;
            file.stat.gid = finalGid != null ? finalGid : file.stat.gid;
+           if (finalUid && finalGid) {
+               fs.chown(file.path, finalUid, finalGid, function (err) {
+                   if (err) {
+                       cb(new gutil.PluginError('gulp-chown', err, {fileName: file.path}));
+                       return;
+                   }
+                   cb(null, file);
+               });
+               return;
+           }
+
            cb(null, file);
        }

but it doesn't feel right...

@kevva
Copy link
Contributor

kevva commented May 19, 2015

You have to write the file to a destination using gulp.dest().

@kevva kevva closed this as completed May 19, 2015
@LexLythius
Copy link
Author

Yes, that's what I tried in the first place, but for some reason it didn't put the files with modified permissions, it just left them unchanged. o_O

@LexLythius
Copy link
Author

For example, having:

# ls -la
total 24
drwxrwxr-x 5 www-data www-data 4096 may 19 14:23 .
drwxr-xr-x 6 root     root     4096 may 18 16:32 ..
dr-xrws--- 2 root     root     4096 may 19 14:23 css
-rw-r--r-- 1 lisandro www-data 3740 may 19 14:43 index.html
dr-xrws--- 2 root     root     4096 may 19 14:23 js
dr-xrws--- 5 root     root     4096 may 19 14:23 lib

and

gulp.task('default', function () {
    gulp.src(paths.deploy + '/index.html')
            .pipe(chown('lisandro', 'adm'))
            .pipe(chmod(755))
            .pipe(gulp.dest(paths.deploy))
            ;
});

I run

# gulp
[14:43:27] Using gulpfile /home/lisandro/Blink/repo-nuevo/blink-v2-spa/gulpfile.js
[14:43:27] Starting 'default'...
[14:43:27] Finished 'default' after 4.46 ms

and get

# ls -la
total 24
drwxrwxr-x 5 www-data www-data 4096 may 19 14:23 .
drwxr-xr-x 6 root     root     4096 may 18 16:32 ..
dr-xrws--- 2 root     root     4096 may 19 14:23 css
-rwxr-xr-x 1 lisandro www-data 3740 may 19 14:46 index.html
dr-xrws--- 2 root     root     4096 may 19 14:23 js
dr-xrws--- 5 root     root     4096 may 19 14:23 lib

As you can see, chmod works but chown doesn't.
Using chown without chmod makes no difference.

Using Ubuntu 14.04.2 LTS x86_64, Node.js v0.10.25.

@c0d3rman
Copy link

Is this issue solved? I'm having the same issue. I can chmod just fine but chown doesn't work. I am using gulp.dest as well. When overwriting the same file nothing happens, and when putting it in a new directory it's set to root:root (running as root.)

@LexLythius
Copy link
Author

I now think this is actually a bug with dest ignoring chown's metadata, not a bug with chown itself.

@c0d3rman
Copy link

I ran some tests (modified chown to console.log its intended permissions) and confirmed chown is doing its job, and the problem is with dest.

@sindresorhus
Copy link
Owner

Best way forward would be to submit a failing test here: https://github.com/wearefractal/vinyl-fs

@c0d3rman
Copy link

I'm working on making the required changes to vinyl-fs and submitting them. But I'd love if someone also did what @sindresorhus suggested (I haven't worked with vinyl-fs before.)

@c0d3rman
Copy link

After running more tests, it seems that chown is setting the correct uid and gid (33:33 in my case,) but that dest is receiving the current one (1000:1000 in my case.) This could be a deeper problem.

@LexLythius
Copy link
Author

Probably a matter of mixing effective and real user IDs.

@c0d3rman
Copy link

This is very strange - I just commented out .pipe(chmod(770)) in my pipeline and it successfully chowned the files. Working on figuring out why.

@c0d3rman
Copy link

OK - a crude fix:
Adding fs.chown(writePath, file.stat.uid, file.stat.gid, function(){}); at the end of the fs.stat call in the written function in vinyl-fs/lib/dest/writeContents/index.js (line 31 for me) fixes it, but only if you don't also use gulp-chmod.
This obviously is a terrible fix (no error handling, breaks gulp-chmod, etc.) but it's the beginning of a solution.
The main problem now is that the done (or complete, depends on vinyl-fs version) callback can't be used by both fs.chmos and fs.chown.

@c0d3rman
Copy link

c0d3rman commented Jul 1, 2015

Until this issue is fixed (by the vinyl-fs people), I am using a clone I made of gulp-chown that just fs.chowns the files instead of setting file.stats.uid and file.stats.gid. Not ideal, but it works.

@ghost
Copy link

ghost commented Jun 22, 2017

Best way forward would be to submit a failing test here: https://github.com/wearefractal/vinyl-fs

Iiuc (?), this is the relevant issue @ vinyl-fs proj

	Support changing uid/gid from the vinyl object to the file on disk
	 https://github.com/gulpjs/vinyl-fs/issues/157

which looks like it's got a commited fix @master

	https://github.com/gulpjs/vinyl-fs/commit/9fd1689182314c78dabe5b2fd426a17738265b5b

After installing

	yarn add --dev https://github.com/gulpjs/vinyl-fs.git#master

This task,

	export function scss_strip() {
		return pump([
			gulp.src([
				'./assets/gen/css/min/frontpage.min.css'
			]),
			stripCssComments({ preserve: false}),
			chown('wwwrun','www'),
			gulp.dest( './web/css/' )
		])
		;
	};

, run as root, does create the dest file, but ignores the chown; file's still created as root:root.

I understand this^ is old -- from 2015 -- but it is still open ...

What am I missing in the config/usage? Or, has the fix gone elsewhere?

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

4 participants