-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**Background** We are now using a platform-specific version of flipper storage throughout the project. This is not correct. As part of the overall migration of the project to KMP, we are gradually moving to okio + core:storage **Changes** - Add `core:storage` - Add `core:atomicfile` **Test plan** Try launch tests and also migrating from old version of application
- Loading branch information
Showing
68 changed files
with
913 additions
and
435 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 0 additions & 32 deletions
32
...impl/src/main/java/com/flipperdevices/bridge/dao/impl/comparator/DefaultFileComparator.kt
This file was deleted.
Oops, something went wrong.
59 changes: 56 additions & 3 deletions
59
...ge/dao/impl/src/main/java/com/flipperdevices/bridge/dao/impl/comparator/FileComparator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,64 @@ | ||
package com.flipperdevices.bridge.dao.impl.comparator | ||
|
||
import java.io.InputStream | ||
import com.flipperdevices.core.FlipperStorageProvider | ||
import com.flipperdevices.core.ktx.jre.FlipperDispatchers | ||
import kotlinx.coroutines.withContext | ||
import okio.BufferedSource | ||
import okio.FileSystem | ||
import okio.Path | ||
import okio.buffer | ||
import javax.inject.Inject | ||
|
||
interface FileComparator { | ||
class FileComparator @Inject constructor( | ||
private val flipperStorageProvider: FlipperStorageProvider | ||
) { | ||
|
||
/** | ||
* Check if two streams have identical content | ||
*/ | ||
suspend fun isSameContent(istream1: InputStream, istream2: InputStream): Boolean | ||
suspend fun isSameContent( | ||
source1: BufferedSource, | ||
source2: BufferedSource | ||
): Boolean = withContext(FlipperDispatchers.workStealingDispatcher) { | ||
val isSource1Empty = source1.request(byteCount = 1).not() | ||
val isSource2Empty = source2.request(byteCount = 1).not() | ||
if (isSource1Empty && isSource2Empty) { | ||
return@withContext true | ||
} | ||
if (isSource1Empty || isSource2Empty) { | ||
return@withContext false | ||
} | ||
do { | ||
val ch1 = runCatching { source1.readByte() }.getOrNull() | ||
val ch2 = runCatching { source2.readByte() }.getOrNull() | ||
if (ch1 != ch2) return@withContext false | ||
} while (ch1 != null) | ||
return@withContext true | ||
} | ||
|
||
/** | ||
* Checking two files for same content | ||
* | ||
* If files doesn't exists returns true | ||
*/ | ||
suspend fun isSameContent( | ||
file1: Path, | ||
file2: Path, | ||
fileSystem: FileSystem = flipperStorageProvider.fileSystem | ||
): Boolean = withContext(FlipperDispatchers.workStealingDispatcher) { | ||
if (!fileSystem.exists(file1) && !fileSystem.exists(file2)) return@withContext true | ||
if (!fileSystem.exists(file1) || !fileSystem.exists(file2)) return@withContext false | ||
|
||
if (fileSystem.metadataOrNull(file1)?.size != fileSystem.metadataOrNull(file2)?.size) { | ||
return@withContext false | ||
} | ||
return@withContext fileSystem.source(file1).buffer().use { source1 -> | ||
fileSystem.source(file2).buffer().use { source2 -> | ||
isSameContent( | ||
source1, | ||
source2 | ||
) | ||
} | ||
} | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
...dao/impl/src/main/java/com/flipperdevices/bridge/dao/impl/comparator/FileComparatorExt.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.