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

Fix #749, #620: implement lchown() #752

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ const fs = new Filer.FileSystem(options, callback);
* [fs.utimes(path, atime, mtime, callback)](#utimes)
* [fs.chown(path, uid, gid, callback)](#chown)
* [fs.fchown(fd, uid, gid, callback)](#fchown)
* [fs.lchown(path, uid, gid, callback)](#lchown)
* [fs.chmod(path, mode, callback)](#chmod)
* [fs.fchmod(fd, mode, callback)](#fchmod)
* [fs.futimes(fd, atime, mtime, callback)](#fsutimes)
Expand Down Expand Up @@ -923,6 +924,19 @@ fs.open('/myfile.txt', function(err, fd) {
});
```

#### fs.lchown(path, uid, gid, callback)<a name="lchown"></a>

lchown() is like chown(), but does not dereference symbolic links. Asynchronous [lchown(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/chown.html). Callback gets no additional arguments. Both `uid` (user id) and `gid` (group id) arguments should be a JavaScript Number. By default, `0x0` is used (i.e., `root:root` ownership).

Example:

```javascript
fs.lchown('/myfile.txt', 500, 500, function(err) {
if(err) throw err;
// /myfile.txt is now owned by user with id 500, group 500
});
```

#### fs.chmod(path, mode, callback)<a name="chmod"></a>

Changes the mode of a file. Asynchronous [chmod(2)](http://pubs.opengroup.org/onlinepubs/009695399/functions/chmod.html). Callback gets no additional arguments. The `mode` argument should be a JavaScript Number, which combines file type and permission information. Here are a list of common values useful for setting the `mode`:
Expand Down
74 changes: 73 additions & 1 deletion src/filesystem/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,54 @@ function find_node(context, path, callback) {
}
}

/**
* find_symlink_node
*/
// in: file or directory path
// out: symlink node structure, or error
function find_symlink_node(context, path, callback) {
path = normalize(path);
var name = basename(path);
var parentPath = dirname(path);

var directoryNode;
var directoryData;

if(ROOT_DIRECTORY_NAME === name) {
find_node(context, path, callback);
} else {
find_node(context, parentPath, read_directory_data);
humphd marked this conversation as resolved.
Show resolved Hide resolved
}

function read_directory_data(error, result) {
if(error) {
callback(error);
} else {
directoryNode = result;
context.getObject(directoryNode.data, check_if_file_exists);
}
}

function create_node(error, data) {
if(error) {
return callback(error);
}
Node.create(data, callback);
}

function check_if_file_exists(error, result) {
if(error) {
callback(error);
} else {
directoryData = result;
if(!directoryData.hasOwnProperty(name)) {
callback(new Errors.ENOENT('a component of the path does not name an existing file', path));
} else {
context.getObject(directoryData[name].id, create_node);
}
}
}
}

/**
* set extended attribute (refactor)
Expand Down Expand Up @@ -2060,6 +2108,19 @@ function fchown_file(context, ofd, uid, gid, callback) {
ofd.getNode(context, update_owner);
}

function lchown_file(context, path, uid, gid, callback) {
function update_owner(error, node) {
if (error) {
callback(error);
} else {
node.uid = uid;
node.gid = gid;
update_node_times(context, path, node, { mtime: Date.now() }, callback);
}
}
find_symlink_node(context, path, update_owner);
}

function getxattr(context, path, name, callback) {
getxattr_file(context, path, name, callback);
}
Expand Down Expand Up @@ -2244,6 +2305,17 @@ function fchown(context, fd, uid, gid, callback) {
}
}

function lchown(context, path, uid, gid, callback) {
if(!isUint32(uid)) {
return callback(new Errors.EINVAL('uid must be a valid integer', uid));
}
if(!isUint32(gid)) {
return callback(new Errors.EINVAL('gid must be a valid integer', gid));
}

lchown_file(context, path, uid, gid, callback);
}

function rename(context, oldpath, newpath, callback) {
oldpath = normalize(oldpath);
newpath = normalize(newpath);
Expand Down Expand Up @@ -2425,7 +2497,7 @@ module.exports = {
ftruncate,
futimes,
getxattr,
// lchown - https://github.com/filerjs/filer/issues/620
lchown,
// lchmod - https://github.com/filerjs/filer/issues/619
link,
lseek,
Expand Down
3 changes: 1 addition & 2 deletions src/filesystem/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,7 @@ function FileSystem(options, callback) {
{ name: 'ftruncate' },
{ name: 'futimes' },
{ name: 'getxattr', promises: true, absPathArgs: [0] },
// lchown - https://github.com/filerjs/filer/issues/620
// lchmod - https://github.com/filerjs/filer/issues/619
{ name: 'lchown' },
andrewkoung marked this conversation as resolved.
Show resolved Hide resolved
{ name: 'link', promises: true, absPathArgs: [0, 1] },
{ name: 'lseek' },
{ name: 'lstat', promises: true },
Expand Down
42 changes: 41 additions & 1 deletion tests/spec/fs.chown.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var util = require('../lib/test-utils.js');
var expect = require('chai').expect;

describe('fs.chown, fs.fchown', function() {
describe('fs.chown, fs.fchown, fs.lchown', function() {
beforeEach(util.setup);
afterEach(util.cleanup);

Expand All @@ -27,6 +27,20 @@ describe('fs.chown, fs.fchown', function() {
});
});

it('lchown should expect an interger value for uid', function(done) {
var fs = util.fs();

fs.open('/file', 'w', function(err, fd) {
if(err) throw err;

fs.lchown(fd, '1001', 1001, function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
fs.close(fd, done);
});
});
});

it('fchown should expect an interger value for uid', function(done) {
var fs = util.fs();

Expand Down Expand Up @@ -55,6 +69,20 @@ describe('fs.chown, fs.fchown', function() {
});
});

it('lchown should expect an interger value for gid', function(done) {
var fs = util.fs();

fs.open('/file', 'w', function(err, fd) {
if(err) throw err;

fs.lchown(fd, 1001, '1001', function(err) {
expect(err).to.exist;
expect(err.code).to.equal('EINVAL');
fs.close(fd, done);
});
});
});

it('fchown should expect an interger value for gid', function(done) {
var fs = util.fs();

Expand Down Expand Up @@ -107,6 +135,18 @@ describe('fs.chown, fs.fchown', function() {
fs.stat('/file', function(err, stats) {
if(err) throw err;

expect(stats.uid).to.equal(500);
expect(stats.gid).to.equal(500);
//done();
});
});

fs.lchown('/file', 500, 500, function(err) {
if(err) throw err;

fs.lstat('/file', function(err, stats) {
if(err) throw err;

expect(stats.uid).to.equal(500);
expect(stats.gid).to.equal(500);
done();
Expand Down