Commit 60c97054 authored by John Pogas's avatar John Pogas Committed by Julien Fontanet

fix(readdir): read all items (#50)

Fixes #49

Readdir only lists 510 items. This fix returns the full directory listing.
parent 5687d375
......@@ -17,28 +17,76 @@ var SMB2Request = SMB2Forge.request;
module.exports = function readdir(path, cb) {
var connection = this;
// SMB2 open directory
SMB2Request('open', { path: path }, connection, function(err, file) {
if (err) cb && cb(err);
// SMB2 query directory
else
function queryDirectory(filesBatch, file, connection, cb) {
SMB2Request('query_directory', file, connection, function(err, files) {
SMB2Request('close', file, connection, function() {
if (err) cb && cb(err);
// SMB2 close directory
else
cb &&
cb(
null,
if (err) {
if(err.code === 'STATUS_NO_MORE_FILES') {
cb(null, filesBatch);
} else {
cb(err);
}
} else {
filesBatch.push(
files
.map(function(v) {
// get the filename only
return v.Filename;
}) // get the filename only
})
.filter(function(v) {
// remove '.' and '..' values
return v !== '.' && v !== '..';
}) // remove '.' and '..' values
})
);
queryDirectory(filesBatch, file, connection, cb);
}
});
}
function openDirectory(path, connection, cb) {
SMB2Request('open', { path: path }, connection, function(err, file) {
if (err) {
cb(err);
} else {
return cb(null, file);
}
});
}
function closeDirectory(file, connection, cb) {
// SMB2 query directory
SMB2Request('close', file, connection, function(err, res) {
if (err) {
if(err.code !== 'STATUS_FILE_CLOSED') {
cb(err);
}
}
// SMB2 close directory
cb(null, res);
});
}
openDirectory(path, connection, function(err, file) {
var totalFiles = [];
var filesBatch = [];
if (err) {
cb(err);
} else {
queryDirectory(filesBatch, file, connection, function(err, file) {
if (err) {
cb(err);
} else {
closeDirectory(file, connection, function(err, file) {
if (err) {
cb(err);
} else {
totalFiles = [].concat(...filesBatch);
cb(null, totalFiles);
}
});
}
})
}
});
};
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