From 11054263c13fd3f4626534fa1bfd78ecc4fa0dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=89=E6=AD=A3=E8=B6=85?= Date: Wed, 20 May 2026 12:00:32 +0800 Subject: [PATCH] fix(utils): map common Linux filesystem magic values (#3023) --- crates/utils/src/os/fs_type.rs | 53 ++++++++++++++++++++++++++++++++++ crates/utils/src/os/linux.rs | 31 +------------------- crates/utils/src/os/mod.rs | 2 ++ 3 files changed, 56 insertions(+), 30 deletions(-) create mode 100644 crates/utils/src/os/fs_type.rs diff --git a/crates/utils/src/os/fs_type.rs b/crates/utils/src/os/fs_type.rs new file mode 100644 index 000000000..24149ef24 --- /dev/null +++ b/crates/utils/src/os/fs_type.rs @@ -0,0 +1,53 @@ +// Copyright 2024 RustFS Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Returns the filesystem type of the underlying mounted filesystem. +/// +/// TODO: Verify these less common magic values against stable Linux headers or +/// filesystem documentation before adding them: +/// +/// "137d" => "EXT", +/// "4244" => "HFS", +/// "5346544e" => "NTFS", +/// "61756673" => "AUFS", +/// "ef51" => "EXT2OLD", +/// "2fc12fc1" => "zfs", +/// "ff534d42" => "cifs", +/// "53464846" => "wslfs", +pub(crate) fn get_fs_type(fs_type: u64) -> &'static str { + // Magic numbers for various filesystems. + match fs_type { + 0x01021994 => "TMPFS", + 0x4d44 => "MSDOS", + 0x6969 => "NFS", + 0xEF53 => "EXT4", + 0xf15f => "ecryptfs", + 0x794c7630 => "overlayfs", + 0x52654973 => "REISERFS", + 0x58465342 => "XFS", + 0x9123683E => "BTRFS", + _ => "UNKNOWN", + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn map_common_linux_filesystem_magic_numbers() { + assert_eq!(get_fs_type(0x58465342), "XFS"); + assert_eq!(get_fs_type(0x9123683E), "BTRFS"); + } +} diff --git a/crates/utils/src/os/linux.rs b/crates/utils/src/os/linux.rs index 1c346e48e..c5f23c670 100644 --- a/crates/utils/src/os/linux.rs +++ b/crates/utils/src/os/linux.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::os::{DiskInfo, IOStats}; +use super::{DiskInfo, IOStats, fs_type::get_fs_type}; use rustix::fs::statfs; use std::collections::BTreeSet; use std::fs; @@ -86,35 +86,6 @@ pub fn get_info(p: impl AsRef) -> std::io::Result { }) } -/// Returns the filesystem type of the underlying mounted filesystem -/// -/// TODO The following mapping could not find the corresponding constant in `nix`: -/// -/// "137d" => "EXT", -/// "4244" => "HFS", -/// "5346544e" => "NTFS", -/// "61756673" => "AUFS", -/// "ef51" => "EXT2OLD", -/// "2fc12fc1" => "zfs", -/// "ff534d42" => "cifs", -/// "53464846" => "wslfs", -fn get_fs_type(fs_type: u64) -> &'static str { - // Magic numbers for various filesystems - match fs_type { - 0x01021994 => "TMPFS", - 0x4d44 => "MSDOS", - 0x6969 => "NFS", - 0xEF53 => "EXT4", - 0xf15f => "ecryptfs", - 0x794c7630 => "overlayfs", - 0x52654973 => "REISERFS", - // Additional common ones can be added here: - // 0x58465342 => "XFS", - // 0x9123683E => "BTRFS", - _ => "UNKNOWN", - } -} - pub fn same_disk(disk1: &str, disk2: &str) -> std::io::Result { let stat1 = rustix::fs::stat(disk1)?; let stat2 = rustix::fs::stat(disk2)?; diff --git a/crates/utils/src/os/mod.rs b/crates/utils/src/os/mod.rs index 5226318d8..9f7577f53 100644 --- a/crates/utils/src/os/mod.rs +++ b/crates/utils/src/os/mod.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +mod fs_type; + #[cfg(target_os = "linux")] mod linux; #[cfg(all(unix, not(target_os = "linux")))]