Skip to content

Commit 07b75a5

Browse files
committed
rewrite root folders handling, do not use "awk" command
1 parent 9b957a7 commit 07b75a5

File tree

6 files changed

+88
-40
lines changed

6 files changed

+88
-40
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ext {
4242
}
4343

4444
dependencies {
45-
implementation 'com.simplemobiletools:commons:3.8.7'
45+
implementation 'com.simplemobiletools:commons:3.8.8'
4646

4747
implementation files('../libs/RootTools.jar')
4848

app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ class MainActivity : SimpleActivity() {
284284
Thread {
285285
config.isRootAvailable = RootTools.isRootAvailable()
286286
if (config.isRootAvailable && config.enableRootAccess) {
287-
RootHelpers().askRootIFNeeded(this) {
287+
RootHelpers().askRootIfNeeded(this) {
288288
config.enableRootAccess = it
289289
}
290290
}

app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SettingsActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class SettingsActivity : SimpleActivity() {
119119
settings_enable_root_access.isChecked = config.enableRootAccess
120120
settings_enable_root_access_holder.setOnClickListener {
121121
if (!config.enableRootAccess) {
122-
RootHelpers().askRootIFNeeded(this) {
122+
RootHelpers().askRootIfNeeded(this) {
123123
toggleRootAccess(it)
124124
}
125125
} else {

app/src/main/kotlin/com/simplemobiletools/filemanager/fragments/ItemsFragment.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
122122
scrollStates[currentPath] = getScrollState()
123123
currentPath = realPath
124124
showHidden = context!!.config.shouldShowHidden
125-
getItems(currentPath) {
126-
if (!isAdded) {
125+
getItems(currentPath) { originalPath, fileDirItems ->
126+
if (currentPath != originalPath || !isAdded) {
127127
return@getItems
128128
}
129129

130130
FileDirItem.sorting = context!!.config.getFolderSorting(currentPath)
131-
it.sort()
131+
fileDirItems.sort()
132132
activity!!.runOnUiThread {
133-
addItems(it)
133+
addItems(fileDirItems)
134134
}
135135
}
136136
}
@@ -170,7 +170,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
170170

171171
private fun getRecyclerLayoutManager() = (mView.items_list.layoutManager as LinearLayoutManager)
172172

173-
private fun getItems(path: String, callback: (items: ArrayList<FileDirItem>) -> Unit) {
173+
private fun getItems(path: String, callback: (originalPath: String, items: ArrayList<FileDirItem>) -> Unit) {
174174
skipItemUpdating = false
175175
Thread {
176176
if (activity?.isActivityDestroyed() == false) {
@@ -183,7 +183,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
183183
}.start()
184184
}
185185

186-
private fun getRegularItemsOf(path: String, callback: (items: ArrayList<FileDirItem>) -> Unit) {
186+
private fun getRegularItemsOf(path: String, callback: (originalPath: String, items: ArrayList<FileDirItem>) -> Unit) {
187187
val items = ArrayList<FileDirItem>()
188188
val files = File(path).listFiles()?.filterNotNull()
189189
if (files != null) {
@@ -200,7 +200,7 @@ class ItemsFragment : Fragment(), ItemOperationsListener, Breadcrumbs.Breadcrumb
200200
items.add(fileDirItem)
201201
}
202202
}
203-
callback(items)
203+
callback(path, items)
204204
}
205205

206206
private fun getChildrenCount(file: File): Int {

app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/Config.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,4 @@ class Config(context: Context) : BaseConfig(context) {
7575
var enableRootAccess: Boolean
7676
get() = prefs.getBoolean(ENABLE_ROOT_ACCESS, false)
7777
set(enableRootAccess) = prefs.edit().putBoolean(ENABLE_ROOT_ACCESS, enableRootAccess).apply()
78-
79-
var lsHasHardLinksColumn: Boolean
80-
get() = prefs.getBoolean(LS_HAS_HARD_LINKS_COLUMN, false)
81-
set(lsHasHardLinksColumn) = prefs.edit().putBoolean(LS_HAS_HARD_LINKS_COLUMN, lsHasHardLinksColumn).apply()
8278
}

app/src/main/kotlin/com/simplemobiletools/filemanager/helpers/RootHelpers.kt

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
package com.simplemobiletools.filemanager.helpers
22

3-
import android.text.TextUtils
43
import com.simplemobiletools.commons.extensions.areDigitsOnly
54
import com.simplemobiletools.commons.extensions.showErrorToast
65
import com.simplemobiletools.commons.models.FileDirItem
76
import com.simplemobiletools.filemanager.activities.SimpleActivity
87
import com.simplemobiletools.filemanager.extensions.config
98
import com.stericson.RootShell.execution.Command
109
import com.stericson.RootTools.RootTools
10+
import java.io.File
1111
import java.util.*
1212

1313
class RootHelpers {
14-
fun askRootIFNeeded(activity: SimpleActivity, callback: (success: Boolean) -> Unit) {
15-
val command = object : Command(0, "ls -la | awk '{ print $2 }'") {
14+
fun askRootIfNeeded(activity: SimpleActivity, callback: (success: Boolean) -> Unit) {
15+
val command = object : Command(0, "ls -lA") {
1616
override fun commandOutput(id: Int, line: String) {
17-
activity.config.lsHasHardLinksColumn = line.areDigitsOnly()
1817
callback(true)
1918
super.commandOutput(id, line)
2019
}
@@ -28,43 +27,96 @@ class RootHelpers {
2827
}
2928
}
3029

31-
fun getFiles(activity: SimpleActivity, path: String, callback: (fileDirItems: ArrayList<FileDirItem>) -> Unit) {
30+
fun getFiles(activity: SimpleActivity, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
3231
val files = ArrayList<FileDirItem>()
33-
val showHidden = activity.config.shouldShowHidden
34-
val sizeColumnIndex = if (activity.config.lsHasHardLinksColumn) 5 else 4
32+
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
33+
val cmd = "ls $hiddenArgument$path"
3534

36-
val cmd = "ls -la $path | awk '{ system(\"echo \"\$1\" \"\$$sizeColumnIndex\" `find ${path.trimEnd('/')}/\"\$NF\" -mindepth 1 -maxdepth 1 | wc -l` \"\$NF\" \")}'"
3735
val command = object : Command(0, cmd) {
3836
override fun commandOutput(id: Int, line: String) {
39-
val parts = line.split(" ")
40-
if (parts.size >= 4) {
41-
val permissions = parts[0].trim()
42-
val isDirectory = permissions.startsWith("d")
43-
val isFile = permissions.startsWith("-")
44-
val size = if (isFile) parts[1].trim() else "0"
45-
val childrenCnt = if (isFile) "0" else parts[2].trim()
46-
val filename = TextUtils.join(" ", parts.subList(3, parts.size)).trimStart('/')
47-
48-
if ((!showHidden && filename.startsWith(".")) || (!isDirectory && !isFile) || !size.areDigitsOnly() || !childrenCnt.areDigitsOnly()) {
49-
super.commandOutput(id, line)
50-
return
51-
}
37+
val file = File(path, line)
38+
val isDirectory = file.isDirectory
39+
val fileDirItem = FileDirItem(file.absolutePath, line, isDirectory, 0, 0)
40+
files.add(fileDirItem)
41+
super.commandOutput(id, line)
42+
}
43+
44+
override fun commandCompleted(id: Int, exitcode: Int) {
45+
getChildrenCount(activity, files, path, callback)
46+
super.commandCompleted(id, exitcode)
47+
}
48+
}
49+
50+
runCommand(activity, command)
51+
}
52+
53+
private fun getChildrenCount(activity: SimpleActivity, files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
54+
val hiddenArgument = if (activity.config.shouldShowHidden) "-A " else ""
55+
var cmd = ""
56+
files.forEach {
57+
cmd += if (it.isDirectory) {
58+
"ls $hiddenArgument${it.path} |wc -l;"
59+
} else {
60+
"echo 0;"
61+
}
62+
}
63+
cmd = cmd.trimEnd(';') + " | cat"
64+
65+
val lines = ArrayList<String>()
66+
val command = object : Command(0, cmd) {
67+
override fun commandOutput(id: Int, line: String) {
68+
lines.add(line)
69+
super.commandOutput(id, line)
70+
}
5271

53-
val fileSize = size.toLong()
54-
val filePath = "${path.trimEnd('/')}/$filename"
55-
val fileDirItem = FileDirItem(filePath, filename, isDirectory, childrenCnt.toInt(), fileSize)
56-
files.add(fileDirItem)
72+
override fun commandCompleted(id: Int, exitcode: Int) {
73+
files.forEachIndexed { index, fileDirItem ->
74+
val childrenCount = lines[index]
75+
if (childrenCount.areDigitsOnly()) {
76+
fileDirItem.children = childrenCount.toInt()
77+
}
5778
}
79+
getFileSizes(activity, files, path, callback)
80+
super.commandCompleted(id, exitcode)
81+
}
82+
}
83+
84+
runCommand(activity, command)
85+
}
5886

87+
private fun getFileSizes(activity: SimpleActivity, files: ArrayList<FileDirItem>, path: String, callback: (originalPath: String, fileDirItems: ArrayList<FileDirItem>) -> Unit) {
88+
var cmd = ""
89+
files.forEach {
90+
cmd += if (it.isDirectory) {
91+
"echo 0;"
92+
} else {
93+
"stat -c %s ${it.path};"
94+
}
95+
}
96+
97+
val lines = ArrayList<String>()
98+
val command = object : Command(0, cmd) {
99+
override fun commandOutput(id: Int, line: String) {
100+
lines.add(line)
59101
super.commandOutput(id, line)
60102
}
61103

62104
override fun commandCompleted(id: Int, exitcode: Int) {
63-
callback(files)
105+
files.forEachIndexed { index, fileDirItem ->
106+
val childrenCount = lines[index]
107+
if (childrenCount.areDigitsOnly()) {
108+
fileDirItem.size = childrenCount.toLong()
109+
}
110+
}
111+
callback(path, files)
64112
super.commandCompleted(id, exitcode)
65113
}
66114
}
67115

116+
runCommand(activity, command)
117+
}
118+
119+
private fun runCommand(activity: SimpleActivity, command: Command) {
68120
try {
69121
RootTools.getShell(true).add(command)
70122
} catch (e: Exception) {

0 commit comments

Comments
 (0)