-
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1692 from kiwix/feature/macgills/1663-filesystem-…
…detection #1663 Improve FileSystem detection - read from mount and fallback to …
- Loading branch information
Showing
5 changed files
with
177 additions
and
24 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
29 changes: 29 additions & 0 deletions
29
app/src/main/java/org/kiwix/kiwixmobile/zim_manager/FileSystemChecker.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Kiwix Android | ||
* Copyright (c) 2020 Kiwix <android.kiwix.org> | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.kiwix.kiwixmobile.zim_manager | ||
|
||
interface FileSystemChecker { | ||
fun checkFilesystemSupports4GbFiles(path: String): FileSystemCapability | ||
} | ||
|
||
enum class FileSystemCapability { | ||
CAN_WRITE_4GB, | ||
CANNOT_WRITE_4GB, | ||
INCONCLUSIVE | ||
} |
49 changes: 49 additions & 0 deletions
49
app/src/main/java/org/kiwix/kiwixmobile/zim_manager/FileWritingFileSystemChecker.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Kiwix Android | ||
* Copyright (c) 2020 Kiwix <android.kiwix.org> | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.kiwix.kiwixmobile.zim_manager | ||
|
||
import android.util.Log | ||
import org.kiwix.kiwixmobile.zim_manager.FileSystemCapability.CANNOT_WRITE_4GB | ||
import org.kiwix.kiwixmobile.zim_manager.FileSystemCapability.CAN_WRITE_4GB | ||
import java.io.File | ||
import java.io.RandomAccessFile | ||
|
||
class FileWritingFileSystemChecker : FileSystemChecker { | ||
override fun checkFilesystemSupports4GbFiles(path: String): FileSystemCapability { | ||
with(File("$path/large_file_test.txt")) { | ||
deleteIfExists() | ||
try { | ||
RandomAccessFile(this.path, "rw").use { | ||
it.setLength(Fat32Checker.FOUR_GIGABYTES_IN_BYTES) | ||
return@checkFilesystemSupports4GbFiles CAN_WRITE_4GB | ||
} | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
Log.d("Fat32Checker", e.message) | ||
return@checkFilesystemSupports4GbFiles CANNOT_WRITE_4GB | ||
} finally { | ||
deleteIfExists() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun File.deleteIfExists() { | ||
if (exists()) delete() | ||
} |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/org/kiwix/kiwixmobile/zim_manager/MountFileSystemChecker.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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Kiwix Android | ||
* Copyright (c) 2020 Kiwix <android.kiwix.org> | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.kiwix.kiwixmobile.zim_manager | ||
|
||
import org.kiwix.kiwixmobile.zim_manager.FileSystemCapability.CANNOT_WRITE_4GB | ||
import org.kiwix.kiwixmobile.zim_manager.FileSystemCapability.CAN_WRITE_4GB | ||
import org.kiwix.kiwixmobile.zim_manager.FileSystemCapability.INCONCLUSIVE | ||
import java.io.File | ||
|
||
class MountFileSystemChecker : FileSystemChecker { | ||
override fun checkFilesystemSupports4GbFiles(path: String) = | ||
recursivelyDetermineFilesystem(mountPoints(), path) | ||
|
||
private fun recursivelyDetermineFilesystem(mountPoints: List<MountInfo>, path: String): | ||
FileSystemCapability = | ||
mountPoints.maxBy { it.matchCount(path) } | ||
?.takeIf { it.matchCount(path) > 0 } | ||
?.let { | ||
when { | ||
it.isVirtual -> recursivelyDetermineFilesystem(mountPoints - it, it.device) | ||
it.supports4GBFiles -> CAN_WRITE_4GB | ||
it.doesNotSupport4GBFiles -> CANNOT_WRITE_4GB | ||
else -> INCONCLUSIVE | ||
} | ||
} ?: INCONCLUSIVE | ||
|
||
private fun mountPoints() = | ||
File("proc/mounts") | ||
.takeIf(File::exists) | ||
?.readLines() | ||
?.map { MountInfo(it.split(" ")) } | ||
?: emptyList() | ||
} | ||
|
||
data class MountInfo(val device: String, val mountPoint: String, val fileSystem: String) { | ||
constructor(split: List<String>) : this(split[0], split[1], split[2]) | ||
|
||
fun matchCount(storage: String) = storage.split("/") | ||
.zip(mountPoint.split("/")) | ||
.fold(0, { acc, pair -> | ||
if (pair.first == pair.second) acc + 1 | ||
else acc | ||
}) | ||
|
||
val isVirtual = VIRTUAL_FILE_SYSTEMS.contains(fileSystem) | ||
val supports4GBFiles = SUPPORTS_4GB_FILE_SYSTEMS.contains(fileSystem) | ||
val doesNotSupport4GBFiles = DOES_NOT_SUPPORT_4GB_FILE_SYSTEMS.contains(fileSystem) | ||
|
||
companion object { | ||
private val VIRTUAL_FILE_SYSTEMS = listOf("fuse", "sdcardfs") | ||
private val SUPPORTS_4GB_FILE_SYSTEMS = listOf("ext4", "exfat") | ||
private val DOES_NOT_SUPPORT_4GB_FILE_SYSTEMS = listOf("fat32", "vfat") | ||
} | ||
} |