Skip to content
Browse files

try to emulate PWD, CDUP, SIZE, and MDTM when they are not available …

…on the server
  • Loading branch information...
1 parent 2b982d8 commit b5111e99d974007321dcc4c1e10d8d7754078a27 @mscdex committed Dec 18, 2012
Showing with 41 additions and 6 deletions.
  1. +41 −6 lib/ftp.js
View
47 lib/ftp.js
@@ -574,15 +574,24 @@ FTP.prototype.append = function(input, path, zcomp, cb) {
};
FTP.prototype.pwd = function(cb) { // PWD is optional
- this._send('PWD', function(err, text) {
- if (err)
+ var self = this;
+ this._send('PWD', function(err, text, code) {
+ if (code == 502)
+ return self.cwd('.', cb, true);
+ else if (err)
return cb(err);
cb(undefined, /^"(.+)"(?: |$)/.exec(text)[1]);
});
};
FTP.prototype.cdup = function(cb) { // CDUP is optional
- this._send('CDUP', cb);
+ var self = this;
+ this._send('CDUP', function(err, text, code) {
+ if (code === 502)
+ self.cwd('..', cb, true);
+ else
+ cb(err);
+ });
};
FTP.prototype.mkdir = function(path, cb) { // MKD is optional
@@ -603,16 +612,42 @@ FTP.prototype.system = function(cb) { // SYST is optional
// "Extended" (RFC 3659) commands
FTP.prototype.size = function(path, cb) {
- this._send('SIZE ' + path, function(err, text) {
- if (err)
+ var self = this;
+ this._send('SIZE ' + path, function(err, text, code) {
+ if (code === 502) {
+ // Note: this may cause a problem as list() is _appended_ to the queue
+ return self.list(path, function(err, list) {
+ if (err)
+ return cb(err);
+ if (list.length === 1)
+ cb(undefined, list[0].size);
+ else {
+ // path could have been a directory and we got a listing of its
+ // contents, but here we echo the behavior of the real SIZE and
+ // return 'File not found' for directories
+ cb(new Error('File not found'));
+ }
+ });
+ } else if (err)
return cb(err);
cb(undefined, parseInt(text, 10));
});
};
FTP.prototype.lastMod = function(path, cb) {
+ var self = this;
this._send('MDTM ' + path, function(err, text, code) {
- if (err)
+ if (code === 502) {
+ // Note: this may cause a problem as list() is _appended_ to the queue
+ return self.list(path, function(err, list) {
+ if (err)
+ return cb(err);
+ if (list.length === 1)
+ cb(undefined, list[0].date);
+ else
+ cb(new Error('File not found'));
+ });
+ } else if (err)
return cb(err);
var val = XRegExp.exec(text, reXTimeval), ret;
if (!val)

0 comments on commit b5111e9

Please sign in to comment.
Something went wrong with that request. Please try again.