Retry every hdiutil operation

On newer macOS version hdiutil fails even more often
This commit is contained in:
Dmitry Vedenko 2024-03-26 12:57:20 +03:00
parent 71e220d61c
commit 5dee4e7a97
No known key found for this signature in database
GPG Key ID: F4C37A6204F983A2
2 changed files with 62 additions and 25 deletions

File diff suppressed because one or more lines are too long

View File

@ -60,6 +60,11 @@ async function detachImage(mountedPath) {
for (let attempt = 1; attempt < maxAttempts; ++attempt) { for (let attempt = 1; attempt < maxAttempts; ++attempt) {
try { try {
if (!fs.existsSync(mountedPath)) {
helpers.log(`Image ${mountedPath} already detached`);
return;
}
return await helpers.execWithLog('hdiutil', [ return await helpers.execWithLog('hdiutil', [
'detach', mountedPath 'detach', mountedPath
]); ]);
@ -68,35 +73,52 @@ async function detachImage(mountedPath) {
await helpers.sleep(2000 * attempt); await helpers.sleep(2000 * attempt);
} }
} }
try { try {
if (!fs.existsSync(mountedPath)) {
helpers.log(`Image ${mountedPath} already detached`);
return;
}
return await helpers.execWithLog('hdiutil', [ return await helpers.execWithLog('hdiutil', [
'detach', mountedPath, '-force' 'detach', mountedPath, '-force'
]); ]);
} catch (err) { } catch (err) {
helpers.error(err.message); helpers.error(err.message);
throw new Error(`Failed to detach image ${mountedPath}`);
} }
} }
async function attachImage(imagePath, finalizers) { async function attachImage(imagePath, finalizers) {
const output = await helpers.getExecOutput('hdiutil', [ const maxAttempts = 10;
'attach', imagePath,
'-nobrowse',
'-noverify',
'-noautoopen'
]);
const match = /Apple_HFS\s+(.*)\s*$/.exec(output.stdout); for (let attempt = 1; attempt < maxAttempts; ++attempt) {
const mountedPath = match[1]; try {
const output = await helpers.getExecOutput('hdiutil', [
'attach', imagePath,
'-nobrowse',
'-noverify',
'-noautoopen'
]);
finalizers.push(async () => { const match = /Apple_HFS\s+(.*)\s*$/.exec(output.stdout);
if (fs.existsSync(mountedPath)) { const mountedPath = match[1];
helpers.log(`Detaching image ${imagePath} mounted at ${mountedPath}`);
await detachImage(mountedPath); finalizers.push(async () => {
if (fs.existsSync(mountedPath)) {
helpers.log(`Detaching image ${imagePath} mounted at ${mountedPath}`);
await detachImage(mountedPath);
}
});
return mountedPath;
} catch (err) {
helpers.error(err.message);
await helpers.sleep(2000 * attempt);
} }
}); }
return mountedPath; throw new Error(`Failed to attach image ${imagePath}`);
} }
async function copyFiles(targetDir, appDir, files) { async function copyFiles(targetDir, appDir, files) {
@ -151,17 +173,32 @@ async function createDSStore(mountPath, name) {
} }
async function convertDMG(tempDmgPath, dmgPath) { async function convertDMG(tempDmgPath, dmgPath) {
if (fs.existsSync(dmgPath)){ const maxAttempts = 10;
fs.rmSync(dmgPath);
for (let attempt = 1; attempt < maxAttempts; ++attempt) {
try {
if (fs.existsSync(dmgPath)){
fs.rmSync(dmgPath);
}
await helpers.execWithLog('hdiutil', [
'convert', tempDmgPath,
'-format', 'UDZO',
'-imagekey',
'zlib-level=9',
'-o', dmgPath
]);
return;
} catch (err) {
helpers.error(err.message);
await helpers.sleep(2000 * attempt);
}
} }
await helpers.execWithLog('hdiutil', [ if (!fs.existsSync(dmgPath)) {
'convert', tempDmgPath, throw new Error(`Failed to convert image ${tempDmgPath} to ${dmgPath}`);
'-format', 'UDZO', }
'-imagekey',
'zlib-level=9',
'-o', dmgPath
]);
} }
async function packageDMG(dmgPath, appPath) { async function packageDMG(dmgPath, appPath) {