]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/tools.rs: file_get_json() - add new default parameter
authorDietmar Maurer <dietmar@proxmox.com>
Thu, 4 Apr 2019 10:24:18 +0000 (12:24 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Thu, 4 Apr 2019 10:24:18 +0000 (12:24 +0200)
src/bin/proxmox-backup-client.rs
src/client/http_client.rs
src/tools.rs

index 31d459d5301c62dc7a65c14bc57356480a337e0d..4df7de160be02fa761cdddc98ee942193d2e537a 100644 (file)
@@ -43,7 +43,7 @@ fn record_repository(repo: &BackupRepository) {
         _ => return,
     };
 
-    let mut data = tools::file_get_json(&path).unwrap_or(json!({}));
+    let mut data = tools::file_get_json(&path, None).unwrap_or(json!({}));
 
     let repo = repo.to_string();
 
@@ -93,7 +93,7 @@ fn complete_repository(_arg: &str, _param: &HashMap<String, String>) -> Vec<Stri
         _ => return result,
     };
 
-    let data = tools::file_get_json(&path).unwrap_or(json!({}));
+    let data = tools::file_get_json(&path, None).unwrap_or(json!({}));
 
     if let Some(map) = data.as_object() {
         for (repo, _count) in map {
index a9813223f6472f07363ab79a04597e4b2f0a45ed..e13a0ea3b4a9c686a611acad0df9ba45abfb1600 100644 (file)
@@ -33,7 +33,7 @@ fn store_ticket_info(server: &str, username: &str, ticket: &str, token: &str) ->
 
     let mode = nix::sys::stat::Mode::from_bits_truncate(0o0600);
 
-    let mut data = tools::file_get_json(&path).unwrap_or(json!({}));
+    let mut data = tools::file_get_json(&path, Some(json!({})))?;
 
     let now = Utc::now().timestamp();
 
@@ -72,7 +72,10 @@ fn load_ticket_info(server: &str, username: &str) -> Option<(String, String)> {
         _ => return None,
     };
 
-    let data = tools::file_get_json(&path).unwrap_or(json!({}));
+    let data = match tools::file_get_json(&path, None) {
+        Ok(v) => v,
+        _ => return None,
+    };
 
     let now = Utc::now().timestamp();
 
index 90499644c28334d497b3332dae36ba659acbdc9e..f63102cbf0327bddf83773b91ab943bca7d60109 100644 (file)
@@ -126,17 +126,27 @@ pub fn file_get_contents<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, Error> {
     }).map_err(|err| format_err!("unable to read {:?} - {}", path, err))
 }
 
-pub fn file_get_json<P: AsRef<Path>>(path: P) -> Result<Value, Error> {
+pub fn file_get_json<P: AsRef<Path>>(path: P, default: Option<Value>) -> Result<Value, Error> {
 
     let path = path.as_ref();
 
-    let raw = file_get_contents(path)?;
+    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 read json from {:?} - {}", path, err))
+    }).map_err(|err: Error| format_err!("unable to parse json from {:?} - {}", path, err))
 }
 
 /// Atomically write a file. We first create a temporary file, which