Node-SMB / marsaud-smb2

SMB2 Client
53 stars 46 forks source link

Bug in readstream size compare? #20

Open hello-josh opened 7 years ago

hello-josh commented 7 years ago

I'm trying out your fork today because I need to stream a 120-ish MB file from a Windows share to s3 and the readable stream never emits an end event. I added some logging code and it might be miscalculating the file size.

My code is similar to this:

smb2Client.createReadStream(filename, function (err, readStream) {
        if (err) {
            LOG.error(`Error creating ReadStream from windows share`, share, filename, err);
            return callback(err);
        }

        let total = 0;
        readStream.on('readable', () => {
            let chunk;
            while (null !== (chunk = readStream.read())) {
                total += chunk.length;
                LOG.info(`total: ${total} row size ${chunk.length} offset ${readStream.offset.toNumber()} filelength: ${readStream.fileLength}`);
            }
        });
});

When the sizes in the LOG.info stop moving, it ends with reading 3080192 but the total size is 119356491

Using the library you are forking, I get the entire file in memory

smb2Client.readFile(filename, function (err, data) {
        if (err) {
            LOG.error(`Error reading from windows share`, share, filename, err);
            return callback(err);
        }
        LOG.info(data.length);
    });

Outputs 119356491

marsaud commented 7 years ago

I'll try to look at this ASAP. Just to be sure, can you just check if the second test you show with the forked library using readFile is also ok with my fork please ?

hello-josh commented 7 years ago

Sure. I didn't even think of that. On Fri, May 12, 2017 at 4:28 AM Fabrice Marsaud notifications@github.com wrote:

I'll try to look at this ASAP. Just to be sure, can you just check if the second test you show with the forked library using readFile is also ok with my fork please ?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/Node-SMB/marsaud-smb2/issues/20#issuecomment-301016671, or mute the thread https://github.com/notifications/unsubscribe-auth/AAm8eWgpS19E3S4wGJiZBdMTQ_X0c_ksks5r5BglgaJpZM4NYUAl .

hello-josh commented 7 years ago

Here is a complete code example. Fill in the relevant details for user account and file to read. The const event stuff is because I am going to plug this code into AWS Lambda eventually. So far this is running locally on my Mac using node v6.10.2 via nvm

josh$ node --version
v6.10.2
let SMB2 = require('@marsaud/smb2');

const event = {
    username: '--------',
    password: '--------',
    domain: '--------'
};

let username = event.username || process.env['username'];
let password = event.password || process.env['password'];
let domain = event.domain || process.env['domain'];
let share = event.share || process.env['share'];
let filename = event.filename || process.env['filename'];

const options = {
    'domain': domain,
    'username': username,
    'password': password,
    'share': share
};

let smb2Client = new SMB2(options);

smb2Client.readFile(filename, function (err, data) {
    if (err) {
        return console.error(`Error reading from windows share`, share, filename, err);
    }
    console.info(`File read with ${data.length} length`);
});

OUTPUT

> File read with 120484083 length

Running the same code, but using the createReadStream

let SMB2 = require('@marsaud/smb2');

const event = {
    username: '--------',
    password: '--------',
    domain: '--------'
};

let username = event.username || process.env['username'];
let password = event.password || process.env['password'];
let domain = event.domain || process.env['domain'];
let share = event.share || process.env['share'];
let filename = event.filename || process.env['filename'];

const options = {
    'domain': domain,
    'username': username,
    'password': password,
    'share': share
};

let smb2Client = new SMB2(options);
smb2Client.createReadStream(filename, function (err, readStream) {
    if (err) {
        return console.error(`Error creating ReadStream from windows share`, share, filename, err);
    }

    let total = 0;
    readStream.on('readable', () => {
        let chunk;
        while (null !== (chunk = readStream.read())) {
            total += chunk.length;
            console.info(`total: ${total} row size ${chunk.length} offset ${readStream.offset.toNumber()} filelength: ${readStream.fileLength}`);
        }
    });
});

OUTPUT

