[pbs-devel] [PATCH backup v3] fix-3699: pbs-client/tools: use xdg basedirectories for tmp files

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Feb 5 17:15:28 CET 2024


please keep to the commonly used `fix #id: ...` format for the subject,
the correct that would "client" here, and could be slightly reworded to
better convey what's done here, e.g. something like:

fix #3699: client: prefer xdg cache directory for tmp files

Am 01/02/2024 um 14:49 schrieb Maximiliano Sandoval:
> Adds a helper to create temporal files in XDG_CACHE_HOME. If the we
> cannot use that path, we fallback to /tmp as before.
> 
> Signed-off-by: Maximiliano Sandoval <m.sandoval at proxmox.com>
> ---
> Differences from v2:
>  - Files are created at XDG_CACHE_HOME directly
>  - If opening fails, we try opening at /tmp
>  - We check if the directory exists as part of the error handling of open()
> 
>  pbs-client/src/backup_reader.rs      | 31 ++++++++--------------------
>  pbs-client/src/backup_writer.rs      | 13 ++----------
>  pbs-client/src/tools/mod.rs          | 26 +++++++++++++++++++++++
>  proxmox-backup-client/src/catalog.rs | 19 +++--------------
>  4 files changed, 40 insertions(+), 49 deletions(-)
> 
> diff --git a/pbs-client/src/backup_reader.rs b/pbs-client/src/backup_reader.rs
> index 36d8ebcf..6483f5b2 100644
> --- a/pbs-client/src/backup_reader.rs
> +++ b/pbs-client/src/backup_reader.rs

> @@ -141,18 +140,14 @@ impl BackupReader {
>  
>      /// Download a .blob file
>      ///
> -    /// This creates a temporary file in /tmp (using O_TMPFILE). The data is verified using
> -    /// the provided manifest.
> +    /// This creates a temporary file (using O_TMPFILE). The data is verified

I'd keep the location info, or better, refer to the underlying
"create_tmp_file" function with a docs link so that it can be easily
checked out w.r.t. semantics.

> +    /// using the provided manifest.



> diff --git a/pbs-client/src/tools/mod.rs b/pbs-client/src/tools/mod.rs
> index 1b0123a3..a721fbc4 100644
> --- a/pbs-client/src/tools/mod.rs
> +++ b/pbs-client/src/tools/mod.rs

> @@ -526,3 +528,27 @@ pub fn place_xdg_file(
>          .and_then(|base| base.place_config_file(file_name).map_err(Error::from))
>          .with_context(|| format!("failed to place {} in xdg home", description))
>  }
> +
> +/// Creates a temporary file (created with O_TMPFILE) in either XDG_CACHE_HOME
> +/// if the directory exists, otherwise it will be created in /tmp otherwise.

duplicate "otherwise", could be fixed up on applying though.

> +pub fn create_tmp_file() -> std::io::Result<std::fs::File> {

Hmm, wondering if "create_xdg_tmp_file" could be slightly better here,
but the fallback behavior is not the best fit with that name, so no
hard feelings from my side.

> +    static TMP_PATH: OnceLock<std::path::PathBuf> = OnceLock::new();
> +    let tmp_path = TMP_PATH.get_or_init(|| {
> +        xdg::BaseDirectories::new()
> +            .map(|base| base.xdg_cache_dir())

I'm really not to sure if the cache one is the best fit, but it certainly
isn't wrong either.
What I'd like though is some sentence w.r.t. choosing this  over the
runtime one in the commit message, i.e., one that states that there isn't
an explicit one for temporary files and that both, run and cache ones,
would be OK choices, but we choose the cache one <arbitrarily|$reasons>

> +            .unwrap_or_else(|| std::path::PathBuf::from("/tmp"))
> +    });
> +
> +    let mut open_opts_binding = std::fs::OpenOptions::new();
> +    let builder = open_opts_binding
> +        .write(true)
> +        .read(true)
> +        .custom_flags(libc::O_TMPFILE);
> +    builder.open(tmp_path).or_else(|err| {
> +        if tmp_path != std::path::Path::new("/tmp") {
> +            builder.open("/tmp")
> +        } else {
> +            Err(err)
> +        }
> +    })
> +}






More information about the pbs-devel mailing list