Skip to content

Commit 725590f

Browse files
authored
Merge pull request #29 from bchelli/readlargedir
Read large directories
2 parents a90b399 + 8cdf0b3 commit 725590f

File tree

5 files changed

+83
-46
lines changed

5 files changed

+83
-46
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ This function will close the open connection if opened, it will be called automa
146146
- [Benjamin Chelli](https://github.com/bchelli)
147147
- [Fabrice Marsaud](https://github.com/marsaud)
148148
- [Jay McAliley](https://github.com/jaymcaliley)
149+
- [eldrago](https://github.com/eldrago)
149150

150151
## References
151152

lib/api/readdir.js

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,50 @@ module.exports = function(path, cb){
2424
SMB2Request('open', {path:path}, connection, function(err, file){
2525
if(err) cb && cb(err);
2626
// SMB2 query directory
27-
else SMB2Request('query_directory', file, connection, function(err, files){
28-
if(err) cb && cb(err);
29-
// SMB2 close directory
30-
else SMB2Request('close', file, connection, function(err){
31-
cb && cb(
32-
null
33-
, files
27+
else queryDir(file, connection, [], cb);
28+
});
29+
}
30+
31+
32+
33+
34+
35+
/*
36+
* Helpers
37+
*/
38+
39+
/**
40+
* queryDir - recursive querying until all files in a directory have been added to the listing.
41+
* @param file
42+
* @param connection
43+
* @param completeFileListing
44+
* @param cb
45+
*/
46+
function queryDir(file, connection, completeFileListing, cb) {
47+
SMB2Request('query_directory', file, connection, function(err, files){
48+
var allFiles = completeFileListing.concat(files || []);
49+
50+
// no more file
51+
// => close and send response
52+
if(err && err.code === 'STATUS_NO_MORE_FILES') {
53+
return SMB2Request('close', file, connection, function(err){
54+
var fileNames = allFiles
3455
.map(function(v){ return v.Filename }) // get the filename only
3556
.filter(function(v){ return v!='.' && v!='..' }) // remove '.' and '..' values
36-
);
57+
;
58+
cb && cb(null, fileNames);
3759
});
38-
});
60+
}
61+
62+
// error
63+
// => close and send error
64+
if (err) {
65+
return cb && cb(err);
66+
}
67+
68+
// still receiving files
69+
// => maybe the folder is not empty
70+
queryDir(file, connection, allFiles, cb)
3971
});
40-
4172
}
4273

43-

lib/tools/message.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ var defaults = {
2222
, self.parseResponse && self.parseResponse(response)
2323
);
2424
} else {
25-
cb && cb(new Error(MsErref.getErrorMessage(err)));
25+
var error = new Error(MsErref.getErrorMessage(err));
26+
error.code = err.code;
27+
cb && cb(error);
2628
}
2729
};
2830

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
11
{
2-
3-
"name":"smb2"
4-
, "description":"SMB2 Client"
5-
, "homepage": "https://github.com/bchelli/node-smb2"
6-
7-
, "version":"0.2.8"
8-
9-
, "engines": [
2+
"name": "smb2",
3+
"description": "SMB2 Client",
4+
"homepage": "https://github.com/bchelli/node-smb2",
5+
"version": "0.2.8",
6+
"engines": [
107
"node"
11-
]
12-
13-
, "author":{
14-
"name": "Benjamin Chelli"
15-
, "email": "benjamin@chelli.net"
16-
, "url": "https://github.com/bchelli"
17-
}
18-
19-
, "main":"lib/smb2.js"
20-
21-
, "repository": {
22-
"type": "git"
23-
, "url": "https://github.com/bchelli/node-smb2"
24-
}
25-
26-
, "dependencies": {
8+
],
9+
"author": {
10+
"name": "Benjamin Chelli",
11+
"email": "benjamin@chelli.net",
12+
"url": "https://github.com/bchelli"
13+
},
14+
"main": "lib/smb2.js",
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/bchelli/node-smb2"
18+
},
19+
"dependencies": {
2720
"ntlm": "~0.1.1"
28-
}
29-
30-
, "keywords": [
31-
"SMB"
32-
, "SMB2"
33-
, "SMB3"
34-
, "NTLM"
35-
, "CIFS"
36-
, "Samba"
21+
},
22+
"keywords": [
23+
"SMB",
24+
"SMB2",
25+
"SMB3",
26+
"NTLM",
27+
"CIFS",
28+
"Samba"
3729
]
38-
3930
}

0 commit comments

Comments
 (0)