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

Deduplicator: Improve error handling and skip unreadable files #944

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
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 @@ -30,7 +30,7 @@ class FileOpsClient @AssistedInject constructor(
if (Bugs.isTrace) log(TAG) { "listFiles($path): $it" }
}
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

/**
Expand All @@ -41,23 +41,23 @@ class FileOpsClient @AssistedInject constructor(
if (Bugs.isTrace) log(TAG) { "listFilesStream($path) finished streaming, ${it.size} items" }
}
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun lookupFiles(path: LocalPath): Collection<LocalPathLookup> = try {
fileOpsConnection.lookupFiles(path).also {
if (Bugs.isTrace) log(TAG, VERBOSE) { "lookupFiles($path): $it" }
}
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun lookUp(path: LocalPath): LocalPathLookup = try {
fileOpsConnection.lookUp(path).also {
if (Bugs.isTrace) log(TAG, VERBOSE) { "lookup($path): $it" }
}
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

/**
Expand All @@ -68,7 +68,7 @@ class FileOpsClient @AssistedInject constructor(
if (Bugs.isTrace) log(TAG, VERBOSE) { "lookupFilesStream($path) finished streaming, ${it.size} items" }
}
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

/**
Expand All @@ -82,89 +82,90 @@ class FileOpsClient @AssistedInject constructor(
) { "lookupFilesExtendedStream($path) finished streaming, ${it.size} items" }
}
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun readFile(path: LocalPath): Source = try {
fileOpsConnection.readFile(path).source()
} catch (e: IOException) {
throw fakeIOException(e.getRootCause())
} catch (e: Exception) {
throw e.toFakeIOException()
}

fun writeFile(path: LocalPath): Sink = try {
fileOpsConnection.writeFile(path).sink()
} catch (e: IOException) {
throw fakeIOException(e.getRootCause())
} catch (e: Exception) {
throw e.toFakeIOException()
}

fun mkdirs(path: LocalPath): Boolean = try {
fileOpsConnection.mkdirs(path)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun createNewFile(path: LocalPath): Boolean = try {
fileOpsConnection.createNewFile(path)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun canRead(path: LocalPath): Boolean = try {
fileOpsConnection.canRead(path)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun canWrite(path: LocalPath): Boolean = try {
fileOpsConnection.canWrite(path)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun exists(path: LocalPath): Boolean = try {
fileOpsConnection.exists(path)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun delete(path: LocalPath): Boolean = try {
fileOpsConnection.delete(path)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun createSymlink(linkPath: LocalPath, targetPath: LocalPath): Boolean = try {
fileOpsConnection.createSymlink(linkPath, targetPath)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun setModifiedAt(path: LocalPath, modifiedAt: Instant): Boolean = try {
fileOpsConnection.setModifiedAt(path, modifiedAt.toEpochMilli())
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun setPermissions(path: LocalPath, permissions: Permissions): Boolean = try {
fileOpsConnection.setPermissions(path, permissions)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

fun setOwnership(path: LocalPath, ownership: Ownership): Boolean = try {
fileOpsConnection.setOwnership(path, ownership)
} catch (e: Exception) {
throw fakeIOException(e.getRootCause())
throw e.toFakeIOException()
}

private fun fakeIOException(e: Throwable): IOException {
val gulpExceptionPrefix = "java.io.IOException: "
private fun Exception.toFakeIOException(): IOException {
val org = this.getRootCause()
val gulpPrefix = "java.io.IOException: "
val message = when {
e.message.isNullOrEmpty() -> e.toString()
e.message?.startsWith(gulpExceptionPrefix) == true -> e.message!!.replace(gulpExceptionPrefix, "")
org.message.isNullOrEmpty() -> org.toString()
org.message?.startsWith(gulpPrefix) == true -> org.message!!.replace(gulpPrefix, "")
else -> ""
}
return IOException(message, e.cause)
return IOException(message, org.cause)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ class FileOpsHost @Inject constructor(
}

private fun wrapPropagating(e: Exception): Exception {
return if (e is UnsupportedOperationException) e
else UnsupportedOperationException(e)
return if (e is UnsupportedOperationException) e else UnsupportedOperationException(e)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import eu.darken.sdmse.common.areas.currentAreas
import eu.darken.sdmse.common.ca.toCaString
import eu.darken.sdmse.common.coroutine.DispatcherProvider
import eu.darken.sdmse.common.datastore.value
import eu.darken.sdmse.common.debug.logging.Logging.Priority.ERROR
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.Logging.Priority.WARN
import eu.darken.sdmse.common.debug.logging.asLog
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.APathLookup
Expand Down Expand Up @@ -155,7 +157,12 @@ class ChecksumSleuth @Inject constructor(
val checksummedGroup = items.map { item ->
val start = System.currentTimeMillis()

val hash = gatewaySwitch.read(item.lookedUp).hash(Hasher.Type.SHA256)
val hash = try {
gatewaySwitch.read(item.lookedUp).hash(Hasher.Type.SHA256)
} catch (e: Exception) {
log(TAG, ERROR) { "Failed to read $item: ${e.asLog()}" }
return@flow
}
val hexHash = hash.format()

val stop = System.currentTimeMillis()
Expand Down