lazy_static = "1.1"
regex = "1.0"
libc = "0.2"
-nix = "0.13"
+nix = "0.14"
shellwords = "1.0"
uuid = { version = "0.7", features = ["v4"] }
chrono = "0.4" # Date and time library for Rust
use std::path::PathBuf;
use std::sync::Arc;
+use proxmox::tools::{try_block, fs::file_set_contents};
+
use crate::config::datastore;
use crate::backup::*;
// always comput CRC at server side
blob.set_crc(blob.compute_crc());
let raw_data = blob.raw_data();
- crate::tools::file_set_contents(&path, raw_data, None)?;
+ file_set_contents(&path, raw_data, None)?;
Ok(())
})
.and_then(move |_| {
use serde_json::Value;
-use proxmox::tools;
+use proxmox::tools::{
+ digest_to_hex,
+ fs::file_set_contents,
+};
use crate::api_schema::router::{RpcEnvironment, RpcEnvironmentType};
use crate::server::WorkerTask;
fn log_upload_stat(&self, archive_name: &str, csum: &[u8; 32], uuid: &[u8; 16], size: u64, chunk_count: u64, upload_stat: &UploadStatistic) {
self.log(format!("Upload statistics for '{}'", archive_name));
- self.log(format!("UUID: {}", tools::digest_to_hex(uuid)));
- self.log(format!("Checksum: {}", tools::digest_to_hex(csum)));
+ self.log(format!("UUID: {}", digest_to_hex(uuid)));
+ self.log(format!("Checksum: {}", digest_to_hex(csum)));
self.log(format!("Size: {}", size));
self.log(format!("Chunk count: {}", chunk_count));
blob.set_crc(blob.compute_crc());
let raw_data = blob.raw_data();
- crate::tools::file_set_contents(&path, raw_data, None)?;
+ file_set_contents(&path, raw_data, None)?;
self.log(format!("add blob {:?} ({} bytes, comp: {})", path, orig_len, blob_len));
use failure::*;
-
-use crate::tools;
+use proxmox::tools::fs::{file_get_contents, file_set_contents};
use crate::api2::*;
use crate::api_schema::*;
//use crate::api_schema::router::*;
let mut nscount = 0;
- let raw = tools::file_get_contents(RESOLV_CONF_FN)?;
+ let raw = file_get_contents(RESOLV_CONF_FN)?;
result["digest"] = Value::from(proxmox::tools::digest_to_hex(&sha::sha256(&raw)));
let _guard = MUTEX.lock();
- let search = tools::required_string_param(¶m, "search")?;
+ let search = crate::tools::required_string_param(¶m, "search")?;
- let raw = tools::file_get_contents(RESOLV_CONF_FN)?;
+ let raw = file_get_contents(RESOLV_CONF_FN)?;
let old_digest = proxmox::tools::digest_to_hex(&sha::sha256(&raw));
if let Some(digest) = param["digest"].as_str() {
- tools::assert_if_modified(&old_digest, &digest)?;
+ crate::tools::assert_if_modified(&old_digest, &digest)?;
}
let old_data = String::from_utf8(raw)?;
data.push('\n');
}
- tools::file_set_contents(RESOLV_CONF_FN, data.as_bytes(), None)?;
+ file_set_contents(RESOLV_CONF_FN, data.as_bytes(), None)?;
Ok(Value::Null)
}
use failure::*;
+use proxmox::tools::fs::{file_read_firstline, file_set_contents};
-use crate::tools;
use crate::api_schema::*;
use crate::api_schema::router::*;
use crate::api2::types::*;
fn read_etc_localtime() -> Result<String, Error> {
// use /etc/timezone
- if let Ok(line) = tools::file_read_firstline("/etc/timezone") {
+ if let Ok(line) = file_read_firstline("/etc/timezone") {
return Ok(line.trim().to_owned());
}
_rpcenv: &mut dyn RpcEnvironment,
) -> Result<Value, Error> {
- let timezone = tools::required_string_param(¶m, "timezone")?;
+ let timezone = crate::tools::required_string_param(¶m, "timezone")?;
let path = std::path::PathBuf::from(format!("/usr/share/zoneinfo/{}", timezone));
bail!("No such timezone.");
}
- tools::file_set_contents("/etc/timezone", timezone.as_bytes(), None)?;
+ file_set_contents("/etc/timezone", timezone.as_bytes(), None)?;
let _ = std::fs::remove_file("/etc/localtime");
-use crate::tools;
-
use failure::*;
use lazy_static::lazy_static;
use std::path::PathBuf;
+use proxmox::tools::{
+ try_block,
+ fs::{file_get_contents, file_set_contents, file_set_contents_full},
+};
+
fn compute_csrf_secret_digest(
timestamp: i64,
secret: &[u8],
use nix::sys::stat::Mode;
- let (_, backup_gid) = tools::getpwnam_ugid("backup")?;
+ let (_, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
let uid = Some(nix::unistd::ROOT);
let gid = Some(nix::unistd::Gid::from_raw(backup_gid));
- tools::file_set_contents_full(
+ file_set_contents_full(
&path, &pem, Some(Mode::from_bits_truncate(0o0640)), uid, gid)?;
Ok(())
use nix::sys::stat::Mode;
- tools::file_set_contents(
+ file_set_contents(
&priv_path, &priv_pem, Some(Mode::from_bits_truncate(0o0600)))?;
let public_pem = rsa.public_key_to_pem()?;
- tools::file_set_contents(&public_path, &public_pem, None)?;
+ file_set_contents(&public_path, &public_pem, None)?;
Ok(())
}
lazy_static! {
static ref SECRET: Vec<u8> =
- tools::file_get_contents(configdir!("/csrf.key")).unwrap();
+ file_get_contents(configdir!("/csrf.key")).unwrap();
}
&SECRET
fn load_private_auth_key() -> Result<PKey<Private>, Error> {
- let pem = tools::file_get_contents(configdir!("/authkey.key"))?;
+ let pem = file_get_contents(configdir!("/authkey.key"))?;
let rsa = Rsa::private_key_from_pem(&pem)?;
let key = PKey::from_rsa(rsa)?;
fn load_public_auth_key() -> Result<PKey<Public>, Error> {
- let pem = tools::file_get_contents(configdir!("/authkey.pub"))?;
+ let pem = file_get_contents(configdir!("/authkey.pub"))?;
let rsa = Rsa::public_key_from_pem(&pem)?;
let key = PKey::from_rsa(rsa)?;
use serde::{Deserialize, Serialize};
use chrono::{Local, TimeZone, DateTime};
+use proxmox::tools::{
+ try_block,
+ fs::{file_get_contents, file_set_contents},
+};
+
#[derive(Deserialize, Serialize, Debug)]
pub enum KeyDerivationConfig {
Scrypt {
try_block!({
if replace {
let mode = nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR;
- crate::tools::file_set_contents(&path, data.as_bytes(), Some(mode))?;
+ file_set_contents(&path, data.as_bytes(), Some(mode))?;
} else {
use std::os::unix::fs::OpenOptionsExt;
pub fn load_and_decrtypt_key(path: &std::path::Path, passphrase: fn() -> Result<Vec<u8>, Error>) -> Result<([u8;32], DateTime<Local>), Error> {
- let raw = crate::tools::file_get_contents(&path)?;
+ let raw = file_get_contents(&path)?;
let data = String::from_utf8(raw)?;
let key_config: KeyConfig = serde_json::from_str(&data)?;
println!("READ CHUNK {}", digest_str);
let (path, _) = self.store.chunk_path(digest);
- let raw_data = crate::tools::file_get_contents(&path)?;
+ let raw_data = proxmox::tools::fs::file_get_contents(&path)?;
let chunk = DataChunk::from_raw(raw_data, *digest)?;
chunk.verify_crc()?;
//use proxmox_backup::tools;
-use proxmox_backup::try_block;
use proxmox_backup::api_schema::router::*;
use proxmox_backup::api_schema::config::*;
use proxmox_backup::server::rest::*;
use failure::*;
use lazy_static::lazy_static;
+use proxmox::tools::try_block;
use futures::*;
use futures::future::Future;
use std::path::{Path, PathBuf};
use std::collections::{HashSet, HashMap};
use std::io::Write;
+use proxmox::tools::fs::{file_get_contents, file_get_json, file_set_contents, image_size};
use proxmox_backup::tools;
use proxmox_backup::cli::*;
_ => return,
};
- let mut data = tools::file_get_json(&path, None).unwrap_or(json!({}));
+ let mut data = file_get_json(&path, None).unwrap_or(json!({}));
let repo = repo.to_string();
let new_data = json!(map);
- let _ = tools::file_set_contents(path, new_data.to_string().as_bytes(), None);
+ let _ = file_set_contents(path, new_data.to_string().as_bytes(), None);
}
fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
_ => return result,
};
- let data = tools::file_get_json(&path, None).unwrap_or(json!({}));
+ let data = file_get_json(&path, None).unwrap_or(json!({}));
if let Some(map) = data.as_object() {
for (repo, _count) in map {
bail!("got unexpected file type (expected file or block device)");
}
- let size = tools::image_size(&PathBuf::from(filename))?;
+ let size = image_size(&PathBuf::from(filename))?;
if size == 0 { bail!("got zero-sized file '{}'", filename); }
let path = master_pubkey_path()?;
if path.exists() {
- let pem_data = proxmox_backup::tools::file_get_contents(&path)?;
+ let pem_data = file_get_contents(&path)?;
let rsa = openssl::rsa::Rsa::public_key_from_pem(&pem_data)?;
let enc_key = crypt_config.generate_rsa_encoded_key(rsa, created)?;
(Some(Arc::new(crypt_config)), Some(enc_key))
// openssl rsautl -decrypt -inkey master-private.pem -in rsa-encrypted.key -out t
/*
let mut buffer2 = vec![0u8; rsa.size() as usize];
- let pem_data = proxmox_backup::tools::file_get_contents("master-private.pem")?;
+ let pem_data = file_get_contents("master-private.pem")?;
let rsa = openssl::rsa::Rsa::private_key_from_pem(&pem_data)?;
let len = rsa.private_decrypt(&buffer, &mut buffer2, openssl::rsa::Padding::PKCS1)?;
println!("TEST {} {:?}", len, buffer2);
};
if let Some(target) = target {
- crate::tools::file_set_contents(target, &raw_data, None)?;
+ file_set_contents(target, &raw_data, None)?;
} else {
let stdout = std::io::stdout();
let mut writer = stdout.lock();
}
};
- let data = crate::tools::file_get_contents(logfile)?;
+ let data = file_get_contents(logfile)?;
let blob = if let Some(ref crypt_config) = crypt_config {
DataBlob::encode(&data, Some(crypt_config), true)?
let path = tools::required_string_param(¶m, "path")?;
let path = PathBuf::from(path);
- let pem_data = proxmox_backup::tools::file_get_contents(&path)?;
+ let pem_data = file_get_contents(&path)?;
if let Err(err) = openssl::pkey::PKey::public_key_from_pem(&pem_data) {
bail!("Unable to decode PEM data - {}", err);
let target_path = master_pubkey_path()?;
- proxmox_backup::tools::file_set_contents(&target_path, &pem_data, None)?;
+ file_set_contents(&target_path, &pem_data, None)?;
println!("Imported public master key to {:?}", target_path);
let pub_key: Vec<u8> = pkey.public_key_to_pem()?;
let filename_pub = "master-public.pem";
println!("Writing public master key to {}", filename_pub);
- proxmox_backup::tools::file_set_contents(filename_pub, pub_key.as_slice(), None)?;
+ file_set_contents(filename_pub, pub_key.as_slice(), None)?;
let cipher = openssl::symm::Cipher::aes_256_cbc();
let priv_key: Vec<u8> = pkey.private_key_to_pem_pkcs8_passphrase(cipher, new_pw.as_bytes())?;
let filename_priv = "master-private.pem";
println!("Writing private master key to {}", filename_priv);
- proxmox_backup::tools::file_set_contents(filename_priv, priv_key.as_slice(), None)?;
+ file_set_contents(filename_priv, priv_key.as_slice(), None)?;
Ok(Value::Null)
}
-use proxmox_backup::try_block;
use proxmox_backup::configdir;
use proxmox_backup::server;
use proxmox_backup::tools::daemon;
use proxmox_backup::auth_helpers::*;
use failure::*;
+use proxmox::tools::try_block;
use lazy_static::lazy_static;
use futures::*;
use serde_json::{json, Value};
use url::percent_encoding::{percent_encode, DEFAULT_ENCODE_SET};
+use proxmox::tools::{
+ digest_to_hex,
+ fs::{file_get_json, file_set_contents},
+};
+
use crate::tools::{self, BroadcastFuture, tty};
use crate::tools::futures::{cancellable, Canceller};
use super::pipe_to_stream::*;
let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600);
- let mut data = tools::file_get_json(&path, Some(json!({})))?;
+ let mut data = file_get_json(&path, Some(json!({})))?;
let now = Utc::now().timestamp();
}
}
- tools::file_set_contents(path, new_data.to_string().as_bytes(), Some(mode))?;
+ file_set_contents(path, new_data.to_string().as_bytes(), Some(mode))?;
Ok(())
}
_ => return None,
};
- let data = match tools::file_get_json(&path, None) {
+ let data = match file_get_json(&path, None) {
Ok(v) => v,
_ => return None,
};
output: W,
) -> impl Future<Item=W, Error=Error> {
let path = "chunk";
- let param = json!({ "digest": proxmox::tools::digest_to_hex(digest) });
+ let param = json!({ "digest": digest_to_hex(digest) });
self.h2.download(path, Some(param), output)
}
let mut offset_list = vec![];
for (offset, digest) in chunk_list {
//println!("append chunk {} (offset {})", proxmox::tools::digest_to_hex(&digest), offset);
- digest_list.push(proxmox::tools::digest_to_hex(&digest));
+ digest_list.push(digest_to_hex(&digest));
offset_list.push(offset);
}
println!("append chunks list len ({})", digest_list.len());
DigestListDecoder::new(body.map_err(Error::from))
.for_each(move |chunk| {
let _ = release_capacity.release_capacity(chunk.len());
- println!("GOT DOWNLOAD {}", proxmox::tools::digest_to_hex(&chunk));
+ println!("GOT DOWNLOAD {}", digest_to_hex(&chunk));
known_chunks.lock().unwrap().insert(chunk);
Ok(())
})
if let MergedChunkInfo::New(chunk_info) = merged_chunk_info {
let offset = chunk_info.offset;
let digest = *chunk_info.chunk.digest();
- let digest_str = proxmox::tools::digest_to_hex(&digest);
+ let digest_str = digest_to_hex(&digest);
let upload_queue = upload_queue.clone();
println!("upload new chunk {} ({} bytes, offset {})", digest_str,
pub mod datastore;
-use crate::tools;
+use proxmox::tools::try_block;
+
use crate::buildcfg;
/// Check configuration directory permissions
pub fn check_configdir_permissions() -> Result<(), Error> {
let cfgdir = buildcfg::CONFIGDIR;
- let (backup_uid, backup_gid) = tools::getpwnam_ugid("backup")?;
+ let (backup_uid, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
try_block!({
let stat = nix::sys::stat::stat(cfgdir)?;
use nix::sys::stat::Mode;
let cfgdir = buildcfg::CONFIGDIR;
- let (backup_uid, backup_gid) = tools::getpwnam_ugid("backup")?;
+ let (backup_uid, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
match nix::unistd::mkdir(cfgdir, Mode::from_bits_truncate(0o700)) {
Ok(()) => {},
use std::collections::HashMap;
//use std::sync::Arc;
-use crate::tools;
+use proxmox::tools::{try_block, fs::file_set_contents};
+
use crate::api_schema::*;
use crate::section_config::*;
let raw = CONFIG.write(DATASTORE_CFG_FILENAME, &config)?;
- tools::file_set_contents(DATASTORE_CFG_FILENAME, raw.as_bytes(), None)?;
+ file_set_contents(DATASTORE_CFG_FILENAME, raw.as_bytes(), None)?;
Ok(())
}
use serde_json::{json, Value};
use std::sync::Arc;
+
+use proxmox::tools::try_block;
+
use crate::api_schema::*;
pub struct SectionConfigPlugin {
use std::os::unix::io::AsRawFd;
use nix::sys::socket;
+use proxmox::tools::try_block;
+
/// Listens on a Unix Socket to handle simple command asynchronously
pub fn create_control_socket<P, F>(path: P, f: F) -> Result<impl Future<Item=(), Error=()>, Error>
where P: Into<PathBuf>,
use serde_json::{json, Value};
+use proxmox::tools::{
+ try_block,
+ fs::{create_dir_chown, file_set_contents_full},
+};
+
use super::UPID;
-use crate::tools::{self, FileLogger};
+use crate::tools::FileLogger;
macro_rules! PROXMOX_BACKUP_VAR_RUN_DIR_M { () => ("/var/run/proxmox-backup") }
macro_rules! PROXMOX_BACKUP_LOG_DIR_M { () => ("/var/log/proxmox-backup") }
static ref WORKER_TASK_LIST: Mutex<HashMap<usize, Arc<WorkerTask>>> = Mutex::new(HashMap::new());
static ref MY_PID: i32 = unsafe { libc::getpid() };
- static ref MY_PID_PSTART: u64 = tools::procfs::read_proc_pid_stat(*MY_PID).unwrap().starttime;
+ static ref MY_PID_PSTART: u64 = crate::tools::procfs::read_proc_pid_stat(*MY_PID).unwrap().starttime;
}
/// Test if the task is still running
false
}
} else {
- match tools::procfs::check_process_running_pstart(upid.pid, upid.pstart) {
+ match crate::tools::procfs::check_process_running_pstart(upid.pid, upid.pstart) {
Some(_) => true,
_ => false,
}
pub fn create_task_log_dirs() -> Result<(), Error> {
try_block!({
- let (backup_uid, backup_gid) = tools::getpwnam_ugid("backup")?;
+ let (backup_uid, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
let uid = Some(nix::unistd::Uid::from_raw(backup_uid));
let gid = Some(nix::unistd::Gid::from_raw(backup_gid));
- tools::create_dir_chown(PROXMOX_BACKUP_LOG_DIR, None, uid, gid)?;
- tools::create_dir_chown(PROXMOX_BACKUP_TASK_DIR, None, uid, gid)?;
- tools::create_dir_chown(PROXMOX_BACKUP_VAR_RUN_DIR, None, uid, gid)?;
+ create_dir_chown(PROXMOX_BACKUP_LOG_DIR, None, uid, gid)?;
+ create_dir_chown(PROXMOX_BACKUP_TASK_DIR, None, uid, gid)?;
+ create_dir_chown(PROXMOX_BACKUP_VAR_RUN_DIR, None, uid, gid)?;
Ok(())
}).map_err(|err: Error| format_err!("unable to create task log dir - {}", err))?;
// Returns a sorted list of known tasks,
fn update_active_workers(new_upid: Option<&UPID>) -> Result<Vec<TaskListInfo>, Error> {
- let (backup_uid, backup_gid) = tools::getpwnam_ugid("backup")?;
+ let (backup_uid, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
let uid = Some(nix::unistd::Uid::from_raw(backup_uid));
let gid = Some(nix::unistd::Gid::from_raw(backup_gid));
- let lock = tools::open_file_locked(PROXMOX_BACKUP_TASK_LOCK_FN, std::time::Duration::new(10, 0))?;
+ let lock = crate::tools::open_file_locked(PROXMOX_BACKUP_TASK_LOCK_FN, std::time::Duration::new(10, 0))?;
nix::unistd::chown(PROXMOX_BACKUP_TASK_LOCK_FN, uid, gid)?;
let reader = match File::open(PROXMOX_BACKUP_ACTIVE_TASK_FN) {
}
}
- tools::file_set_contents_full(PROXMOX_BACKUP_ACTIVE_TASK_FN, raw.as_bytes(), None, uid, gid)?;
+ file_set_contents_full(PROXMOX_BACKUP_ACTIVE_TASK_FN, raw.as_bytes(), None, uid, gid)?;
drop(lock);
path.push(format!("{:02X}", upid.pstart % 256));
- let (backup_uid, backup_gid) = tools::getpwnam_ugid("backup")?;
+ let (backup_uid, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
let uid = Some(nix::unistd::Uid::from_raw(backup_uid));
let gid = Some(nix::unistd::Gid::from_raw(backup_gid));
- tools::create_dir_chown(&path, None, uid, gid)?;
+ create_dir_chown(&path, None, uid, gid)?;
path.push(upid.to_string());
//!
//! This is a collection of small and useful tools.
use failure::*;
-use nix::unistd;
-use nix::sys::stat;
-use nix::{convert_ioctl_res, request_code_read, ioc};
-
use lazy_static::lazy_static;
use std::fs::{File, OpenOptions};
-use std::io::Write;
use std::path::Path;
use std::io::Read;
use std::io::ErrorKind;
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 `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.
Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) })
}
-pub fn file_read_firstline<P: AsRef<Path>>(path: P) -> Result<String, Error> {
-
- let path = path.as_ref();
-
- try_block!({
- let file = std::fs::File::open(path)?;
-
- use std::io::{BufRead, BufReader};
-
- let mut reader = BufReader::new(file);
-
- let mut line = String::new();
-
- let _ = reader.read_line(&mut 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>, Error> {
-
- let path = path.as_ref();
-
- try_block!({
- std::fs::read(path)
- }).map_err(|err| format_err!("unable to read {:?} - {}", path, err))
-}
-
-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();
-
- // Note: we use mkstemp heŕe, because this worka with different
- // processes, threads, and even tokio tasks.
- let mut template = path.to_owned();
- template.set_extension("tmp_XXXXXX");
- let (fd, tmp_path) = match unistd::mkstemp(&template) {
- Ok((fd, path)) => (fd, path),
- Err(err) => bail!("mkstemp {:?} failed: {}", template, err),
- };
-
- let tmp_path = tmp_path.as_path();
-
- let mode : stat::Mode = perm.unwrap_or(stat::Mode::from(
- stat::Mode::S_IRUSR | stat::Mode::S_IWUSR |
- stat::Mode::S_IRGRP | stat::Mode::S_IROTH
- ));
-
- if perm != None {
- if let Err(err) = stat::fchmod(fd, mode) {
- let _ = unistd::unlink(tmp_path);
- bail!("fchmod {:?} failed: {}", tmp_path, err);
- }
- }
-
- 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) {
- let _ = unistd::unlink(tmp_path);
- bail!("write failed: {}", err);
- }
-
- if let Err(err) = std::fs::rename(tmp_path, path) {
- let _ = unistd::unlink(tmp_path);
- bail!("Atomic rename failed for file {:?} - {}", path, err);
- }
-
- Ok(())
-}
/// Create a file lock using fntl. This function allows you to specify
/// a timeout if you want to avoid infinite blocking.
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 {
static FILENAME: &str = "/etc/ssh/ssh_host_rsa_key.pub";
- let contents = file_get_contents(FILENAME)?;
+ let contents = proxmox::tools::fs::file_get_contents(FILENAME)?;
let digest = md5::compute(contents);
Ok(format!("{:0x}", digest))
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);
- }
-}
}
}
-use nix::{convert_ioctl_res, request_code_read, request_code_write, ioc};
-
// /usr/include/linux/fs.h: #define FS_IOC_GETFLAGS _IOR('f', 1, long)
// read Linux file system attributes (see man chattr)
nix::ioctl_read!(read_attr_fd, b'f', 1, usize);
use std::collections::HashSet;
use std::process;
use std::net::{Ipv4Addr, Ipv6Addr};
-use crate::tools;
+use proxmox::tools::fs::file_read_firstline;
use lazy_static::lazy_static;
use regex::Regex;
use libc;
pub fn read_proc_pid_stat(pid: libc::pid_t) -> Result<ProcFsPidStat, Error> {
- let statstr = tools::file_read_firstline(format!("/proc/{}/stat", pid))?;
+ let statstr = file_read_firstline(format!("/proc/{}/stat", pid))?;
lazy_static! {
static ref REGEX: Regex = Regex::new(concat!(
pub fn read_proc_uptime() -> Result<(f64, f64), Error> {
let path = "/proc/uptime";
- let line = tools::file_read_firstline(&path)?;
+ let line = file_read_firstline(&path)?;
let mut values = line.split_whitespace().map(|v| v.parse::<f64>());
match (values.next(), values.next()) {
meminfo.swapused = meminfo.swaptotal - meminfo.swapfree;
- let spages_line = tools::file_read_firstline("/sys/kernel/mm/ksm/pages_sharing")?;
+ let spages_line = file_read_firstline("/sys/kernel/mm/ksm/pages_sharing")?;
meminfo.memshared = spages_line.trim_end().parse::<u64>()? * 4096;
Ok(meminfo)
pub fn read_memory_usage() -> Result<ProcFsMemUsage, Error> {
let path = format!("/proc/{}/statm", process::id());
- let line = tools::file_read_firstline(&path)?;
+ let line = file_read_firstline(&path)?;
let mut values = line.split_whitespace().map(|v| v.parse::<u64>());
let ps = 4096;
use failure::*;
-use crate::try_block;
+use proxmox::tools::try_block;
/// Returns whether the current stdin is a tty .
pub fn stdin_isatty() -> bool {