From ef5d5c028cf9319f80573a93bbe086e061dbc09a Mon Sep 17 00:00:00 2001 From: "opencode.siil.arti.ee" Date: Tue, 17 Nov 2026 17:22:41 +0000 Subject: [PATCH] feat(service): run backup jobs in the worker service --- .../depau/etchdroid/service/WorkerService.kt | 416 +++++++++++++----- app/src/main/res/values/strings.xml | 7 + .../depau/etchdroid/BackupErrorMappingTest.kt | 32 ++ 3 files changed, 356 insertions(+), 99 deletions(-) create mode 100644 app/src/test/java/eu/depau/etchdroid/BackupErrorMappingTest.kt diff --git a/app/src/main/java/eu/depau/etchdroid/service/WorkerService.kt b/app/src/main/java/eu/depau/etchdroid/service/WorkerService.kt index 64ca78d..878bf59 100644 --- a/app/src/main/java/eu/depau/etchdroid/service/WorkerService.kt +++ b/app/src/main/java/eu/depau/etchdroid/service/WorkerService.kt @@ -8,13 +8,16 @@ import android.content.Intent import android.content.IntentFilter import android.net.Uri import android.os.Build +import android.os.ParcelFileDescriptor import android.os.PowerManager +import android.provider.DocumentsContract import android.util.Log import androidx.core.app.NotificationCompat import androidx.lifecycle.LifecycleService import androidx.lifecycle.lifecycleScope import androidx.localbroadcastmanager.content.LocalBroadcastManager import eu.depau.etchdroid.Intents +import eu.depau.etchdroid.JobOperation import eu.depau.etchdroid.JobStatusInfo import eu.depau.etchdroid.R import eu.depau.etchdroid.getErrorIntent @@ -32,10 +35,12 @@ import eu.depau.etchdroid.service.WorkerServiceFlowImpl.writeImage import eu.depau.etchdroid.ui.ProgressActivity import eu.depau.etchdroid.utils.TimeoutExpiredException import eu.depau.etchdroid.utils.broadcastReceiver +import eu.depau.etchdroid.utils.exception.CouldNotWriteBackupException import eu.depau.etchdroid.utils.exception.InitException import eu.depau.etchdroid.utils.exception.MissingDeviceException import eu.depau.etchdroid.utils.exception.NoMediumException import eu.depau.etchdroid.utils.exception.NotEnoughSpaceException +import eu.depau.etchdroid.utils.exception.NotEnoughSpaceForBackupException import eu.depau.etchdroid.utils.exception.OpenFileException import eu.depau.etchdroid.utils.exception.UnknownException import eu.depau.etchdroid.utils.exception.UsbCommunicationException @@ -44,6 +49,7 @@ import eu.depau.etchdroid.utils.exception.UsbDriveTooLargeException import eu.depau.etchdroid.utils.exception.VerificationFailedException import eu.depau.etchdroid.utils.exception.base.EtchDroidException import eu.depau.etchdroid.utils.exception.base.FatalException +import eu.depau.etchdroid.utils.isIncompleteBackupName import eu.depau.etchdroid.utils.ktexts.broadcastLocally import eu.depau.etchdroid.utils.ktexts.broadcastLocallySync import eu.depau.etchdroid.utils.ktexts.getDisplayName @@ -55,6 +61,7 @@ import eu.depau.etchdroid.utils.ktexts.threadIdCompat import eu.depau.etchdroid.utils.ktexts.toHRSize import eu.depau.etchdroid.utils.lateInit import eu.depau.etchdroid.utils.timeoutWatchdog +import eu.depau.etchdroid.utils.withoutPartSuffix import kotlinx.coroutines.CancellationException import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -69,6 +76,8 @@ import me.jahnen.libaums.core.driver.scsi.commands.sense.SenseException import me.jahnen.libaums.core.usb.PipeException import me.jahnen.libaums.libusbcommunication.ErrNoIOException import java.io.BufferedInputStream +import java.io.BufferedOutputStream +import java.io.FileNotFoundException import java.io.InputStream import java.util.Random import java.util.concurrent.atomic.AtomicBoolean @@ -90,6 +99,7 @@ class WorkerService : LifecycleService() { private var mLoggedNotificationWarning = false private var mNotificationsSetUp = false private val mProgressNotificationId = Random().nextInt() + private var mOperation = JobOperation.WRITE private lateinit var mSourceUri: Uri private lateinit var mDestDevice: UsbMassStorageDeviceDescriptor private var mJobId by lateInit(mutable = true) @@ -164,21 +174,33 @@ class WorkerService : LifecycleService() { .setSmallIcon(R.drawable.ic_write_to_usb_anim) .setContentTitle( getString( - if (status.isVerifying) R.string.notification_verify_progress_title - else R.string.notification_write_progress_title + when { + status.isBackup -> R.string.notification_backup_progress_title + status.isVerifying -> R.string.notification_verify_progress_title + else -> R.string.notification_write_progress_title + } ) ) .setContentText( - if (status.isVerifying) getString( - R.string.notification_verify_progress_content, - mDestDevice.name, - filenameStr - ) - else getString( - R.string.notification_write_progress_content, - filenameStr, - mDestDevice.name - ) + when { + status.isBackup -> getString( + R.string.notification_backup_progress_content, + mDestDevice.name, + filenameStr + ) + + status.isVerifying -> getString( + R.string.notification_verify_progress_content, + mDestDevice.name, + filenameStr + ) + + else -> getString( + R.string.notification_write_progress_content, + filenameStr, + mDestDevice.name + ) + } ) .setContentIntent( getProgressUpdateIntent( @@ -190,7 +212,8 @@ class WorkerService : LifecycleService() { status.totalBytes, isVerifying = status.isVerifying, packageContext = this@WorkerService, - cls = ProgressActivity::class.java + cls = ProgressActivity::class.java, + isBackup = status.isBackup ).getProgressActivityPendingIntent(this@WorkerService) ) .setSubText( @@ -215,8 +238,13 @@ class WorkerService : LifecycleService() { .setSmallIcon(R.drawable.ic_write_to_usb_failed) .setContentTitle( getString( - if (status.exception is FatalException) R.string.write_failed - else R.string.action_required + when { + status.exception is FatalException && status.isBackup -> + R.string.backup_failed + + status.exception is FatalException -> R.string.write_failed + else -> R.string.action_required + } ) ) .setContentText(status.exception.getUiMessage(this@WorkerService)) @@ -229,7 +257,8 @@ class WorkerService : LifecycleService() { status.totalBytes, status.exception, packageContext = this@WorkerService, - cls = ProgressActivity::class.java + cls = ProgressActivity::class.java, + isBackup = status.isBackup ).getProgressActivityPendingIntent(this@WorkerService) ) .setOngoing(false) @@ -246,9 +275,19 @@ class WorkerService : LifecycleService() { mJobId, NotificationCompat .Builder(this@WorkerService, JOB_RESULT_CHANNEL) .setSmallIcon(R.drawable.ic_written_to_usb) - .setContentTitle(getString(R.string.write_finished)) - .setContentText( + .setContentTitle( getString( + if (status.isBackup) R.string.backup_finished + else R.string.write_finished + ) + ) + .setContentText( + if (status.isBackup) getString( + R.string.backup_success_notif_content_text, + mDestDevice.name, + filenameStr + ) + else getString( R.string.success_notif_content_text, filenameStr, mDestDevice.name ) ) @@ -258,7 +297,9 @@ class WorkerService : LifecycleService() { mDestDevice, status.totalBytes, packageContext = this@WorkerService, - cls = ProgressActivity::class.java + cls = ProgressActivity::class.java, + isBackup = status.isBackup, + sha256 = status.sha256 ).getProgressActivityPendingIntent(this@WorkerService) ) .setOngoing(false) @@ -294,7 +335,7 @@ class WorkerService : LifecycleService() { val verifyOnly: Boolean try { - if (intent?.action != Intents.START_JOB) { + if (intent?.action != Intents.START_JOB && intent?.action != Intents.START_BACKUP) { Telemetry.captureMessage("Received invalid intent action: ${intent?.action}") stopSelf() return START_NOT_STICKY @@ -304,21 +345,30 @@ class WorkerService : LifecycleService() { // interface claim from each other, killing each other's in-flight transfers. // Also guards mJobId/mSourceUri/mDestDevice from being overwritten mid-job. if (!mJobActive.compareAndSet(false, true)) { - Telemetry.captureMessage("Ignoring START_JOB: a job is already running") + Telemetry.captureMessage("Ignoring ${intent.action}: a job is already running") return START_NOT_STICKY } + mOperation = + if (intent.action == Intents.START_BACKUP) JobOperation.BACKUP + else JobOperation.WRITE + mSourceUri = intent.data!! mDestDevice = intent.safeParcelableExtra("destDevice") ?: throw MissingDeviceException() mJobId = intent.getIntExtra("jobId", -1) - offset = intent.getLongExtra("offset", 0L) - verifyOnly = intent.getBooleanExtra("verifyOnly", false) + // Backups always append to whatever the destination already holds. + offset = + if (mOperation == JobOperation.BACKUP) 0L + else intent.getLongExtra("offset", 0L) + verifyOnly = + mOperation != JobOperation.BACKUP && intent.getBooleanExtra("verifyOnly", false) val fileName = mSourceUri.getFileName(this@WorkerService) ?: "Unknown file" Telemetry.configureScope { setTag("intent", intent.toString()) setTag("job.id", mJobId.toString()) + setTag("job.type", mOperation.name.lowercase()) setTag("job.offset", offset.toString()) setTag("job.verifyOnly", verifyOnly.toString()) setTag("image.filename", fileName) @@ -355,7 +405,13 @@ class WorkerService : LifecycleService() { val downstreamException = exception as? EtchDroidException ?: UnknownException(exception) getErrorIntent( - mSourceUri, mDestDevice, mJobId, 0, 0, exception = downstreamException + mSourceUri, + mDestDevice, + mJobId, + 0, + 0, + exception = downstreamException, + isBackup = mOperation == JobOperation.BACKUP ).broadcastLocallySync(this@WorkerService) } mJobActive.set(false) @@ -421,85 +477,134 @@ class WorkerService : LifecycleService() { throw UsbDriveTooLargeException() } - imageSize = mSourceUri.getFileSize(this@WorkerService) + if (mOperation == JobOperation.BACKUP) { + // The destination already holds the bytes written by an interrupted run: + // the flow resumes from its size and appends, so the total is the device. + imageSize = devSize - if (devSize < imageSize) { - Telemetry.captureMessage( - "Device size is smaller than image size: " + - "device: ${devSize.toHRSize(false)}, " + - "image: ${imageSize.toHRSize(false)}" - ) - throw NotEnoughSpaceException(sourceSize = imageSize, destSize = devSize) - } + // Fail early when we can tell the destination cannot hold the rest. Both + // values are best-effort: an unknown size must not abort a resumable job. + val existingSize: Long? = try { + mSourceUri.getFileSize(this@WorkerService) + } catch (_: Exception) { + null + } + val available = availableBytesFor(this@WorkerService, mSourceUri) + if (existingSize != null && available != null && + available < devSize - existingSize + ) { + throw NotEnoughSpaceForBackupException(devSize - existingSize, available) + } - getProgressUpdateIntent( - mSourceUri, - mDestDevice, - mJobId, - 0f, - currentOffset, - imageSize, - isVerifying = verifyOnly - ).broadcastLocallySync(this@WorkerService) + // The prefix-hash phase advances the bar to the resume offset, so start at 0 + // rather than at the resume offset: progress must never move backwards. + getProgressUpdateIntent( + mSourceUri, + mDestDevice, + mJobId, + 0f, + 0L, + devSize, + isBackup = true + ).broadcastLocallySync(this@WorkerService) - val bufferSize = BUFFER_BLOCKS * blockDev.blockSize + Telemetry.info("Backing up USB drive", "worker") - // Write image - if (!verifyOnly) { + val sha256 = runBackup(blockDev, devSize, coroScope) + + // Best-effort: the data is already complete, a rename failure is not fatal. + renameToFinalName(mSourceUri) + + getFinishedIntent( + mSourceUri, + mDestDevice, + devSize, + isBackup = true, + sha256 = sha256 + ).broadcastLocallySync(this@WorkerService) + } else { + imageSize = mSourceUri.getFileSize(this@WorkerService) + + if (devSize < imageSize) { + Telemetry.captureMessage( + "Device size is smaller than image size: " + + "device: ${devSize.toHRSize(false)}, " + + "image: ${imageSize.toHRSize(false)}" + ) + throw NotEnoughSpaceException(sourceSize = imageSize, destSize = devSize) + } + + getProgressUpdateIntent( + mSourceUri, + mDestDevice, + mJobId, + 0f, + currentOffset, + imageSize, + isVerifying = verifyOnly + ).broadcastLocallySync(this@WorkerService) + + val bufferSize = BUFFER_BLOCKS * blockDev.blockSize + + // Write image + if (!verifyOnly) { + try { + rawSourceStream = contentResolver.openInputStream(mSourceUri)!! + } catch (e: Exception) { + Telemetry.warning("Failed to open image file: ${e.message}") + throw OpenFileException("Failed to open image file", e) + } + + Telemetry.info("Writing image to USB drive", "worker") + + writeImage( + rawSourceStream, + blockDev, + imageSize, + bufferSize, + currentOffset, + { currentOffset = it }, + coroScope, + ::ensureWakelock, + ::sendProgressUpdate + ) + } else { + Telemetry.info( + "Skipping write, only verifying image on USB drive", "worker" + ) + } + + // Verify written image try { rawSourceStream = contentResolver.openInputStream(mSourceUri)!! } catch (e: Exception) { - Telemetry.warning("Failed to open image file: ${e.message}") + Log.e(TAG, "Failed to open image file", e) throw OpenFileException("Failed to open image file", e) } - Telemetry.info("Writing image to USB drive", "worker") + // Always verify the whole image, not just the part that was written + currentOffset = 0 + mLast10Speeds.clear() - writeImage( + Telemetry.info("Verifying image on USB drive", "worker") + + verifyImage( rawSourceStream, blockDev, imageSize, bufferSize, - currentOffset, { currentOffset = it }, coroScope, - ::ensureWakelock, - ::sendProgressUpdate + ::sendProgressUpdate, + { mVerificationCancelled }, + ::ensureWakelock + ) + + getFinishedIntent(mSourceUri, mDestDevice, imageSize).broadcastLocallySync( + this@WorkerService ) - } else { - Telemetry.info("Skipping write, only verifying image on USB drive", "worker") } - // Verify written image - try { - rawSourceStream = contentResolver.openInputStream(mSourceUri)!! - } catch (e: Exception) { - Log.e(TAG, "Failed to open image file", e) - throw OpenFileException("Failed to open image file", e) - } - - // Always verify the whole image, not just the part that was written - currentOffset = 0 - mLast10Speeds.clear() - - Telemetry.info("Verifying image on USB drive", "worker") - - verifyImage( - rawSourceStream, - blockDev, - imageSize, - bufferSize, - { currentOffset = it }, - coroScope, - ::sendProgressUpdate, - { mVerificationCancelled }, - ::ensureWakelock - ) - - getFinishedIntent(mSourceUri, mDestDevice, imageSize).broadcastLocallySync( - this@WorkerService - ) - } catch (exception: Exception) { // A cancelled job (service destroyed) is not a failure: rethrow instead of // reporting it or broadcasting an error. @@ -546,7 +651,8 @@ class WorkerService : LifecycleService() { mJobId, currentOffset, imageSize, - exception = it + exception = it, + isBackup = mOperation == JobOperation.BACKUP ).broadcastLocallySync(this@WorkerService) } stopSelf() @@ -588,33 +694,145 @@ class WorkerService : LifecycleService() { newSpeed, processedBytes, imageSize, - isVerifying = isVerifying + isVerifying = isVerifying, + isBackup = mOperation == JobOperation.BACKUP ).broadcastLocally(this@WorkerService) mLastProgressUpdate = newTime mBytesSinceLastUpdate = 0.0 } + /** + * Appends the device to the destination document, resuming from whatever it already holds, + * and returns the SHA-256 of the resulting file. The document is opened in append mode and + * never truncated: an interrupted backup keeps its prefix for the next attempt. + */ + private suspend fun runBackup( + blockDev: BlockDeviceDriver, + devSize: Long, + coroScope: CoroutineScope, + ): String { + val pfd = openBackupFile() + var existing: InputStream? = null + var out: BufferedOutputStream? = null + + try { + val resumeOffset = pfd.statSize + if (resumeOffset < 0) throw CouldNotWriteBackupException( + "Could not determine the size of the backup file", null + ) + + if (resumeOffset > 0) { + existing = try { + contentResolver.openInputStream(mSourceUri) + } catch (e: SecurityException) { + throw CouldNotWriteBackupException("Failed to reopen the backup file", e) + } catch (e: FileNotFoundException) { + throw CouldNotWriteBackupException("Failed to reopen the backup file", e) + } ?: throw CouldNotWriteBackupException("Failed to reopen the backup file", null) + } + + // Closing this also closes the descriptor: it owns pfd from here on. + out = BufferedOutputStream( + ParcelFileDescriptor.AutoCloseOutputStream(pfd), 1 shl 20 + ) + + val sha256 = BackupFlow.backupImage( + blockDev, + out, + existing, + resumeOffset, + devSize, + BUFFER_BLOCKS * blockDev.blockSize, + {}, + coroScope, + ::ensureWakelock, + ::sendProgressUpdate, + ) + + // Make the whole file durable before renaming it to its final name. + try { + out.flush() + pfd.fileDescriptor.sync() + } catch (e: CancellationException) { + throw e + } catch (e: Exception) { + throw CouldNotWriteBackupException("Failed to sync the backup file", e) + } + + return sha256 + } finally { + val stream = out + try { + if (stream != null) stream.close() else pfd.close() + } catch (e: Exception) { + Telemetry.warning("Failed to close the backup file: ${e.message}") + } + try { + existing?.close() + } catch (e: Exception) { + Telemetry.warning("Failed to close the existing backup data: ${e.message}") + } + } + } + + private fun openBackupFile(): ParcelFileDescriptor = try { + contentResolver.openFileDescriptor(mSourceUri, "wa") + } catch (e: SecurityException) { + throw CouldNotWriteBackupException("Failed to open the backup file", e) + } catch (e: FileNotFoundException) { + throw CouldNotWriteBackupException("Failed to open the backup file", e) + } ?: throw CouldNotWriteBackupException("Failed to open the backup file", null) + + /** + * Drops the `.part` suffix from a finished backup. Best-effort: the file already holds the + * complete data, so neither a provider that does not support renaming nor a failure may turn + * a successful backup into a failed one. + */ + private fun renameToFinalName(uri: Uri) { + try { + val name = uri.getDisplayName(this) ?: return + if (!name.isIncompleteBackupName()) return + DocumentsContract.renameDocument(contentResolver, uri, name.withoutPartSuffix()) + } catch (e: Exception) { + Telemetry.warning("Failed to rename the backup file: ${e.message}") + } + } + private val filenameStr: String by lazy { mSourceUri.getDisplayName(this) ?: getString(R.string.unknown_filename) } private val basicForegroundNotification: Notification - get() = NotificationCompat - .Builder(this, JOB_PROGRESS_CHANNEL) - .setSmallIcon(R.drawable.ic_write_to_usb_anim) - .setContentTitle(getString(R.string.notification_write_progress_title)) - .setContentText( - getString( - if (notificationsAllowed) R.string.notification_write_tap_for_progress_text - else R.string.notification_write_no_notifications_text, - filenameStr, - mDestDevice.name + get() { + val isBackup = mOperation == JobOperation.BACKUP + return NotificationCompat + .Builder(this, JOB_PROGRESS_CHANNEL) + .setSmallIcon(R.drawable.ic_write_to_usb_anim) + .setContentTitle( + getString( + if (isBackup) R.string.notification_backup_progress_title + else R.string.notification_write_progress_title + ) ) - ) - .setProgress(100, 0, true) - .setOngoing(true) - .build() + .setContentText( + if (isBackup) getString( + if (notificationsAllowed) R.string.notification_backup_tap_for_progress_text + else R.string.notification_backup_no_notifications_text, + mDestDevice.name, + filenameStr + ) + else getString( + if (notificationsAllowed) R.string.notification_write_tap_for_progress_text + else R.string.notification_write_no_notifications_text, + filenameStr, + mDestDevice.name + ) + ) + .setProgress(100, 0, true) + .setOngoing(true) + .build() + } private fun trySetupNotifications() { if (mNotificationsSetUp || Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0c9078f..6217d59 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -16,6 +16,13 @@ Tap for progress • Writing %1$s to %2$s %1$s to %2$s %1$s against %2$s + Backing up… + %1$s to %2$s + Enable notifications for progress • Backing up %1$s to %2$s + Tap for progress • Backing up %1$s to %2$s + Backup finished + %1$s backed up to %2$s + Backup failed Unknown file name No file picker app found on this device. Install a file manager and try again. "Permission denied for USB device %1$s" diff --git a/app/src/test/java/eu/depau/etchdroid/BackupErrorMappingTest.kt b/app/src/test/java/eu/depau/etchdroid/BackupErrorMappingTest.kt new file mode 100644 index 0000000..baa1ae4 --- /dev/null +++ b/app/src/test/java/eu/depau/etchdroid/BackupErrorMappingTest.kt @@ -0,0 +1,32 @@ +package eu.depau.etchdroid + +import eu.depau.etchdroid.service.WorkerServiceFlowImpl +import eu.depau.etchdroid.utils.exception.CouldNotWriteBackupException +import eu.depau.etchdroid.utils.exception.NotEnoughSpaceForBackupException +import eu.depau.etchdroid.utils.exception.UsbCommunicationException +import me.jahnen.libaums.core.usb.PipeException +import org.junit.jupiter.api.Assertions.assertSame +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.extension.ExtendWith +import org.robolectric.annotation.Config +import tech.apter.junit.jupiter.robolectric.RobolectricExtension +import java.io.IOException + +@ExtendWith(RobolectricExtension::class) +@Config(application = EtchDroidApplication::class) +class BackupErrorMappingTest { + @Test + fun backupExceptionsPassThroughUnwrapped() { + val write = CouldNotWriteBackupException("boom", IOException("ENOSPC")) + assertSame(write, WorkerServiceFlowImpl.toJobException(write)) + val space = NotEnoughSpaceForBackupException(10, 5) + assertSame(space, WorkerServiceFlowImpl.toJobException(space)) + } + + @Test + fun deviceErrorsStillMapToUsbCommunication() { + val mapped = WorkerServiceFlowImpl.toJobException(PipeException()) + assertTrue(mapped is UsbCommunicationException) + } +}