From a92695f8d5ff34d991068e977f2d964e80d828d8 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sat, 25 Jun 2022 21:36:22 +0200 Subject: [PATCH] Cleaning --- app/libcryfs | 2 +- .../sushi/hardcore/droidfs/MainActivity.kt | 4 +-- .../sushi/hardcore/droidfs/VolumeDatabase.kt | 8 +---- .../add_volume/CreateVolumeFragment.kt | 4 +-- .../droidfs/add_volume/SelectPathFragment.kt | 2 +- .../droidfs/explorers/BaseExplorerActivity.kt | 13 ++++--- .../droidfs/explorers/ExplorerActivity.kt | 4 +-- .../droidfs/file_viewers/ImageViewer.kt | 2 +- .../droidfs/filesystems/EncryptedVolume.kt | 6 ++-- .../sushi/hardcore/droidfs/util/PathUtils.kt | 35 +++++++++---------- .../sushi/hardcore/droidfs/util/WidgetUtil.kt | 2 +- app/src/main/native/gocryptfs_jni.c | 2 +- 12 files changed, 40 insertions(+), 44 deletions(-) diff --git a/app/libcryfs b/app/libcryfs index c0af6c0..267fb40 160000 --- a/app/libcryfs +++ b/app/libcryfs @@ -1 +1 @@ -Subproject commit c0af6c0bc2045bb53f65fde02d192d5aca436eae +Subproject commit 267fb40fe77e5ef079759bebd395c7a92ef072d2 diff --git a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt index 4d679f0..05f91ca 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/MainActivity.kt @@ -429,7 +429,7 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener { val dstPath = File(srcPath.parent, newName).canonicalFile val newDBName: String val success = if (volume.isHidden) { - if (newName.contains("/")) { + if (newName.contains(PathUtils.SEPARATOR)) { Toast.makeText(this, R.string.error_slash_in_name, Toast.LENGTH_SHORT).show() renameVolume(volume, position) return@EditTextDialog @@ -528,7 +528,7 @@ class MainActivity : BaseActivity(), VolumeAdapter.Listener { openVolumeWithPassword( volume, position, - WidgetUtil.editTextContentEncode(dialogBinding.editPassword), + WidgetUtil.encodeEditTextContent(dialogBinding.editPassword), dialogBinding.checkboxSavePassword.isChecked, ) } diff --git a/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt b/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt index 0948c8d..1c57388 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/VolumeDatabase.kt @@ -104,17 +104,11 @@ class VolumeDatabase(private val context: Context): SQLiteOpenHelper(context, Co val list: MutableList = ArrayList() val cursor = readableDatabase.rawQuery("SELECT * FROM $TABLE_NAME", null) while (cursor.moveToNext()){ - val typeColumnIndex = cursor.getColumnIndex(COLUMN_TYPE) - val volumeType = if (typeColumnIndex == -1) { - EncryptedVolume.GOCRYPTFS_VOLUME_TYPE - } else { - cursor.getBlob(typeColumnIndex)[0] - } list.add( SavedVolume( cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_NAME)), cursor.getShort(cursor.getColumnIndexOrThrow(COLUMN_HIDDEN)) == 1.toShort(), - volumeType, + cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_TYPE))[0], cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_HASH)), cursor.getBlob(cursor.getColumnIndexOrThrow(COLUMN_IV)) ) diff --git a/app/src/main/java/sushi/hardcore/droidfs/add_volume/CreateVolumeFragment.kt b/app/src/main/java/sushi/hardcore/droidfs/add_volume/CreateVolumeFragment.kt index 279581e..4c53870 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/add_volume/CreateVolumeFragment.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/add_volume/CreateVolumeFragment.kt @@ -159,8 +159,8 @@ class CreateVolumeFragment: Fragment() { } private fun createVolume() { - val password = WidgetUtil.editTextContentEncode(binding.editPassword) - val passwordConfirm = WidgetUtil.editTextContentEncode(binding.editPasswordConfirm) + val password = WidgetUtil.encodeEditTextContent(binding.editPassword) + val passwordConfirm = WidgetUtil.encodeEditTextContent(binding.editPasswordConfirm) if (!password.contentEquals(passwordConfirm)) { Toast.makeText(requireContext(), R.string.passwords_mismatch, Toast.LENGTH_SHORT).show() Arrays.fill(password, 0) diff --git a/app/src/main/java/sushi/hardcore/droidfs/add_volume/SelectPathFragment.kt b/app/src/main/java/sushi/hardcore/droidfs/add_volume/SelectPathFragment.kt index 7423c4c..f27ef24 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/add_volume/SelectPathFragment.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/add_volume/SelectPathFragment.kt @@ -190,7 +190,7 @@ class SelectPathFragment: Fragment() { if (isHidden) R.string.enter_volume_name else R.string.enter_volume_path, Toast.LENGTH_SHORT ).show() - } else if (isHidden && currentVolumeValue.contains("/")) { + } else if (isHidden && currentVolumeValue.contains(PathUtils.SEPARATOR)) { Toast.makeText(requireContext(), R.string.error_slash_in_name, Toast.LENGTH_SHORT).show() } else { val volumePath = getCurrentVolumePath() diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index f017c46..ad5e057 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -7,7 +7,6 @@ import android.content.ServiceConnection import android.net.Uri import android.os.Bundle import android.os.IBinder -import android.util.Log import android.view.Menu import android.view.MenuItem import android.view.View @@ -403,7 +402,13 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene if (!ready){ CustomAlertDialogBuilder(this, themeValue) .setTitle(R.string.warning) - .setMessage(getString(if (items[i].isDirectory){R.string.dir_overwrite_question} else {R.string.file_overwrite_question}, testDstPath)) + .setMessage(getString( + if (items[i].isDirectory) { + R.string.dir_overwrite_question + } else { + R.string.file_overwrite_question + }, testDstPath + )) .setPositiveButton(R.string.yes) {_, _ -> items[i].dstPath = testDstPath items[i].overwriteConfirmed = true @@ -588,7 +593,7 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene } protected open fun closeVolumeOnDestroy() { - if (!encryptedVolume.isClosed()){ + if (!encryptedVolume.isClosed()) { encryptedVolume.close() } RestrictedFileProvider.wipeAll(this) //additional security @@ -617,7 +622,7 @@ open class BaseExplorerActivity : BaseActivity(), ExplorerElementAdapter.Listene if (isCreating){ isCreating = false } else { - if (encryptedVolume.isClosed()){ + if (encryptedVolume.isClosed()) { finish() } else { isStartingActivity = false diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt index ef0fc29..4175798 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -115,7 +115,7 @@ class ExplorerActivity : BaseExplorerActivity() { private val pickImportDirectory = registerForActivityResult(ActivityResultContracts.OpenDocumentTree()) { rootUri -> rootUri?.let { val tree = DocumentFile.fromTreeUri(this, it)!! //non-null after Lollipop - val operation = OperationFile(PathUtils.pathJoin(tree.name!!, currentDirectoryPath), Stat.S_IFDIR) + val operation = OperationFile(PathUtils.pathJoin(currentDirectoryPath, tree.name!!), Stat.S_IFDIR) checkPathOverwrite(arrayListOf(operation), currentDirectoryPath) { checkedOperation -> checkedOperation?.let { lifecycleScope.launch { @@ -170,7 +170,7 @@ class ExplorerActivity : BaseExplorerActivity() { setContentView(binding.root) binding.fab.setOnClickListener { if (currentItemAction != ItemsActions.NONE){ - //openDialogCreateFolder() + openDialogCreateFolder() } else { val adapter = IconTextDialogAdapter(this) adapter.items = listOf( diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt index 2dfce6f..b71c18c 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/ImageViewer.kt @@ -275,7 +275,7 @@ class ImageViewer: FileViewerActivity() { Bitmap.CompressFormat.JPEG }, 100, outputStream) == true ){ - if (encryptedVolume.importFile(ByteArrayInputStream(outputStream.toByteArray()), filePath)){ + if (encryptedVolume.importFile(ByteArrayInputStream(outputStream.toByteArray()), filePath)) { Toast.makeText(this, R.string.image_saved_successfully, Toast.LENGTH_SHORT).show() callback() } else { diff --git a/app/src/main/java/sushi/hardcore/droidfs/filesystems/EncryptedVolume.kt b/app/src/main/java/sushi/hardcore/droidfs/filesystems/EncryptedVolume.kt index 0ea6465..9895fef 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/filesystems/EncryptedVolume.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/filesystems/EncryptedVolume.kt @@ -81,7 +81,7 @@ abstract class EncryptedVolume: Parcelable { var offset: Long = 0 val ioBuffer = ByteArray(ConstValues.IO_BUFF_SIZE) var length: Int - while (read(fileHandle, ioBuffer, offset).also { length = it } > 0){ + while (read(fileHandle, ioBuffer, offset).also { length = it } > 0) { os.write(ioBuffer, 0, length) offset += length.toLong() } @@ -105,7 +105,7 @@ abstract class EncryptedVolume: Parcelable { fun exportFile(context: Context, src_path: String, output_path: Uri): Boolean { val os = context.contentResolver.openOutputStream(output_path) - if (os != null){ + if (os != null) { return exportFile(src_path, os) } return false @@ -137,7 +137,7 @@ abstract class EncryptedVolume: Parcelable { fun importFile(context: Context, src_uri: Uri, dst_path: String): Boolean { val inputStream = context.contentResolver.openInputStream(src_uri) - if (inputStream != null){ + if (inputStream != null) { return importFile(inputStream, dst_path) } return false diff --git a/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt b/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt index edabb98..c272167 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/util/PathUtils.kt @@ -6,7 +6,6 @@ import android.net.Uri import android.os.storage.StorageManager import android.provider.DocumentsContract import android.provider.OpenableColumns -import android.util.Log import androidx.activity.result.ActivityResultLauncher import androidx.core.content.ContextCompat import sushi.hardcore.droidfs.R @@ -18,20 +17,18 @@ import kotlin.math.max import kotlin.math.pow object PathUtils { + const val SEPARATOR = '/' + fun getParentPath(path: String): String { - return if (path.endsWith("/")) { - val a = path.substring(0, max(1, path.length - 1)) - if (a.count { it == '/' } == 1) { - "/" - } else { - a.substring(0, a.lastIndexOf("/")) - } + val strippedPath = if (path.endsWith(SEPARATOR)) { + path.substring(0, max(1, path.length - 1)) } else { - if (path.count { it == '/' } <= 1) { - "/" - } else { - path.substring(0, path.lastIndexOf("/")) - } + path + } + return if (strippedPath.count { it == SEPARATOR } <= 1) { + SEPARATOR.toString() + } else { + strippedPath.substring(0, strippedPath.lastIndexOf(SEPARATOR)) } } @@ -39,17 +36,17 @@ object PathUtils { val result = StringBuilder() for (element in strings) { if (element.isNotEmpty()) { - result.append(element) - if (!element.endsWith("/")) { - result.append("/") + if (!element.startsWith(SEPARATOR) && result.last() != SEPARATOR) { + result.append(SEPARATOR) } + result.append(element) } } - return result.substring(0, result.length - 1) + return result.toString() } fun getRelativePath(parentPath: String, childPath: String): String { - return childPath.substring(parentPath.length + if (parentPath.endsWith("/")) { + return childPath.substring(parentPath.length + if (parentPath.endsWith(SEPARATOR)) { 0 } else { 1 @@ -75,7 +72,7 @@ object PathUtils { if (result == null) { result = uri.path result?.let { - val cut = it.lastIndexOf('/') + val cut = it.lastIndexOf(SEPARATOR) if (cut != -1) { result = it.substring(cut + 1) } diff --git a/app/src/main/java/sushi/hardcore/droidfs/util/WidgetUtil.kt b/app/src/main/java/sushi/hardcore/droidfs/util/WidgetUtil.kt index ea37e01..910ff58 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/util/WidgetUtil.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/util/WidgetUtil.kt @@ -6,7 +6,7 @@ import java.nio.charset.StandardCharsets import java.util.* object WidgetUtil { - fun editTextContentEncode(editText: EditText): ByteArray { + fun encodeEditTextContent(editText: EditText): ByteArray { val charArray = CharArray(editText.text.length) editText.text.getChars(0, editText.text.length, charArray, 0) val byteArray = StandardCharsets.UTF_8.encode( diff --git a/app/src/main/native/gocryptfs_jni.c b/app/src/main/native/gocryptfs_jni.c index 0dc3113..e5dfd3e 100644 --- a/app/src/main/native/gocryptfs_jni.c +++ b/app/src/main/native/gocryptfs_jni.c @@ -257,7 +257,7 @@ Java_sushi_hardcore_droidfs_filesystems_GocryptfsVolume_native_1list_1dir(JNIEnv elementList = (*env)->NewObject(env, arrayList, arrayListInit, elements.r2); unsigned int c = 0; - for (unsigned int i=0; i