Permalink
Browse files

Updated the file-engine template with sample implementations of some …

…routines.
  • Loading branch information...
kriskowal committed Mar 1, 2010
1 parent 1bf0bd1 commit 0d8a6772006b993ab4201c5da809a7e1477791e6
Showing with 59 additions and 7 deletions.
  1. +59 −7 engines/template/lib/file-engine.js
@@ -8,6 +8,9 @@ exports.SEPARATOR = '/';
exports.cwd = function () {
throw Error("cwd not yet implemented.");
+ // might work if you don't implement canonical
+ // in terms of cwd:
+ return exports.canonical('.');
};
// TODO necessary for package loading
@@ -18,6 +21,47 @@ exports.list = function (path) {
// TODO necessary for package loading
exports.canonical = function (path) {
throw Error("canonical not yet implemented.");
+ // an implementation in terms of readlink, cwd, and the pure-js
+ // methods provided by Narwhal's file-bootstrap, join, split,
+ // and isDrive, which depends on system.os to distinguish
+ // windows and unix drives
+ var paths = [exports.cwd(), path];
+ var outs = [];
+ var prev;
+ for (var i = 0, ii = paths.length; i < ii; i++) {
+ var path = paths[i];
+ var ins = exports.split(path);
+ if (exports.isDrive(ins[0]))
+ outs = [ins.shift()];
+ while (ins.length) {
+ var leaf = ins.shift();
+ var consider = exports.join.apply(
+ undefined,
+ outs.concat([leaf])
+ );
+
+ // cycle breaker; does not throw an error since every
+ // invalid path must also have an intrinsic canonical
+ // name.
+ if (consider == prev) {
+ ins.unshift.apply(ins, exports.split(link));
+ break;
+ }
+ prev = consider;
+
+ try {
+ var link = exports.readlink(consider);
+ } catch (exception) {
+ link = undefined;
+ }
+ if (link !== undefined) {
+ ins.unshift.apply(ins, exports.split(link));
+ } else {
+ outs.push(leaf)
+ }
+ }
+ }
+ return exports.join.apply(undefined, outs);
};
exports.exists = function (path) {
@@ -34,6 +78,9 @@ exports.size = function (path) {
};
exports.stat = function (path) {
+ throw Error("stat not yet implemented.");
+ // this might work if you've got mtime and size implemented
+ // separately. this isn't a complete stat implementation.
return {
mtime: exports.mtime(path),
size: exports.size(path)
@@ -48,12 +95,11 @@ exports.isDirectory = function (path) {
// TODO necessary for module loading
exports.isFile = function (path) {
throw Error("isFile not yet implemented.");
+ // XXX note that isFile is one of the routines
+ // probably at least partially implemented by
+ // the partial "file" module injected by the
+ // engine bootstrap.js.
};
-// XXX remove this if you implement isFile here
-// from bootstrap system object:
-exports.isFile = system.fs.isFile;
-
-exports.isFile = system.fs.isFile; // TEMPORARY HACK
exports.isLink = function (path) {
throw Error("isLink not yet implemented.");
@@ -91,8 +137,14 @@ exports.touch = function (path, mtime) {
throw Error("touch not yet implemented.");
};
-// FIXME temporary hack
-var read = system.fs.read; // from bootstrap system object
+exports.read = function (path) {
+ throw Error("read not yet implemented.");
+ // XXX Note: read is one of the methods that was
+ // at least partially implemented in the engine's
+ // bootstrap.js. you might take advanage of that
+ // at least temporarily; until you can implement
+ // read in terms of open(path).read()
+};
exports.FileIO = function (path, mode, permissions) {
mode = exports.mode(mode);

0 comments on commit 0d8a677

Please sign in to comment.