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 E4A0B75DE1 for ; Wed, 14 Jul 2021 09:30:57 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D252CC955 for ; Wed, 14 Jul 2021 09:30:27 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (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 3DE13C94C for ; Wed, 14 Jul 2021 09:30:27 +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 0B4E9416C9 for ; Wed, 14 Jul 2021 09:30:27 +0200 (CEST) From: Dominik Csapak To: pbs-devel@lists.proxmox.com Date: Wed, 14 Jul 2021 09:30:26 +0200 Message-Id: <20210714073026.757274-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.602 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pbs-devel] [PATCH proxmox-backup v2] fix #3526: correctly filter tasks with 'since' and 'until' 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: Wed, 14 Jul 2021 07:30:57 -0000 The previous assumption was that the Tasks returned by the Iterator are sorted by the starttime, but that is not actually the case, and could never have been, since we append the tasks into the log when they are finished (not started) and running tasks are always iterated first. To correctly filter (and simplify the the api call) we forgo the combinators, and use a for loop instead. This way we only have to do the since/until checks only once per Task, but have to do the start/limit counting ourselves. Signed-off-by: Dominik Csapak --- changes from v1: * replace iterator combinators with for loop src/api2/node/tasks.rs | 76 ++++++++++++++++++++++++------------------ 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/src/api2/node/tasks.rs b/src/api2/node/tasks.rs index 34e71af1..1602de6f 100644 --- a/src/api2/node/tasks.rs +++ b/src/api2/node/tasks.rs @@ -451,63 +451,73 @@ pub fn list_tasks( let list = TaskListInfoIterator::new(running)?; let limit = if limit > 0 { limit as usize } else { usize::MAX }; - let result: Vec = list - .skip_while(|info| { - match (info, until) { - (Ok(info), Some(until)) => info.upid.starttime > until, - (Ok(_), None) => false, - (Err(_), _) => false, - } - }) - .take_while(|info| { - match (info, since) { - (Ok(info), Some(since)) => info.upid.starttime > since, - (Ok(_), None) => true, - (Err(_), _) => false, - } - }) - .filter_map(|info| { + let mut skipped = 0; + let mut result: Vec = Vec::new(); + + for info in list { let info = match info { Ok(info) => info, - Err(_) => return None, + Err(_) => break, }; + if let Some(until) = until { + if info.upid.starttime > until { + continue; + } + } + + if let Some(since) = since { + if let Some(ref state) = info.state { + if state.endtime() < since { + // we reached the tasks that ended before our 'since' + // so we can stop iterating + break; + } + } + if info.upid.starttime < since { + continue; + } + } + if !list_all && check_task_access(&auth_id, &info.upid).is_err() { - return None; + continue; } if let Some(needle) = &userfilter { - if !info.upid.auth_id.to_string().contains(needle) { return None; } + if !info.upid.auth_id.to_string().contains(needle) { continue; } } if let Some(store) = store { - if !check_job_store(&info.upid, store) { - return None; - } + if !check_job_store(&info.upid, store) { continue; } } if let Some(typefilter) = &typefilter { - if !info.upid.worker_type.contains(typefilter) { - return None; - } + if !info.upid.worker_type.contains(typefilter) { continue; } } match (&info.state, &statusfilter) { - (Some(_), _) if running => return None, - (Some(crate::server::TaskState::OK { .. }), _) if errors => return None, + (Some(_), _) if running => continue, + (Some(crate::server::TaskState::OK { .. }), _) if errors => continue, (Some(state), Some(filters)) => { if !filters.contains(&state.tasktype()) { - return None; + continue; } }, - (None, Some(_)) => return None, + (None, Some(_)) => continue, _ => {}, } - Some(info.into()) - }).skip(start as usize) - .take(limit) - .collect(); + if skipped < start as usize { + skipped += 1; + continue; + } + + result.push(info.into()); + + if result.len() >= limit { + break; + } + } let mut count = result.len() + start as usize; if !result.is_empty() && result.len() >= limit { // we have a 'virtual' entry as long as we have any new -- 2.30.2