shell: rework code to use shell.spawn

spawn gives out streams and we have more control over the stdout/stderr
buffers. otherwise, we have to provide a max buffer capture size to exec
This commit is contained in:
Girish Ramakrishnan
2024-10-15 10:10:15 +02:00
parent 7b648cddfd
commit 6c3ca9c364
18 changed files with 101 additions and 88 deletions

View File

@@ -163,13 +163,13 @@ async function copy(apiConfig, oldFilePath, newFilePath, progressCallback) {
const sshOptions = [ '-o', '"StrictHostKeyChecking no"', '-i', identityFilePath, '-p', apiConfig.mountOptions.port, `${apiConfig.mountOptions.user}@${apiConfig.mountOptions.host}` ];
const sshArgs = sshOptions.concat([ 'cp', cpOptions, oldFilePath.replace('/mnt/cloudronbackup/', ''), newFilePath.replace('/mnt/cloudronbackup/', '') ]);
const [remoteCopyError] = await safe(shell.execArgs('ssh', sshArgs, { shell: true }));
const [remoteCopyError] = await safe(shell.spawn('ssh', sshArgs, { shell: true }));
if (!remoteCopyError) return;
if (remoteCopyError.code === 255) throw new BoxError(BoxError.EXTERNAL_ERROR, `SSH connection error: ${remoteCopyError.message}`); // do not attempt fallback copy for ssh errors
debug('SSH remote copy failed, trying ssfs copy'); // this can happen for sshfs mounted windows server
}
const [copyError] = await safe(shell.execArgs('cp', [ cpOptions, oldFilePath, newFilePath ], {}));
const [copyError] = await safe(shell.spawn('cp', [ cpOptions, oldFilePath, newFilePath ], {}));
if (copyError) throw new BoxError(BoxError.EXTERNAL_ERROR, copyError.message);
}
@@ -194,7 +194,7 @@ async function removeDir(apiConfig, pathPrefix, progressCallback) {
progressCallback({ message: `Removing directory ${pathPrefix}` });
const [error] = await safe(shell.execArgs('rm', [ '-rf', pathPrefix ], {}));
const [error] = await safe(shell.spawn('rm', [ '-rf', pathPrefix ], {}));
if (error) throw new BoxError(BoxError.EXTERNAL_ERROR, error.message);
}
@@ -232,7 +232,7 @@ async function testConfig(apiConfig) {
const error = validateBackupTarget(apiConfig.mountPoint);
if (error) throw error;
const [mountError] = await safe(shell.exec(`mountpoint -q -- ${apiConfig.mountPoint}`, { timeout: 5000 }));
const [mountError] = await safe(shell.spawn('mountpoint', ['-q', '--', apiConfig.mountPoint], { timeout: 5000 }));
if (mountError) throw new BoxError(BoxError.BAD_FIELD, `${apiConfig.mountPoint} is not mounted: ${mountError.message}`);
}