Add fstat and tests. Fix position bug in read/write.
This commit is contained in:
parent
33791ca950
commit
850758e37a
|
@ -6292,7 +6292,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
|||
callback(error);
|
||||
} else {
|
||||
fileData = result;
|
||||
var _position = (undefined !== position) ? position : ofd.position;
|
||||
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
|
||||
var newSize = Math.max(fileData.length, _position + length);
|
||||
var newData = new Uint8Array(newSize);
|
||||
if(fileData) {
|
||||
|
@ -6348,7 +6348,7 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
|||
callback(error);
|
||||
} else {
|
||||
fileData = result;
|
||||
var _position = (undefined !== position) ? position : ofd.position;
|
||||
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
|
||||
length = (_position + length > buffer.length) ? length - _position : length;
|
||||
var dataView = fileData.subarray(_position, _position + length);
|
||||
buffer.set(dataView, offset);
|
||||
|
@ -6375,6 +6375,18 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
|||
};
|
||||
};
|
||||
|
||||
function fstat_file(objectStore, ofd, callback) {
|
||||
read_object(objectStore, ofd.id, check_file);
|
||||
|
||||
function check_file(error, result) {
|
||||
if(error) {
|
||||
callback(error);
|
||||
} else {
|
||||
callback(undefined, result);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
function link_node(objectStore, oldpath, newpath, callback) {
|
||||
oldpath = normalize(oldpath);
|
||||
var oldname = basename(oldpath);
|
||||
|
@ -6773,7 +6785,53 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
|||
);
|
||||
};
|
||||
FileSystem.prototype.fstat = function fstat(fd, 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);
|
||||
|
||||
function check_result(error, result) {
|
||||
if(error) {
|
||||
// if(transaction.error) transaction.abort();
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
var stats = {
|
||||
node: result.id,
|
||||
dev: that.name,
|
||||
size: result.size,
|
||||
nlinks: result.nlinks,
|
||||
atime: result.atime,
|
||||
mtime: result.mtime,
|
||||
ctime: result.ctime,
|
||||
type: result.mode,
|
||||
};
|
||||
deferred.resolve(stats);
|
||||
}
|
||||
};
|
||||
|
||||
var ofd = that.openFiles[fd];
|
||||
|
||||
if(!ofd) {
|
||||
deferred.reject(new EBadFileDescriptor('invalid file descriptor'));
|
||||
} else {
|
||||
fstat_file(files, ofd, check_result);
|
||||
}
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback(undefined, result);
|
||||
},
|
||||
function(error) {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
function() {
|
||||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.link = function link(oldpath, newpath, callback) {
|
||||
var that = this;
|
||||
|
@ -6840,12 +6898,6 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
|||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.getxattr = function getxattr(path, name, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.read = function read(fd, buffer, offset, length, position, callback) {
|
||||
var that = this;
|
||||
|
@ -6936,11 +6988,79 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
|
|||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.seek = function seek(fd, offset, whence, callback) {
|
||||
FileSystem.prototype.getxattr = function getxattr(path, name, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.seek = function seek(fd, offset, whence, 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);
|
||||
|
||||
function check_result(error, offset) {
|
||||
if(error) {
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
deferred.resolve(offset);
|
||||
}
|
||||
};
|
||||
|
||||
var ofd = that.openFiles[fd];
|
||||
|
||||
if(!ofd) {
|
||||
deferred.reject(new EBadFileDescriptor('invalid file descriptor'));
|
||||
}
|
||||
|
||||
if('SET' === whence) {
|
||||
if(offset < 0) {
|
||||
deferred.reject(new EInvalid('resulting file offset would be negative'));
|
||||
} else {
|
||||
ofd.position = offset;
|
||||
deferred.resolve(ofd.position);
|
||||
}
|
||||
} else if('CUR' === whence) {
|
||||
if(ofd.position + offset < 0) {
|
||||
deferred.reject(new EInvalid('resulting file offset would be negative'));
|
||||
} else {
|
||||
ofd.position += offset;
|
||||
deferred.resolve(ofd.position);
|
||||
}
|
||||
} else if('END' === whence) {
|
||||
// do fstat
|
||||
} else {
|
||||
deferred.reject(new EInvalid('whence argument is not a proper value'));
|
||||
}
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback(undefined, result);
|
||||
},
|
||||
function(error) {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
function() {
|
||||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.utime = function utime(path, atime, mtime, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.rename = function rename(oldpath, newpath, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.truncate = function truncate(path, length, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.ftruncate = function ftruncate(fd, length, callback) {
|
||||
|
||||
};
|
||||
|
||||
return {
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -25,26 +25,22 @@ var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
|
|||
console.log('buffer', buffer);
|
||||
var data = new Uint8Array(buffer.length);
|
||||
|
||||
fs.open('/myfile', 'w+', function(error, fd) {
|
||||
try {
|
||||
fs.open('/tmp', 'w+', function(error, fd) {
|
||||
if(error) throw error;
|
||||
console.log('descriptor:', fd);
|
||||
fs.write(fd, buffer, 0, buffer.length, undefined, function(error, nbytes) {
|
||||
console.log('write:', nbytes)
|
||||
fs.fstat(fd, function(error, stats) {
|
||||
if(error) throw error;
|
||||
console.log('stats:', stats);
|
||||
fs.close(fd, function(error) {
|
||||
if(error) throw error;
|
||||
console.log('closed');
|
||||
fs.link('/myfile', '/myotherfile', function(error) {
|
||||
if(error) throw error;
|
||||
console.log('linked');
|
||||
fs.stat('/myfile', function(error, stats) {
|
||||
if(error) throw error;
|
||||
console.log(stats);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch(e) {
|
||||
console.error(e);
|
||||
}
|
||||
|
||||
});
|
||||
</script>
|
||||
|
|
|
@ -492,7 +492,7 @@ define(function(require) {
|
|||
callback(error);
|
||||
} else {
|
||||
fileData = result;
|
||||
var _position = (!(undefined === position && null === position)) ? position : ofd.position;
|
||||
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
|
||||
var newSize = Math.max(fileData.length, _position + length);
|
||||
var newData = new Uint8Array(newSize);
|
||||
if(fileData) {
|
||||
|
@ -548,7 +548,7 @@ define(function(require) {
|
|||
callback(error);
|
||||
} else {
|
||||
fileData = result;
|
||||
var _position = (!(undefined === position && null === position)) ? position : ofd.position;
|
||||
var _position = (!(undefined === position || null === position)) ? position : ofd.position;
|
||||
length = (_position + length > buffer.length) ? length - _position : length;
|
||||
var dataView = fileData.subarray(_position, _position + length);
|
||||
buffer.set(dataView, offset);
|
||||
|
@ -575,6 +575,18 @@ define(function(require) {
|
|||
};
|
||||
};
|
||||
|
||||
function fstat_file(objectStore, ofd, callback) {
|
||||
read_object(objectStore, ofd.id, check_file);
|
||||
|
||||
function check_file(error, result) {
|
||||
if(error) {
|
||||
callback(error);
|
||||
} else {
|
||||
callback(undefined, result);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
function link_node(objectStore, oldpath, newpath, callback) {
|
||||
oldpath = normalize(oldpath);
|
||||
var oldname = basename(oldpath);
|
||||
|
@ -973,7 +985,53 @@ define(function(require) {
|
|||
);
|
||||
};
|
||||
FileSystem.prototype.fstat = function fstat(fd, 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);
|
||||
|
||||
function check_result(error, result) {
|
||||
if(error) {
|
||||
// if(transaction.error) transaction.abort();
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
var stats = {
|
||||
node: result.id,
|
||||
dev: that.name,
|
||||
size: result.size,
|
||||
nlinks: result.nlinks,
|
||||
atime: result.atime,
|
||||
mtime: result.mtime,
|
||||
ctime: result.ctime,
|
||||
type: result.mode,
|
||||
};
|
||||
deferred.resolve(stats);
|
||||
}
|
||||
};
|
||||
|
||||
var ofd = that.openFiles[fd];
|
||||
|
||||
if(!ofd) {
|
||||
deferred.reject(new EBadFileDescriptor('invalid file descriptor'));
|
||||
} else {
|
||||
fstat_file(files, ofd, check_result);
|
||||
}
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback(undefined, result);
|
||||
},
|
||||
function(error) {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
function() {
|
||||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.link = function link(oldpath, newpath, callback) {
|
||||
var that = this;
|
||||
|
@ -1040,12 +1098,6 @@ define(function(require) {
|
|||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.getxattr = function getxattr(path, name, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.read = function read(fd, buffer, offset, length, position, callback) {
|
||||
var that = this;
|
||||
|
@ -1136,13 +1188,72 @@ define(function(require) {
|
|||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.seek = function seek(fd, offset, whence, callback) {
|
||||
FileSystem.prototype.getxattr = function getxattr(path, name, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.seek = function seek(fd, offset, whence, 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);
|
||||
|
||||
function check_result(error, offset) {
|
||||
if(error) {
|
||||
deferred.reject(error);
|
||||
} else {
|
||||
deferred.resolve(offset);
|
||||
}
|
||||
};
|
||||
|
||||
var ofd = that.openFiles[fd];
|
||||
|
||||
if(!ofd) {
|
||||
deferred.reject(new EBadFileDescriptor('invalid file descriptor'));
|
||||
}
|
||||
|
||||
if('SET' === whence) {
|
||||
if(offset < 0) {
|
||||
deferred.reject(new EInvalid('resulting file offset would be negative'));
|
||||
} else {
|
||||
ofd.position = offset;
|
||||
deferred.resolve(ofd.position);
|
||||
}
|
||||
} else if('CUR' === whence) {
|
||||
if(ofd.position + offset < 0) {
|
||||
deferred.reject(new EInvalid('resulting file offset would be negative'));
|
||||
} else {
|
||||
ofd.position += offset;
|
||||
deferred.resolve(ofd.position);
|
||||
}
|
||||
} else if('END' === whence) {
|
||||
// do fstat
|
||||
} else {
|
||||
deferred.reject(new EInvalid('whence argument is not a proper value'));
|
||||
}
|
||||
|
||||
deferred.promise.then(
|
||||
function(result) {
|
||||
callback(undefined, result);
|
||||
},
|
||||
function(error) {
|
||||
callback(error);
|
||||
}
|
||||
);
|
||||
},
|
||||
function() {
|
||||
callback(new EFileSystemError('unknown error'));
|
||||
}
|
||||
);
|
||||
};
|
||||
FileSystem.prototype.utime = function utime(path, atime, mtime, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.rename = function rename(old, new, callback) {
|
||||
FileSystem.prototype.rename = function rename(oldpath, newpath, callback) {
|
||||
|
||||
};
|
||||
FileSystem.prototype.truncate = function truncate(path, length, callback) {
|
||||
|
|
|
@ -19,7 +19,7 @@ function mk_id(length) {
|
|||
};
|
||||
|
||||
function mk_db_name() {
|
||||
var name = TEST_DATABASE_NAME + mk_id(5);
|
||||
var name = TEST_DATABASE_NAME + mk_id(5) + Date.now();
|
||||
test_database_names.push(name);
|
||||
return name;
|
||||
};
|
||||
|
@ -150,6 +150,57 @@ describe('fs.stat', function() {
|
|||
});
|
||||
});
|
||||
|
||||
describe('fs.fstat', 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.fstat).toEqual('function');
|
||||
});
|
||||
|
||||
it('should return a stat object for a valid descriptor', function() {
|
||||
var complete = false;
|
||||
var _error, _result;
|
||||
var that = this;
|
||||
|
||||
that.fs.open('/myfile', 'w+', function(error, result) {
|
||||
if(error) throw error;
|
||||
|
||||
var fd = result;
|
||||
that.fs.fstat(fd, function(error, result) {
|
||||
_error = error;
|
||||
_result = result;
|
||||
|
||||
complete = true;
|
||||
})
|
||||
});
|
||||
|
||||
waitsFor(function() {
|
||||
return complete;
|
||||
}, 'test to complete', DEFAULT_TIMEOUT);
|
||||
|
||||
runs(function() {
|
||||
expect(_result).toBeDefined();
|
||||
expect(_error).not.toBeDefined();
|
||||
expect(_result['node']).toBeDefined();
|
||||
expect(_result['dev']).toEqual(that.db_name);
|
||||
expect(_result['size']).toBeDefined();
|
||||
expect(_result['nlinks']).toEqual(jasmine.any(Number));
|
||||
expect(_result['atime']).toEqual(jasmine.any(Number));
|
||||
expect(_result['mtime']).toEqual(jasmine.any(Number));
|
||||
expect(_result['ctime']).toEqual(jasmine.any(Number));
|
||||
expect(_result['type']).toBeDefined();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('fs.mkdir', function() {
|
||||
beforeEach(function() {
|
||||
this.db_name = mk_db_name();
|
||||
|
|
Loading…
Reference in New Issue