1 //! Tools and utilities
3 //! This is a collection of small and useful tools.
5 use std
::collections
::HashMap
;
6 use std
::hash
::BuildHasher
;
7 use std
::fs
::{File, OpenOptions}
;
8 use std
::io
::ErrorKind
;
10 use std
::os
::unix
::io
::{AsRawFd, FromRawFd, IntoRawFd, RawFd}
;
12 use std
::time
::Duration
;
15 use serde_json
::Value
;
16 use openssl
::hash
::{hash, DigestBytes, MessageDigest}
;
17 use percent_encoding
::AsciiSet
;
19 use proxmox
::tools
::vec
;
32 mod wrapped_reader_stream
;
33 pub use wrapped_reader_stream
::*;
35 mod std_channel_writer
;
36 pub use std_channel_writer
::*;
41 pub use process_locker
::*;
44 pub use file_logger
::*;
47 pub use broadcast_future
::*;
49 /// The `BufferedRead` trait provides a single function
50 /// `buffered_read`. It returns a reference to an internal buffer. The
51 /// purpose of this traid is to avoid unnecessary data copies.
52 pub trait BufferedRead
{
53 /// This functions tries to fill the internal buffers, then
54 /// returns a reference to the available data. It returns an empty
55 /// buffer if `offset` points to the end of the file.
56 fn buffered_read(&mut self, offset
: u64) -> Result
<&[u8], Error
>;
59 /// Directly map a type into a binary buffer. This is mostly useful
60 /// for reading structured data from a byte stream (file). You need to
61 /// make sure that the buffer location does not change, so please
62 /// avoid vec resize while you use such map.
64 /// This function panics if the buffer is not large enough.
65 pub fn map_struct
<T
>(buffer
: &[u8]) -> Result
<&T
, Error
> {
66 if buffer
.len() < ::std
::mem
::size_of
::<T
>() {
67 bail
!("unable to map struct - buffer too small");
69 Ok(unsafe { &*(buffer.as_ptr() as *const T) }
)
72 /// Directly map a type into a mutable binary buffer. This is mostly
73 /// useful for writing structured data into a byte stream (file). You
74 /// need to make sure that the buffer location does not change, so
75 /// please avoid vec resize while you use such map.
77 /// This function panics if the buffer is not large enough.
78 pub fn map_struct_mut
<T
>(buffer
: &mut [u8]) -> Result
<&mut T
, Error
> {
79 if buffer
.len() < ::std
::mem
::size_of
::<T
>() {
80 bail
!("unable to map struct - buffer too small");
82 Ok(unsafe { &mut *(buffer.as_ptr() as *mut T) }
)
85 /// Create a file lock using fntl. This function allows you to specify
86 /// a timeout if you want to avoid infinite blocking.
87 pub fn lock_file
<F
: AsRawFd
>(
90 timeout
: Option
<Duration
>,
91 ) -> Result
<(), Error
> {
92 let lockarg
= if exclusive
{
93 nix
::fcntl
::FlockArg
::LockExclusive
95 nix
::fcntl
::FlockArg
::LockShared
98 let timeout
= match timeout
{
100 nix
::fcntl
::flock(file
.as_raw_fd(), lockarg
)?
;
106 // unblock the timeout signal temporarily
107 let _sigblock_guard
= timer
::unblock_timeout_signal();
109 // setup a timeout timer
110 let mut timer
= timer
::Timer
::create(
111 timer
::Clock
::Realtime
,
112 timer
::TimerEvent
::ThisThreadSignal(timer
::SIGTIMEOUT
),
116 timer
::TimerSpec
::new()
117 .value(Some(timeout
))
118 .interval(Some(Duration
::from_millis(10))),
121 nix
::fcntl
::flock(file
.as_raw_fd(), lockarg
)?
;
125 /// Open or create a lock file (append mode). Then try to
126 /// aquire a lock using `lock_file()`.
127 pub fn open_file_locked
<P
: AsRef
<Path
>>(path
: P
, timeout
: Duration
) -> Result
<File
, Error
> {
128 let path
= path
.as_ref();
129 let mut file
= match OpenOptions
::new().create(true).append(true).open(path
) {
131 Err(err
) => bail
!("Unable to open lock {:?} - {}", path
, err
),
133 match lock_file(&mut file
, true, Some(timeout
)) {
135 Err(err
) => bail
!("Unable to aquire lock {:?} - {}", path
, err
),
139 /// Split a file into equal sized chunks. The last chunk may be
140 /// smaller. Note: We cannot implement an `Iterator`, because iterators
141 /// cannot return a borrowed buffer ref (we want zero-copy)
142 pub fn file_chunker
<C
, R
>(mut file
: R
, chunk_size
: usize, mut chunk_cb
: C
) -> Result
<(), Error
>
144 C
: FnMut(usize, &[u8]) -> Result
<bool
, Error
>,
147 const READ_BUFFER_SIZE
: usize = 4 * 1024 * 1024; // 4M
149 if chunk_size
> READ_BUFFER_SIZE
{
150 bail
!("chunk size too large!");
153 let mut buf
= vec
::undefined(READ_BUFFER_SIZE
);
156 let mut file_pos
= 0;
159 let mut tmp
= &mut buf
[..];
160 // try to read large portions, at least chunk_size
161 while pos
< chunk_size
{
162 match file
.read(tmp
) {
169 if pos
> chunk_size
{
174 Err(ref e
) if e
.kind() == ErrorKind
::Interrupted
=> { /* try again */ }
175 Err(e
) => bail
!("read chunk failed - {}", e
.to_string()),
179 while start
+ chunk_size
<= pos
{
180 if !(chunk_cb
)(file_pos
, &buf
[start
..start
+ chunk_size
])?
{
183 file_pos
+= chunk_size
;
188 (chunk_cb
)(file_pos
, &buf
[start
..pos
])?
;
189 //file_pos += pos - start;
193 let rest
= pos
- start
;
195 let ptr
= buf
.as_mut_ptr();
197 std
::ptr
::copy_nonoverlapping(ptr
.add(start
), ptr
, rest
);
209 pub fn json_object_to_query(data
: Value
) -> Result
<String
, Error
> {
210 let mut query
= url
::form_urlencoded
::Serializer
::new(String
::new());
212 let object
= data
.as_object().ok_or_else(|| {
213 format_err
!("json_object_to_query: got wrong data type (expected object).")
216 for (key
, value
) in object
{
219 query
.append_pair(key
, &b
.to_string());
221 Value
::Number(n
) => {
222 query
.append_pair(key
, &n
.to_string());
224 Value
::String(s
) => {
225 query
.append_pair(key
, &s
);
227 Value
::Array(arr
) => {
231 query
.append_pair(key
, &b
.to_string());
233 Value
::Number(n
) => {
234 query
.append_pair(key
, &n
.to_string());
236 Value
::String(s
) => {
237 query
.append_pair(key
, &s
);
240 "json_object_to_query: unable to handle complex array data types."
245 _
=> bail
!("json_object_to_query: unable to handle complex data types."),
252 pub fn required_string_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
253 match param
[name
].as_str() {
255 None
=> bail
!("missing parameter '{}'", name
),
259 pub fn required_string_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<&'a
str, Error
> {
260 match param
[name
].as_str() {
262 None
=> bail
!("missing property '{}'", name
),
266 pub fn required_integer_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<i64, Error
> {
267 match param
[name
].as_i64() {
269 None
=> bail
!("missing parameter '{}'", name
),
273 pub fn required_integer_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<i64, Error
> {
274 match param
[name
].as_i64() {
276 None
=> bail
!("missing property '{}'", name
),
280 pub fn required_array_param
<'a
>(param
: &'a Value
, name
: &str) -> Result
<Vec
<Value
>, Error
> {
281 match param
[name
].as_array() {
282 Some(s
) => Ok(s
.to_vec()),
283 None
=> bail
!("missing parameter '{}'", name
),
287 pub fn required_array_property
<'a
>(param
: &'a Value
, name
: &str) -> Result
<Vec
<Value
>, Error
> {
288 match param
[name
].as_array() {
289 Some(s
) => Ok(s
.to_vec()),
290 None
=> bail
!("missing property '{}'", name
),
294 pub fn complete_file_name
<S
: BuildHasher
>(arg
: &str, _param
: &HashMap
<String
, String
, S
>) -> Vec
<String
> {
295 let mut result
= vec
![];
297 use nix
::fcntl
::AtFlags
;
298 use nix
::fcntl
::OFlag
;
299 use nix
::sys
::stat
::Mode
;
301 let mut dirname
= std
::path
::PathBuf
::from(if arg
.is_empty() { "./" }
else { arg }
);
303 let is_dir
= match nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &dirname
, AtFlags
::empty()) {
304 Ok(stat
) => (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
,
309 if let Some(parent
) = dirname
.parent() {
310 dirname
= parent
.to_owned();
315 match nix
::dir
::Dir
::openat(libc
::AT_FDCWD
, &dirname
, OFlag
::O_DIRECTORY
, Mode
::empty()) {
317 Err(_
) => return result
,
320 for item
in dir
.iter() {
321 if let Ok(entry
) = item
{
322 if let Ok(name
) = entry
.file_name().to_str() {
323 if name
== "." || name
== ".." {
326 let mut newpath
= dirname
.clone();
330 nix
::sys
::stat
::fstatat(libc
::AT_FDCWD
, &newpath
, AtFlags
::empty())
332 if (stat
.st_mode
& libc
::S_IFMT
) == libc
::S_IFDIR
{
334 if let Some(newpath
) = newpath
.to_str() {
335 result
.push(newpath
.to_owned());
340 if let Some(newpath
) = newpath
.to_str() {
341 result
.push(newpath
.to_owned());
350 /// Scan directory for matching file names.
352 /// Scan through all directory entries and call `callback()` function
353 /// if the entry name matches the regular expression. This function
354 /// used unix `openat()`, so you can pass absolute or relative file
355 /// names. This function simply skips non-UTF8 encoded names.
356 pub fn scandir
<P
, F
>(
359 regex
: ®ex
::Regex
,
361 ) -> Result
<(), Error
>
363 F
: FnMut(RawFd
, &str, nix
::dir
::Type
) -> Result
<(), Error
>,
364 P
: ?Sized
+ nix
::NixPath
,
366 for entry
in self::fs
::scan_subdir(dirfd
, path
, regex
)?
{
368 let file_type
= match entry
.file_type() {
369 Some(file_type
) => file_type
,
370 None
=> bail
!("unable to detect file type"),
375 unsafe { entry.file_name_utf8_unchecked() }
,
382 /// Shortcut for md5 sums.
383 pub fn md5sum(data
: &[u8]) -> Result
<DigestBytes
, Error
> {
384 hash(MessageDigest
::md5(), data
).map_err(Error
::from
)
387 pub fn get_hardware_address() -> Result
<String
, Error
> {
388 static FILENAME
: &str = "/etc/ssh/ssh_host_rsa_key.pub";
390 let contents
= proxmox
::tools
::fs
::file_get_contents(FILENAME
)?
;
391 let digest
= md5sum(&contents
)?
;
393 Ok(proxmox
::tools
::bin_to_hex(&digest
))
396 pub fn assert_if_modified(digest1
: &str, digest2
: &str) -> Result
<(), Error
> {
397 if digest1
!= digest2
{
398 bail
!("detected modified configuration - file changed by other user? Try again.");
403 /// Extract authentication cookie from cookie header.
404 /// We assume cookie_name is already url encoded.
405 pub fn extract_auth_cookie(cookie
: &str, cookie_name
: &str) -> Option
<String
> {
406 for pair
in cookie
.split('
;'
) {
407 let (name
, value
) = match pair
.find('
='
) {
408 Some(i
) => (pair
[..i
].trim(), pair
[(i
+ 1)..].trim()),
409 None
=> return None
, // Cookie format error
412 if name
== cookie_name
{
413 use percent_encoding
::percent_decode
;
414 if let Ok(value
) = percent_decode(value
.as_bytes()).decode_utf8() {
415 return Some(value
.into());
417 return None
; // Cookie format error
425 pub fn join(data
: &Vec
<String
>, sep
: char) -> String
{
426 let mut list
= String
::new();
429 if !list
.is_empty() {
438 /// Detect modified configuration files
440 /// This function fails with a resonable error message if checksums do not match.
441 pub fn detect_modified_configuration_file(digest1
: &[u8;32], digest2
: &[u8;32]) -> Result
<(), Error
> {
442 if digest1
!= digest2
{
443 bail
!("detected modified configuration - file changed by other user? Try again.");
448 /// normalize uri path
450 /// Do not allow ".", "..", or hidden files ".XXXX"
451 /// Also remove empty path components
452 pub fn normalize_uri_path(path
: &str) -> Result
<(String
, Vec
<&str>), Error
> {
453 let items
= path
.split('
/'
);
455 let mut path
= String
::new();
456 let mut components
= vec
![];
462 if name
.starts_with('
.'
) {
463 bail
!("Path contains illegal components.");
467 components
.push(name
);
470 Ok((path
, components
))
473 pub fn fd_change_cloexec(fd
: RawFd
, on
: bool
) -> Result
<(), Error
> {
474 use nix
::fcntl
::{fcntl, FdFlag, F_GETFD, F_SETFD}
;
475 let mut flags
= FdFlag
::from_bits(fcntl(fd
, F_GETFD
)?
)
476 .ok_or_else(|| format_err
!("unhandled file flags"))?
; // nix crate is stupid this way...
477 flags
.set(FdFlag
::FD_CLOEXEC
, on
);
478 fcntl(fd
, F_SETFD(flags
))?
;
482 static mut SHUTDOWN_REQUESTED
: bool
= false;
484 pub fn request_shutdown() {
486 SHUTDOWN_REQUESTED
= true;
488 crate::server
::server_shutdown();
492 pub fn shutdown_requested() -> bool
{
493 unsafe { SHUTDOWN_REQUESTED }
496 pub fn fail_on_shutdown() -> Result
<(), Error
> {
497 if shutdown_requested() {
498 bail
!("Server shutdown requested - aborting task");
503 /// Guard a raw file descriptor with a drop handler. This is mostly useful when access to an owned
504 /// `RawFd` is required without the corresponding handler object (such as when only the file
505 /// descriptor number is required in a closure which may be dropped instead of being executed).
506 pub struct Fd(pub RawFd
);
518 impl AsRawFd
for Fd
{
519 fn as_raw_fd(&self) -> RawFd
{
524 impl IntoRawFd
for Fd
{
525 fn into_raw_fd(mut self) -> RawFd
{
532 impl FromRawFd
for Fd
{
533 unsafe fn from_raw_fd(fd
: RawFd
) -> Self {
538 // wrap nix::unistd::pipe2 + O_CLOEXEC into something returning guarded file descriptors
539 pub fn pipe() -> Result
<(Fd
, Fd
), Error
> {
540 let (pin
, pout
) = nix
::unistd
::pipe2(nix
::fcntl
::OFlag
::O_CLOEXEC
)?
;
541 Ok((Fd(pin
), Fd(pout
)))
544 /// An easy way to convert types to Any
546 /// Mostly useful to downcast trait objects (see RpcEnvironment).
548 fn as_any(&self) -> &dyn Any
;
551 impl<T
: Any
> AsAny
for T
{
552 fn as_any(&self) -> &dyn Any
{
557 /// This used to be: `SIMPLE_ENCODE_SET` plus space, `"`, `#`, `<`, `>`, backtick, `?`, `{`, `}`
558 pub const DEFAULT_ENCODE_SET
: &AsciiSet
= &percent_encoding
::CONTROLS
// 0..1f and 7e
559 // The SIMPLE_ENCODE_SET adds space and anything >= 0x7e (7e itself is already included above)
562 // the DEFAULT_ENCODE_SET added: