spmjs / node-scp2

[MAINTAINER WANTED] A pure javascript scp program based on ssh2.
384 stars 96 forks source link

Wrong remote path when uploading multiple files via glob pattern (issue with path.relative) #117

Open mankey-ru opened 6 years ago

mankey-ru commented 6 years ago

Buggy code located in scp.js

var _upload = function(files, callback) {
    var rootdir = files[0];
    async.eachSeries(files, function(fpath, done) {
      fs.stat(fpath, function(err, stats) {
        // ...
        if (stats.isFile()) {
          var fname = path.relative(rootdir, fpath);

Problem occurs when files argument is [ 'file_A.js', 'fileB.js' ] (glob pattern 'file' or './file_'). First file (A) transfers to remote host successfully, but second one (B) transfers to "one level up directory". It happens because path.relative handles first argument as directory, and path.relative("file_A.js", "file_B.js") returns "..\file_B.js"

Ready code to reproduce:

var client = require('scp2');
client.scp('./file_*', {
    host: '<host>',
    username: '<username>',
    password: '<password>',
    path: '/opt/IBM/HTTPServer/htdocs/myhost/js'
}, function(err) {
    console.log(err)
})
sla89 commented 6 years ago

Same issue here

poolye commented 2 years ago

Same issue here