From 73de2cfae8fec0bbb5525cabd82b448f304a19d0 Mon Sep 17 00:00:00 2001 From: Ryan Gonzalez Date: Fri, 17 Apr 2026 16:01:50 -0500 Subject: [PATCH] Fix compilation errors on platforms with unsigned c_char The docs for `c_char` mention: > On modern architectures this type will always be either `i8` or `u8` It's `u8` specifically on multiple prominent architectures, such as arm64 and riscv: https://github.com/rust-lang/rust/blob/f29256dd1420dc681bf4956e3012ffe9eccdc7e7/library/core/src/ffi/primitives.rs#L42 Thus, this updates the places where `c_char` is intermixed with `i8` to exclusively rely on the former. Signed-off-by: Ryan Gonzalez --- disk/src/superblock.rs | 14 +++++++------- uapi/src/filesystem.rs | 4 ++-- uapi/src/inode.rs | 7 +++++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/disk/src/superblock.rs b/disk/src/superblock.rs index 4c1ab21..45b7dcf 100644 --- a/disk/src/superblock.rs +++ b/disk/src/superblock.rs @@ -481,7 +481,7 @@ fn parse_label(raw_label: &[std::os::raw::c_char; 256]) -> String { let bytes: Vec = raw_label .iter() .take_while(|&&c| c != 0) - .map(|&c| c.cast_unsigned()) + .map(|&c| c as u8) .collect(); String::from_utf8_lossy(&bytes).into_owned() } @@ -679,7 +679,7 @@ pub fn write_superblock_all_mirrors( #[cfg(test)] mod tests { use super::*; - use std::{io::Cursor, mem}; + use std::{io::Cursor, mem, os::raw::c_char}; // --- super_mirror_offset --- @@ -755,24 +755,24 @@ mod tests { #[test] fn parse_label_normal() { - let mut raw_label = [0i8; 256]; + let mut raw_label = [c_char::default(); 256]; for (i, &b) in b"my-volume".iter().enumerate() { - raw_label[i] = b as i8; + raw_label[i] = b as c_char; } assert_eq!(parse_label(&raw_label), "my-volume"); } #[test] fn parse_label_empty() { - let raw_label = [0i8; 256]; + let raw_label = [c_char::default(); 256]; assert_eq!(parse_label(&raw_label), ""); } #[test] fn parse_label_stops_at_nul() { - let mut raw_label = [0i8; 256]; + let mut raw_label = [c_char::default(); 256]; for (i, &b) in b"hello\0world".iter().enumerate() { - raw_label[i] = b as i8; + raw_label[i] = b as c_char; } assert_eq!(parse_label(&raw_label), "hello"); } diff --git a/uapi/src/filesystem.rs b/uapi/src/filesystem.rs index ccf8231..caab6c8 100644 --- a/uapi/src/filesystem.rs +++ b/uapi/src/filesystem.rs @@ -103,7 +103,7 @@ pub fn wait_sync(fd: BorrowedFd, transid: u64) -> nix::Result<()> { /// /// Returns `Err` if the ioctl fails. pub fn label_get(fd: BorrowedFd) -> nix::Result { - let mut buf = [0i8; BTRFS_LABEL_SIZE as usize]; + let mut buf = [c_char::default(); BTRFS_LABEL_SIZE as usize]; unsafe { btrfs_ioc_get_fslabel(fd.as_raw_fd(), &raw mut buf) }?; let cstr = unsafe { CStr::from_ptr(buf.as_ptr()) }; // CStr::to_owned() copies the bytes into a freshly allocated CString, @@ -129,7 +129,7 @@ pub fn label_set(fd: BorrowedFd, label: &CStr) -> nix::Result<()> { if bytes.len() >= BTRFS_LABEL_SIZE as usize { return Err(nix::errno::Errno::EINVAL); } - let mut buf = [0i8; BTRFS_LABEL_SIZE as usize]; + let mut buf = [c_char::default(); BTRFS_LABEL_SIZE as usize]; for (i, &b) in bytes.iter().enumerate() { buf[i] = b as c_char; } diff --git a/uapi/src/inode.rs b/uapi/src/inode.rs index 7834a58..d0b76f0 100644 --- a/uapi/src/inode.rs +++ b/uapi/src/inode.rs @@ -14,7 +14,10 @@ use crate::{ }, tree_search::{SearchFilter, tree_search}, }; -use std::os::fd::{AsRawFd, BorrowedFd}; +use std::os::{ + fd::{AsRawFd, BorrowedFd}, + raw::c_char, +}; /// Look up the tree ID (root ID) of the subvolume containing the given file or directory. /// @@ -104,7 +107,7 @@ pub fn ino_paths(fd: BorrowedFd<'_>, inum: u64) -> nix::Result> { // The path string starts at the base of val array plus the offset let val_base = container.val.as_ptr() as usize; - let path_ptr = (val_base + val_offset) as *const i8; + let path_ptr = (val_base + val_offset) as *const c_char; // Convert C string to Rust String let c_str = unsafe { std::ffi::CStr::from_ptr(path_ptr) };