]> git.proxmox.com Git - proxmox-backup.git/blobdiff - src/tools.rs
src/bin/pxar.rs: allow to pass paths and match patterns as args to pxar extract
[proxmox-backup.git] / src / tools.rs
index 06b7eb18e9bd5d2b6c5d10c3e5727b0992b350e1..3c1c711dabb1b01a566ab97ee5617b9ff39d7bf9 100644 (file)
@@ -4,6 +4,7 @@
 use failure::*;
 use nix::unistd;
 use nix::sys::stat;
+use nix::{convert_ioctl_res, request_code_read, ioc};
 
 use lazy_static::lazy_static;
 
@@ -13,12 +14,17 @@ use std::path::Path;
 use std::io::Read;
 use std::io::ErrorKind;
 use std::time::Duration;
+use std::any::Any;
 
-use std::os::unix::io::RawFd;
-use std::os::unix::io::AsRawFd;
+use std::os::unix::io::{AsRawFd, FromRawFd, IntoRawFd, RawFd};
+
+use std::collections::HashMap;
 
 use serde_json::Value;
 
+use proxmox::tools::vec;
+
+pub mod async_mutex;
 pub mod timer;
 pub mod wrapped_reader_stream;
 #[macro_use]
@@ -26,11 +32,50 @@ pub mod common_regex;
 pub mod ticket;
 pub mod borrow;
 pub mod fs;
+pub mod tty;
+pub mod signalfd;
+pub mod daemon;
+pub mod procfs;
+pub mod acl;
+pub mod xattr;
+pub mod futures;
+
+mod process_locker;
+pub use process_locker::*;
+
+#[macro_use]
+mod file_logger;
+pub use file_logger::*;
+
+mod broadcast_future;
+pub use broadcast_future::*;
+
+/// Macro to write error-handling blocks (like perl eval {})
+///
+/// #### Example:
+/// ```
+/// # #[macro_use] extern crate proxmox_backup;
+/// # use failure::*;
+/// # let some_condition = false;
+/// let result = try_block!({
+///     if (some_condition) {
+///         bail!("some error");
+///     }
+///     Ok(())
+/// })
+/// .map_err(|e| format_err!("my try block returned an error - {}", e));
+/// ```
+
+#[macro_export]
+macro_rules! try_block {
+    { $($token:tt)* } => {{ (|| -> Result<_,_> { $($token)* })() }}
+}
+
 
-/// The `BufferedReader` trait provides a single function
+/// The `BufferedRead` trait provides a single function
 /// `buffered_read`. It returns a reference to an internal buffer. The
 /// purpose of this traid is to avoid unnecessary data copies.
-pub trait BufferedReader {
+pub trait BufferedRead {
     /// This functions tries to fill the internal buffers, then
     /// returns a reference to the available data. It returns an empty
     /// buffer if `offset` points to the end of the file.
@@ -63,34 +108,76 @@ pub fn map_struct_mut<T>(buffer: &mut [u8]) -> Result<&mut T, Error> {
     Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) })
 }
 
-pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, std::io::Error> {
+pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, Error> {
 
     let path = path.as_ref();
 
-    let file = std::fs::File::open(path)?;
+    try_block!({
+        let file = std::fs::File::open(path)?;
 
-    use std::io::{BufRead, BufReader};
+        use std::io::{BufRead, BufReader};
 
-    let mut reader = BufReader::new(file);
+        let mut reader = BufReader::new(file);
 
-    let mut line = String::new();
+        let mut line = String::new();
 
-    let _ = reader.read_line(&mut line)?;
+        let _ = reader.read_line(&mut line)?;
 
-    Ok(line)
+        Ok(line)
+    }).map_err(|err: Error| format_err!("unable to read {:?} - {}", path, err))
 }
 
-pub fn file_get_contents<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, std::io::Error> {
-    std::fs::read(path)
+pub fn file_get_contents<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, Error> {
+
+    let path = path.as_ref();
+
+    try_block!({
+        std::fs::read(path)
+    }).map_err(|err| format_err!("unable to read {:?} - {}", path, err))
 }
 
-/// Atomically write a file. We first create a temporary file, which
-/// is then renamed.
+pub fn file_get_json<P: AsRef<Path>>(path: P, default: Option<Value>) -> Result<Value, Error> {
+
+    let path = path.as_ref();
+
+    let raw = match std::fs::read(path) {
+        Ok(v) => v,
+        Err(err) => {
+            if err.kind() ==  std::io::ErrorKind::NotFound {
+                if let Some(v) = default {
+                    return Ok(v);
+                }
+            }
+            bail!("unable to read json {:?} - {}", path, err);
+        }
+    };
+
+    try_block!({
+        let data = String::from_utf8(raw)?;
+        let json = serde_json::from_str(&data)?;
+        Ok(json)
+    }).map_err(|err: Error| format_err!("unable to parse json from {:?} - {}", path, err))
+}
+
+/// Atomically write a file
+///
+/// We first create a temporary file, which is then renamed.
 pub fn file_set_contents<P: AsRef<Path>>(
     path: P,
     data: &[u8],
     perm: Option<stat::Mode>,
 ) -> Result<(), Error> {
+    file_set_contents_full(path, data, perm, None, None)
+}
+
+/// Atomically write a file with owner and group
+pub fn file_set_contents_full<P: AsRef<Path>>(
+    path: P,
+    data: &[u8],
+    perm: Option<stat::Mode>,
+    owner: Option<unistd::Uid>,
+    group: Option<unistd::Gid>,
+) -> Result<(), Error> {
 
     let path = path.as_ref();
 
@@ -115,7 +202,13 @@ pub fn file_set_contents<P: AsRef<Path>>(
         bail!("fchmod {:?} failed: {}", tmp_path, err);
     }
 
-    use std::os::unix::io::FromRawFd;
+    if owner != None || group != None {
+        if let Err(err) = fchown(fd, owner, group) {
+            let _ = unistd::unlink(tmp_path);
+            bail!("fchown {:?} failed: {}", tmp_path, err);
+        }
+    }
+
     let mut file = unsafe { File::from_raw_fd(fd) };
 
     if let Err(err) = file.write_all(data) {
@@ -137,8 +230,7 @@ pub fn lock_file<F: AsRawFd>(
     file: &mut F,
     exclusive: bool,
     timeout: Option<Duration>,
-    ) -> Result<(), Error>
-{
+) -> Result<(), Error> {
     let lockarg =
         if exclusive {
             nix::fcntl::FlockArg::LockExclusive
@@ -209,7 +301,7 @@ pub fn file_chunker<C, R>(
 
     if chunk_size > READ_BUFFER_SIZE { bail!("chunk size too large!"); }
 
-    let mut buf = vec![0u8; READ_BUFFER_SIZE];
+    let mut buf = vec::undefined(READ_BUFFER_SIZE);
 
     let mut pos = 0;
     let mut file_pos = 0;
@@ -256,7 +348,65 @@ pub fn file_chunker<C, R>(
     Ok(())
 }
 
-/// Returns the hosts node name (UTS node name)
+/// Returns the Unix uid/gid for the sepcified system user.
+pub fn getpwnam_ugid(username: &str) -> Result<(libc::uid_t,libc::gid_t), Error> {
+    let info = unsafe { libc::getpwnam(std::ffi::CString::new(username).unwrap().as_ptr()) };
+    if info == std::ptr::null_mut() {
+        bail!("getwpnam '{}' failed", username);
+    }
+
+    let info = unsafe { *info };
+
+    Ok((info.pw_uid, info.pw_gid))
+}
+
+/// Creates directory at the provided path with specified ownership
+///
+/// Simply returns if the directory already exists.
+pub fn create_dir_chown<P: AsRef<Path>>(
+    path: P,
+    perm: Option<stat::Mode>,
+    owner: Option<unistd::Uid>,
+    group: Option<unistd::Gid>,
+) -> Result<(), nix::Error>
+{
+    let mode : stat::Mode = perm.unwrap_or(stat::Mode::from_bits_truncate(0o770));
+
+    let path = path.as_ref();
+
+    match nix::unistd::mkdir(path, mode) {
+        Ok(()) => {},
+        Err(nix::Error::Sys(nix::errno::Errno::EEXIST)) => {
+            return Ok(());
+        },
+        err => return err,
+    }
+
+    unistd::chown(path, owner, group)?;
+
+    Ok(())
+}
+
+/// Change ownership of an open file handle
+pub fn fchown(
+    fd: RawFd,
+    owner: Option<nix::unistd::Uid>,
+    group: Option<nix::unistd::Gid>
+) -> Result<(), Error> {
+
+    // According to the POSIX specification, -1 is used to indicate that owner and group
+    // are not to be changed.  Since uid_t and gid_t are unsigned types, we have to wrap
+    // around to get -1 (copied fron nix crate).
+    let uid = owner.map(Into::into).unwrap_or((0 as libc::uid_t).wrapping_sub(1));
+    let gid = group.map(Into::into).unwrap_or((0 as libc::gid_t).wrapping_sub(1));
+
+    let res = unsafe { libc::fchown(fd, uid, gid) };
+    nix::errno::Errno::result(res)?;
+
+    Ok(())
+}
+
+// Returns the hosts node name (UTS node name)
 pub fn nodename() -> &'static str {
 
     lazy_static!{
@@ -274,6 +424,36 @@ pub fn nodename() -> &'static str {
     &NODENAME
 }
 
+pub fn json_object_to_query(data: Value) -> Result<String, Error> {
+
+    let mut query = url::form_urlencoded::Serializer::new(String::new());
+
+    let object = data.as_object().ok_or_else(|| {
+        format_err!("json_object_to_query: got wrong data type (expected object).")
+    })?;
+
+    for (key, value) in object {
+        match value {
+            Value::Bool(b) => { query.append_pair(key, &b.to_string()); }
+            Value::Number(n) => { query.append_pair(key, &n.to_string()); }
+            Value::String(s) => { query.append_pair(key, &s); }
+            Value::Array(arr) => {
+                for element in arr {
+                    match element {
+                        Value::Bool(b) => { query.append_pair(key, &b.to_string()); }
+                        Value::Number(n) => { query.append_pair(key, &n.to_string()); }
+                        Value::String(s) => { query.append_pair(key, &s); }
+                        _ => bail!("json_object_to_query: unable to handle complex array data types."),
+                    }
+                }
+            }
+            _ => bail!("json_object_to_query: unable to handle complex data types."),
+        }
+    }
+
+    Ok(query.finish())
+}
+
 pub fn required_string_param<'a>(param: &'a Value, name: &str) -> Result<&'a str, Error> {
     match param[name].as_str()   {
         Some(s) => Ok(s),
@@ -288,7 +468,14 @@ pub fn required_integer_param<'a>(param: &'a Value, name: &str) -> Result<i64, E
     }
 }
 
-pub fn complete_file_name(arg: &str) -> Vec<String> {
+pub fn required_array_param<'a>(param: &'a Value, name: &str) -> Result<Vec<Value>, Error> {
+    match param[name].as_array()   {
+        Some(s) => Ok(s.to_vec()),
+        None => bail!("missing parameter '{}'", name),
+    }
+}
+
+pub fn complete_file_name(arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
 
     let mut result = vec![];
 
@@ -296,7 +483,7 @@ pub fn complete_file_name(arg: &str) -> Vec<String> {
     use nix::sys::stat::Mode;
     use nix::fcntl::AtFlags;
 
-    let mut dirname = std::path::PathBuf::from(arg);
+    let mut dirname = std::path::PathBuf::from(if arg.len() == 0 { "./" } else { arg });
 
     let is_dir = match nix::sys::stat::fstatat(libc::AT_FDCWD, &dirname, AtFlags::empty()) {
         Ok(stat) => (stat.st_mode & libc::S_IFMT) == libc::S_IFDIR,
@@ -349,34 +536,21 @@ pub fn complete_file_name(arg: &str) -> Vec<String> {
 /// names. This function simply skips non-UTF8 encoded names.
 pub fn scandir<P, F>(
     dirfd: RawFd,
-    path: P,
+    path: &P,
     regex: &regex::Regex,
     mut callback: F
 ) -> Result<(), Error>
     where F: FnMut(RawFd, &str, nix::dir::Type) -> Result<(), Error>,
-          P: AsRef<Path>
+          P: ?Sized + nix::NixPath,
 {
-    use nix::fcntl::OFlag;
-    use nix::sys::stat::Mode;
-
-    let mut subdir = nix::dir::Dir::openat(dirfd, path.as_ref(), OFlag::O_RDONLY, Mode::empty())?;
-    let subdir_fd = subdir.as_raw_fd();
-
-    for entry in subdir.iter() {
+    for entry in self::fs::scan_subdir(dirfd, path, regex)? {
         let entry = entry?;
         let file_type = match entry.file_type() {
             Some(file_type) => file_type,
             None => bail!("unable to detect file type"),
         };
-        let filename = entry.file_name();
-        let filename_str = match filename.to_str() {
-            Ok(name) => name,
-            Err(_) => continue /* ignore non utf8 entries*/,
-        };
 
-        if !regex.is_match(filename_str) { continue; }
-
-        (callback)(subdir_fd, filename_str, file_type)?;
+        callback(entry.parent_fd(), unsafe { entry.file_name_utf8_unchecked() }, file_type)?;
     }
     Ok(())
 }
@@ -391,19 +565,6 @@ pub fn get_hardware_address() -> Result<String, Error> {
     Ok(format!("{:0x}", digest))
 }
 
-pub fn digest_to_hex(digest: &[u8]) -> String {
-
-    const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
-
-    let mut buf = Vec::<u8>::with_capacity(digest.len()*2);
-
-    for i in 0..digest.len() {
-        buf.push(HEX_CHARS[(digest[i] >> 4) as usize]);
-        buf.push(HEX_CHARS[(digest[i] & 0xf) as usize]);
-    }
-
-    unsafe { String::from_utf8_unchecked(buf) }
-}
 
 pub fn assert_if_modified(digest1: &str, digest2: &str) -> Result<(), Error> {
     if digest1 != digest2 {
@@ -435,3 +596,149 @@ pub fn extract_auth_cookie(cookie: &str, cookie_name: &str) -> Option<String> {
 
     None
 }
+
+pub fn join(data: &Vec<String>, sep: char) -> String {
+
+    let mut list = String::new();
+
+    for item in data {
+        if list.len() != 0 { list.push(sep); }
+        list.push_str(item);
+    }
+
+    list
+}
+
+/// normalize uri path
+///
+/// Do not allow ".", "..", or hidden files ".XXXX"
+/// Also remove empty path components
+pub fn normalize_uri_path(path: &str) -> Result<(String, Vec<&str>), Error> {
+
+    let items = path.split('/');
+
+    let mut path = String::new();
+    let mut components = vec![];
+
+    for name in items {
+        if name.is_empty() { continue; }
+        if name.starts_with(".") {
+            bail!("Path contains illegal components.");
+        }
+        path.push('/');
+        path.push_str(name);
+        components.push(name);
+    }
+
+    Ok((path, components))
+}
+
+pub fn fd_change_cloexec(fd: RawFd, on: bool) -> Result<(), Error> {
+    use nix::fcntl::{fcntl, F_GETFD, F_SETFD, FdFlag};
+    let mut flags = FdFlag::from_bits(fcntl(fd, F_GETFD)?)
+        .ok_or_else(|| format_err!("unhandled file flags"))?; // nix crate is stupid this way...
+    flags.set(FdFlag::FD_CLOEXEC, on);
+    fcntl(fd, F_SETFD(flags))?;
+    Ok(())
+}
+
+
+static mut SHUTDOWN_REQUESTED: bool = false;
+
+pub fn request_shutdown() {
+    unsafe { SHUTDOWN_REQUESTED = true; }
+    crate::server::server_shutdown();
+}
+
+#[inline(always)]
+pub fn shutdown_requested() -> bool {
+    unsafe { SHUTDOWN_REQUESTED }
+}
+
+pub fn fail_on_shutdown() -> Result<(), Error> {
+    if shutdown_requested() {
+        bail!("Server shutdown requested - aborting task");
+    }
+    Ok(())
+}
+
+/// Guard a raw file descriptor with a drop handler. This is mostly useful when access to an owned
+/// `RawFd` is required without the corresponding handler object (such as when only the file
+/// descriptor number is required in a closure which may be dropped instead of being executed).
+pub struct Fd(pub RawFd);
+
+impl Drop for Fd {
+    fn drop(&mut self) {
+        if self.0 != -1 {
+            unsafe {
+                libc::close(self.0);
+            }
+        }
+    }
+}
+
+impl AsRawFd for Fd {
+    fn as_raw_fd(&self) -> RawFd {
+        self.0
+    }
+}
+
+impl IntoRawFd for Fd {
+    fn into_raw_fd(mut self) -> RawFd {
+        let fd = self.0;
+        self.0 = -1;
+        fd
+    }
+}
+
+impl FromRawFd for Fd {
+    unsafe fn from_raw_fd(fd: RawFd) -> Self {
+        Self(fd)
+    }
+}
+
+// wrap nix::unistd::pipe2 + O_CLOEXEC into something returning guarded file descriptors
+pub fn pipe() -> Result<(Fd, Fd), Error> {
+    let (pin, pout) = nix::unistd::pipe2(nix::fcntl::OFlag::O_CLOEXEC)?;
+    Ok((Fd(pin), Fd(pout)))
+}
+
+/// An easy way to convert types to Any
+///
+/// Mostly useful to downcast trait objects (see RpcEnvironment).
+pub trait AsAny {
+    fn as_any(&self) -> &dyn Any;
+}
+
+impl<T: Any> AsAny for T {
+    fn as_any(&self) -> &dyn Any { self }
+}
+
+
+// /usr/include/linux/fs.h: #define BLKGETSIZE64 _IOR(0x12,114,size_t)
+// return device size in bytes (u64 *arg)
+nix::ioctl_read!(blkgetsize64, 0x12, 114, u64);
+
+/// Return file or block device size
+pub fn image_size(path: &Path) -> Result<u64, Error> {
+
+    use std::os::unix::fs::FileTypeExt;
+
+    let file = std::fs::File::open(path)?;
+    let metadata = file.metadata()?;
+    let file_type = metadata.file_type();
+
+    if file_type.is_block_device() {
+        let mut size : u64 = 0;
+        let res = unsafe { blkgetsize64(file.as_raw_fd(), &mut size) };
+
+        if let Err(err) = res {
+            bail!("blkgetsize64 failed for {:?} - {}", path, err);
+        }
+        Ok(size)
+    } else if file_type.is_file() {
+        Ok(metadata.len())
+    } else {
+        bail!("image size failed - got unexpected file type {:?}", file_type);
+    }
+}