]> git.proxmox.com Git - pxar.git/blame - src/util.rs
make ReadAt trait more correct
[pxar.git] / src / util.rs
CommitLineData
6cd4f635
WB
1#![allow(dead_code)]
2
3use std::future::Future;
4use std::io;
5use std::pin::Pin;
6use std::task::{Context, Poll};
7
e72062a9
WB
8pub const MAX_READ_BUF_LEN: usize = 4 * 1024 * 1024;
9
10pub fn scale_read_buffer(buffer: &mut Vec<u8>, target: usize) {
11 let target = target.min(MAX_READ_BUF_LEN);
12
13 if buffer.len() >= target {
14 buffer.truncate(target);
15 return;
16 }
17
18 buffer.reserve(target - buffer.len());
19 unsafe {
20 buffer.set_len(target);
21 }
22}
23
6cd4f635
WB
24// from /usr/include/linux/magic.h
25// and from casync util.h
26#[rustfmt::skip]
27#[allow(clippy::unreadable_literal)]
28mod consts {
29 pub const BINFMTFS_MAGIC : i64 = 0x42494e4d;
30 pub const CGROUP2_SUPER_MAGIC : i64 = 0x63677270;
31 pub const CGROUP_SUPER_MAGIC : i64 = 0x0027e0eb;
32 pub const CONFIGFS_MAGIC : i64 = 0x62656570;
33 pub const DEBUGFS_MAGIC : i64 = 0x64626720;
34 pub const DEVPTS_SUPER_MAGIC : i64 = 0x00001cd1;
35 pub const EFIVARFS_MAGIC : i64 = 0xde5e81e4;
36 pub const FUSE_CTL_SUPER_MAGIC: i64 = 0x65735543;
37 pub const HUGETLBFS_MAGIC : i64 = 0x958458f6;
38 pub const MQUEUE_MAGIC : i64 = 0x19800202;
39 pub const NFSD_MAGIC : i64 = 0x6e667364;
40 pub const PROC_SUPER_MAGIC : i64 = 0x00009fa0;
41 pub const PSTOREFS_MAGIC : i64 = 0x6165676C;
42 pub const RPCAUTH_GSSMAGIC : i64 = 0x67596969;
43 pub const SECURITYFS_MAGIC : i64 = 0x73636673;
44 pub const SELINUX_MAGIC : i64 = 0xf97cff8c;
45 pub const SMACK_MAGIC : i64 = 0x43415d53;
46 pub const RAMFS_MAGIC : i64 = 0x858458f6;
47 pub const TMPFS_MAGIC : i64 = 0x01021994;
48 pub const SYSFS_MAGIC : i64 = 0x62656572;
49 pub const MSDOS_SUPER_MAGIC : i64 = 0x00004d44;
50 pub const BTRFS_SUPER_MAGIC : i64 = 0x9123683E;
51 pub const FUSE_SUPER_MAGIC : i64 = 0x65735546;
52 pub const EXT4_SUPER_MAGIC : i64 = 0x0000EF53;
53 pub const XFS_SUPER_MAGIC : i64 = 0x58465342;
54 pub const ZFS_SUPER_MAGIC : i64 = 0x2FC12FC1;
55}
56
57pub fn is_virtual_file_system(magic: i64) -> bool {
58 match magic {
59 consts::BINFMTFS_MAGIC
60 | consts::CGROUP2_SUPER_MAGIC
61 | consts::CGROUP_SUPER_MAGIC
62 | consts::CONFIGFS_MAGIC
63 | consts::DEBUGFS_MAGIC
64 | consts::DEVPTS_SUPER_MAGIC
65 | consts::EFIVARFS_MAGIC
66 | consts::FUSE_CTL_SUPER_MAGIC
67 | consts::HUGETLBFS_MAGIC
68 | consts::MQUEUE_MAGIC
69 | consts::NFSD_MAGIC
70 | consts::PROC_SUPER_MAGIC
71 | consts::PSTOREFS_MAGIC
72 | consts::RPCAUTH_GSSMAGIC
73 | consts::SECURITYFS_MAGIC
74 | consts::SELINUX_MAGIC
75 | consts::SMACK_MAGIC
76 | consts::SYSFS_MAGIC => true,
77 _ => false,
78 }
79}
80
81/// Helper function to extract file names from binary archive.
82pub fn read_os_string(buffer: &[u8]) -> std::ffi::OsString {
83 use std::os::unix::ffi::OsStrExt;
84 std::ffi::OsStr::from_bytes(if buffer.ends_with(&[0]) {
85 &buffer[..(buffer.len() - 1)]
86 } else {
87 buffer
88 })
89 .into()
90}
91
92#[inline]
93pub fn vec_new(size: usize) -> Vec<u8> {
94 let mut data = Vec::with_capacity(size);
95 unsafe {
96 data.set_len(size);
97 }
98 data
99}
100
101pub fn io_err_other<E: std::fmt::Display>(err: E) -> io::Error {
102 io::Error::new(io::ErrorKind::Other, err.to_string())
103}
104
105pub fn poll_result_once<T, R>(mut fut: T) -> io::Result<R>
106where
107 T: Future<Output = io::Result<R>>,
108{
109 let waker = std::task::RawWaker::new(std::ptr::null(), &WAKER_VTABLE);
110 let waker = unsafe { std::task::Waker::from_raw(waker) };
111 let mut cx = Context::from_waker(&waker);
112 unsafe {
113 match Pin::new_unchecked(&mut fut).poll(&mut cx) {
114 Poll::Pending => Err(io_err_other("got Poll::Pending synchronous context")),
115 Poll::Ready(r) => r,
116 }
117 }
118}
119
120const WAKER_VTABLE: std::task::RawWakerVTable =
121 std::task::RawWakerVTable::new(forbid_clone, forbid_wake, forbid_wake, ignore_drop);
122
123unsafe fn forbid_clone(_: *const ()) -> std::task::RawWaker {
124 panic!("tried to clone waker for synchronous task");
125}
126
127unsafe fn forbid_wake(_: *const ()) {
128 panic!("tried to wake synchronous task");
129}
130
131unsafe fn ignore_drop(_: *const ()) {}
f3ac1c51 132
ec0761f9
FG
133pub const MAX_PATH_LEN:u64 = 4 * 1024;
134// let's play it safe
135pub const MAX_FILENAME_LEN:u64 = MAX_PATH_LEN;
136// name + attr
137pub const MAX_XATTR_LEN:u64 = 255 + 64*1024;
138
f3ac1c51
WB
139pub fn validate_filename(name: &[u8]) -> io::Result<()> {
140 if name.is_empty() {
141 io_bail!("illegal path found (empty)");
142 }
143
144 if name.contains(&b'/') {
145 io_bail!("illegal path found (contains slashes, this is a security concern)");
146 }
147
148 if name == b"." {
149 io_bail!("illegal path found: '.'");
150 }
151
152 if name == b".." {
153 io_bail!("illegal path found: '..'");
154 }
155
ec0761f9
FG
156 if (name.len() as u64) > MAX_FILENAME_LEN {
157 io_bail!("filename too long (> {})", MAX_FILENAME_LEN);
158 }
159
f3ac1c51
WB
160 Ok(())
161}