Skip to content

Commit c52e2f3

Browse files
yujincheng08backslashxx
authored andcommitted
Drop some legacy codes (tiann#1981)
1 parent 52582f8 commit c52e2f3

File tree

4 files changed

+35
-122
lines changed

4 files changed

+35
-122
lines changed

userspace/ksud/src/defs.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ pub const MODULE_UPDATE_TMP_DIR: &str = concatcp!(ADB_DIR, "modules_update/");
2929
pub const SYSTEM_RW_DIR: &str = concatcp!(MODULE_DIR, ".rw/");
3030

3131
pub const TEMP_DIR: &str = "/debug_ramdisk";
32-
pub const TEMP_DIR_LEGACY: &str = "/sbin";
33-
3432
pub const MODULE_WEB_DIR: &str = "webroot";
3533
pub const DISABLE_FILE_NAME: &str = "disable";
3634
pub const UPDATE_FILE_NAME: &str = "update";

userspace/ksud/src/init_event.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ pub fn on_post_data_fs() -> Result<()> {
191191
}
192192

193193
// mount temp dir
194-
if let Err(e) = mount::mount_tmpfs(utils::get_tmp_path()) {
194+
if let Err(e) = mount::mount_tmpfs(defs::TEMP_DIR) {
195195
warn!("do temp dir mount failed: {}", e);
196196
}
197197

userspace/ksud/src/mount.rs

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,18 @@ pub fn mount_ext4(source: impl AsRef<Path>, target: impl AsRef<Path>) -> Result<
6363
.attach(source)
6464
.with_context(|| "Failed to attach loop")?;
6565
let lo = new_loopback.path().ok_or(anyhow!("no loop"))?;
66-
if let Result::Ok(fs) = fsopen("ext4", FsOpenFlags::FSOPEN_CLOEXEC) {
67-
let fs = fs.as_fd();
68-
fsconfig_set_string(fs, "source", lo)?;
69-
fsconfig_create(fs)?;
70-
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
71-
move_mount(
72-
mount.as_fd(),
73-
"",
74-
CWD,
75-
target.as_ref(),
76-
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
77-
)?;
78-
} else {
79-
mount(lo, target.as_ref(), "ext4", MountFlags::empty(), "")?;
80-
}
66+
let fs = fsopen("ext4", FsOpenFlags::FSOPEN_CLOEXEC)?;
67+
let fs = fs.as_fd();
68+
fsconfig_set_string(fs, "source", lo)?;
69+
fsconfig_create(fs)?;
70+
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
71+
move_mount(
72+
mount.as_fd(),
73+
"",
74+
CWD,
75+
target.as_ref(),
76+
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
77+
)?;
8178
Ok(())
8279
}
8380

@@ -157,27 +154,18 @@ pub fn mount_overlayfs(
157154
#[cfg(any(target_os = "linux", target_os = "android"))]
158155
pub fn mount_tmpfs(dest: impl AsRef<Path>) -> Result<()> {
159156
info!("mount tmpfs on {}", dest.as_ref().display());
160-
if let Result::Ok(fs) = fsopen("tmpfs", FsOpenFlags::FSOPEN_CLOEXEC) {
161-
let fs = fs.as_fd();
162-
fsconfig_set_string(fs, "source", KSU_OVERLAY_SOURCE)?;
163-
fsconfig_create(fs)?;
164-
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
165-
move_mount(
166-
mount.as_fd(),
167-
"",
168-
CWD,
169-
dest.as_ref(),
170-
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
171-
)?;
172-
} else {
173-
mount(
174-
KSU_OVERLAY_SOURCE,
175-
dest.as_ref(),
176-
"tmpfs",
177-
MountFlags::empty(),
178-
"",
179-
)?;
180-
}
157+
let fs = fsopen("tmpfs", FsOpenFlags::FSOPEN_CLOEXEC)?;
158+
let fs = fs.as_fd();
159+
fsconfig_set_string(fs, "source", KSU_OVERLAY_SOURCE)?;
160+
fsconfig_create(fs)?;
161+
let mount = fsmount(fs, FsMountFlags::FSMOUNT_CLOEXEC, MountAttrFlags::empty())?;
162+
move_mount(
163+
mount.as_fd(),
164+
"",
165+
CWD,
166+
dest.as_ref(),
167+
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
168+
)?;
181169
Ok(())
182170
}
183171

@@ -188,29 +176,20 @@ pub fn bind_mount(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
188176
from.as_ref().display(),
189177
to.as_ref().display()
190178
);
191-
if let Result::Ok(tree) = open_tree(
179+
let tree = open_tree(
192180
CWD,
193181
from.as_ref(),
194182
OpenTreeFlags::OPEN_TREE_CLOEXEC
195183
| OpenTreeFlags::OPEN_TREE_CLONE
196184
| OpenTreeFlags::AT_RECURSIVE,
197-
) {
198-
move_mount(
199-
tree.as_fd(),
200-
"",
201-
CWD,
202-
to.as_ref(),
203-
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
204-
)?;
205-
} else {
206-
mount(
207-
from.as_ref(),
208-
to.as_ref(),
209-
"",
210-
MountFlags::BIND | MountFlags::REC,
211-
"",
212-
)?;
213-
}
185+
)?;
186+
move_mount(
187+
tree.as_fd(),
188+
"",
189+
CWD,
190+
to.as_ref(),
191+
MoveMountFlags::MOVE_MOUNT_F_EMPTY_PATH,
192+
)?;
214193
Ok(())
215194
}
216195

userspace/ksud/src/utils.rs

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
use anyhow::{bail, Context, Error, Ok, Result};
22
use std::{
3-
fs::{self, create_dir_all, remove_file, write, File, OpenOptions},
3+
fs::{create_dir_all, remove_file, write, File, OpenOptions},
44
io::{
55
ErrorKind::{AlreadyExists, NotFound},
66
Write,
77
},
88
path::Path,
99
process::Command,
10-
sync::OnceLock,
1110
};
1211

1312
use crate::{assets, boot_patch, defs, ksucalls, module, restorecon};
14-
use std::fs::metadata;
1513
#[allow(unused_imports)]
1614
use std::fs::{set_permissions, Permissions};
1715
#[cfg(unix)]
@@ -189,68 +187,6 @@ pub fn has_magisk() -> bool {
189187
which::which("magisk").is_ok()
190188
}
191189

192-
fn is_ok_empty(dir: &str) -> bool {
193-
use std::result::Result::Ok;
194-
195-
match fs::read_dir(dir) {
196-
Ok(mut entries) => entries.next().is_none(),
197-
Err(_) => false,
198-
}
199-
}
200-
201-
fn find_temp_path() -> String {
202-
use std::result::Result::Ok;
203-
204-
if is_ok_empty(defs::TEMP_DIR) {
205-
return defs::TEMP_DIR.to_string();
206-
}
207-
208-
// Try to create a random directory in /dev/
209-
let r = tempfile::tempdir_in("/dev/");
210-
match r {
211-
Ok(tmp_dir) => {
212-
if let Some(path) = tmp_dir.into_path().to_str() {
213-
return path.to_string();
214-
}
215-
}
216-
Err(_e) => {}
217-
}
218-
219-
let dirs = [
220-
defs::TEMP_DIR,
221-
"/patch_hw",
222-
"/oem",
223-
"/root",
224-
defs::TEMP_DIR_LEGACY,
225-
];
226-
227-
// find empty directory
228-
for dir in dirs {
229-
if is_ok_empty(dir) {
230-
return dir.to_string();
231-
}
232-
}
233-
234-
// Fallback to non-empty directory
235-
for dir in dirs {
236-
if metadata(dir).is_ok() {
237-
return dir.to_string();
238-
}
239-
}
240-
241-
"".to_string()
242-
}
243-
244-
pub fn get_tmp_path() -> &'static str {
245-
static CHOSEN_TMP_PATH: OnceLock<String> = OnceLock::new();
246-
247-
CHOSEN_TMP_PATH.get_or_init(|| {
248-
let r = find_temp_path();
249-
log::info!("Chosen temp_path: {}", r);
250-
r
251-
})
252-
}
253-
254190
#[cfg(target_os = "android")]
255191
fn link_ksud_to_bin() -> Result<()> {
256192
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);

0 commit comments

Comments
 (0)