Skip to content

Commit

Permalink
Fixed parsing of FTP directory listing from some servers. Closes #3.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi committed Aug 25, 2011
1 parent aeadd77 commit 6752c13
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
6 changes: 6 additions & 0 deletions lib/ftp_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ var compact = function(array) {
* @returns {Object} Parsed object with the file entry properties
*/
exports.entryParser = function(entry) {
// Some servers include an official code-multiline sign at the beginning of
// every string. We must strip it if that's the case.
if (RE_MULTI_RESPONSE.test(entry))
entry = entry.substr(3);

entry = entry.trim();
var c = entry.charAt(0);

Expand All @@ -109,6 +114,7 @@ exports.entryParser = function(entry) {

// Filter file-listing results from 'STAT' command, since they include
// server responses before and after the file listing.
// Issue: https://github.com/sergi/jsftp/issues/3
if (RE_SERVER_RESPONSE.test(entry) || RE_MULTI_RESPONSE.test(entry))
return null;

Expand Down
85 changes: 83 additions & 2 deletions lib/ftp_parser_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = {
},

"test ftp unix LIST responses" : function(next) {
var str = "211-Status of /:\r\n\
var str = "211-Status of /:\r\n\
drwx--x--- 10 mrclash adm 4096 Aug 9 14:48 .\r\n\
drwx--x--- 10 mrclash adm 4096 Aug 9 14:48 ..\r\n\
-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias\r\n\
Expand All @@ -38,6 +38,8 @@ var str = "211-Status of /:\r\n\
-rw-r--r-- 1 mrclash pg223090 4 Aug 4 09:11 testfile.txt\r\n\
211 End of status";



var unixEntries = [
{
//line: "-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias",
Expand Down Expand Up @@ -343,6 +345,58 @@ var str = "211-Status of /:\r\n\
}
];

var str2 = "211-Status of /www/userName/test:\
211-drwxr-x--- 2 userName alternc 4096 Aug 22 03:45 .\r\n\
211-drwxr-x--- 5 userName alternc 4096 Aug 22 03:45 ..\r\n\
211--rw-r----- 1 userName alternc 460 Aug 22 03:45 test1\r\n\
211--rw-r----- 1 userName alternc 560 Aug 22 03:45 test2\r\n\
211 End of status";

var unixEntries2 = [
{
//line: "-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias",
type: 0,
size: 460,
name: "test1",
time: 998444700000,
owner: "userName",
group: "alternc",

userReadPerm : true,
userWritePerm : true,
userExecPerm : false,

groupReadPerm : true,
groupWritePerm : false,
groupExecPerm : false,

otherReadPerm : false,
otherWritePerm : false,
otherExecPerm : false
},
{
//line: "-rw-r--r-- 1 mrclash pg223090 260 Mar 25 2008 .alias",
type: 0,
size: 560,
name: "test2",
time: 998444700000,
owner: "userName",
group: "alternc",

userReadPerm : true,
userWritePerm : true,
userExecPerm : false,

groupReadPerm : true,
groupWritePerm : false,
groupExecPerm : false,

otherReadPerm : false,
otherWritePerm : false,
otherExecPerm : false
}
]

str
.split(/\r\n/)
.map(function(entry) {
Expand All @@ -351,7 +405,6 @@ var str = "211-Status of /:\r\n\
// Flatten the array
.filter(function(value){ return !!value; })
.forEach(function(entry, i) {
console.log(entry)
assert.equal(unixEntries[i].type, entry.type);
assert.equal(unixEntries[i].size, entry.size);
assert.equal(unixEntries[i].name, entry.name);
Expand All @@ -372,6 +425,34 @@ var str = "211-Status of /:\r\n\
assert.equal(unixEntries[i].otherExecPerm, entry.otherPermissions.exec);
});

str2
.split(/\r\n/)
.map(function(entry) {
return Parser.entryParser(entry.replace("\n", ""));
})
// Flatten the array
.filter(function(value){ return !!value; })
.forEach(function(entry, i) {
assert.equal(unixEntries2[i].type, entry.type);
assert.equal(unixEntries2[i].size, entry.size);
assert.equal(unixEntries2[i].name, entry.name);
assert.equal(unixEntries2[i].time, entry.time);
assert.equal(unixEntries2[i].owner, entry.owner);
assert.equal(unixEntries2[i].group, entry.group);

assert.equal(unixEntries2[i].userReadPerm, entry.userPermissions.read);
assert.equal(unixEntries2[i].userWritePerm, entry.userPermissions.write);
assert.equal(unixEntries2[i].userExecPerm, entry.userPermissions.exec);

assert.equal(unixEntries2[i].groupReadPerm, entry.groupPermissions.read);
assert.equal(unixEntries2[i].groupWritePerm, entry.groupPermissions.write);
assert.equal(unixEntries2[i].groupExecPerm, entry.groupPermissions.exec);

assert.equal(unixEntries2[i].otherReadPerm, entry.otherPermissions.read);
assert.equal(unixEntries2[i].otherWritePerm, entry.otherPermissions.write);
assert.equal(unixEntries2[i].otherExecPerm, entry.otherPermissions.exec);
});

next();
},
"test ftp windows/DOS LIST responses" : function(next) {
Expand Down

0 comments on commit 6752c13

Please sign in to comment.