support rename()
This commit is contained in:
parent
638109f911
commit
c76fc01a20
|
@ -8290,7 +8290,6 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
var name = basename(path);
|
||||
var parentPath = dirname(path);
|
||||
|
||||
if(ROOT_DIRECTORY_NAME == name) {
|
||||
function check_root_directory_node(error, rootDirectoryNode) {
|
||||
if(error) {
|
||||
callback(error);
|
||||
|
@ -8301,8 +8300,6 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
}
|
||||
}
|
||||
|
||||
read_object(objectStore, ROOT_NODE_ID, check_root_directory_node);
|
||||
} else {
|
||||
// in: parent directory node
|
||||
// out: parent directory data
|
||||
function read_parent_directory_data(error, parentDirectoryNode) {
|
||||
|
@ -8330,6 +8327,9 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
}
|
||||
}
|
||||
|
||||
if(ROOT_DIRECTORY_NAME == name) {
|
||||
read_object(objectStore, ROOT_NODE_ID, check_root_directory_node);
|
||||
} else {
|
||||
find_node(objectStore, parentPath, read_parent_directory_data);
|
||||
}
|
||||
}
|
||||
|
@ -9183,6 +9183,7 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
}
|
||||
|
||||
stat_file(files, path, check_result);
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback(undefined, result);
|
||||
|
@ -9543,6 +9544,19 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
}
|
||||
}
|
||||
|
||||
function update_descriptor_position(error, stats) {
|
||||
if(error) {
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
if(stats.size + offset < 0) {
|
||||
deferred.reject(new EInvalid('resulting file offset would be negative'));
|
||||
} else {
|
||||
ofd.position = stats.size + offset;
|
||||
deferred.resolve(ofd.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var ofd = that.openFiles[fd];
|
||||
|
||||
if(!ofd) {
|
||||
|
@ -9567,19 +9581,6 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
|
||||
var files = transaction.objectStore(FILE_STORE_NAME);
|
||||
|
||||
function update_descriptor_position(error, stats) {
|
||||
if(error) {
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
if(stats.size + offset < 0) {
|
||||
deferred.reject(new EInvalid('resulting file offset would be negative'));
|
||||
} else {
|
||||
ofd.position = stats.size + offset;
|
||||
deferred.resolve(ofd.position);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fstat_file(files, ofd, update_descriptor_position);
|
||||
} else {
|
||||
deferred.reject(new EInvalid('whence argument is not a proper value'));
|
||||
|
@ -9636,7 +9637,46 @@ define('src/fs',['require','lodash','when','encoding-indexes','encoding','src/pa
|
|||
|
||||
};
|
||||
FileSystem.prototype.rename = function rename(oldpath, newpath, callback) {
|
||||
var that = this;
|
||||
this.promise.then(
|
||||
function() {
|
||||
var deferred = when.defer();
|
||||
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
|
||||
var files = transaction.objectStore(FILE_STORE_NAME);
|
||||
|
||||
link_node(files, oldpath, newpath, unlink_old_node);
|
||||
|
||||
function unlink_old_node(error) {
|
||||
if(error) {
|
||||
// if(transaction.error) transaction.abort();
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
unlink_node(files, oldpath, check_result);
|
||||
}
|
||||
}
|
||||
|
||||
function check_result(error) {
|
||||
if(error) {
|
||||
// if(transaction.error) transaction.abort();
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback();
|
||||
},
|
||||
function(error) {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
function() {
|
||||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.truncate = function truncate(path, length, callback) {
|
||||
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -15,7 +15,7 @@ require.config({
|
|||
}
|
||||
});
|
||||
|
||||
require(["src/file-system"], function(IDBFS) {
|
||||
require(["src/fs"], function(IDBFS) {
|
||||
|
||||
var flags = 'FORMAT';
|
||||
//var flags;
|
||||
|
@ -38,9 +38,9 @@ fs.open('/tmp', 'w+', function(error, fd) {
|
|||
fs.stat('/tmp', function(error, stats) {
|
||||
if(error) throw error;
|
||||
console.log('stats:', stats);
|
||||
fs.readdir('/', function(error, files) {
|
||||
fs.rename('/tmp', '/tmp1', function(error) {
|
||||
if(error) throw error;
|
||||
console.log('ls:', files);
|
||||
console.log('done');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
42
src/fs.js
42
src/fs.js
|
@ -136,7 +136,7 @@ define(function(require) {
|
|||
function read_parent_directory_data(error, parentDirectoryNode) {
|
||||
if(error) {
|
||||
callback(error);
|
||||
} else if(!(_(parentDirectoryNode).has('data') && parentDirectoryNode.type == MODE_DIRECTORY)) {
|
||||
} else if(!parentDirectoryNode.type == MODE_DIRECTORY) {
|
||||
callback(new ENotDirectory('a component of the path prefix is not a directory'));
|
||||
} else {
|
||||
read_object(objectStore, parentDirectoryNode.data, get_node_id_from_parent_directory_data);
|
||||
|
@ -1014,6 +1014,7 @@ define(function(require) {
|
|||
}
|
||||
|
||||
stat_file(files, path, check_result);
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback(undefined, result);
|
||||
|
@ -1467,7 +1468,46 @@ define(function(require) {
|
|||
|
||||
};
|
||||
FileSystem.prototype.rename = function rename(oldpath, newpath, callback) {
|
||||
var that = this;
|
||||
this.promise.then(
|
||||
function() {
|
||||
var deferred = when.defer();
|
||||
var transaction = that.db.transaction([FILE_STORE_NAME], IDB_RW);
|
||||
var files = transaction.objectStore(FILE_STORE_NAME);
|
||||
|
||||
link_node(files, oldpath, newpath, unlink_old_node);
|
||||
|
||||
function unlink_old_node(error) {
|
||||
if(error) {
|
||||
// if(transaction.error) transaction.abort();
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
unlink_node(files, oldpath, check_result);
|
||||
}
|
||||
}
|
||||
|
||||
function check_result(error) {
|
||||
if(error) {
|
||||
// if(transaction.error) transaction.abort();
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
deferred.resolve();
|
||||
}
|
||||
}
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback();
|
||||
},
|
||||
function(error) {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
function() {
|
||||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.truncate = function truncate(path, length, callback) {
|
||||
|
||||
|
|
|
@ -1112,6 +1112,62 @@ describe('fs.unlink', function() {
|
|||
|
||||
runs(function() {
|
||||
expect(_error).toBeDefined();
|
||||
expect(_stats.nlinks).toEqual(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('fs.rename', function() {
|
||||
beforeEach(function() {
|
||||
this.db_name = mk_db_name();
|
||||
this.fs = new IDBFS.FileSystem(this.db_name, 'FORMAT');
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
indexedDB.deleteDatabase(this.db_name);
|
||||
delete this.fs;
|
||||
});
|
||||
|
||||
it('should be a function', function() {
|
||||
expect(typeof this.fs.rename).toEqual('function');
|
||||
});
|
||||
|
||||
it('should rename an existing file', function() {
|
||||
var complete1 = false;
|
||||
var complete2 = false;
|
||||
var _error, _stats;
|
||||
var that = this;
|
||||
|
||||
that.fs.open('/myfile', 'w+', function(error, result) {
|
||||
if(error) throw error;
|
||||
|
||||
var fd = result;
|
||||
that.fs.close(fd, function(error) {
|
||||
if(error) throw error;
|
||||
|
||||
that.fs.rename('/myfile', '/myotherfile', function(error) {
|
||||
if(error) throw error;
|
||||
|
||||
that.fs.stat('/myfile', function(error, result) {
|
||||
_error = error;
|
||||
complete1 = true;
|
||||
});
|
||||
|
||||
that.fs.stat('/myotherfile', function(error, result) {
|
||||
if(error) throw error;
|
||||
|
||||
_stats = result;
|
||||
complete2 = true;
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
waitsFor(function() {
|
||||
return complete1 && complete2;
|
||||
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||
|
||||
runs(function() {
|
||||
expect(_error).toBeDefined();
|
||||
expect(_stats.nlinks).toEqual(1);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue