Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DmgExtraction: Fix mount and copy race condition #583

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ import java.util.concurrent.TimeUnit
abstract class ExtractElectrumAppFromDmgFile : DefaultTask() {

companion object {
private const val MOUNT_DIR = "/Volumes/Electrum"
private const val VOLUMES_DIR = "/Volumes"
private const val MOUNT_DIR = "$VOLUMES_DIR/Electrum"

private const val ELECTRUM_APP = "Electrum.app"
private const val MOUNTED_ELECTRUM_APP_PATH = "$MOUNT_DIR/$ELECTRUM_APP"

private const val CMD_TIMEOUT: Long = 25
private const val MOUNT_TIMEOUT_IN_MILLISECONDS = 30 * 1000
}

@get:InputFile
Expand All @@ -47,10 +50,24 @@ abstract class ExtractElectrumAppFromDmgFile : DefaultTask() {
val dmgImageMounter = DmgImageMounter(dmgFile.get().asFile, File(MOUNT_DIR))
dmgImageMounter.use {
dmgImageMounter.mount()
waitUntilDmgImageMounted()
copyElectrumAppToOutputDirectory()
}
}

private fun waitUntilDmgImageMounted() {
val startTime: Long = System.currentTimeMillis()
val mountDir = File(MOUNT_DIR)
while (!mountDir.exists()) {
Thread.sleep(200)

val currentTime: Long = System.currentTimeMillis()
if (currentTime - startTime >= MOUNT_TIMEOUT_IN_MILLISECONDS) {
throw IllegalStateException("$MOUNT_DIR is still missing after 30 seconds of mounting the DMG image.")
}
}
}

private fun deleteElectrumAppFile() {
val electrumAppFilePath = electrumAppDestinationFile.get().asFile.toPath()
Files.walk(electrumAppFilePath)
Expand Down