From 9941932a2a5e88048c8386ee2bfa451578994b99 Mon Sep 17 00:00:00 2001 From: Valentin Ivanov Date: Mon, 4 Mar 2019 18:35:34 -0500 Subject: [PATCH] SFTPStream: add fileSize option for fast* methods Supplying a valid file size will skip fstat/stat calls. --- lib/sftp.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/sftp.js b/lib/sftp.js index 9b88c4dc..65ead41c 100644 --- a/lib/sftp.js +++ b/lib/sftp.js @@ -995,6 +995,7 @@ function fastXfer(src, dst, srcPath, dstPath, opts, cb) { //var preserve = false; var onstep; var mode; + var fileSize; if (typeof opts === 'function') { cb = opts; @@ -1007,6 +1008,10 @@ function fastXfer(src, dst, srcPath, dstPath, opts, cb) { && opts.chunkSize > 0 && !isNaN(opts.chunkSize)) chunkSize = opts.chunkSize; + if (typeof opts.fileSize === 'number' + && opts.fileSize > 0 + && !isNaN(opts.fileSize)) + fileSize = opts.fileSize; if (typeof opts.step === 'function') onstep = opts.step; //preserve = (opts.preserve ? true : false); @@ -1056,7 +1061,12 @@ function fastXfer(src, dst, srcPath, dstPath, opts, cb) { srcHandle = sourceHandle; - src.fstat(srcHandle, function tryStat(err, attrs) { + if (fileSize !== undefined) + src.fstat(srcHandle, tryStat); + else + tryStat(null, { size: fileSize }); + + function tryStat(err, attrs) { if (err) { if (src !== fs) { // Try stat() for sftp servers that may not support fstat() for @@ -1192,7 +1202,7 @@ function fastXfer(src, dst, srcPath, dstPath, opts, cb) { } } }); - }); + } }); } SFTPStream.prototype.fastGet = function(remotePath, localPath, opts, cb) {