Commit c27eaa4c authored by Julien Fontanet's avatar Julien Fontanet

fix(read): various issues

parent 77f48dc0
...@@ -4,39 +4,39 @@ var BigInt = require('../tools/bigint'); ...@@ -4,39 +4,39 @@ var BigInt = require('../tools/bigint');
var request = require('../tools/smb2-forge').request; var request = require('../tools/smb2-forge').request;
var MAX_READ_LENGTH = require('../structures/constants').MAX_READ_LENGTH; var MAX_READ_LENGTH = require('../structures/constants').MAX_READ_LENGTH;
module.exports = function read(file, buffer, offset, length, position, cb) { module.exports = function read(file, buffer, offset, length, start, cb) {
assert(position !== null, 'null position is not supported'); assert(pos !== null, 'null pos is not supported');
var fileSize = BigInt.fromBuffer(file.EndofFile);
var bytesRead = 0;
var connection = this; var connection = this;
var end = Math.min(
position = new BigInt(8, position); start + length,
BigInt.fromBuffer(file.EndofFile).toNumber()
);
var pos = start;
function onRead(err, chunk) { function onRead(err, chunk) {
if (err != null) { if (err != null) {
return cb(err, bytesRead, buffer); return cb(err, pos - start, buffer);
} }
chunk.copy(buffer, offset); chunk.copy(buffer, offset);
var n = chunk.length; var n = chunk.length;
bytesRead += n;
offset += n; offset += n;
position = position.add(n); pos += n;
readChunk(); readChunk();
} }
function readChunk() { function readChunk() {
if (bytesRead >= length || position.ge(fileSize)) { if (pos >= end) {
return cb(null, bytesRead, buffer); return cb(null, pos - start, buffer);
} }
request( request(
'read', 'read',
{ {
FileId: file.FileId, FileId: file.FileId,
Length: Math.min(MAX_READ_LENGTH, length - bytesRead), Length: Math.min(MAX_READ_LENGTH, end - pos),
Offset: position.toBuffer(), Offset: new BigInt(8, pos).toBuffer(),
}, },
connection, connection,
onRead onRead
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment