From 99bf75010b696e62b2c7f3097192654120a75b6b Mon Sep 17 00:00:00 2001 From: Pepe Barbe Date: Wed, 10 Jan 2024 12:26:53 -0600 Subject: [PATCH] Handle 'EXDEV: cross-device link not permitted' --- lib/main.js | 33 +++++++++++++++++++++++++-------- src/main.ts | 36 +++++++++++++++++++++++++++--------- 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/lib/main.js b/lib/main.js index d8325d88..24f67425 100644 --- a/lib/main.js +++ b/lib/main.js @@ -238,21 +238,38 @@ function run() { fs.mkdirSync(dest, { 'recursive': true }); const outputPath = path.join(dest, renameTo !== "" ? renameTo : path.basename(binPath)); core.info(`Created output directory ${dest}`); + var moveFailed = false; try { fs.renameSync(binPath, outputPath); - core.info(`Moved release asset ${asset.name} to ${outputPath}`); - if (chmodTo !== "") { + } + catch (renameErr) { + if (renameErr instanceof Error && 'code' in renameErr && renameErr.code === 'EXDEV') { + core.debug(`Falling back to copy and remove, due to: ${renameErr}`); try { - fs.chmodSync(outputPath, chmodTo); - core.info(`chmod'd ${outputPath} to ${chmodTo}`); + fs.copyFileSync(binPath, outputPath); + fs.rmSync(binPath); } - catch (chmodErr) { - core.setFailed(`Failed to chmod ${outputPath} to ${chmodTo}: ${chmodErr}`); + catch (copyRemoveErr) { + moveFailed = true; + core.setFailed(`Failed to copy and remove downloaded release asset ${asset.name} from ${binPath} to ${outputPath}: ${copyRemoveErr}`); } } + else { + moveFailed = true; + core.setFailed(`Failed to move downloaded release asset ${asset.name} from ${binPath} to ${outputPath}: ${renameErr}`); + } } - catch (renameErr) { - core.setFailed(`Failed to move downloaded release asset ${asset.name} from ${binPath} to ${outputPath}: ${renameErr}`); + if (!moveFailed) { + core.info(`Moved release asset ${asset.name} to ${outputPath}`); + } + if ((chmodTo !== "") && !moveFailed) { + try { + fs.chmodSync(outputPath, chmodTo); + core.info(`chmod'd ${outputPath} to ${chmodTo}`); + } + catch (chmodErr) { + core.setFailed(`Failed to chmod ${outputPath} to ${chmodTo}: ${chmodErr}`); + } } } catch (err) { diff --git a/src/main.ts b/src/main.ts index f49f689d..597b3b6c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -244,20 +244,38 @@ async function run() { const outputPath = path.join(dest, renameTo !== "" ? renameTo : path.basename(binPath)); core.info(`Created output directory ${dest}`); + + var moveFailed = false; + try { fs.renameSync(binPath, outputPath); - core.info(`Moved release asset ${asset.name} to ${outputPath}`); - - if (chmodTo !== "") { + } catch (renameErr) { + if (renameErr instanceof Error && 'code' in renameErr && renameErr.code === 'EXDEV') { + core.debug(`Falling back to copy and remove, due to: ${renameErr}`); try { - fs.chmodSync(outputPath, chmodTo); - core.info(`chmod'd ${outputPath} to ${chmodTo}`) - } catch (chmodErr) { - core.setFailed(`Failed to chmod ${outputPath} to ${chmodTo}: ${chmodErr}`); + fs.copyFileSync(binPath, outputPath); + fs.rmSync(binPath); + } catch (copyRemoveErr) { + moveFailed = true; + core.setFailed(`Failed to copy and remove downloaded release asset ${asset.name} from ${binPath} to ${outputPath}: ${copyRemoveErr}`); } + } else { + moveFailed = true; + core.setFailed(`Failed to move downloaded release asset ${asset.name} from ${binPath} to ${outputPath}: ${renameErr}`); + } + } + + if (!moveFailed) { + core.info(`Moved release asset ${asset.name} to ${outputPath}`); + } + + if ((chmodTo !== "") && !moveFailed) { + try { + fs.chmodSync(outputPath, chmodTo); + core.info(`chmod'd ${outputPath} to ${chmodTo}`) + } catch (chmodErr) { + core.setFailed(`Failed to chmod ${outputPath} to ${chmodTo}: ${chmodErr}`); } - } catch (renameErr) { - core.setFailed(`Failed to move downloaded release asset ${asset.name} from ${binPath} to ${outputPath}: ${renameErr}`); } } catch (err) { core.setFailed(`Failed to create required output directory ${dest}`);