WIP
This commit is contained in:
parent
11d0a4de7f
commit
a2ff2823dc
|
@ -38,7 +38,7 @@ function remove_tmp_directory() {
|
||||||
fs.promise.then(make_tmp_directory)
|
fs.promise.then(make_tmp_directory)
|
||||||
.then(remove_tmp_directory)
|
.then(remove_tmp_directory)
|
||||||
.then(function() { console.log('done'); })
|
.then(function() { console.log('done'); })
|
||||||
.otherwise(function(error) { console.error(error.message); });
|
.otherwise(function(error) { console.error(error.stack); });
|
||||||
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
106
src/fs2.js
106
src/fs2.js
|
@ -5,9 +5,59 @@ define(function(require) {
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var when = require('when');
|
var when = require('when');
|
||||||
var Path = require('src/path');
|
var Path = require('src/path');
|
||||||
var guid = require("src/guid");
|
var guid = require('src/guid');
|
||||||
require("crypto-js/rollups/sha256"); var Crypto = CryptoJS;
|
require("crypto-js/rollups/sha256"); var Crypto = CryptoJS;
|
||||||
|
|
||||||
|
function EPathExists(){ Error.apply(this, arguments); }
|
||||||
|
EPathExists.prototype = new Error();
|
||||||
|
EPathExists.prototype.name = "EPathExists";
|
||||||
|
EPathExists.prototype.constructor = EPathExists;
|
||||||
|
|
||||||
|
function EIsDirectory(){ Error.apply(this, arguments); }
|
||||||
|
EIsDirectory.prototype = new Error();
|
||||||
|
EIsDirectory.prototype.name = "EIsDirectory";
|
||||||
|
EIsDirectory.prototype.constructor = EIsDirectory;
|
||||||
|
|
||||||
|
function ENoEntry(){ Error.apply(this, arguments); }
|
||||||
|
ENoEntry.prototype = new Error();
|
||||||
|
ENoEntry.prototype.name = "ENoEntry";
|
||||||
|
ENoEntry.prototype.constructor = ENoEntry;
|
||||||
|
|
||||||
|
function EBusy(){ Error.apply(this, arguments); }
|
||||||
|
EBusy.prototype = new Error();
|
||||||
|
EBusy.prototype.name = "EBusy";
|
||||||
|
EBusy.prototype.constructor = EBusy;
|
||||||
|
|
||||||
|
function ENotEmpty(){ Error.apply(this, arguments); }
|
||||||
|
ENotEmpty.prototype = new Error();
|
||||||
|
ENotEmpty.prototype.name = "ENotEmpty";
|
||||||
|
ENotEmpty.prototype.constructor = ENotEmpty;
|
||||||
|
|
||||||
|
function ENotDirectory(){ Error.apply(this, arguments); }
|
||||||
|
ENotDirectory.prototype = new Error();
|
||||||
|
ENotDirectory.prototype.name = "NotADirectoryError";
|
||||||
|
ENotDirectory.prototype.constructor = ENotDirectory;
|
||||||
|
|
||||||
|
function EBadFileDescriptor(){ Error.apply(this, arguments); }
|
||||||
|
EBadFileDescriptor.prototype = new Error();
|
||||||
|
EBadFileDescriptor.prototype.name = "EBadFileDescriptor";
|
||||||
|
EBadFileDescriptor.prototype.constructor = EBadFileDescriptor;
|
||||||
|
|
||||||
|
function ENotImplemented(){ Error.apply(this, arguments); }
|
||||||
|
ENotImplemented.prototype = new Error();
|
||||||
|
ENotImplemented.prototype.name = "ENotImplemented";
|
||||||
|
ENotImplemented.prototype.constructor = ENotImplemented;
|
||||||
|
|
||||||
|
function ENotMounted(){ Error.apply(this, arguments); }
|
||||||
|
ENotMounted.prototype = new Error();
|
||||||
|
ENotMounted.prototype.name = "ENotMounted";
|
||||||
|
ENotMounted.prototype.constructor = ENotMounted;
|
||||||
|
|
||||||
|
function EFileExists(){ Error.apply(this, arguments); }
|
||||||
|
EFileExists.prototype = new Error();
|
||||||
|
EFileExists.prototype.name = "EFileExists";
|
||||||
|
EFileExists.prototype.constructor = EFileExists;
|
||||||
|
|
||||||
var METADATA_STORE_NAME = 'metadata';
|
var METADATA_STORE_NAME = 'metadata';
|
||||||
var FILE_STORE_NAME = 'files';
|
var FILE_STORE_NAME = 'files';
|
||||||
|
|
||||||
|
@ -103,7 +153,7 @@ define(function(require) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else if(!rootDirectoryNode) {
|
} else if(!rootDirectoryNode) {
|
||||||
callback(new Error('ENOENT'));
|
callback(new ENoEntry());
|
||||||
} else {
|
} else {
|
||||||
callback(undefined, rootDirectoryNode);
|
callback(undefined, rootDirectoryNode);
|
||||||
}
|
}
|
||||||
|
@ -117,7 +167,7 @@ define(function(require) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else if(!_(parentDirectoryNode).has('data') || !parentDirectoryNode.type == MODE_DIRECTORY) {
|
} else if(!_(parentDirectoryNode).has('data') || !parentDirectoryNode.type == MODE_DIRECTORY) {
|
||||||
callback(new Error('ENOTDIR'));
|
callback(new ENotDirectory());
|
||||||
} else {
|
} else {
|
||||||
read_object(objectStore, parentDirectoryNode.data, get_node_id_from_parent_directory_data);
|
read_object(objectStore, parentDirectoryNode.data, get_node_id_from_parent_directory_data);
|
||||||
}
|
}
|
||||||
|
@ -130,7 +180,7 @@ define(function(require) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
if(!_(parentDirectoryData).has(name)) {
|
if(!_(parentDirectoryData).has(name)) {
|
||||||
callback(new Error('ENOENT'));
|
callback(new ENoEntry());
|
||||||
} else {
|
} else {
|
||||||
var nodeId = parentDirectoryData[name].id;
|
var nodeId = parentDirectoryData[name].id;
|
||||||
read_object(objectStore, nodeId, callback);
|
read_object(objectStore, nodeId, callback);
|
||||||
|
@ -150,8 +200,8 @@ define(function(require) {
|
||||||
|
|
||||||
function write_directory_node(error, existingNode) {
|
function write_directory_node(error, existingNode) {
|
||||||
if(!error && existingNode) {
|
if(!error && existingNode) {
|
||||||
callback(new Error('EEXIST'));
|
callback(new EPathExists());
|
||||||
} else if(error && 'ENOENT' != error.message) {
|
} else if(error && !error instanceof ENoEntry) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
directoryNode = new Node(ROOT_NODE_ID, MODE_DIRECTORY);
|
directoryNode = new Node(ROOT_NODE_ID, MODE_DIRECTORY);
|
||||||
|
@ -177,38 +227,38 @@ define(function(require) {
|
||||||
var name = Path.basename(path);
|
var name = Path.basename(path);
|
||||||
var parentPath = Path.dirname(path);
|
var parentPath = Path.dirname(path);
|
||||||
|
|
||||||
var _directoryNode;
|
var directoryNode;
|
||||||
var _directoryData;
|
var directoryData;
|
||||||
var _parentDirectoryNode;
|
var parentDirectoryNode;
|
||||||
var _parentDirectoryData;
|
var parentDirectoryData;
|
||||||
|
|
||||||
function check_if_directory_exists(error, existingNode) {
|
function check_if_directory_exists(error, result) {
|
||||||
if(!error && existingNode) {
|
if(!error && result) {
|
||||||
callback(new Error('EEXIST'));
|
callback(new EPathExists());
|
||||||
} else if(error && 'ENOENT' != error.message) {
|
} else if(error && !error instanceof ENoEntry) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
find_node(objectStore, parentPath, read_parent_directory_data);
|
find_node(objectStore, parentPath, read_parent_directory_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function read_parent_directory_data(error, parentDirectoryNode) {
|
function read_parent_directory_data(error, result) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
_parentDirectoryNode = parentDirectoryNode;
|
parentDirectoryNode = result;
|
||||||
read_object(objectStore, _parentDirectoryNode.data, write_directory_node);
|
read_object(objectStore, parentDirectoryNode.data, write_directory_node);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function write_directory_node(error, parentDirectoryData) {
|
function write_directory_node(error, result) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
_parentDirectoryData = parentDirectoryData;
|
parentDirectoryData = result;
|
||||||
_directoryNode = new Node(undefined, MODE_DIRECTORY);
|
directoryNode = new Node(undefined, MODE_DIRECTORY);
|
||||||
_directoryNode.links += 1;
|
directoryNode.links += 1;
|
||||||
write_object(objectStore, _directoryNode, _directoryNode.id, write_directory_data);
|
write_object(objectStore, directoryNode, directoryNode.id, write_directory_data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -216,8 +266,8 @@ define(function(require) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
_directoryData = {};
|
directoryData = {};
|
||||||
write_object(objectStore, _directoryData, _directoryNode.data, update_parent_directory_data);
|
write_object(objectStore, directoryData, directoryNode.data, update_parent_directory_data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -225,8 +275,8 @@ define(function(require) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else {
|
} else {
|
||||||
_parentDirectoryData[name] = new DirectoryEntry(_directoryNode.id, MODE_DIRECTORY);
|
parentDirectoryData[name] = new DirectoryEntry(directoryNode.id, MODE_DIRECTORY);
|
||||||
write_object(objectStore, _parentDirectoryData, _parentDirectoryNode.data, callback);
|
write_object(objectStore, parentDirectoryData, parentDirectoryNode.data, callback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,7 +297,7 @@ define(function(require) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
} else if(!result) {
|
} else if(!result) {
|
||||||
callback(new Error('ENOENT'));
|
callback(new ENoEntry());
|
||||||
} else {
|
} else {
|
||||||
directoryNode = result;
|
directoryNode = result;
|
||||||
read_object(objectStore, directoryNode.data, check_if_directory_is_empty);
|
read_object(objectStore, directoryNode.data, check_if_directory_is_empty);
|
||||||
|
@ -260,7 +310,7 @@ define(function(require) {
|
||||||
} else {
|
} else {
|
||||||
directoryData = result;
|
directoryData = result;
|
||||||
if(_(directoryData).size() > 0) {
|
if(_(directoryData).size() > 0) {
|
||||||
callback(new Error('ENOTEMPTY'));
|
callback(new ENotEmpty());
|
||||||
} else {
|
} else {
|
||||||
find_node(objectStore, parentPath, read_parent_directory_data);
|
find_node(objectStore, parentPath, read_parent_directory_data);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue