From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 177EE6104A for ; Fri, 11 Sep 2020 14:34:54 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 074C124C24 for ; Fri, 11 Sep 2020 14:34:54 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [212.186.127.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 56D9524C1A for ; Fri, 11 Sep 2020 14:34:53 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 254AD44B6D for ; Fri, 11 Sep 2020 14:34:53 +0200 (CEST) From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= To: pbs-devel@lists.proxmox.com Date: Fri, 11 Sep 2020 14:34:36 +0200 Message-Id: <20200911123439.1876094-4-f.gruenbichler@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200911123439.1876094-1-f.gruenbichler@proxmox.com> References: <20200911123439.1876094-1-f.gruenbichler@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.027 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [format.rs, proxmox-backup-client.rs] Subject: [pbs-devel] [PATCH proxmox-backup 3/6] use non-panicky timestamp_opt where appropriate X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 11 Sep 2020 12:34:54 -0000 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 --- src/backup/fixed_index.rs | 7 +++++-- src/bin/proxmox-backup-client.rs | 17 ++++++++++++++--- src/tools/format.rs | 9 ++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/src/backup/fixed_index.rs b/src/backup/fixed_index.rs index 5d6cc1ff..309f4503 100644 --- a/src/backup/fixed_index.rs +++ b/src/backup/fixed_index.rs @@ -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); } diff --git a/src/bin/proxmox-backup-client.rs b/src/bin/proxmox-backup-client.rs index 1e9cc680..19f8ccda 100644 --- a/src/bin/proxmox-backup-client.rs +++ b/src/bin/proxmox-backup-client.rs @@ -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 +989,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); diff --git a/src/tools/format.rs b/src/tools/format.rs index aa67ea38..1e593ff3 100644 --- a/src/tools/format.rs +++ b/src/tools/format.rs @@ -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 { 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() } -- 2.20.1