Skip to content

Commit

Permalink
chore: turn directories to process into their RealPath and unique'em …
Browse files Browse the repository at this point in the history
…before processing
  • Loading branch information
guicamest committed Nov 20, 2023
1 parent a737c93 commit 4aec11c
Showing 1 changed file with 13 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import java.nio.file.FileVisitResult
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.security.MessageDigest
import kotlin.io.path.ExperimentalPathApi
Expand All @@ -31,7 +32,7 @@ class CoroutinesFindDuplicatesExecution(
job =
coroutineScope.launch {
val allFiles =
directories.flatMap { directory ->
directories.uniqueAndReal.flatMap { directory ->
buildList {
directory.visitFileTree(followLinks = true) {
onVisitFile { file, attributes ->
Expand All @@ -42,6 +43,7 @@ class CoroutinesFindDuplicatesExecution(
}
}
}

val withSameSize = allFiles.withSameSize()
val withSameContent =
withSameSize.mapNotNull { (_, groupWithSameSize) ->
Expand All @@ -68,6 +70,16 @@ class CoroutinesFindDuplicatesExecution(

private fun Path.contentHash(type: String = "MD5"): String = readBytes().contentHash(type)

private val Collection<Path>.uniqueAndReal: Collection<Path>
get() =
mapNotNull {
try {
it.toRealPath()
} catch (e: NoSuchFileException) {
null
}
}.distinct()

private val HEX_CHARS = "0123456789ABCDEF".toCharArray()

private fun ByteArray.contentHash(type: String = "MD5"): String {
Expand Down

0 comments on commit 4aec11c

Please sign in to comment.