]> git.proxmox.com Git - proxmox-backup.git/commitdiff
use non-panicky timestamp_opt where appropriate
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 11 Sep 2020 12:34:36 +0000 (14:34 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 11 Sep 2020 13:48:24 +0000 (15:48 +0200)
by either printing the original, out-of-range timestamp as-is, or
bailing with a proper error message instead of panicking.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
src/backup/fixed_index.rs
src/bin/proxmox-backup-client.rs
src/tools/format.rs

index 5d6cc1ff19a49c8770e7b5f2e599d2ba8d2fb829..309f4503a3e48443f8c77f3614024fb18fe0c418 100644 (file)
@@ -6,7 +6,7 @@ use super::chunk_store::*;
 use super::{IndexFile, ChunkReadInfo};
 use crate::tools::{self, epoch_now_u64};
 
-use chrono::{Local, TimeZone};
+use chrono::{Local, LocalResult, TimeZone};
 use std::fs::File;
 use std::io::Write;
 use std::os::unix::io::AsRawFd;
@@ -150,7 +150,10 @@ impl FixedIndexReader {
         println!("ChunkSize: {}", self.chunk_size);
         println!(
             "CTime: {}",
-            Local.timestamp(self.ctime as i64, 0).format("%c")
+            match Local.timestamp_opt(self.ctime as i64, 0) {
+                LocalResult::Single(ctime) => ctime.format("%c").to_string(),
+                _ => (self.ctime as i64).to_string(),
+            }
         );
         println!("UUID: {:?}", self.uuid);
     }
index 1e9cc68063c819a7ba6cf17cf0065a8c0a8911ce..25376bc9bfa2ce8cd6702b4f29cc87773ee79bd9 100644 (file)
@@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex};
 use std::task::Context;
 
 use anyhow::{bail, format_err, Error};
-use chrono::{Local, DateTime, Utc, TimeZone};
+use chrono::{Local, LocalResult, DateTime, Utc, TimeZone};
 use futures::future::FutureExt;
 use futures::stream::{StreamExt, TryStreamExt};
 use serde_json::{json, Value};
@@ -257,7 +257,11 @@ pub async fn api_datastore_latest_snapshot(
 
     list.sort_unstable_by(|a, b| b.backup_time.cmp(&a.backup_time));
 
-    let backup_time = Utc.timestamp(list[0].backup_time, 0);
+    let backup_time = match Utc.timestamp_opt(list[0].backup_time, 0) {
+        LocalResult::Single(time) => time,
+        _ => bail!("last snapshot of backup group {:?} has invalid timestmap {}.",
+                   group.group_path(), list[0].backup_time),
+    };
 
     Ok((group.backup_type().to_owned(), group.backup_id().to_owned(), backup_time))
 }
@@ -986,7 +990,15 @@ async fn create_backup(
         }
     }
 
-    let backup_time = Utc.timestamp(backup_time_opt.unwrap_or_else(|| Utc::now().timestamp()), 0);
+    let backup_time = match backup_time_opt {
+        Some(timestamp) => {
+            match Utc.timestamp_opt(timestamp, 0) {
+                LocalResult::Single(time) => time,
+                _ => bail!("Invalid backup-time parameter: {}", timestamp),
+            }
+        },
+        _ => Utc::now(),
+    };
 
     let client = connect(repo.host(), repo.user())?;
     record_repository(&repo);
index aa67ea389831bbdd1acfa45327058f39ccba39ee..1e593ff36ce4c97601f3b32a266878b9169ce1a2 100644 (file)
@@ -1,6 +1,6 @@
 use anyhow::{Error};
 use serde_json::Value;
-use chrono::{Local, TimeZone};
+use chrono::{Local, TimeZone, LocalResult};
 
 pub fn strip_server_file_expenstion(name: &str) -> String {
 
@@ -25,8 +25,11 @@ pub fn render_epoch(value: &Value, _record: &Value) -> Result<String, Error> {
     if value.is_null() { return Ok(String::new()); }
     let text = match value.as_i64() {
         Some(epoch) => {
-            Local.timestamp(epoch, 0).format("%c").to_string()
-        }
+            match Local.timestamp_opt(epoch, 0) {
+                LocalResult::Single(epoch) => epoch.format("%c").to_string(),
+                _ => epoch.to_string(),
+            }
+        },
         None => {
             value.to_string()
         }