#mount #linux #syscalls #high-level #sys #umount

sys-mount

sys mount & umount2 系统调用的高层 FFI 绑定

13 个稳定版本

3.0.1 2024 年 2 月 5 日
2.1.1 2024 年 1 月 5 日
2.1.0 2023 年 8 月 4 日
2.0.2 2023 年 1 月 31 日
1.1.0 2018 年 11 月 1 日

#29 in 文件系统

Download history 42275/week @ 2024-04-08 43373/week @ 2024-04-15 46705/week @ 2024-04-22 30825/week @ 2024-04-29 39076/week @ 2024-05-06 34646/week @ 2024-05-13 32860/week @ 2024-05-20 27074/week @ 2024-05-27 28226/week @ 2024-06-03 28531/week @ 2024-06-10 32240/week @ 2024-06-17 34237/week @ 2024-06-24 26312/week @ 2024-07-01 32107/week @ 2024-07-08 33745/week @ 2024-07-15 30563/week @ 2024-07-22

124,455 每月下载量
用于 12 个 Crates (11 直接)

MIT/Apache

35KB
534

sys-mount

Rust Compatibility License

为 Rust 提供对 mountumount2 系统调用的高层 FFI 绑定。

示例

挂载

这是使用此 API 编写 mount 命令的方式。

extern crate clap;
extern crate sys_mount;

use clap::{App, Arg};
use sys_mount::{Mount, MountFlags, SupportedFilesystems};
use std::process::exit;

fn main() {
    let matches = App::new("mount")
        .arg(Arg::with_name("source").required(true))
        .arg(Arg::with_name("directory").required(true))
        .get_matches();

    let src = matches.value_of("source").unwrap();
    let dir = matches.value_of("directory").unwrap();

    // Fetch a listed of supported file systems on this system. This will be used
    // as the fstype to `Mount::new`, as the `Auto` mount parameter.
    let supported = match SupportedFilesystems::new() {
        Ok(supported) => supported,
        Err(why) => {
            eprintln!("failed to get supported file systems: {}", why);
            exit(1);
        }
    };

    // The source block will be mounted to the target directory, and the fstype is likely
    // one of the supported file systems.
    let result = Mount::builder()
        .fstype(FilesystemType::from(&supported))
        .mount(src, dir);

    match result {
        Ok(mount) => {
            println!("mounted {} ({}) to {}", src, mount.get_fstype(), dir);
        }
        Err(why) => {
            eprintln!("failed to mount {} to {}: {}", src, dir, why);
            exit(1);
        }
    }
}

卸载

这是实现 umount 命令的方式。

extern crate clap;
extern crate sys_mount;

use clap::{App, Arg};
use sys_mount::{unmount, UnmountFlags};
use std::process::exit;

fn main() {
    let matches = App::new("umount")
        .arg(Arg::with_name("lazy")
            .short("l")
            .long("lazy"))
        .arg(Arg::with_name("source").required(true))
        .get_matches();

    let src = matches.value_of("source").unwrap();

    let flags = if matches.is_present("lazy") {
        UnmountFlags::DETACH
    } else {
        UnmountFlags::empty()
    };

    match unmount(src, flags) {
        Ok(()) => (),
        Err(why) => {
            eprintln!("failed to unmount {}: {}", src, why);
            exit(1);
        }
    }
}

依赖项

~0.7–8MB
~66K SLoC