Reduce more Linux specific code in base

This commit is contained in:
topjohnwu
2026-10-02 17:17:48 -07:00
parent b76d674a60
commit 0e228db61f
4 changed files with 18 additions and 31 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
APP_BUILD_SCRIPT := src/Android.mk
APP_CFLAGS := -Wall -Oz -fomit-frame-pointer
APP_CFLAGS := -Wall -Oz -fomit-frame-pointer -D_FILE_OFFSET_BITS=64
APP_CPPFLAGS := -std=c++23
APP_STL := none
APP_PLATFORM := android-23
+1 -12
View File
@@ -1,22 +1,11 @@
#include <sys/mman.h>
#include <android/log.h>
#include <linux/fs.h>
#include <base.hpp>
#include <flags.h>
using namespace std;
#ifndef __call_bypassing_fortify
#define __call_bypassing_fortify(fn) (&fn)
#endif
#ifdef __LP64__
static_assert(BLKGETSIZE64 == 0x80081272);
#else
static_assert(BLKGETSIZE64 == 0x80041272);
#endif
rust::Vec<size_t> byte_data::patch(byte_view from, byte_view to) const {
rust::Vec<size_t> v;
if (ptr == nullptr)
@@ -128,7 +117,7 @@ static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {
char buf[sz];
buf[0] = '\0';
// Fortify logs when a fatal error occurs. Do not run through fortify again
int len = std::min(__call_bypassing_fortify(vsnprintf)(buf, sz, fmt, ap), sz - 1);
int len = std::min((&vsnprintf)(buf, sz, fmt, ap), sz - 1);
log_with_rs(level, Utf8CStr(buf, len + 1));
return len;
}
+7 -18
View File
@@ -757,11 +757,6 @@ impl Drop for MappedFile {
}
}
unsafe extern "C" {
// Don't use the declaration from the libc crate as request should be u32 not i32
fn ioctl(fd: RawFd, request: u32, ...) -> i32;
}
// We mark the returned slice static because it is valid until explicitly unmapped
pub(crate) fn map_file(path: &Utf8CStr, rw: bool) -> OsResult<'_, &'static mut [u8]> {
map_file_at(AT_FDCWD, path, rw)
@@ -772,27 +767,21 @@ pub(crate) fn map_file_at<'a>(
path: &'a Utf8CStr,
rw: bool,
) -> OsResult<'a, &'static mut [u8]> {
#[cfg(target_pointer_width = "64")]
const BLKGETSIZE64: u32 = 0x80081272;
#[cfg(target_pointer_width = "32")]
const BLKGETSIZE64: u32 = 0x80041272;
let flag = if rw { OFlag::O_RDWR } else { OFlag::O_RDONLY };
let fd = nix::fcntl::openat(dirfd, path, flag | OFlag::O_CLOEXEC, Mode::empty())
.into_os_result("openat", Some(path), None)?;
let attr = fd_get_attr(fd.as_raw_fd())?;
let mut file = File::from(fd);
let attr = fd_get_attr(file.as_raw_fd())?;
let sz = if attr.is_block_device() {
let mut sz = 0_u64;
unsafe {
ioctl(fd.as_raw_fd(), BLKGETSIZE64, &mut sz).check_os_err("ioctl", Some(path), None)?;
}
sz
// Use the seek function from std::fs instead of nix so that the API is consistent
// across all Unix OS (lseek64 does not exist on macOS).
file.seek(SeekFrom::End(0))
.into_os_result("lseek", Some(path), None)?
} else {
attr.st.st_size as u64
};
map_fd(fd.as_fd(), sz as usize, rw).map_err(|e| e.set_args(Some(path), None))
map_fd(file.as_fd(), sz as usize, rw).map_err(|e| e.set_args(Some(path), None))
}
pub(crate) fn map_fd(fd: BorrowedFd, sz: usize, rw: bool) -> OsResult<'static, &'static mut [u8]> {
+9
View File
@@ -270,6 +270,15 @@ impl<T> LibcReturn for *mut T {
}
}
impl<T> LibcReturn for std::io::Result<T> {
type Value = T;
#[inline(always)]
fn check_err(self) -> nix::Result<Self::Value> {
self.map_err(|e| e.raw_os_error().map_or_else(Errno::last, Errno::from_raw))
}
}
impl<T> LibcReturn for nix::Result<T> {
type Value = T;