Merge pull request #231 from humphd/issue230

Fix Issue #230
This commit is contained in:
Alan K 2014-06-25 13:53:49 -04:00
commit 26742e635b
1 changed files with 43 additions and 2 deletions

View File

@ -56,6 +56,33 @@ function standard_check_result_cb(callback) {
}; };
} }
/**
* Coerce array-like data to Buffer so we can .copy(), etc.
* Allow null, a Buffer, or an object that can be dealt with
* by the Buffer constructor (e.g., Typed Array, Array, ...)
*
* WARNING: be very careful not to call this on parameters of
* API methods that pass storage (like read). You don't want to
* overwrite a buffer that a caller is holding a reference to,
* and expects to be filled via the read. If the caller passes
* in a non-Buffer, we should throw instead of coerce.
*/
function ensureBuffer(maybeBuffer) {
if(!maybeBuffer) {
return null;
}
if(Buffer.isBuffer(maybeBuffer)) {
return maybeBuffer;
}
try {
return new Buffer(maybeBuffer);
} catch(e) {
return null;
}
}
/** /**
* Update node times. Only passed times are modified (undefined times are ignored) * Update node times. Only passed times are modified (undefined times are ignored)
* and filesystem flags are examined in order to override update logic. * and filesystem flags are examined in order to override update logic.
@ -753,7 +780,10 @@ function write_data(context, ofd, buffer, offset, length, position, callback) {
if(error) { if(error) {
callback(error); callback(error);
} else { } else {
fileData = result; fileData = ensureBuffer(result);
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
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 newSize = Math.max(fileData.length, _position + length);
var newData = new Buffer(newSize); var newData = new Buffer(newSize);
@ -793,7 +823,10 @@ function read_data(context, ofd, buffer, offset, length, position, callback) {
if(error) { if(error) {
callback(error); callback(error);
} else { } else {
fileData = result; fileData = ensureBuffer(result);
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
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; length = (_position + length > buffer.length) ? length - _position : length;
fileData.copy(buffer, offset, _position, _position + length); fileData.copy(buffer, offset, _position, _position + length);
@ -1180,6 +1213,10 @@ function truncate_file(context, path, length, callback) {
if (error) { if (error) {
callback(error); callback(error);
} else { } else {
fileData = ensureBuffer(fileData);
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
var data = new Buffer(length); var data = new Buffer(length);
data.fill(0); data.fill(0);
if(fileData) { if(fileData) {
@ -1234,6 +1271,10 @@ function ftruncate_file(context, ofd, length, callback) {
callback(error); callback(error);
} else { } else {
var data; var data;
fileData = ensureBuffer(fileData);
if(!fileData) {
return callback(new Errors.EIO('Expected Buffer'));
}
if(fileData) { if(fileData) {
data = fileData.slice(0, length); data = fileData.slice(0, length);
} else { } else {