total: 65536 row size 65536 offset 65536 filelength: 120484083
total: 131072 row size 65536 offset 131072 filelength: 120484083
total: 196608 row size 65536 offset 196608 filelength: 120484083
total: 262144 row size 65536 offset 262144 filelength: 120484083
total: 327680 row size 65536 offset 327680 filelength: 120484083
total: 393216 row size 65536 offset 393216 filelength: 120484083
total: 458752 row size 65536 offset 458752 filelength: 120484083
total: 524288 row size 65536 offset 524288 filelength: 120484083
total: 589824 row size 65536 offset 589824 filelength: 120484083
total: 655360 row size 65536 offset 655360 filelength: 120484083
total: 720896 row size 65536 offset 720896 filelength: 120484083
total: 786432 row size 65536 offset 786432 filelength: 120484083
total: 851968 row size 65536 offset 851968 filelength: 120484083
total: 917504 row size 65536 offset 917504 filelength: 120484083
total: 983040 row size 65536 offset 983040 filelength: 120484083
total: 1048576 row size 65536 offset 1048576 filelength: 120484083
total: 1114112 row size 65536 offset 1114112 filelength: 120484083
total: 1179648 row size 65536 offset 1179648 filelength: 120484083
total: 1245184 row size 65536 offset 1245184 filelength: 120484083
total: 1310720 row size 65536 offset 1310720 filelength: 120484083
total: 1376256 row size 65536 offset 1376256 filelength: 120484083
total: 1441792 row size 65536 offset 1441792 filelength: 120484083
total: 1507328 row size 65536 offset 1507328 filelength: 120484083
total: 1572864 row size 65536 offset 1572864 filelength: 120484083
total: 1638400 row size 65536 offset 1638400 filelength: 120484083
total: 1703936 row size 65536 offset 1703936 filelength: 120484083
total: 1769472 row size 65536 offset 1769472 filelength: 120484083
total: 1835008 row size 65536 offset 1835008 filelength: 120484083
total: 1900544 row size 65536 offset 1900544 filelength: 120484083
total: 1966080 row size 65536 offset 1966080 filelength: 120484083
total: 2031616 row size 65536 offset 2031616 filelength: 120484083
total: 2097152 row size 65536 offset 2097152 filelength: 120484083
total: 2162688 row size 65536 offset 2162688 filelength: 120484083
total: 2228224 row size 65536 offset 2228224 filelength: 120484083
total: 2293760 row size 65536 offset 2293760 filelength: 120484083
total: 2359296 row size 65536 offset 2359296 filelength: 120484083
total: 2424832 row size 65536 offset 2424832 filelength: 120484083
total: 2490368 row size 65536 offset 2490368 filelength: 120484083
total: 2555904 row size 65536 offset 2555904 filelength: 120484083
total: 2621440 row size 65536 offset 2621440 filelength: 120484083
total: 2686976 row size 65536 offset 2686976 filelength: 120484083
total: 2752512 row size 65536 offset 2752512 filelength: 120484083
total: 2818048 row size 65536 offset 2818048 filelength: 120484083
total: 2883584 row size 65536 offset 2883584 filelength: 120484083
total: 2949120 row size 65536 offset 2949120 filelength: 120484083
total: 3014656 row size 65536 offset 3014656 filelength: 120484083
total: 3080192 row size 65536 offset 3080192 filelength: 120484083
total: 3145728 row size 65536 offset 3145728 filelength: 120484083
total: 3211264 row size 65536 offset 3211264 filelength: 120484083
total: 3276800 row size 65536 offset 3276800 filelength: 120484083
total: 3342336 row size 65536 offset 3342336 filelength: 120484083
total: 3407872 row size 65536 offset 3407872 filelength: 120484083

Then it stops, no end event

orekav commented 4 years ago

I was having a similar issue today. Have you check the closeTimeout parameter when you create an instance of SMB2?

torped commented 2 years ago

I had the exactly the same problem and it is the autoCloseTimeout that is set to default: 1000...

var smb2Client = new SMB2({ share:'\\192.168.1.125\Filmer' //${ellen} , domain:'WORKGROUP' , username:'ellen' //user , password:'123' //password , autoCloseTimeout: 2147483647 <---- Here you can modify it });