From cf0753e74a423c9311f1c80f0c78013c71572eaf Mon Sep 17 00:00:00 2001 From: Hardcore Sushi Date: Sun, 19 Jul 2020 21:46:29 +0200 Subject: [PATCH] "New file" feature --- app/build.gradle | 2 +- app/libgocryptfs/main.go | 4 +- .../rewrites/syscallcompat/sys_common.go | 8 +++- ...rActivityRO.kt => BaseExplorerActivity.kt} | 22 ++++----- .../droidfs/explorers/ExplorerActivity.kt | 48 ++++++++++++++++++- .../droidfs/explorers/ExplorerActivityDrop.kt | 2 +- .../droidfs/explorers/ExplorerActivityPick.kt | 2 +- app/src/main/res/drawable/cursor.xml | 5 -- app/src/main/res/layout/activity_explorer.xml | 12 ++++- ...orer_ro.xml => activity_explorer_base.xml} | 2 +- app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 3 ++ 12 files changed, 85 insertions(+), 26 deletions(-) rename app/src/main/java/sushi/hardcore/droidfs/explorers/{ExplorerActivityRO.kt => BaseExplorerActivity.kt} (95%) delete mode 100644 app/src/main/res/drawable/cursor.xml rename app/src/main/res/layout/{activity_explorer_ro.xml => activity_explorer_base.xml} (93%) diff --git a/app/build.gradle b/app/build.gradle index c912b58..afca7ae 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -11,7 +11,7 @@ android { minSdkVersion 21 targetSdkVersion 28 versionCode 1 - versionName "1.0" + versionName "1.0.1" ndk { abiFilters 'x86_64', 'armeabi-v7a', 'arm64-v8a' diff --git a/app/libgocryptfs/main.go b/app/libgocryptfs/main.go index 8c26f11..f1f7fa5 100644 --- a/app/libgocryptfs/main.go +++ b/app/libgocryptfs/main.go @@ -48,7 +48,6 @@ type SessionVars struct { fileIDs map[int][]byte } -//var fdebug *os.File var sessions map[int]SessionVars func err_to_bool(e error) bool { @@ -519,7 +518,6 @@ func init_new_session(root_cipher_dir string, masterkey []byte) int { c++ } if sessions == nil { - //fdebug, _ = os.OpenFile("/storage/emulated/0/gologs.txt", os.O_WRONLY | os.O_TRUNC | os.O_CREATE, os.FileMode(file_mode)) sessions = make(map[int]SessionVars) } sessions[sessionID] = new_session; @@ -797,7 +795,7 @@ func gcf_open_write_mode(sessionID int, path string) int { return -1 } // Create content - fd, err = syscallcompat.Openat(dirfd, cName, newFlags|syscall.O_CREAT, file_mode) + fd, err = syscallcompat.Openat(dirfd, cName, newFlags|syscall.O_CREAT, file_mode) if err != nil { nametransform.DeleteLongNameAt(dirfd, cName) } diff --git a/app/libgocryptfs/rewrites/syscallcompat/sys_common.go b/app/libgocryptfs/rewrites/syscallcompat/sys_common.go index 6ab2455..6311510 100644 --- a/app/libgocryptfs/rewrites/syscallcompat/sys_common.go +++ b/app/libgocryptfs/rewrites/syscallcompat/sys_common.go @@ -44,7 +44,7 @@ func Faccessat(dirfd int, path string, mode uint32) error { // Openat wraps the Openat syscall. func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { - if flags&syscall.O_CREAT != 0 { + /*if flags&syscall.O_CREAT != 0 { // O_CREAT should be used with O_EXCL. O_NOFOLLOW has no effect with O_EXCL. if flags&syscall.O_EXCL == 0 { flags |= syscall.O_EXCL @@ -54,6 +54,12 @@ func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) if flags&syscall.O_NOFOLLOW == 0 { flags |= syscall.O_NOFOLLOW } + }*/ + if flags&syscall.O_CREAT == 0 { + // If O_CREAT is not used, we should use O_NOFOLLOW + if flags&syscall.O_NOFOLLOW == 0 { + flags |= syscall.O_NOFOLLOW + } } return unix.Openat(dirfd, path, flags, mode) } diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityRO.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt similarity index 95% rename from app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityRO.kt rename to app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt index af76bcd..85b61a8 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityRO.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/BaseExplorerActivity.kt @@ -13,7 +13,7 @@ import android.widget.EditText import android.widget.ListView import android.widget.Toast import com.github.clans.fab.FloatingActionMenu -import kotlinx.android.synthetic.main.activity_explorer_ro.* +import kotlinx.android.synthetic.main.activity_explorer_base.* import kotlinx.android.synthetic.main.explorer_info_bar.* import kotlinx.android.synthetic.main.toolbar.* import sushi.hardcore.droidfs.ColoredActivity @@ -36,7 +36,7 @@ import sushi.hardcore.droidfs.util.GocryptfsVolume import sushi.hardcore.droidfs.widgets.ColoredAlertDialog import java.util.* -open class ExplorerActivityRO : ColoredActivity() { +open class BaseExplorerActivity : ColoredActivity() { private lateinit var shared_prefs_editor: SharedPreferences.Editor private lateinit var sort_modes_entries: Array private lateinit var sort_modes_values: Array @@ -81,7 +81,7 @@ open class ExplorerActivityRO : ColoredActivity() { } protected open fun init() { - setContentView(R.layout.activity_explorer_ro) + setContentView(R.layout.activity_explorer_base) } private fun startFileViewer(cls: Class<*>, filePath: String){ @@ -232,21 +232,21 @@ open class ExplorerActivityRO : ColoredActivity() { fun onClickAddFolder(view: View?) { findViewById(R.id.fam_explorer).close(true) - val dialog_edit_text_view = layoutInflater.inflate(R.layout.dialog_edit_text, null) - val dialog_edit_text = dialog_edit_text_view.findViewById(R.id.dialog_edit_text) + val dialogEditTextView = layoutInflater.inflate(R.layout.dialog_edit_text, null) + val dialogEditText = dialogEditTextView.findViewById(R.id.dialog_edit_text) val dialog = ColoredAlertDialog(this) - .setView(dialog_edit_text_view) + .setView(dialogEditTextView) .setTitle(R.string.enter_folder_name) .setPositiveButton(R.string.ok) { _, _ -> - val folder_name = dialog_edit_text.text.toString() - createFolder(folder_name) + val folderName = dialogEditText.text.toString() + createFolder(folderName) } .setNegativeButton(R.string.cancel, null) .create() - dialog_edit_text.setOnEditorActionListener { _, _, _ -> - val folder_name = dialog_edit_text.text.toString() + dialogEditText.setOnEditorActionListener { _, _, _ -> + val folderName = dialogEditText.text.toString() dialog.dismiss() - createFolder(folder_name) + createFolder(folderName) true } dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) 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 ad26fc5..0df9ca1 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivity.kt @@ -6,6 +6,10 @@ import android.net.Uri import android.view.Menu import android.view.MenuItem import android.view.View +import android.view.WindowManager +import android.widget.EditText +import android.widget.Toast +import com.github.clans.fab.FloatingActionMenu import kotlinx.android.synthetic.main.activity_explorer.* import sushi.hardcore.droidfs.OpenActivity import sushi.hardcore.droidfs.R @@ -17,7 +21,7 @@ import sushi.hardcore.droidfs.widgets.ColoredAlertDialog import java.io.File import java.util.* -class ExplorerActivity : ExplorerActivityRO() { +class ExplorerActivity : BaseExplorerActivity() { private val PICK_DIRECTORY_REQUEST_CODE = 1 private val PICK_FILES_REQUEST_CODE = 2 private val PICK_OTHER_VOLUME_ITEMS_REQUEST_CODE = 3 @@ -29,6 +33,48 @@ class ExplorerActivity : ExplorerActivityRO() { usf_share = sharedPrefs.getBoolean("usf_share", false) } + private fun createNewFile(fileName: String){ + if (fileName.isEmpty()) { + Toast.makeText(this, R.string.error_filename_empty, Toast.LENGTH_SHORT).show() + } else { + val handleID = gocryptfsVolume.open_write_mode(FilesUtils.path_join(current_path, fileName)) + if (handleID == -1) { + ColoredAlertDialog(this) + .setTitle(R.string.error) + .setMessage(getString(R.string.file_creation_failed)) + .setPositiveButton(R.string.ok, null) + .show() + } else { + gocryptfsVolume.close_file(handleID) + setCurrentPath(current_path) + invalidateOptionsMenu() + } + } + } + + fun onClickCreateFile(view: View) { + findViewById(R.id.fam_explorer).close(true) + val dialogEditTextView = layoutInflater.inflate(R.layout.dialog_edit_text, null) + val dialogEditText = dialogEditTextView.findViewById(R.id.dialog_edit_text) + val dialog = ColoredAlertDialog(this) + .setView(dialogEditTextView) + .setTitle(getString(R.string.enter_file_name)) + .setPositiveButton(R.string.ok) { _, _ -> + val fileName = dialogEditText.text.toString() + createNewFile(fileName) + } + .setNegativeButton(R.string.cancel, null) + .create() + dialogEditText.setOnEditorActionListener { _, _, _ -> + val fileName = dialogEditText.text.toString() + dialog.dismiss() + createNewFile(fileName) + true + } + dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) + dialog.show() + } + fun onClickAddFile(view: View?) { fam_explorer.close(true) val i = Intent(Intent.ACTION_OPEN_DOCUMENT) diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityDrop.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityDrop.kt index 7e7aef9..0888073 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityDrop.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityDrop.kt @@ -8,7 +8,7 @@ import sushi.hardcore.droidfs.R import sushi.hardcore.droidfs.util.FilesUtils import sushi.hardcore.droidfs.widgets.ColoredAlertDialog -class ExplorerActivityDrop : ExplorerActivityRO() { +class ExplorerActivityDrop : BaseExplorerActivity() { override fun init() { setContentView(R.layout.activity_explorer_drop) } diff --git a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt index c661abe..ff6120a 100644 --- a/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt +++ b/app/src/main/java/sushi/hardcore/droidfs/explorers/ExplorerActivityPick.kt @@ -9,7 +9,7 @@ import sushi.hardcore.droidfs.provider.TemporaryFileProvider import sushi.hardcore.droidfs.util.FilesUtils import java.util.* -class ExplorerActivityPick : ExplorerActivityRO() { +class ExplorerActivityPick : BaseExplorerActivity() { private var result_intent = Intent() override fun init() { super.init() diff --git a/app/src/main/res/drawable/cursor.xml b/app/src/main/res/drawable/cursor.xml deleted file mode 100644 index fb130e7..0000000 --- a/app/src/main/res/drawable/cursor.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_explorer.xml b/app/src/main/res/layout/activity_explorer.xml index 163c3d2..81550ca 100644 --- a/app/src/main/res/layout/activity_explorer.xml +++ b/app/src/main/res/layout/activity_explorer.xml @@ -62,6 +62,15 @@ android:src="@drawable/icon_add" android:onClick="onClickAddFile"/> + + + android:onClick="onClickAddFolder" + tools:ignore="OnClick" /> diff --git a/app/src/main/res/layout/activity_explorer_ro.xml b/app/src/main/res/layout/activity_explorer_base.xml similarity index 93% rename from app/src/main/res/layout/activity_explorer_ro.xml rename to app/src/main/res/layout/activity_explorer_base.xml index 58497b4..1b287b3 100644 --- a/app/src/main/res/layout/activity_explorer_ro.xml +++ b/app/src/main/res/layout/activity_explorer_base.xml @@ -4,7 +4,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" - tools:context="sushi.hardcore.droidfs.explorers.ExplorerActivityRO"> + tools:context="sushi.hardcore.droidfs.explorers.BaseExplorerActivity"> diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 81727db..5ac0994 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -6,6 +6,7 @@ @color/colorPrimaryDark @color/colorPrimaryDark @color/colorAccent + @color/themeColor #333333 #000000 #ffffff diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 2011010..d242e2b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -118,4 +118,7 @@ Discard Word Wrap OutOfMemoryError: This file is too large to be loaded in memory. + New File + File name: + Failed to create the file.