]> git.proxmox.com Git - proxmox-backup.git/commitdiff
replace file_set_contents with replace_file
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 18 Dec 2019 10:05:30 +0000 (11:05 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Wed, 18 Dec 2019 10:16:04 +0000 (11:16 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/api2/admin/datastore.rs
src/api2/backup/environment.rs
src/api2/node/dns.rs
src/api2/node/time.rs
src/auth_helpers.rs
src/backup/key_derivation.rs
src/bin/proxmox-backup-client.rs
src/client/http_client.rs
src/server/worker_task.rs

index 2510b0c0ce6dc34aec8ba29fc16c950d44b5fbaf..8b4999076decb2cc2115d0a2977cd38d9249608d 100644 (file)
@@ -12,7 +12,8 @@ use proxmox::api::{http_err, list_subdirs_api_method};
 use proxmox::api::{ApiResponseFuture, ApiHandler, ApiMethod, Router, RpcEnvironment, RpcEnvironmentType};
 use proxmox::api::router::SubdirMap;
 use proxmox::api::schema::*;
-use proxmox::tools::{try_block, fs::file_get_contents, fs::file_set_contents};
+use proxmox::tools::try_block;
+use proxmox::tools::fs::{file_get_contents, replace_file, CreateOptions};
 
 use crate::api2::types::*;
 use crate::backup::*;
@@ -583,7 +584,7 @@ fn upload_backup_log(
         // always verify CRC at server side
         blob.verify_crc()?;
         let raw_data = blob.raw_data();
-        file_set_contents(&path, raw_data, None)?;
+        replace_file(&path, raw_data, CreateOptions::new())?;
 
         // fixme: use correct formatter
         Ok(crate::server::formatter::json_response(Ok(Value::Null)))
index 278ca251ab70b09d913218a62408c154139f6a10..edef9b8833a16b662a9ef76865ba58d29f58293f 100644 (file)
@@ -4,10 +4,8 @@ use std::collections::HashMap;
 
 use serde_json::Value;
 
-use proxmox::tools::{
-    digest_to_hex,
-    fs::file_set_contents,
-};
+use proxmox::tools::digest_to_hex;
+use proxmox::tools::fs::{replace_file, CreateOptions};
 use proxmox::api::{RpcEnvironment, RpcEnvironmentType};
 
 use crate::server::WorkerTask;
@@ -415,7 +413,7 @@ impl BackupEnvironment {
         blob.verify_crc()?;
 
         let raw_data = blob.raw_data();
-        file_set_contents(&path, raw_data, None)?;
+        replace_file(&path, raw_data, CreateOptions::new())?;
 
         self.log(format!("add blob {:?} ({} bytes, comp: {})", path, orig_len, blob_len));
 
index c4f7ac063167f414020992e858c89b48e38bcf67..065af411082570de0999441c1ba74d1061bdf9dc 100644 (file)
@@ -9,7 +9,7 @@ use serde_json::{json, Value};
 use proxmox::{sortable, identity};
 use proxmox::api::{ApiHandler, ApiMethod, Router, RpcEnvironment};
 use proxmox::api::schema::*;
-use proxmox::tools::fs::{file_get_contents, file_set_contents};
+use proxmox::tools::fs::{file_get_contents, replace_file, CreateOptions};
 use proxmox::tools::*; // required to use IPRE!() macro ???
 
 use crate::api2::types::*;
@@ -91,7 +91,7 @@ fn update_dns(
         data.push('\n');
     }
 
-    file_set_contents(RESOLV_CONF_FN, data.as_bytes(), None)?;
+    replace_file(RESOLV_CONF_FN, data.as_bytes(), CreateOptions::new())?;
 
     Ok(Value::Null)
 }
index 77a8a361cf46c6ece06623139f916031ee3aa59e..855dcc2a9980f41420cf4bda05ac743c9708104b 100644 (file)
@@ -7,7 +7,7 @@ use serde_json::{json, Value};
 use proxmox::{sortable, identity};
 use proxmox::api::{ApiHandler, ApiMethod, Router, RpcEnvironment};
 use proxmox::api::schema::*;
-use proxmox::tools::fs::{file_read_firstline, file_set_contents};
+use proxmox::tools::fs::{file_read_firstline, replace_file, CreateOptions};
 
 use crate::api2::types::*;
 
@@ -71,7 +71,7 @@ fn set_timezone(
         bail!("No such timezone.");
     }
 
-    file_set_contents("/etc/timezone", timezone.as_bytes(), None)?;
+    replace_file("/etc/timezone", timezone.as_bytes(), CreateOptions::new())?;
 
     let _ = std::fs::remove_file("/etc/localtime");
 
index fe981fd97f705ce075e4f59a82f64564619382ea..6e2d04f820f58539e907cba195c314cc17d999e3 100644 (file)
@@ -9,7 +9,7 @@ use std::path::PathBuf;
 
 use proxmox::tools::{
     try_block,
-    fs::{file_get_contents, file_set_contents, file_set_contents_full},
+    fs::{file_get_contents, replace_file, CreateOptions},
 };
 
 fn compute_csrf_secret_digest(
@@ -98,11 +98,15 @@ pub fn generate_csrf_key() -> Result<(), Error> {
     use nix::sys::stat::Mode;
 
     let (_, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
-    let uid = Some(nix::unistd::ROOT);
-    let gid = Some(nix::unistd::Gid::from_raw(backup_gid));
 
-    file_set_contents_full(
-        &path, &pem, Some(Mode::from_bits_truncate(0o0640)), uid, gid)?;
+    replace_file(
+        &path,
+        &pem,
+        CreateOptions::new()
+            .perm(Mode::from_bits_truncate(0o0640))
+            .owner(nix::unistd::ROOT)
+            .group(nix::unistd::Gid::from_raw(backup_gid)),
+    )?;
 
     Ok(())
 }
@@ -122,18 +126,21 @@ pub fn generate_auth_key() -> Result<(), Error> {
 
     use nix::sys::stat::Mode;
 
-    file_set_contents(
-        &priv_path, &priv_pem, Some(Mode::from_bits_truncate(0o0600)))?;
-
+    replace_file(
+        &priv_path, &priv_pem, CreateOptions::new().perm(Mode::from_bits_truncate(0o0600)))?;
 
     let public_pem = rsa.public_key_to_pem()?;
 
     let (_, backup_gid) = crate::tools::getpwnam_ugid("backup")?;
-    let uid = Some(nix::unistd::ROOT);
-    let gid = Some(nix::unistd::Gid::from_raw(backup_gid));
 
-    file_set_contents_full(
-        &public_path, &public_pem, Some(Mode::from_bits_truncate(0o0640)), uid, gid)?;
+    replace_file(
+        &public_path,
+        &public_pem,
+        CreateOptions::new()
+            .perm(Mode::from_bits_truncate(0o0640))
+            .owner(nix::unistd::ROOT)
+            .group(nix::unistd::Gid::from_raw(backup_gid)),
+    )?;
 
     Ok(())
 }
index cb27ff352ce9c9b16344a201a3ef60fa2b1b6c5a..a842e9be749158c2ba5962fd5fcc5e1fef3de3c2 100644 (file)
@@ -5,7 +5,7 @@ use chrono::{Local, TimeZone, DateTime};
 
 use proxmox::tools::{
     try_block,
-    fs::{file_get_contents, file_set_contents},
+    fs::{file_get_contents, replace_file, CreateOptions},
 };
 
 #[derive(Deserialize, Serialize, Debug)]
@@ -84,7 +84,7 @@ pub fn store_key_config(
     try_block!({
         if replace {
             let mode = nix::sys::stat::Mode::S_IRUSR | nix::sys::stat::Mode::S_IWUSR;
-            file_set_contents(&path, data.as_bytes(), Some(mode))?;
+            replace_file(&path, data.as_bytes(), CreateOptions::new().perm(mode))?;
         } else {
             use std::os::unix::fs::OpenOptionsExt;
 
index c218c49cb0af6801df98a30f1e5ae350e3a7799b..a877eea2f48c1e3190fd7aca864ae4f353402a28 100644 (file)
@@ -9,7 +9,7 @@ use std::io::{Write, Seek, SeekFrom};
 use std::os::unix::fs::OpenOptionsExt;
 
 use proxmox::{sortable, identity};
-use proxmox::tools::fs::{file_get_contents, file_get_json, file_set_contents, image_size};
+use proxmox::tools::fs::{file_get_contents, file_get_json, replace_file, CreateOptions, image_size};
 use proxmox::api::{ApiHandler, ApiMethod, RpcEnvironment};
 use proxmox::api::schema::*;
 use proxmox::api::cli::*;
@@ -134,7 +134,7 @@ fn record_repository(repo: &BackupRepository) {
 
     let new_data = json!(map);
 
-    let _ = file_set_contents(path, new_data.to_string().as_bytes(), None);
+    let _ = replace_file(path, new_data.to_string().as_bytes(), CreateOptions::new());
 }
 
 fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<String> {
@@ -1181,7 +1181,7 @@ async fn restore(param: Value) -> Result<Value, Error> {
     if server_archive_name == MANIFEST_BLOB_NAME {
         let backup_index_data = manifest.into_json().to_string();
         if let Some(target) = target {
-            file_set_contents(target, backup_index_data.as_bytes(), None)?;
+            replace_file(target, backup_index_data.as_bytes(), CreateOptions::new())?;
         } else {
             let stdout = std::io::stdout();
             let mut writer = stdout.lock();
@@ -1701,7 +1701,7 @@ fn key_import_master_pubkey(
 
     let target_path = master_pubkey_path()?;
 
-    file_set_contents(&target_path, &pem_data, None)?;
+    replace_file(&target_path, &pem_data, CreateOptions::new())?;
 
     println!("Imported public master key to {:?}", target_path);
 
@@ -1736,14 +1736,14 @@ fn key_create_master_key(
     let pub_key: Vec<u8> = pkey.public_key_to_pem()?;
     let filename_pub = "master-public.pem";
     println!("Writing public master key to {}", filename_pub);
-    file_set_contents(filename_pub, pub_key.as_slice(), None)?;
+    replace_file(filename_pub, pub_key.as_slice(), CreateOptions::new())?;
 
     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);
-    file_set_contents(filename_priv, priv_key.as_slice(), None)?;
+    replace_file(filename_priv, priv_key.as_slice(), CreateOptions::new())?;
 
     Ok(Value::Null)
 }
index 25f3dda94e439bae4150774fc1a0928753943d03..ad46eae3f34024e19b1dc70eb3d3573eb5b8f3f3 100644 (file)
@@ -15,7 +15,7 @@ use percent_encoding::percent_encode;
 use xdg::BaseDirectories;
 
 use proxmox::tools::{
-    fs::{file_get_json, file_set_contents},
+    fs::{file_get_json, replace_file, CreateOptions},
 };
 
 use super::pipe_to_stream::PipeToSendStream;
@@ -52,7 +52,7 @@ pub fn delete_ticket_info(server: &str, username: &str) -> Result<(), Error> {
         map.remove(username);
     }
 
-    file_set_contents(path, data.to_string().as_bytes(), Some(mode))?;
+    replace_file(path, data.to_string().as_bytes(), CreateOptions::new().perm(mode))?;
 
     Ok(())
 }
@@ -88,7 +88,7 @@ fn store_ticket_info(server: &str, username: &str, ticket: &str, token: &str) ->
         }
     }
 
-    file_set_contents(path, new_data.to_string().as_bytes(), Some(mode))?;
+    replace_file(path, new_data.to_string().as_bytes(), CreateOptions::new().perm(mode))?;
 
     Ok(())
 }
index 341f4a9aa54a97ddef75fab22a23fc6f458647dd..5a07c98a39abff0c86c0b8b213fd1535ac8116af 100644 (file)
@@ -15,7 +15,7 @@ use serde_json::{json, Value};
 
 use proxmox::tools::{
     try_block,
-    fs::{create_path, file_set_contents_full, CreateOptions},
+    fs::{create_path, replace_file, CreateOptions},
 };
 
 use super::UPID;
@@ -202,11 +202,11 @@ pub struct TaskListInfo {
 fn update_active_workers(new_upid: Option<&UPID>) -> Result<Vec<TaskListInfo>, Error> {
 
     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 uid = nix::unistd::Uid::from_raw(backup_uid);
+    let gid = nix::unistd::Gid::from_raw(backup_gid);
 
     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)?;
+    nix::unistd::chown(PROXMOX_BACKUP_TASK_LOCK_FN, Some(uid), Some(gid))?;
 
     let reader = match File::open(PROXMOX_BACKUP_ACTIVE_TASK_FN) {
         Ok(f) => Some(BufReader::new(f)),
@@ -301,7 +301,13 @@ fn update_active_workers(new_upid: Option<&UPID>) -> Result<Vec<TaskListInfo>, E
         }
     }
 
-    file_set_contents_full(PROXMOX_BACKUP_ACTIVE_TASK_FN, raw.as_bytes(), None, uid, gid)?;
+    replace_file(
+        PROXMOX_BACKUP_ACTIVE_TASK_FN,
+        raw.as_bytes(),
+        CreateOptions::new()
+            .owner(uid)
+            .group(gid),
+    )?;
 
     drop(lock);