Restructure layout of node

This commit is contained in:
David Humphrey 2018-05-24 14:55:36 -04:00
parent 9244e9be6e
commit ee412d4abe
2 changed files with 29 additions and 74 deletions

View File

@ -48,15 +48,6 @@ var Buffer = require('../buffer.js');
*/ */
function update_node_times(context, path, node, times, callback) { function update_node_times(context, path, node, times, callback) {
var update = false; var update = false;
// If updating the P9 info only, don't send change events (honour flags).
var queueChangeEvent = false;
if(times.ctime || times.mtime) {
// Update the qid's version field, since node has changed.
// TODO: this might be more than I need to do...
node.p9.qid.version = times.ctime || times.mtime;
update = true;
}
// Honour mount flags for how we update times // Honour mount flags for how we update times
var flags = context.flags; var flags = context.flags;
@ -72,27 +63,20 @@ function update_node_times(context, path, node, times, callback) {
// We don't do atime tracking for perf reasons, but do mirror ctime // We don't do atime tracking for perf reasons, but do mirror ctime
node.atime = times.ctime; node.atime = times.ctime;
update = true; update = true;
queueChangeEvent = true;
} }
if(times.atime) { if(times.atime) {
// The only time we explicitly pass atime is when utimes(), futimes() is called. // The only time we explicitly pass atime is when utimes(), futimes() is called.
// Override ctime mirror here if so // Override ctime mirror here if so
node.atime = times.atime; node.atime = times.atime;
update = true; update = true;
queueChangeEvent = true;
} }
if(times.mtime) { if(times.mtime) {
node.mtime = times.mtime; node.mtime = times.mtime;
update = true; update = true;
queueChangeEvent = true;
} }
function complete(error) { function complete(error) {
// Queue this change so we can send watch events. context.changes.push({ event: 'change', path: path });
// Unlike node.js, we send the full path vs. basename/dirname only.
if(queueChangeEvent) {
context.changes.push({ event: 'change', path: path });
}
callback(error); callback(error);
} }
@ -745,7 +729,7 @@ function replace_data(context, ofd, buffer, offset, length, callback) {
ofd.position = length; ofd.position = length;
fileNode.size = length; fileNode.size = length;
fileNode.version += 1; fileNode.qid_version += 1;
context.putBuffer(fileNode.data, newData, update_file_node); context.putBuffer(fileNode.data, newData, update_file_node);
} }
@ -804,7 +788,7 @@ function write_data(context, ofd, buffer, offset, length, position, callback) {
} }
fileNode.size = newSize; fileNode.size = newSize;
fileNode.version += 1; fileNode.qid_version += 1;
context.putBuffer(fileNode.data, newData, update_file_node); context.putBuffer(fileNode.data, newData, update_file_node);
} }
@ -935,7 +919,7 @@ function link_node(context, oldpath, newpath, callback) {
} else { } else {
fileNode = Node.fromObject(result); fileNode = Node.fromObject(result);
fileNode.nlinks += 1; fileNode.nlinks += 1;
fileNode.updatePathInfo(newpath, ctime); fileNode.updatePathInfo(newpath);
context.putObject(fileNode.id, fileNode, update_time); context.putObject(fileNode.id, fileNode, update_time);
} }
} }
@ -1274,7 +1258,7 @@ function truncate_file(context, path, length, callback) {
callback(error); callback(error);
} else { } else {
fileNode.size = length; fileNode.size = length;
fileNode.version += 1; fileNode.qid_version += 1;
context.putObject(fileNode.id, fileNode, update_time); context.putObject(fileNode.id, fileNode, update_time);
} }
} }
@ -1332,7 +1316,7 @@ function ftruncate_file(context, ofd, length, callback) {
callback(error); callback(error);
} else { } else {
fileNode.size = length; fileNode.size = length;
fileNode.version += 1; fileNode.qid_version += 1;
context.putObject(fileNode.id, fileNode, update_time); context.putObject(fileNode.id, fileNode, update_time);
} }
} }
@ -2043,7 +2027,7 @@ function rename(fs, context, oldpath, newpath, callback) {
fileNode = Node.fromObject(result); fileNode = Node.fromObject(result);
// Don't think I need this here... // Don't think I need this here...
// fileNode.nlinks += 1; // fileNode.nlinks += 1;
fileNode.updatePathInfo(newpath, ctime); fileNode.updatePathInfo(newpath);
context.putObject(fileNode.id, fileNode, update_times); context.putObject(fileNode.id, fileNode, update_times);
} }
} }

View File

