Skip to content
This repository was archived by the owner on Oct 24, 2025. It is now read-only.

Commit a474ffe

Browse files
committed
fix: return file size in bytes
1 parent 31f4fab commit a474ffe

File tree

7 files changed

+14
-12
lines changed

7 files changed

+14
-12
lines changed

sqlite-storage/src/androidMain/kotlin/org/asyncstorage/sqlitestorage/AndroidDatabaseFile.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ internal class AndroidDatabaseFile(private val dbFile: File) : DatabaseFiles {
3333
override fun size(type: DatabaseFileType): Long? {
3434
val file = File(path(type))
3535
return if (file.exists()) {
36-
(dbFile.length() / 1024L)
36+
val length = dbFile.length()
37+
if (length == 0L) return null else length
3738
} else {
3839
null
3940
}

sqlite-storage/src/androidUnitTest/kotlin/org/asyncstorage/sqlitestorage/utils/FileTestHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ actual class FileTestHelper actual constructor(path: String) {
2323
}
2424
}
2525

26-
actual fun fillWithChars(charCount: Int) {
27-
file.writeText("x".repeat(charCount))
26+
actual fun fillWithChars(charCount: Int, char: String) {
27+
file.writeText(char.repeat(charCount))
2828
}
2929

3030
actual fun exists() = file.exists()

sqlite-storage/src/commonMain/kotlin/org/asyncstorage/sqlitestorage/DatabaseFiles.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface DatabaseFiles {
1919
fun delete(): Boolean
2020

2121
/**
22-
* Returns a size of a selected database file
22+
* Returns a size of a selected database file in bytes
2323
* If the file does not exists or cannot be read for some reason, returns null
2424
*/
2525
fun size(type: DatabaseFileType = DatabaseFileType.Main): Long?

sqlite-storage/src/commonTest/kotlin/org/asyncstorage/sqlitestorage/DatabaseFileTest.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class DatabaseFileTest {
2525
val file = createDatabaseFile(dbName)
2626
val helper = FileTestHelper.createFileAt(file.path())
2727
assertTrue(helper.exists(), "db file not created")
28-
helper.fillWithChars(2048)
29-
val size = file.size()
30-
assertTrue(size == 2L, "size not matching $size")
28+
helper.fillWithChars(500, "a") // 1 byte in UTF8
29+
val size1 = file.size()
30+
val expected1 = 500L
31+
assertEquals(expected1, size1, "size miss match")
3132
}
3233

3334
@Test

sqlite-storage/src/commonTest/kotlin/org/asyncstorage/sqlitestorage/utils/FileTestHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ expect class FileTestHelper(path: String) {
88
): FileTestHelper
99
}
1010

11-
fun fillWithChars(charCount: Int)
11+
fun fillWithChars(charCount: Int, char: String = "")
1212

1313
fun exists(): Boolean
1414

sqlite-storage/src/iosMain/kotlin/org/asyncstorage/sqlitestorage/IosDatabaseFile.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ internal class IosDatabaseFile(private val dbName: String) : DatabaseFiles {
5555
return try {
5656
val attrs = NSFileManager.defaultManager.attributesOfItemAtPath(file, null)
5757
val fileSize = attrs?.get(NSFileSize) as Long?
58-
fileSize?.let {
59-
it / 1024L
58+
fileSize?.let { length ->
59+
if (length == 0L) null else length
6060
}
6161
} catch (e: Exception) {
6262
return null

sqlite-storage/src/iosTest/kotlin/org/asyncstorage/sqlitestorage/utils/FileTestHelper.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ actual class FileTestHelper actual constructor(path: String) {
4242
}
4343
}
4444

45-
actual fun fillWithChars(charCount: Int) {
46-
val content = "x".repeat(charCount)
45+
actual fun fillWithChars(charCount: Int, char: String) {
46+
val content = char.repeat(charCount)
4747
try {
4848
(content as NSString).writeToFile(file, true)
4949
} catch (e: Throwable) {

0 commit comments

Comments
 (0)