Encrypted overlay filesystems implementation for Android. Also available on GitHub: https://github.com/hardcore-sushi/DroidFS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DroidFS/app/src/main/java/sushi/hardcore/droidfs/GocryptfsVolume.kt

247 lines
8.8 KiB

package sushi.hardcore.droidfs
2 years ago
import android.content.Context
import android.net.Uri
import sushi.hardcore.droidfs.explorers.ExplorerElement
import sushi.hardcore.droidfs.util.PathUtils
import java.io.File
import java.io.FileOutputStream
import java.io.InputStream
import java.io.OutputStream
2 years ago
11 months ago
class GocryptfsVolume(val applicationContext: Context, var sessionID: Int) {
2 years ago
private external fun native_close(sessionID: Int)
private external fun native_is_closed(sessionID: Int): Boolean
2 years ago
private external fun native_list_dir(sessionID: Int, dir_path: String): MutableList<ExplorerElement>
private external fun native_open_read_mode(sessionID: Int, file_path: String): Int
private external fun native_open_write_mode(sessionID: Int, file_path: String, mode: Int): Int
2 years ago
private external fun native_read_file(sessionID: Int, handleID: Int, offset: Long, buff: ByteArray): Int
private external fun native_write_file(sessionID: Int, handleID: Int, offset: Long, buff: ByteArray, buff_size: Int): Int
private external fun native_truncate(sessionID: Int, handleID: Int, offset: Long): Boolean
2 years ago
private external fun native_path_exists(sessionID: Int, file_path: String): Boolean
private external fun native_get_size(sessionID: Int, file_path: String): Long
private external fun native_close_file(sessionID: Int, handleID: Int)
private external fun native_remove_file(sessionID: Int, file_path: String): Boolean
private external fun native_mkdir(sessionID: Int, dir_path: String, mode: Int): Boolean
2 years ago
private external fun native_rmdir(sessionID: Int, dir_path: String): Boolean
private external fun native_rename(sessionID: Int, old_path: String, new_path: String): Boolean
companion object {
const val KeyLen = 32
const val ScryptDefaultLogN = 16
const val DefaultBS = 4096
const val CONFIG_FILE_NAME = "gocryptfs.conf"
7 months ago
external fun createVolume(root_cipher_dir: String, password: CharArray, plainTextNames: Boolean, xchacha: Int, logN: Int, creator: String, returnedHash: ByteArray?): Boolean
2 years ago
external fun init(root_cipher_dir: String, password: CharArray?, givenHash: ByteArray?, returnedHash: ByteArray?): Int
2 years ago
external fun changePassword(root_cipher_dir: String, old_password: CharArray?, givenHash: ByteArray?, new_password: CharArray, returnedHash: ByteArray?): Boolean
2 years ago
fun isGocryptfsVolume(path: File): Boolean {
if (path.isDirectory){
return File(path, CONFIG_FILE_NAME).isFile
}
return false
}
2 years ago
init {
System.loadLibrary("gocryptfs_jni")
}
}
fun close() {
native_close(sessionID)
2 years ago
}
fun isClosed(): Boolean {
return native_is_closed(sessionID)
}
2 years ago
fun listDir(dir_path: String): MutableList<ExplorerElement> {
return native_list_dir(sessionID, dir_path)
2 years ago
}
fun mkdir(dir_path: String): Boolean {
return native_mkdir(sessionID, dir_path, ConstValues.DIRECTORY_MODE)
2 years ago
}
fun rmdir(dir_path: String): Boolean {
return native_rmdir(sessionID, dir_path)
2 years ago
}
2 years ago
fun removeFile(file_path: String): Boolean {
return native_remove_file(sessionID, file_path)
2 years ago
}
2 years ago
fun pathExists(file_path: String): Boolean {
return native_path_exists(sessionID, file_path)
2 years ago
}
2 years ago
fun getSize(file_path: String): Long {
return native_get_size(sessionID, file_path)
2 years ago
}
2 years ago
fun closeFile(handleID: Int) {
native_close_file(sessionID, handleID)
2 years ago
}
2 years ago
fun openReadMode(file_path: String): Int {
return native_open_read_mode(sessionID, file_path)
2 years ago
}
2 years ago
fun openWriteMode(file_path: String): Int {
return native_open_write_mode(sessionID, file_path, ConstValues.FILE_MODE)
2 years ago
}
2 years ago
fun readFile(handleID: Int, offset: Long, buff: ByteArray): Int {
return native_read_file(sessionID, handleID, offset, buff)
2 years ago
}
2 years ago
fun writeFile(handleID: Int, offset: Long, buff: ByteArray, buff_size: Int): Int {
return native_write_file(sessionID, handleID, offset, buff, buff_size)
2 years ago
}
fun truncate(handleID: Int, offset: Long): Boolean {
return native_truncate(sessionID, handleID, offset)
2 years ago
}
fun rename(old_path: String, new_path: String): Boolean {
return native_rename(sessionID, old_path, new_path)
2 years ago
}
2 years ago
fun exportFile(handleID: Int, os: OutputStream): Boolean {
2 years ago
var offset: Long = 0
val ioBuffer = ByteArray(DefaultBS)
2 years ago
var length: Int
while (readFile(handleID, offset, ioBuffer).also { length = it } > 0){
os.write(ioBuffer, 0, length)
2 years ago
offset += length.toLong()
}
os.close()
return true
}
2 years ago
fun exportFile(src_path: String, os: OutputStream): Boolean {
2 years ago
var success = false
2 years ago
val srcHandleId = openReadMode(src_path)
if (srcHandleId != -1) {
2 years ago
success = exportFile(srcHandleId, os)
closeFile(srcHandleId)
2 years ago
}
return success
}
2 years ago
fun exportFile(src_path: String, dst_path: String): Boolean {
return exportFile(src_path, FileOutputStream(dst_path))
2 years ago
}
2 years ago
fun exportFile(context: Context, src_path: String, output_path: Uri): Boolean {
2 years ago
val os = context.contentResolver.openOutputStream(output_path)
if (os != null){
2 years ago
return exportFile(src_path, os)
2 years ago
}
return false
}
2 years ago
fun importFile(inputStream: InputStream, handleID: Int): Boolean {
2 years ago
var offset: Long = 0
val ioBuffer = ByteArray(DefaultBS)
2 years ago
var length: Int
while (inputStream.read(ioBuffer).also { length = it } > 0) {
val written = writeFile(handleID, offset, ioBuffer, length).toLong()
2 years ago
if (written == length.toLong()) {
offset += written
} else {
inputStream.close()
2 years ago
return false
}
}
closeFile(handleID)
inputStream.close()
2 years ago
return true
}
2 years ago
fun importFile(inputStream: InputStream, dst_path: String): Boolean {
2 years ago
var success = false
2 years ago
val dstHandleId = openWriteMode(dst_path)
if (dstHandleId != -1) {
2 years ago
success = importFile(inputStream, dstHandleId)
closeFile(dstHandleId)
2 years ago
}
return success
}
2 years ago
fun importFile(context: Context, src_uri: Uri, dst_path: String): Boolean {
val inputStream = context.contentResolver.openInputStream(src_uri)
if (inputStream != null){
2 years ago
return importFile(inputStream, dst_path)
2 years ago
}
return false
}
2 years ago
fun recursiveMapFiles(rootPath: String): MutableList<ExplorerElement> {
val result = mutableListOf<ExplorerElement>()
val explorerElements = listDir(rootPath)
result.addAll(explorerElements)
for (e in explorerElements){
if (e.isDirectory){
result.addAll(recursiveMapFiles(e.fullPath))
2 years ago
}
}
return result
}
fun recursiveRemoveDirectory(plain_directory_path: String): String? {
val explorerElements = listDir(plain_directory_path)
for (e in explorerElements) {
val fullPath = PathUtils.pathJoin(plain_directory_path, e.name)
if (e.isDirectory) {
val result = recursiveRemoveDirectory(fullPath)
result?.let { return it }
} else {
if (!removeFile(fullPath)) {
return fullPath
}
}
}
return if (!rmdir(plain_directory_path)) {
plain_directory_path
} else {
null
}
}
11 months ago
8 months ago
fun loadWholeFile(fullPath: String, size: Long? = null, maxSize: Long? = null): Pair<ByteArray?, Int> {
val fileSize = size ?: getSize(fullPath)
11 months ago
return if (fileSize >= 0) {
maxSize?.let {
if (fileSize > it) {
return Pair(null, 0)
}
}
try {
val fileBuff = ByteArray(fileSize.toInt())
val handleID = openReadMode(fullPath)
if (handleID == -1) {
Pair(null, 3)
} else {
var offset: Long = 0
val ioBuffer = ByteArray(DefaultBS)
var length: Int
while (readFile(handleID, offset, ioBuffer).also { length = it } > 0) {
System.arraycopy(ioBuffer, 0, fileBuff, offset.toInt(), length)
offset += length.toLong()
}
closeFile(handleID)
if (offset == fileBuff.size.toLong()) {
Pair(fileBuff, 0)
} else {
Pair(null, 4)
}
}
} catch (e: OutOfMemoryError) {
Pair(null, 2)
}
} else {
Pair(null, 1)
}
}
2 years ago
}