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

Added support for changing uid/gid (fix #157) #188

Merged
merged 3 commits into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 69 additions & 2 deletions lib/file-operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ function closeFd(propagatedErr, fd, callback) {
}
}

function isValidUnixId(id) {
if (typeof id !== 'number') {
return false;
}

if (id < 0) {
return false;
}

return true;
}

function getModeDiff(fsMode, vinylMode) {
var modeDiff = 0;

Expand Down Expand Up @@ -66,6 +78,35 @@ function getTimesDiff(fsStat, vinylStat) {
return timesDiff;
}

function getOwnerDiff(fsStat, vinylStat) {
if (!isValidUnixId(vinylStat.uid) &&
!isValidUnixId(vinylStat.gid)) {
return;
}

var uid = isValidUnixId(fsStat.uid) ? fsStat.uid : undefined; // Default to current uid.
if (isValidUnixId(vinylStat.uid)) {
uid = vinylStat.uid;
}

var gid = isValidUnixId(fsStat.gid) ? fsStat.gid : undefined; // Default to current gid.
if (isValidUnixId(vinylStat.gid)) {
gid = vinylStat.gid;
}

if (isEqual(uid, fsStat.uid) &&
isEqual(gid, fsStat.gid)) {
return;
}

var ownerDiff = {
uid: uid,
gid: gid,
};

return ownerDiff;
}

function isOwner(fsStat) {
var hasGetuid = (typeof process.getuid === 'function');
var hasGeteuid = (typeof process.geteuid === 'function');
Expand Down Expand Up @@ -106,11 +147,14 @@ function updateMetadata(fd, file, callback) {
// Check if atime/mtime need to be updated
var timesDiff = getTimesDiff(stat, file.stat);

// Check if uid/gid need to be updated
var ownerDiff = getOwnerDiff(stat, file.stat);

// Set file.stat to the reflect current state on disk
assign(file.stat, stat);

// Nothing to do
if (!modeDiff && !timesDiff) {
if (!modeDiff && !timesDiff && !ownerDiff) {
return callback(null);
}

Expand All @@ -123,7 +167,10 @@ function updateMetadata(fd, file, callback) {
if (modeDiff) {
return mode();
}
times();
if (timesDiff) {
return times();
}
owner();

function mode() {
var mode = stat.mode ^ modeDiff;
Expand All @@ -137,6 +184,9 @@ function updateMetadata(fd, file, callback) {
if (timesDiff) {
return times(fchmodErr);
}
if (ownerDiff) {
return owner(fchmodErr);
}
callback(fchmodErr);
}
}
Expand All @@ -149,9 +199,24 @@ function updateMetadata(fd, file, callback) {
file.stat.atime = timesDiff.atime;
file.stat.mtime = timesDiff.mtime;
}
if (ownerDiff) {
return owner(fchmodErr || futimesErr);
}
callback(fchmodErr || futimesErr);
}
}

function owner(earlierErr) {
fs.fchown(fd, ownerDiff.uid, ownerDiff.gid, onFchown);

function onFchown(fchownErr) {
if (!fchownErr) {
file.stat.uid = ownerDiff.uid;
file.stat.gid = ownerDiff.gid;
}
callback(earlierErr || fchownErr);
}
}
}
}

Expand Down Expand Up @@ -257,8 +322,10 @@ function mkdirp(dirpath, customMode, callback) {

module.exports = {
closeFd: closeFd,
isValidUnixId: isValidUnixId,
getModeDiff: getModeDiff,
getTimesDiff: getTimesDiff,
getOwnerDiff: getOwnerDiff,
isOwner: isOwner,
updateMetadata: updateMetadata,
writeFile: writeFile,
Expand Down
99 changes: 99 additions & 0 deletions test/dest-owner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
'use strict';

var os = require('os');
var path = require('path');

var fs = require('graceful-fs');
var del = require('del');
var File = require('vinyl');
var expect = require('expect');

var vfs = require('../');

function wipeOut() {
this.timeout(20000);

expect.restoreSpies();

// Async del to get sort-of-fix for https://github.com/isaacs/rimraf/issues/72
return del(path.join(__dirname, './out-fixtures/'));
}

var isWindows = (os.platform() === 'win32');

describe('.dest() with custom owner', function() {
beforeEach(wipeOut);
afterEach(wipeOut);

it('should call fchown when the uid and/or gid are provided on the vinyl stat', function(done) {
if (isWindows) {
this.skip();
return;
}

var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
var expectedPath = path.join(__dirname, './out-fixtures/test.coffee');
var expectedContents = fs.readFileSync(inputPath);

var fchownSpy = expect.spyOn(fs, 'fchown').andCallThrough();

var expectedFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: expectedContents,
stat: {
uid: 1001,
gid: 1001,
},
});

var onEnd = function() {
expect(fchownSpy.calls.length).toEqual(1);
expect(fchownSpy.calls[0].arguments[1]).toEqual(1001);
expect(fchownSpy.calls[0].arguments[2]).toEqual(1001);
done();
};

var stream = vfs.dest('./out-fixtures/', { cwd: __dirname });
stream.on('end', onEnd);
stream.write(expectedFile);
stream.end();
});

it('should not call fchown when the uid and gid provided on the vinyl stat are invalid', function(done) {
if (isWindows) {
this.skip();
return;
}

var inputPath = path.join(__dirname, './fixtures/test.coffee');
var inputBase = path.join(__dirname, './fixtures/');
var expectedPath = path.join(__dirname, './out-fixtures/test.coffee');
var expectedContents = fs.readFileSync(inputPath);

var fchownSpy = expect.spyOn(fs, 'fchown').andCallThrough();

var expectedFile = new File({
base: inputBase,
cwd: __dirname,
path: inputPath,
contents: expectedContents,
stat: {
uid: -1,
gid: -1,
},
});

var onEnd = function() {
expect(fchownSpy.calls.length).toEqual(0);
done();
};

var stream = vfs.dest('./out-fixtures/', { cwd: __dirname });
stream.on('end', onEnd);
stream.write(expectedFile);
stream.end();
});
});
Loading