@ -25,21 +25,13 @@ function getQType(type) {
case NODE_TYPE_SYMBOLIC_LINK: case NODE_TYPE_SYMBOLIC_LINK:
return P9_QTSYMLINK; return P9_QTSYMLINK;
default: default:
return null; return P9_QTFILE;
} }
} }
function getPOSIXMode(type) { // Name of file/dir. Must be '/' if the file is the root directory of the server
switch(type) { function pathToName(pathName) {
case NODE_TYPE_FILE: return pathName === ROOT_DIRECTORY_NAME ? ROOT_DIRECTORY_NAME : path.basename(pathName);
return S_IFREG;
case NODE_TYPE_DIRECTORY:
return S_IFDIR;
case NODE_TYPE_SYMBOLIC_LINK:
return S_IFLNK;
default:
return null;
}
} }
function Node(options) { function Node(options) {
@ -47,6 +39,7 @@ function Node(options) {
this.id = options.id; this.id = options.id;
this.type = options.type || NODE_TYPE_FILE; // node type (file, directory, etc) this.type = options.type || NODE_TYPE_FILE; // node type (file, directory, etc)
this.name = options.name || (pathToName(options.path));
this.size = options.size || 0; // size (bytes for files, entries for directories) 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.atime = options.atime || now; // access time (will mirror ctime after creation)
this.ctime = options.ctime || now; // creation/change time this.ctime = options.ctime || now; // creation/change time
@ -54,9 +47,6 @@ function Node(options) {
this.flags = options.flags || []; // file flags this.flags = options.flags || []; // file flags
this.xattrs = options.xattrs || {}; // extended attributes this.xattrs = options.xattrs || {}; // extended attributes
this.nlinks = options.nlinks || 0; // links count this.nlinks = options.nlinks || 0; // links count
this.version = options.version || 0; // node version
this.blksize = undefined; // block size
this.nblocks = 1; // blocks count
this.data = options.data; // id for data object this.data = options.data; // id for data object
/** /**
@ -75,35 +65,21 @@ function Node(options) {
* should be different. The version is a version number for a file; typically, * should be different. The version is a version number for a file; typically,
* it is incremented every time the file is modified." * it is incremented every time the file is modified."
*/ */
this.qid_type = options.qid_type || (getQType(this.type));
this.qid_version = options.qid_version || 1;
this.qid_path = options.qid_path || hash32(options.path + this.qid_version);
options.p9 = options.p9 || {qid: {}}; // permissions and flags
this.mode = options.mode || (this.type === NODE_TYPE_DIRECTORY ? /* 755 */ 493 : /* 644 */ 420);
this.p9 = { this.uid = options.uid || 0x0; // owner name
qid: { this.gid = options.gid || 0x0; // group name
type: options.p9.qid.type || (getQType(this.type) || P9_QTFILE),
// use mtime for version info, since we already keep that updated
version: options.p9.qid.now || now,
// files have a unique `path` number, which takes into account files with same
// name but created at different times.
path: options.p9.qid.path || hash32(options.path + this.ctime)
},
// permissions and flags
// TODO: I don't think I'm doing this correctly yet...
mode: options.p9.mode || (getPOSIXMode(this.type) || S_IFREG),
// Name of file/dir. Must be / if the file is the root directory of the server
// TODO: do I need this or can I derive it from abs path?
name: options.p9.name || (options.path === ROOT_DIRECTORY_NAME ? ROOT_DIRECTORY_NAME : path.basename(options.path)),
uid: options.p9.uid || 0x0, // owner name
gid: options.p9.gid || 0x0, // group name
muid: options.p9.muid || 0x0 // name of the user who last modified the file
};
} }
// When the node's path changes, update info that relates to it. // When the node's path changes, update info that relates to it.
Node.prototype.updatePathInfo = function(newPath, ctime) { Node.prototype.updatePathInfo = function(newPath) {
// XXX: need to confirm that qid's path actually changes on rename. // XXX: need to confirm that qid's path actually changes on rename.
this.p9.qid.path = hash32(newPath + (ctime || this.ctime)); this.qid_path = hash32(newPath + this.qid_version);
this.p9.name = newPath === ROOT_DIRECTORY_NAME ? ROOT_DIRECTORY_NAME : path.basename(newPath); this.name = pathToName(newPath);
}; };
// Make sure the options object has an id on property, // Make sure the options object has an id on property,
@ -142,6 +118,7 @@ Node.fromObject = function(object) {
return new Node({ return new Node({
id: object.id, id: object.id,
type: object.type, type: object.type,
name: object.name,
size: object.size, size: object.size,
atime: object.atime, atime: object.atime,
ctime: object.ctime, ctime: object.ctime,
@ -150,18 +127,12 @@ Node.fromObject = function(object) {
xattrs: object.xattrs, xattrs: object.xattrs,
nlinks: object.nlinks, nlinks: object.nlinks,
data: object.data, data: object.data,
p9: { mode: object.mode,
qid: { uid: object.uid,
type: object.p9.qid.type, gid: object.gid,
version: object.p9.qid.version, qid_type: object.qid_type,
path: object.p9.qid.path qid_version: object.qid_version,
}, qid_path: object.qid_path
mode: object.p9.mode,
name: object.p9.name,
uid: object.p9.uid,
gid: object.p9.gid,
muid: object.p9.muid
}
}); });
}; };