2018-05-24 18:15:09 +00:00
|
|
|
var NODE_TYPE_FILE = require('./constants.js').NODE_TYPE_FILE;
|
|
|
|
var NODE_TYPE_DIRECTORY = require('./constants.js').NODE_TYPE_DIRECTORY;
|
|
|
|
var NODE_TYPE_SYMBOLIC_LINK = require('./constants.js').NODE_TYPE_SYMBOLIC_LINK;
|
2018-05-15 17:33:45 +00:00
|
|
|
|
2018-05-26 20:57:57 +00:00
|
|
|
var S_IFREG = require('./constants.js').S_IFREG;
|
|
|
|
var S_IFDIR = require('./constants.js').S_IFDIR;
|
|
|
|
var S_IFLNK = require('./constants.js').S_IFLNK;
|
|
|
|
|
|
|
|
var DEFAULT_FILE_PERMISSIONS = require('./constants.js').DEFAULT_FILE_PERMISSIONS;
|
|
|
|
var DEFAULT_DIR_PERMISSIONS = require('./constants.js').DEFAULT_DIR_PERMISSIONS;
|
|
|
|
|
2018-05-29 17:47:16 +00:00
|
|
|
function getMode(type, mode) {
|
2018-05-26 20:57:57 +00:00
|
|
|
switch(type) {
|
2018-07-15 17:25:35 +00:00
|
|
|
case NODE_TYPE_DIRECTORY:
|
|
|
|
return (mode || DEFAULT_DIR_PERMISSIONS) | S_IFDIR;
|
|
|
|
case NODE_TYPE_SYMBOLIC_LINK:
|
|
|
|
return (mode || DEFAULT_FILE_PERMISSIONS) | S_IFLNK;
|
2018-05-26 20:57:57 +00:00
|
|
|
/* jshint -W086 */
|
2018-07-15 17:25:35 +00:00
|
|
|
case NODE_TYPE_FILE:
|
|
|
|
// falls through
|
|
|
|
default:
|
|
|
|
return (mode || DEFAULT_FILE_PERMISSIONS) | S_IFREG;
|
2018-05-26 20:57:57 +00:00
|
|
|
}
|
2018-05-15 17:33:45 +00:00
|
|
|
}
|
2014-03-18 16:29:20 +00:00
|
|
|
|
2014-06-02 20:44:20 +00:00
|
|
|
function Node(options) {
|
2014-05-23 18:14:06 +00:00
|
|
|
var now = Date.now();
|
2014-03-18 16:29:20 +00:00
|
|
|
|
2014-06-02 20:44:20 +00:00
|
|
|
this.id = options.id;
|
2018-05-24 18:32:27 +00:00
|
|
|
this.type = options.type || NODE_TYPE_FILE; // node type (file, directory, etc)
|
2014-06-02 20:44:20 +00:00
|
|
|
this.size = options.size || 0; // size (bytes for files, entries for directories)
|
|
|
|
this.atime = options.atime || now; // access time (will mirror ctime after creation)
|
|
|
|
this.ctime = options.ctime || now; // creation/change time
|
|
|
|
this.mtime = options.mtime || now; // modified time
|
|
|
|
this.flags = options.flags || []; // file flags
|
|
|
|
this.xattrs = options.xattrs || {}; // extended attributes
|
|
|
|
this.nlinks = options.nlinks || 0; // links count
|
|
|
|
this.data = options.data; // id for data object
|
2018-05-24 19:41:54 +00:00
|
|
|
this.version = options.version || 1;
|
2018-05-24 18:55:36 +00:00
|
|
|
|
|
|
|
// permissions and flags
|
2018-05-26 20:57:57 +00:00
|
|
|
this.mode = options.mode || (getMode(this.type));
|
2018-05-24 18:55:36 +00:00
|
|
|
this.uid = options.uid || 0x0; // owner name
|
|
|
|
this.gid = options.gid || 0x0; // group name
|
2014-06-02 20:44:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the options object has an id on property,
|
|
|
|
// either from caller or one we generate using supplied guid fn.
|
|
|
|
function ensureID(options, prop, callback) {
|
|
|
|
if(options[prop]) {
|
|
|
|
callback(null);
|
|
|
|
} else {
|
|
|
|
options.guid(function(err, id) {
|
|
|
|
options[prop] = id;
|
|
|
|
callback(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Node.create = function(options, callback) {
|
|
|
|
// We expect both options.id and options.data to be provided/generated.
|
|
|
|
ensureID(options, 'id', function(err) {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ensureID(options, 'data', function(err) {
|
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null, new Node(options));
|
|
|
|
});
|
|
|
|
});
|
2014-05-23 18:14:06 +00:00
|
|
|
};
|
2014-06-02 20:44:20 +00:00
|
|
|
|
2018-05-29 17:47:16 +00:00
|
|
|
// Update the node's mode (permissions), taking file type bits into account.
|
|
|
|
Node.setMode = function(mode, node) {
|
|
|
|
node.mode = getMode(node.type, mode);
|
|
|
|
};
|
|
|
|
|
2014-06-02 20:44:20 +00:00
|
|
|
module.exports = Node;
|