Skip to content

Commit

Permalink
🐛 Avoid circular reference between fileUtils and CodecsUtils, which w…
Browse files Browse the repository at this point in the history
…ill cause compilation failure in iOS environment (#2223)
  • Loading branch information
guiyanakuang authored Nov 21, 2024
1 parent 44b0025 commit 5f9f52a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,7 @@ interface CodecsUtils {
}

fun hash(path: Path): String {
val streamingMurmurHash3 = StreamingMurmurHash3(CROSS_PASTE_SEED)
val bufferSize = fileUtils.fileBufferSize
val buffer = ByteArray(bufferSize)

fileUtils.fileSystem.source(path).buffer().use { bufferedSource ->
while (true) {
val bytesRead = bufferedSource.read(buffer, 0, bufferSize)
if (bytesRead == -1) break
streamingMurmurHash3.update(buffer, 0, bytesRead)
}
}

val (hash1, hash2) = streamingMurmurHash3.finish()
return buildString(32) {
appendHex(hash1)
appendHex(hash2)
}
return fileUtils.getFileHash(path)
}

fun hashByString(string: String): String {
Expand All @@ -74,12 +58,4 @@ interface CodecsUtils {
val hash = sha256Hasher.hashToByteArray()
return hash.toHexString()
}

fun StringBuilder.appendHex(value: ULong) {
for (i in 0 until 8) {
val byte = (value shr i * 8).toByte()
append(HEX_DIGITS[(byte.toInt() shr 4) and 0xf])
append(HEX_DIGITS[byte.toInt() and 0xf])
}
}
}
23 changes: 21 additions & 2 deletions composeApp/src/commonMain/kotlin/com/crosspaste/utils/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.crosspaste.presist.SingleFileInfoTree
import io.ktor.utils.io.*
import okio.FileSystem
import okio.Path
import okio.buffer
import okio.use

expect fun getFileUtils(): FileUtils

Expand Down Expand Up @@ -84,7 +86,7 @@ interface FileUtils {
private fun getDirFileInfoTree(path: Path): FileInfoTree {
val builder = FileInfoTreeBuilder()

FileSystem.SYSTEM.list(path).sorted().forEach { childPath ->
fileSystem.list(path).sorted().forEach { childPath ->
val fileName = childPath.name
val fileTree = getFileInfoTree(childPath)
builder.addFileInfoTree(fileName, fileTree)
Expand All @@ -101,7 +103,24 @@ interface FileUtils {

fun getFileSize(path: Path): Long

fun getFileHash(path: Path): String
fun getFileHash(path: Path): String {
val streamingMurmurHash3 = StreamingMurmurHash3(CROSS_PASTE_SEED)
val buffer = ByteArray(fileBufferSize)

fileSystem.source(path).buffer().use { bufferedSource ->
while (true) {
val bytesRead = bufferedSource.read(buffer, 0, fileBufferSize)
if (bytesRead == -1) break
streamingMurmurHash3.update(buffer, 0, bytesRead)
}
}

val (hash1, hash2) = streamingMurmurHash3.finish()
return buildString(32) {
appendHex(hash1)
appendHex(hash2)
}
}

fun existFile(path: Path): Boolean {
return fileSystem.exists(path)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.crosspaste.utils

fun StringBuilder.appendHex(value: ULong) {
for (i in 0 until 8) {
val byte = (value shr i * 8).toByte()
append(HEX_DIGITS[(byte.toInt() shr 4) and 0xf])
append(HEX_DIGITS[byte.toInt() and 0xf])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ actual fun getFileUtils(): FileUtils {

object DesktopFileUtils : FileUtils {

private val codecsUtils = getCodecsUtils()

private val dateUtils = getDateUtils()

override val fileSystem: FileSystem = FileSystem.SYSTEM
Expand Down Expand Up @@ -75,10 +73,6 @@ object DesktopFileUtils : FileUtils {
return path.toFile().length()
}

override fun getFileHash(path: Path): String {
return codecsUtils.hash(path)
}

override fun createEmptyPasteFile(
path: Path,
length: Long,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.crosspaste.utils

import com.crosspaste.utils.DesktopCodecsUtils.appendHex
import kotlin.test.Test
import kotlin.test.assertEquals

Expand Down

0 comments on commit 5f9f52a

Please sign in to comment.