ssh2 icon indicating copy to clipboard operation
ssh2 copied to clipboard

Not able to get hold of the good and bad scenario of sftp.rename()

Open muthursyamburi opened this issue 9 years ago • 2 comments

I am trying with the below code:

            sftp.rename(fromPath, toPath, function (err) {
                if(err)  {
                    console.log(`ERROR: Remote file ${fromPath} issue! Skipping it...`);
                    conn.end();
                    throw err;
                }
                console.log(`INFO: Successfully moved ${fromPath} to ${toPath}! [${success}]`);
            });

            sftp.on('error', function (err) {
                console.log(err.message);
                console.log(`ERROR: ON ERROR: Remote file ${fromPath} issue!!!!!!`);
            });

            conn.end();

When it runs, it actually does the work of renaming the file from fromPath to toPath correctly. When I try to rename a non-existing file, it does nothing (as expected). However I am not able to differentiate the good and bad scenarios as the sftp.rename() looks to be treating both in the same way.

In fact, in either case, error or no-error, I wanted a proper message to be printed. I thought the above logs will work, but apparently its not hitting neither the cb in the rename nor the error event.

Is there a bug in the above code? Or is there some issue with the rename()?

Oh..btw, here is what the package.json of ssh2 says:

  "_args": [
    [
      {
        "raw": "ssh2",
        "scope": null,
        "escapedName": "ssh2",
        "name": "ssh2",
        "rawSpec": "",
        "spec": "latest",
        "type": "tag"
      },
      "C:\\spool\\git\\cte_fuel_apps\\FuelPricingAuto\\node"
    ]
  ],
  "_from": "ssh2@latest",
  "_id": "[email protected]",
  "_inCache": true,
  "_location": "/ssh2",
  "_nodeVersion": "6.9.1",
  "_npmOperationalInternal": {
    "host": "s3://npm-registry-packages",
    "tmp": "tmp/ssh2-0.5.5.tgz_1496022263266_0.4106363251339644"
  },
  "_npmUser": {
    "name": "mscdex",
    "email": "[email protected]"
  },
  "_npmVersion": "3.10.8",
  "_phantomChildren": {},
  "_requested": {
    "raw": "ssh2",
    "scope": null,
    "escapedName": "ssh2",
    "name": "ssh2",
    "rawSpec": "",
    "spec": "latest",
    "type": "tag"
  },
  "_requiredBy": [
    "#USER",
    "/"
  ],
  "_resolved": "https://npme.walmart.com/ssh2/-/ssh2-0.5.5.tgz",
  "_shasum": "c7781ecd2ece7304a253cf620fab5a5c22bb2235",
  "_shrinkwrap": null,
  "_spec": "ssh2",
  "_where": "C:\\spool\\git\\cte_fuel_apps\\FuelPricingAuto\\node",
  "author": {
    "name": "Brian White",
    "email": "[email protected]"
  },
  "bugs": {
    "url": "https://github.com/mscdex/ssh2/issues"
  },
  "dependencies": {
    "ssh2-streams": "~0.1.18"
  },
  "description": "SSH2 client and server modules written in pure JavaScript for node.js",
  "devDependencies": {
    "semver": "^5.1.0"
  },
  "directories": {},
  "dist": {
    "shasum": "c7781ecd2ece7304a253cf620fab5a5c22bb2235",
    "tarball": "http://npme.walmart.com/ssh2/-/ssh2-0.5.5.tgz"
  },
  "engines": {
    "node": ">=0.10.0"
  },
  "homepage": "https://github.com/mscdex/ssh2#readme",
  "keywords": [
    "ssh",
    "ssh2",
    "sftp",
    "secure",
    "shell",
    "exec",
    "remote",
    "client"
  ],
  "licenses": [
    {
      "type": "MIT",
      "url": "http://github.com/mscdex/ssh2/raw/master/LICENSE"
    }
  ],
  "main": "./lib/client",
  "maintainers": [
    {
      "name": "mscdex",
      "email": "[email protected]"
    }
  ],
  "name": "ssh2",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+ssh://[email protected]/mscdex/ssh2.git"
  },
  "scripts": {
    "test": "node test/test.js"
  },
  "version": "0.5.5"
}

muthursyamburi avatar Nov 23 '17 11:11 muthursyamburi

The problem is probably that you're ending the connection immediately, although that should still call any outstanding callbacks on connection close. Try moving your conn.end() to inside your sftp.rename() callback.

mscdex avatar Nov 23 '17 17:11 mscdex

Wow!!! perfect... That worked! Thanks a ton @mscdex for your help. Really appreciate the quick TAT!

muthursyamburi avatar Nov 24 '17 00:11 muthursyamburi