Skip to content

Commit

Permalink
LIST command misbehaves when file or directory doesn't exist. Fixes #95.
Browse files Browse the repository at this point in the history
  • Loading branch information
sergi committed Sep 12, 2014
1 parent f8d9e31 commit a8fd90c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
47 changes: 25 additions & 22 deletions lib/jsftp.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Ftp.prototype.reemit = function(event) {
if (self.debugMode) {
self.emit('jsftp_debug', 'event:' + event, data || {});
}
}
};
};

Ftp.prototype._createSocket = function(port, host, firstAction) {
Expand Down Expand Up @@ -143,12 +143,10 @@ Ftp.prototype.parseResponse = function(data) {

var next = this.cmdBuffer_[0][1];
if (data.isMark) {
// If we receive a Mark and it is not expected, we ignore
// that command
// If we receive a Mark and it is not expected, we ignore that command
if (!next.expectsMark || next.expectsMark.marks.indexOf(data.code) === -1)
return;
// We might have to ignore the command that comes after the
// mark.
// We might have to ignore the command that comes after the mark.
if (next.expectsMark.ignore)
this.ignoreCmdCode = next.expectsMark.ignore;
}
Expand Down Expand Up @@ -353,7 +351,7 @@ Ftp.prototype.setType = function(type, callback) {
/**
* Lists a folder's contents using a passive connection.
*
* @param {String} [path] Remote path for the file/folder to retrieve
* @param {String} path Remote path for the file/folder to retrieve
* @param {Function} callback Function to call with errors or results
*/
Ftp.prototype.list = function(path, callback) {
Expand All @@ -363,30 +361,35 @@ Ftp.prototype.list = function(path, callback) {
}

var self = this;
var cb = function(err, listing) {
var cb = once(function(err, listing) {
self.setType("I", once(function() {
callback(err, listing);
}));
};
cb.expectsMark = {
marks: [125, 150],
ignore: 226
};
});

var listing = "";
this.setType("A", function() {
self.getPasvSocket(function(err, socket) {
self.pasvTimeout.bind(self, socket, cb);
self.pasvTimeout.call(self, socket, cb);

socket.on("data", function(data) {
listing += data;
});
socket.on("close", function(err) {
cb(err || null, listing);
});
socket.on("data", function(data) { listing += data; });
socket.on("close", function(err) { cb(err, listing); });
socket.on("error", cb);

self.send("list " + (path || ""));
function cmdCallback(err, res) {
if (err) return cb(err);

if (res.code !== 125 && res.code !== 150) {
cb(new Error("Unexpected command " + res.text));
}
}

cmdCallback.expectsMark = {
marks: [125, 150],
ignore: 226
};

self.execute("list " + (path || ""), cmdCallback);
});
});
};
Expand Down Expand Up @@ -454,7 +457,7 @@ Ftp.prototype.getGetSocket = function(path, callback) {
this.getPasvSocket(function(err, socket) {
if (err) return cmdCallback(err);

self.pasvTimeout.bind(self, socket, cmdCallback);
self.pasvTimeout.call(self, socket, cmdCallback);
socket.pause();

function cmdCallback(err, res) {
Expand Down Expand Up @@ -547,7 +550,7 @@ Ftp.prototype.getPutSocket = function(path, callback, doneCallback) {
if (res.code === 125 || res.code === 150) {
socket.on('close', doneCallback);
socket.on('error', doneCallback);
self.pasvTimeout.bind(self, socket, doneCallback);
self.pasvTimeout.call(self, socket, doneCallback);
_callback(null, socket);
} else {
return _callback(new Error("Unexpected command " + res.text));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jsftp",
"id": "jsftp",
"version": "1.3.3",
"version": "1.3.4",
"description": "A sane FTP client implementation for NodeJS",
"keywords": [ "ftp", "protocol", "files", "server", "client", "async" ],
"author": "Sergi Mansilla <[email protected]> (http://sergimansilla.com)",
Expand Down
8 changes: 8 additions & 0 deletions test/jsftp_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ describe("jsftp test suite", function() {
});
});

it("test passive listing of nonexisting directory", function(next) {
ftp.list('does-not-exist/', function(err, res) {
assert.equal(typeof err, 'object');
assert.ok(err.code === 450 || err.code === 550);
next();
});
});

it("test ftp node stat", function(next) {
ftp.raw.pwd(function(err, res) {
var parent = new RegExp('.*"(.*)".*').exec(res.text)[1];
Expand Down

0 comments on commit a8fd90c

Please sign in to comment.