From 9a0d0ff774e123d1a5c23bd174f89d1906ffd873 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 4 Apr 2019 13:49:35 +0200 Subject: [PATCH] server/worker_task.rs: implement UPID parser --- src/server/worker_task.rs | 44 +++++++++++++++++++++++++++++++++++---- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/server/worker_task.rs b/src/server/worker_task.rs index 4452b253..56d63dbb 100644 --- a/src/server/worker_task.rs +++ b/src/server/worker_task.rs @@ -1,5 +1,6 @@ use failure::*; use lazy_static::lazy_static; +use regex::Regex; use chrono::Local; use tokio::sync::oneshot; @@ -7,10 +8,13 @@ use futures::*; use std::sync::{Arc, Mutex}; use std::collections::HashMap; use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering, ATOMIC_USIZE_INIT}; +use serde_json::{json, Value}; use crate::tools::{self, FileLogger}; -const PROXMOX_BACKUP_TASK_DIR: &str = "/var/log/proxmox-backup/tasks"; +macro_rules! PROXMOX_BACKUP_TASK_DIR { () => ("/var/log/proxmox-backup/tasks") } +macro_rules! PROXMOX_BACKUP_TASK_LOCK_FN { () => (concat!(PROXMOX_BACKUP_TASK_DIR!(), "/.active.lock")) } +macro_rules! PROXMOX_BACKUP_ACTIVE_TASK_FN { () => (concat!(PROXMOX_BACKUP_TASK_DIR!(), "/active")) } lazy_static! { static ref WORKER_TASK_LIST: Mutex>> = Mutex::new(HashMap::new()); @@ -30,6 +34,38 @@ pub struct UPID { pub node: String, } +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) { + + return Ok(UPID { + pid: i32::from_str_radix(&cap["pid"], 16).unwrap(), + pstart: u64::from_str_radix(&cap["pstart"], 16).unwrap(), + starttime: i64::from_str_radix(&cap["starttime"], 16).unwrap(), + task_id: usize::from_str_radix(&cap["task_id"], 16).unwrap(), + worker_type: cap["wtype"].to_string(), + worker_id: if cap["wid"].is_empty() { None } else { Some(cap["wid"].to_string()) }, + username: cap["username"].to_string(), + node: cap["node"].to_string(), + }); + } else { + bail!("unable to parse UPID '{}'", s); + } + + } +} + impl std::fmt::Display for UPID { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { @@ -39,8 +75,8 @@ impl std::fmt::Display for UPID { // Note: pstart can be > 32bit if uptime > 497 days, so this can result in // more that 8 characters for pstart - write!(f, "UPID:{}:{:08X}:{:08X}:{:08X}:{}:{}:{}:", - self.node, self.pid, self.pstart, self.starttime, self.worker_type, wid, self.username) + write!(f, "UPID:{}:{:08X}:{:08X}:{:08X}:{:08X}:{}:{}:{}:", + self.node, self.pid, self.pstart, self.task_id, self.starttime, self.worker_type, wid, self.username) } } @@ -115,7 +151,7 @@ impl WorkerTask { node: tools::nodename().to_owned(), }; - let mut path = std::path::PathBuf::from(PROXMOX_BACKUP_TASK_DIR); + let mut path = std::path::PathBuf::from(PROXMOX_BACKUP_TASK_DIR!()); path.push(format!("{:02X}", upid.pstart % 256)); let _ = std::fs::create_dir_all(&path); // ignore errors here