Permalink
Browse files

connection: add listSafe() command

  • Loading branch information...
1 parent 4e0b076 commit 78a2791a2e276390de05f1e77e32643cedff25ac @mscdex committed Aug 1, 2013
Showing with 26 additions and 0 deletions.
  1. +2 −0 README.md
  2. +24 −0 lib/connection.js
View
@@ -176,6 +176,8 @@ Methods
* **system**(< _function_ >callback) - _(void)_ - Retrieves the server's operating system. `callback` has 2 parameters: < _Error_ >err, < _string_ >OS.
+* **listSafe**([< _string_ >path, ][< _boolean_ >useCompression, ]< _function_ >callback) - _(void)_ - Similar to list(), except the directory is temporarily changed to `path` to retrieve the directory listing. This is useful for servers that do not handle characters like spaces and quotes in directory names well for the LIST command. This function is "optional" because it relies on pwd() being available.
+
### Extended commands (RFC 3659)
* **size**(< _string_ >path, < _function_ >callback) - _(void)_ - Retrieves the size of `path`. `callback` has 2 parameters: < _Error_ >err, < _integer_ >numBytes.
View
@@ -347,6 +347,30 @@ FTP.prototype.rename = function(from, to, cb) {
});
};
+FTP.prototype.listSafe = function(path, zcomp, cb) {
+ if (typeof path === 'string') {
+ var self = this;
+ // store current path
+ this.pwd(function(err, origpath) {
+ if (err) return cb(err);
+ // change to destination path
+ self.cwd(path, function(err) {
+ if (err) return cb(err);
+ // get dir listing
+ self.list(zcomp || false, function(err, list) {
+ // change back to original path
+ if (err) return self.cwd(origpath, cb);
+ self.cwd(origpath, function(err) {
+ if (err) return cb(err);
+ cb(err, list);
+ });
+ });
+ });
+ });
+ } else
+ this.list(path, zcomp, cb);
+};
+
FTP.prototype.list = function(path, zcomp, cb) {
var self = this, cmd;

0 comments on commit 78a2791

Please sign in to comment.