From 49ec2eaf49dac210a8ece650a7826607b90900a3 Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Thu, 20 Apr 2023 16:38:15 +0200 Subject: [PATCH] Stop always opening files in write mode --- .../java/sushi/hardcore/droidfs/CameraActivity.kt | 13 ++++++++++--- .../hardcore/droidfs/explorers/ExplorerActivity.kt | 2 +- .../droidfs/file_operations/FileOperationService.kt | 4 ++-- .../file_viewers/EncryptedVolumeDataSource.kt | 2 +- .../hardcore/droidfs/file_viewers/TextEditor.kt | 2 +- .../hardcore/droidfs/filesystems/CryfsVolume.kt | 6 +++++- .../hardcore/droidfs/filesystems/EncryptedVolume.kt | 9 +++++---- .../hardcore/droidfs/filesystems/GocryptfsVolume.kt | 6 +++++- gradle/wrapper/gradle-wrapper.properties | 3 ++- 9 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt b/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt index 7a38daf..96518d4 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/CameraActivity.kt @@ -481,8 +481,7 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener { CustomAlertDialogBuilder(this@CameraActivity, theme) .setTitle(R.string.error) .setMessage(R.string.picture_save_failed) - .setCancelable(false) - .setPositiveButton(R.string.ok) { _, _ -> finish() } + .setPositiveButton(R.string.ok, null) .show() } } @@ -502,6 +501,15 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener { videoRecording?.stop() } else if (!isWaitingForTimer) { val path = getOutputPath(true) + val fileHandle = encryptedVolume.openFileWriteMode(path) + if (fileHandle == -1L) { + CustomAlertDialogBuilder(this, theme) + .setTitle(R.string.error) + .setMessage(R.string.file_creation_failed) + .setPositiveButton(R.string.ok, null) + .show() + return + } startTimerThen { var withAudio = true if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { @@ -513,7 +521,6 @@ class CameraActivity : BaseActivity(), SensorOrientationListener.Listener { this, MuxerOutputOptions( FFmpegMuxer(object : SeekableWriter { - private val fileHandle = encryptedVolume.openFile(path) private var offset = 0L override fun close() { 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 22801a4..50c1c26 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -241,7 +241,7 @@ class ExplorerActivity : BaseExplorerActivity() { Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show() } else { val filePath = PathUtils.pathJoin(currentDirectoryPath, fileName) - val handleID = encryptedVolume.openFile(filePath) + val handleID = encryptedVolume.openFileWriteMode(filePath) if (handleID == -1L) { CustomAlertDialogBuilder(this, theme) .setTitle(R.string.error) diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt b/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt index 26c8364..e5e3413 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_operations/FileOperationService.kt @@ -125,9 +125,9 @@ class FileOperationService : Service() { private fun copyFile(srcPath: String, dstPath: String, remoteEncryptedVolume: EncryptedVolume = encryptedVolume): Boolean { var success = true - val srcFileHandle = remoteEncryptedVolume.openFile(srcPath) + val srcFileHandle = remoteEncryptedVolume.openFileReadMode(srcPath) if (srcFileHandle != -1L) { - val dstFileHandle = encryptedVolume.openFile(dstPath) + val dstFileHandle = encryptedVolume.openFileWriteMode(dstPath) if (dstFileHandle != -1L) { var offset: Long = 0 val ioBuffer = ByteArray(Constants.IO_BUFF_SIZE) diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/EncryptedVolumeDataSource.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/EncryptedVolumeDataSource.kt index dd21c2f..f89751a 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/EncryptedVolumeDataSource.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/EncryptedVolumeDataSource.kt @@ -15,7 +15,7 @@ class EncryptedVolumeDataSource(private val encryptedVolume: EncryptedVolume, pr private var bytesRemaining: Long = -1 override fun open(dataSpec: DataSpec): Long { - fileHandle = encryptedVolume.openFile(filePath) + fileHandle = encryptedVolume.openFileReadMode(filePath) fileOffset = dataSpec.position val fileSize = encryptedVolume.getAttr(filePath)!!.size bytesRemaining = if (dataSpec.length == C.LENGTH_UNSET.toLong()) { diff --git a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt index 7e56dd7..51bb111 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/file_viewers/TextEditor.kt @@ -67,7 +67,7 @@ class TextEditor: FileViewerActivity() { private fun save(): Boolean{ var success = false val content = editor.text.toString().toByteArray() - val fileHandle = encryptedVolume.openFile(filePath) + val fileHandle = encryptedVolume.openFileWriteMode(filePath) if (fileHandle != -1L) { var offset: Long = 0 while (offset < content.size && encryptedVolume.write(fileHandle, offset, content, offset, content.size.toLong()).also { offset += it } > 0) {} diff --git a/app/src/main/java/sushi/hardcore/droidfs/filesystems/CryfsVolume.kt b/app/src/main/java/sushi/hardcore/droidfs/filesystems/CryfsVolume.kt index 1fb7e14..2889be2 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/filesystems/CryfsVolume.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/filesystems/CryfsVolume.kt @@ -94,7 +94,11 @@ class CryfsVolume(private val fusePtr: Long): EncryptedVolume() { writeLong(fusePtr) } - override fun openFile(path: String): Long { + override fun openFileReadMode(path: String): Long { + return nativeOpen(fusePtr, path, 0) + } + + override fun openFileWriteMode(path: String): Long { val fileHandle = nativeOpen(fusePtr, path, 0) return if (fileHandle == -1L) { nativeCreate(fusePtr, path, 0) 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 aa3a533..864d5e2 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/filesystems/EncryptedVolume.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/filesystems/EncryptedVolume.kt @@ -73,7 +73,8 @@ abstract class EncryptedVolume: Parcelable { override fun describeContents() = 0 - abstract fun openFile(path: String): Long + abstract fun openFileReadMode(path: String): Long + abstract fun openFileWriteMode(path: String): Long abstract fun read(fileHandle: Long, fileOffset: Long, buffer: ByteArray, dstOffset: Long, length: Long): Int abstract fun write(fileHandle: Long, fileOffset: Long, buffer: ByteArray, srcOffset: Long, length: Long): Int abstract fun closeFile(fileHandle: Long): Boolean @@ -106,7 +107,7 @@ abstract class EncryptedVolume: Parcelable { fun exportFile(src_path: String, os: OutputStream): Boolean { var success = false - val srcfileHandle = openFile(src_path) + val srcfileHandle = openFileReadMode(src_path) if (srcfileHandle != -1L) { success = exportFile(srcfileHandle, os) closeFile(srcfileHandle) @@ -127,7 +128,7 @@ abstract class EncryptedVolume: Parcelable { } fun importFile(inputStream: InputStream, dst_path: String): Boolean { - val dstfileHandle = openFile(dst_path) + val dstfileHandle = openFileWriteMode(dst_path) if (dstfileHandle != -1L) { var success = true var offset: Long = 0 @@ -169,7 +170,7 @@ abstract class EncryptedVolume: Parcelable { } try { val fileBuff = ByteArray(fileSize.toInt()) - val fileHandle = openFile(fullPath) + val fileHandle = openFileReadMode(fullPath) if (fileHandle == -1L) { Pair(null, 3) } else { diff --git a/app/src/main/java/sushi/hardcore/droidfs/filesystems/GocryptfsVolume.kt b/app/src/main/java/sushi/hardcore/droidfs/filesystems/GocryptfsVolume.kt index 763e37f..8cba157 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/filesystems/GocryptfsVolume.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/filesystems/GocryptfsVolume.kt @@ -91,7 +91,11 @@ class GocryptfsVolume(private val sessionID: Int): EncryptedVolume() { constructor(parcel: Parcel) : this(parcel.readInt()) - override fun openFile(path: String): Long { + override fun openFileReadMode(path: String): Long { + return native_open_read_mode(sessionID, path).toLong() + } + + override fun openFileWriteMode(path: String): Long { return native_open_write_mode(sessionID, path, 384).toLong() // 0600 } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index bc83a3a..9452bde 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,7 @@ #Wed Feb 01 20:48:39 UTC 2023 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionSha256Sum=7ba68c54029790ab444b39d7e293d3236b2632631fb5f2e012bb28b4ff669e4b zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME