Added lseek and tests.

This commit is contained in:
Alan Kligman 2013-07-29 15:59:17 -04:00
parent 850758e37a
commit dabf8c8c7a
4 changed files with 218 additions and 13 deletions

22
dist/idbfs.js vendored
View File

@ -6994,13 +6994,11 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
};
FileSystem.prototype.seek = function seek(fd, offset, whence, callback) {
FileSystem.prototype.lseek = function lseek(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) {
@ -7031,7 +7029,23 @@ define('src/file-system',['require','lodash','when','src/path','src/path','src/p
deferred.resolve(ofd.position);
}
} else if('END' === whence) {
// do fstat
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'));
}

View File

@ -29,9 +29,9 @@ try {
fs.open('/tmp', 'w+', function(error, fd) {
if(error) throw error;
console.log('descriptor:', fd);
fs.fstat(fd, function(error, stats) {
fs.lseek(fd, 2, 'END', function(error, pos) {
if(error) throw error;
console.log('stats:', stats);
console.log('pos:', pos);
fs.close(fd, function(error) {
if(error) throw error;
console.log('closed');

View File

@ -1194,13 +1194,11 @@ define(function(require) {
FileSystem.prototype.setxattr = function setxattr(path, name, value, callback) {
};
FileSystem.prototype.seek = function seek(fd, offset, whence, callback) {
FileSystem.prototype.lseek = function lseek(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) {
@ -1231,7 +1229,23 @@ define(function(require) {
deferred.resolve(ofd.position);
}
} else if('END' === whence) {
// do fstat
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'));
}

View File

@ -24,7 +24,7 @@ function mk_db_name() {
return name;
};
function array_buffer_equal(left, right) {
function typed_array_equal(left, right) {
if(left.length !== right.length) {
return false;
}
@ -699,7 +699,7 @@ describe('fs.read', function() {
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(rbuffer.length);
expect(array_buffer_equal(wbuffer, rbuffer)).toEqual(true);
expect(typed_array_equal(wbuffer, rbuffer)).toEqual(true);
});
});
@ -740,7 +740,7 @@ describe('fs.read', function() {
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(rbuffer.length);
expect(array_buffer_equal(wbuffer.buffer, rbuffer.buffer)).toEqual(true);
expect(typed_array_equal(wbuffer.buffer, rbuffer.buffer)).toEqual(true);
});
});
});
@ -909,3 +909,180 @@ describe('fs.unlink', function() {
});
});
});
describe('fs.lseek', 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.lseek).toEqual('function');
});
it('should set the current position if whence is SET', function() {
var complete = false;
var _error, _result, _stats;
var that = this;
var offset = 3;
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = new Uint8Array(buffer.length + offset);
that.fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
var fd = result;
that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
that.fs.lseek(fd, offset, 'SET', function(error, result) {
_error = error;
_result = result;
that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
that.fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) {
if(error) throw error;
that.fs.stat('/myfile', function(error, result) {
if(error) throw error;
_stats = result;
complete = true;
});
});
});
});
})
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(offset);
expect(_stats.size).toEqual(offset + buffer.length);
var expected = new Uint8Array([1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8]);
expect(typed_array_equal(result_buffer, expected)).toEqual(true);
});
});
it('should update the current position if whence is CUR', function() {
var complete = false;
var _error, _result, _stats;
var that = this;
var offset = -2;
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer = new Uint8Array(2 * buffer.length + offset);
that.fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
var fd = result;
that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
that.fs.lseek(fd, offset, 'CUR', function(error, result) {
_error = error;
_result = result;
that.fs.write(fd, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
that.fs.read(fd, result_buffer, 0, result_buffer.length, 0, function(error, result) {
if(error) throw error;
that.fs.stat('/myfile', function(error, result) {
if(error) throw error;
_stats = result;
complete = true;
});
});
});
});
})
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(offset + buffer.length);
expect(_stats.size).toEqual(offset + 2 * buffer.length);
var expected = new Uint8Array([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 8]);
expect(typed_array_equal(result_buffer, expected)).toEqual(true);
});
});
it('should update the current position if whence is END', function() {
var complete = false;
var _error, _result, _stats;
var that = this;
var offset = 5;
var buffer = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
var result_buffer;
that.fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
var fd1 = result;
that.fs.write(fd1, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
that.fs.open('/myfile', 'w+', function(error, result) {
if(error) throw error;
var fd2 = result;
that.fs.lseek(fd2, offset, 'END', function(error, result) {
_error = error;
_result = result;
that.fs.write(fd2, buffer, 0, buffer.length, undefined, function(error, result) {
if(error) throw error;
that.fs.stat('/myfile', function(error, result) {
if(error) throw error;
_stats = result;
result_buffer = new Uint8Array(_stats.size);
that.fs.read(fd2, result_buffer, 0, result_buffer.length, 0, function(error, result) {
if(error) throw error;
complete = true;
});
});
});
});
});
})
});
waitsFor(function() {
return complete;
}, 'test to complete', DEFAULT_TIMEOUT);
runs(function() {
expect(_error).not.toBeDefined();
expect(_result).toEqual(offset + buffer.length);
expect(_stats.size).toEqual(offset + 2 * buffer.length);
var expected = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8]);
expect(typed_array_equal(result_buffer, expected)).toEqual(true);
});
});
});