Added path tools (from node.js) and normalization to API methods.

This commit is contained in:
Alan Kligman 2012-10-11 16:26:23 -04:00
parent f4189c7e8b
commit 380b00ca3a
3 changed files with 135 additions and 49 deletions

View File

@ -15,7 +15,7 @@ require.config({
}
});
require(["../../javascript-debug/debug", "src/filesystem"], function(debug, IDBFS) {
require(["src/filesystem"], function(IDBFS) {
IDBFS.mount("default", function(error, fs) {
fs.mkdir("/tmp", function(error) {
@ -29,27 +29,6 @@ require(["../../javascript-debug/debug", "src/filesystem"], function(debug, IDBF
});
}, true);
/*
var fs = new IdbFS(undefined, true);
fs.then(function(fs) {
fs.mkdir("/tmp").then(function(result) {
var d = fs.open("/tmp/0", IdbFS.O_CREATE, IdbFS.FMODE_RW);
d.then(function(fd) {
fs.dump(document.getElementById("stdout"));
}, function(e) {
console.log(e);
});
}, function(error) {
console.log(error);
});
fs.stat("/tmp").then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
});
});
*/
});
</script>
</html>

View File

@ -16,6 +16,7 @@ define(function(require) {
var debug = require("debug");
var _ = require("lodash");
var path = require("src/path");
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
var BlobBuilder = window.BlobBuilder || window.MozBlobBuilder || window.WebKitBlobBuilder;
@ -27,23 +28,8 @@ define(function(require) {
}).toUpperCase();
}
function dirname(path) {
var components = path.split("/");
if(1 === components.length) {
return ".";
}
if(0 === _.last(components).length) {
components.pop();
}
components.pop();
if(components.length <= 1) {
return "/";
}
return components.join("/");
}
function makeDirectoryEntry(name, modtime) {
var parent = ("/" === name) ? null : dirname(name);
var parent = path.dirname(name);
return {
"parent": parent,
"name": name,
@ -53,7 +39,7 @@ define(function(require) {
}
function makeFileEntry(name, oid, size, modtime) {
var parent = ("/" === name) ? null : dirname(name);
var parent = path.dirname(name);
return {
"parent": parent,
"name": name,
@ -64,9 +50,6 @@ define(function(require) {
}
}
/*
function FileSystem(name, optFormat) {
function Transaction(db, scope, mode) {
@ -465,6 +448,8 @@ define(function(require) {
// API
// Flags: CREATE, APPEND, TRUNCATE, DIRECTORY
// Modes: RO, RW
function open(pathname, flags, mode, callback) {
}
@ -475,6 +460,7 @@ define(function(require) {
function mkdir(transaction, pathname, callback) {
debug.info("mkdir -->");
pathname = path.normalize(pathname);
transaction = transaction || db.transaction([METADATA_STORE_NAME], IDB_RW);
var store = transaction.objectStore(METADATA_STORE_NAME);
var nameIndex = store.index(NAME_INDEX);
@ -502,15 +488,12 @@ define(function(require) {
function rmdir(pathname, callback) {
debug.info("rmdir -->");
var onerror = genericIDBErrorHandler("rmdir", callback);
if("/" === pathname) {
onerror(new IDBFSError(T_RMDIR, E_BUSY));
return;
}
pathname = path.normalize(pathname);
transaction = db.transaction([METADATA_STORE_NAME, FILE_STORE_NAME], IDB_RW);
var metaStore = transaction.objectStore(METADATA_STORE_NAME);
var nameIndex = metaStore.index(NAME_INDEX);
var parentIndex = metaStore.index(PARENT_INDEX);
var onerror = genericIDBErrorHandler("rmdir", callback);
var getRequest = nameIndex.get(pathname);
getRequest.onsuccess = function(e) {
@ -544,6 +527,7 @@ define(function(require) {
function stat(transaction, pathname, callback) {
debug.info("stat -->");
pathname = path.normalize(pathname);
transaction = transaction || db.transaction([METADATA_STORE_NAME], IDB_RO);
var store = transaction.objectStore(METADATA_STORE_NAME);
var nameIndex = store.index(NAME_INDEX);
@ -674,7 +658,8 @@ define(function(require) {
var IDBFS = {
mount: mount,
umount: undefined
umount: undefined,
path: path
};
return IDBFS;

122
src/path.js Normal file
View File

@ -0,0 +1,122 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
// https://github.com/joyent/node/blob/41e53e557992a7d552a8e23de035f9463da25c99/lib/path.js
define(function(require) {
// Split a filename into [root, dir, basename, ext], unix version
// 'root' is just a slash, or nothing.
var splitPathRe =
/^(\/?)([\s\S]+\/(?!$)|\/)?((?:\.{1,2}$|[\s\S]+?)?(\.[^.\/]*)?)$/;
var splitPath = function(filename) {
var result = splitPathRe.exec(filename);
return [result[1] || '', result[2] || '', result[3] || '', result[4] || ''];
};
// resolves . and .. elements in a path array with directory names there
// must be no slashes, empty elements, or device names (c:\) in the array
// (so also no leading and trailing slashes - it does not distinguish
// relative and absolute paths)
function normalizeArray(parts, allowAboveRoot) {
// if the path tries to go above the root, `up` ends up > 0
var up = 0;
for (var i = parts.length - 1; i >= 0; i--) {
var last = parts[i];
if (last === '.') {
parts.splice(i, 1);
} else if (last === '..') {
parts.splice(i, 1);
up++;
} else if (up) {
parts.splice(i, 1);
up--;
}
}
// if the path is allowed to go above the root, restore leading ..s
if (allowAboveRoot) {
for (; up--; up) {
parts.unshift('..');
}
}
return parts;
}
// path.normalize(path)
// posix version
function normalize(path) {
var isAbsolute = path.charAt(0) === '/';
// var trailingSlash = path.substr(-1) === '/';
// Normalize the path
path = normalizeArray(path.split('/').filter(function(p) {
return !!p;
}), !isAbsolute).join('/');
if (!path && !isAbsolute) {
path = '.';
}
/*
if (path && trailingSlash) {
path += '/';
}
*/
return (isAbsolute ? '/' : '') + path;
};
function dirname(path) {
var result = splitPath(path),
root = result[0],
dir = result[1];
if (!root && !dir) {
// No dirname whatsoever
return '.';
}
if (dir) {
// It has a dirname, strip trailing slash
dir = dir.substr(0, dir.length - 1);
}
return root + dir;
};
function basename(path, ext) {
var f = splitPath(path)[2];
// TODO: make this comparison case-insensitive on windows?
if (ext && f.substr(-1 * ext.length) === ext) {
f = f.substr(0, f.length - ext.length);
}
return f;
};
return {
normalize: normalize,
dirname: dirname,
basename: basename
}
});