e2p_sys/
lib.rs

1/*
2MIT License
3
4Copyright (c) 2019-2023 Michael Lass
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#![warn(rust_2018_idioms)]
26#![allow(non_upper_case_globals)]
27#![allow(non_camel_case_types)]
28#![allow(non_snake_case)]
29
30//! Low-level Rust bindings for libe2p from e2fsprogs
31//!
32//! The bindings need to be generated using bindgen while building this
33//! crate because libe2p has no stable API and available file flags or
34//! file system features differ between versions. This also means that
35//! publicly availale documentation on this crate might not be accurate
36//! on your system.
37//!
38//! For building this crate, you need to have e2fsprogs-dev (also called
39//! e2fslibs-dev or libext2fs-dev) and libclang installed.
40
41include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
42include!(concat!(env!("OUT_DIR"), "/constants.rs"));
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use std::env;
48    use std::ffi::CString;
49    use std::fs::{remove_file, File};
50    use std::io::Read;
51    use std::mem::transmute;
52    use std::os::raw::c_char;
53
54    #[test]
55    fn set_and_read_flags() {
56        let mut path = env::current_dir().unwrap();
57        path.push("e2p-sys_testfile_Gahlu1ka");
58        let path_cstr = CString::new(path.to_str().unwrap()).unwrap();
59        let path_ptr: *const c_char = path_cstr.as_ptr();
60        let _f = File::create(&path).unwrap();
61
62        unsafe {
63            fsetflags(path_ptr, EXT2_NOATIME_FL as u64);
64        }
65
66        let mut readback: u64 = 0;
67        let readback_ptr: *mut u64 = &mut readback;
68
69        unsafe {
70            fgetflags(path_ptr, readback_ptr);
71        }
72
73        remove_file(path).unwrap();
74        assert_eq!(readback, EXT2_NOATIME_FL as u64)
75    }
76
77    #[test]
78    fn read_superblock() {
79        let mut path = env::current_dir().unwrap();
80        path.push("test_data");
81        path.push("sb.raw");
82        let mut buf = [0; 1024];
83        let mut f = File::open(path).unwrap();
84        f.read_exact(&mut buf).unwrap();
85
86        let sb: ext2_super_block;
87        unsafe {
88            sb = transmute::<[u8; 1024], ext2_super_block>(buf);
89        }
90
91        // Randomly selected values from the superblock
92        assert_eq!(sb.s_inodes_count, 256);
93        assert_eq!(sb.s_blocks_per_group, 8192);
94        assert_eq!(sb.s_magic, 0xEF53);
95    }
96
97    #[test]
98    fn read_constant() {
99        assert!(CONSTANTS.iter().any(|&s| s == "EXT2_IMMUTABLE_FL"));
100        assert!(!CONSTANTS.iter().any(|&s| s == "NO_SUCH_CONST"));
101        assert_eq!(EXT2_IMMUTABLE_FL, 0x10);
102    }
103}