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 7207B6168D for ; Tue, 18 Aug 2020 13:53:37 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 6F0A512F81 for ; Tue, 18 Aug 2020 13:53:07 +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 0930512F78 for ; Tue, 18 Aug 2020 13:53:06 +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 C66F944668 for ; Tue, 18 Aug 2020 13:53:05 +0200 (CEST) From: Mira Limbeck To: pbs-devel@lists.proxmox.com Date: Tue, 18 Aug 2020 13:52:59 +0200 Message-Id: <20200818115259.12558-2-m.limbeck@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200818115259.12558-1-m.limbeck@proxmox.com> References: <20200818115259.12558-1-m.limbeck@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.100 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.379 Envelope sender has no MX or A DNS records 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_NONE 0.001 SPF: sender does not publish an 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. [catalog.rs] Subject: [pbs-devel] [PATCH v5 proxmox-backup] Replace all occurences of open() with O_TMPFILE 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 11:53:37 -0000 with the tempfile() helper function from proxmox::tools. This abstracts away the open() and adds a fallback to mkstemp() should open() with O_TMPFILE fail. This helps in getting the backup client to work under WSL (Windows Subsystem for Linux). Signed-off-by: Mira Limbeck --- This requires the tempfile() addition in the proxmox crate to work. v5: - no changes v4: - no changes v3: - no changes src/bin/proxmox_backup_client/catalog.rs | 20 ++++---------------- src/client/backup_reader.rs | 21 ++++----------------- src/client/backup_writer.rs | 15 +++------------ 3 files changed, 11 insertions(+), 45 deletions(-) diff --git a/src/bin/proxmox_backup_client/catalog.rs b/src/bin/proxmox_backup_client/catalog.rs index b419728e..15df232b 100644 --- a/src/bin/proxmox_backup_client/catalog.rs +++ b/src/bin/proxmox_backup_client/catalog.rs @@ -1,4 +1,3 @@ -use std::os::unix::fs::OpenOptionsExt; use std::io::{Seek, SeekFrom}; use std::sync::Arc; @@ -6,6 +5,7 @@ use anyhow::{bail, format_err, Error}; use serde_json::Value; use proxmox::api::{api, cli::*}; +use proxmox::tools::fs::tempfile; use proxmox_backup::tools; @@ -103,11 +103,7 @@ async fn dump_catalog(param: Value) -> Result { let mut reader = BufferedDynamicReader::new(index, chunk_reader); - let mut catalogfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut catalogfile = tempfile()?; std::io::copy(&mut reader, &mut catalogfile) .map_err(|err| format_err!("unable to download catalog - {}", err))?; @@ -192,11 +188,7 @@ async fn catalog_shell(param: Value) -> Result<(), Error> { true, ).await?; - let mut tmpfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut tmpfile = tempfile()?; let (manifest, _) = client.download_manifest().await?; @@ -224,11 +216,7 @@ async fn catalog_shell(param: Value) -> Result<(), Error> { let file_info = manifest.lookup_file_info(&CATALOG_NAME)?; let chunk_reader = RemoteChunkReader::new(client.clone(), crypt_config, file_info.chunk_crypt_mode(), most_used); let mut reader = BufferedDynamicReader::new(index, chunk_reader); - let mut catalogfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut catalogfile = tempfile()?; std::io::copy(&mut reader, &mut catalogfile) .map_err(|err| format_err!("unable to download catalog - {}", err))?; diff --git a/src/client/backup_reader.rs b/src/client/backup_reader.rs index d4185716..45370141 100644 --- a/src/client/backup_reader.rs +++ b/src/client/backup_reader.rs @@ -2,13 +2,12 @@ use anyhow::{format_err, Error}; use std::io::{Read, Write, Seek, SeekFrom}; use std::fs::File; use std::sync::Arc; -use std::os::unix::fs::OpenOptionsExt; use chrono::{DateTime, Utc}; use futures::future::AbortHandle; use serde_json::{json, Value}; -use proxmox::tools::digest_to_hex; +use proxmox::tools::{digest_to_hex, fs::tempfile}; use crate::backup::*; @@ -148,11 +147,7 @@ impl BackupReader { name: &str, ) -> Result, Error> { - let mut tmpfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut tmpfile = tempfile()?; self.download(name, &mut tmpfile).await?; @@ -174,11 +169,7 @@ impl BackupReader { name: &str, ) -> Result { - let mut tmpfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut tmpfile = tempfile()?; self.download(name, &mut tmpfile).await?; @@ -202,11 +193,7 @@ impl BackupReader { name: &str, ) -> Result { - let mut tmpfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut tmpfile = tempfile()?; self.download(name, &mut tmpfile).await?; diff --git a/src/client/backup_writer.rs b/src/client/backup_writer.rs index 38686f67..e04ad56a 100644 --- a/src/client/backup_writer.rs +++ b/src/client/backup_writer.rs @@ -1,5 +1,4 @@ use std::collections::HashSet; -use std::os::unix::fs::OpenOptionsExt; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::{Arc, Mutex}; @@ -12,7 +11,7 @@ use serde_json::{json, Value}; use tokio::io::AsyncReadExt; use tokio::sync::{mpsc, oneshot}; -use proxmox::tools::digest_to_hex; +use proxmox::tools::{digest_to_hex, fs::tempfile}; use super::merge_known_chunks::{MergedChunkInfo, MergeKnownChunks}; use crate::backup::*; @@ -408,11 +407,7 @@ impl BackupWriter { known_chunks: Arc>>, ) -> Result { - let mut tmpfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut tmpfile = tempfile()?; let param = json!({ "archive-name": archive_name }); self.h2.download("previous", Some(param), &mut tmpfile).await?; @@ -443,11 +438,7 @@ impl BackupWriter { known_chunks: Arc>>, ) -> Result { - let mut tmpfile = std::fs::OpenOptions::new() - .write(true) - .read(true) - .custom_flags(libc::O_TMPFILE) - .open("/tmp")?; + let mut tmpfile = tempfile()?; let param = json!({ "archive-name": archive_name }); self.h2.download("previous", Some(param), &mut tmpfile).await?; -- 2.20.1