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)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 1F6B66140B for ; Tue, 18 Aug 2020 09:04:54 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 0DF2C10287 for ; Tue, 18 Aug 2020 09:04:24 +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) server-digest SHA256) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id B94181027C for ; Tue, 18 Aug 2020 09:04:22 +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 898644465A for ; Tue, 18 Aug 2020 09:04:22 +0200 (CEST) From: Wolfgang Bumiller To: pbs-devel@lists.proxmox.com Date: Tue, 18 Aug 2020 09:04:21 +0200 Message-Id: <20200818070421.19083-1-w.bumiller@proxmox.com> X-Mailer: git-send-email 2.20.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.020 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. [upid.rs] Subject: [pbs-devel] [PATCH backup] turn UPID into an API type 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: Tue, 18 Aug 2020 07:04:54 -0000 It's a string-type. Implement Serialize via Display, Deserialize via FromStr and add an API_SCHEMA so that it can be used as a type within the #[api] macro. Signed-off-by: Wolfgang Bumiller --- src/server/upid.rs | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/server/upid.rs b/src/server/upid.rs index e0d7d8af..9fc5085b 100644 --- a/src/server/upid.rs +++ b/src/server/upid.rs @@ -2,9 +2,9 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use anyhow::{bail, Error}; use chrono::Local; -use lazy_static::lazy_static; -use regex::Regex; +use proxmox::api::schema::{ApiStringFormat, Schema, StringSchema}; +use proxmox::const_regex; use proxmox::sys::linux::procfs; use crate::api2::types::Userid; @@ -20,6 +20,7 @@ use crate::api2::types::Userid; /// ``` /// Please note that we use tokio, so a single thread can run multiple /// tasks. +// #[api] - manually implemented API type #[derive(Debug, Clone)] pub struct UPID { /// The Unix PID @@ -40,7 +41,26 @@ pub struct UPID { pub node: String, } +proxmox::forward_serialize_to_display!(UPID); +proxmox::forward_deserialize_to_from_str!(UPID); + +const_regex! { + pub PROXMOX_UPID_REGEX = concat!( + r"^UPID:(?P[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):(?P[0-9A-Fa-f]{8}):", + r"(?P[0-9A-Fa-f]{8,9}):(?P[0-9A-Fa-f]{8,16}):(?P[0-9A-Fa-f]{8}):", + r"(?P[^:\s]+):(?P[^:\s]*):(?P[^:\s]+):$" + ); +} + +pub const PROXMOX_UPID_FORMAT: ApiStringFormat = + ApiStringFormat::Pattern(&PROXMOX_UPID_REGEX); + impl UPID { + pub const API_SCHEMA: Schema = StringSchema::new("Unique Process/Task Identifier") + .min_length("UPID:N:12345678:12345678:12345678:::".len()) + .max_length(128) // arbitrary + .format(&PROXMOX_UPID_FORMAT) + .schema(); /// Create a new UPID pub fn new( @@ -92,17 +112,7 @@ impl std::str::FromStr for UPID { type Err = Error; fn from_str(s: &str) -> Result { - - lazy_static! { - static ref REGEX: Regex = Regex::new(concat!( - r"^UPID:(?P[a-zA-Z0-9]([a-zA-Z0-9\-]*[a-zA-Z0-9])?):(?P[0-9A-Fa-f]{8}):", - r"(?P[0-9A-Fa-f]{8,9}):(?P[0-9A-Fa-f]{8,16}):(?P[0-9A-Fa-f]{8}):", - r"(?P[^:\s]+):(?P[^:\s]*):(?P[^:\s]+):$" - )).unwrap(); - } - - if let Some(cap) = REGEX.captures(s) { - + if let Some(cap) = PROXMOX_UPID_REGEX.captures(s) { Ok(UPID { pid: i32::from_str_radix(&cap["pid"], 16).unwrap(), pstart: u64::from_str_radix(&cap["pstart"], 16).unwrap(), -- 2.20.1