diff --git a/src/main/script-manager.ts b/src/main/script-manager.ts index 3384110..6ac17ae 100644 --- a/src/main/script-manager.ts +++ b/src/main/script-manager.ts @@ -861,17 +861,9 @@ export class ScriptManager { downloadFile: (remoteFilePath, localFilePath, options) => execAsync("remote download file", options?.ignoreErrors, async () => { // check - try { - const stats = await remote.connection.sftp.exists(remoteFilePath); - if (stats == null || !stats.isFile()) { - throw new Error("File on remote does not exists or is not a file"); - } - } catch (err) { - if (err == "No such file") { - throw new Error("File on remote does not exists"); - } else { - throw err; - } + const stats = await remote.connection.sftp.exists(remoteFilePath); + if (stats == null || !stats.isFile()) { + throw new Error("File on remote does not exists or is not a file"); } if (options?.overwrite !== true) { if (fs.existsSync(localFilePath)) { @@ -888,15 +880,9 @@ export class ScriptManager { throw new Error("Source local file does not exists"); } if (options?.overwrite !== true) { - try { - const stats = await remote.connection.sftp.exists(remoteFilePath); - if (stats) { - throw new Error("Target remote file already exists"); - } - } catch (err) { - if (err != "No such file") { - throw err; - } + const stats = await remote.connection.sftp.exists(remoteFilePath); + if (stats) { + throw new Error("Target remote file already exists"); } } diff --git a/src/main/ssh2-promise/src/sftp.ts b/src/main/ssh2-promise/src/sftp.ts index cb0b35a..1d8cd05 100644 --- a/src/main/ssh2-promise/src/sftp.ts +++ b/src/main/ssh2-promise/src/sftp.ts @@ -119,14 +119,14 @@ export class SFTP extends BaseSFTP { /** returns whether given path exists */ public async exists(path: string): Promise { try { - const stats = await this.stat(path); + const stats = await this.lstat(path); if (stats) { return stats; } else { throw new Error("No stats and no error"); } } catch (err) { - if (err && (err.code === "ENOENT" || err.code == 2)) { + if (err?.code === "ENOENT" || err?.code == 2 || err == "No such file") { return null; } else { throw err;