public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Wolfgang Bumiller <w.bumiller@proxmox.com>
To: Christian Ebner <c.ebner@proxmox.com>
Cc: pbs-devel@lists.proxmox.com
Subject: Re: [pbs-devel] [PATCH v2 proxmox-backup 1/2] fix: #4761: unlink existing entries for hard/symlinks when overwrite
Date: Fri, 4 Aug 2023 14:20:40 +0200	[thread overview]
Message-ID: <7jplu7x73x2ebk4owsu2dpibgykjqgccbzdmc4w4loerpiks5k@wlihalt5h7c7> (raw)
In-Reply-To: <20230801103412.182490-2-c.ebner@proxmox.com>

On Tue, Aug 01, 2023 at 12:34:11PM +0200, Christian Ebner wrote:
> Creating symlinks or hardlinks might fail if a directory entry with the
> same name is already present on the filesystem during restore.
> 
> When the overwrite flag is given, on failure unlink the existing entry
> (except directories) and retry hard/symlink creation.
> 
> Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
> ---
> changes since v1:
> * rebased to current master
> 
>  pbs-client/src/pxar/extract.rs | 39 +++++++++++++++++++++++++++++++---
>  1 file changed, 36 insertions(+), 3 deletions(-)
> 
> diff --git a/pbs-client/src/pxar/extract.rs b/pbs-client/src/pxar/extract.rs
> index 4dbaf52d..c1e7b417 100644
> --- a/pbs-client/src/pxar/extract.rs
> +++ b/pbs-client/src/pxar/extract.rs
> @@ -547,7 +547,21 @@ impl Extractor {
>          link: &OsStr,
>      ) -> Result<(), Error> {
>          let parent = self.parent_fd()?;
> -        nix::unistd::symlinkat(link, Some(parent), file_name)?;
> +
> +        match nix::unistd::symlinkat(link, Some(parent), file_name) {
> +            Ok(()) => {}
> +            Err(nix::errno::Errno::EEXIST) => {
> +                if !self.overwrite {
> +                    return Err(nix::errno::Errno::EEXIST.into());
> +                }

The above could be shortened to a pattern gaurd:

               Err(nix::errno::Errno::EEXIST) if self.overwrite => {

then we don't need the extra `return Err()` since we just run into the
final `Err(err)` case.

> +                // Never unlink directories
> +                let flag = nix::unistd::UnlinkatFlags::NoRemoveDir;
> +                nix::unistd::unlinkat(Some(parent), file_name, flag)?;
> +                nix::unistd::symlinkat(link, Some(parent), file_name)?;
> +            }
> +            Err(err) => return Err(err.into())
> +        }
> +
>          metadata::apply_at(
>              self.feature_flags,
>              metadata,
> @@ -564,13 +578,32 @@ impl Extractor {
>          let parent = self.parent_fd()?;
>          let root = self.dir_stack.root_dir_fd()?;
>          let target = CString::new(link.as_bytes())?;
> -        nix::unistd::linkat(
> +
> +        match nix::unistd::linkat(
>              Some(root.as_raw_fd()),
>              target.as_c_str(),
>              Some(parent),
>              file_name,
>              nix::unistd::LinkatFlags::NoSymlinkFollow,
> -        )?;

We can avoid some duplication by moving the `linkat` call into a
closure:
        let dolink = || {
            nix::unistd::linkat(
                Some(root.as_raw_fd()),
                target.as_c_str(),
                Some(parent),
                file_name,
                nix::unistd::LinkatFlags::NoSymlinkFollow,
            )
        };
        match dolink() {
            ...
            nix::unistd::unlinkat(Some(parent), file_name, flag)?;
            dolink()?;
        }
        ...

> +        ) {
> +            Ok(()) => {}
> +            Err(nix::errno::Errno::EEXIST) => {
> +                if !self.overwrite {
> +                    return Err(nix::errno::Errno::EEXIST.into());
> +                }
> +                // Never unlink directories
> +                let flag = nix::unistd::UnlinkatFlags::NoRemoveDir;
> +                nix::unistd::unlinkat(Some(parent), file_name, flag)?;
> +                nix::unistd::linkat(
> +                    Some(root.as_raw_fd()),
> +                    target.as_c_str(),
> +                    Some(parent),
> +                    file_name,
> +                    nix::unistd::LinkatFlags::NoSymlinkFollow,
> +                )?;
> +            }
> +            Err(err) => return Err(err.into())
> +        }
>  
>          Ok(())
>      }
> -- 
> 2.39.2




  reply	other threads:[~2023-08-04 12:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-01 10:34 [pbs-devel] [PATCH v2 proxmox-backup 0/2] Introduce bitflags for overwrite Christian Ebner
2023-08-01 10:34 ` [pbs-devel] [PATCH v2 proxmox-backup 1/2] fix: #4761: unlink existing entries for hard/symlinks when overwrite Christian Ebner
2023-08-04 12:20   ` Wolfgang Bumiller [this message]
2023-08-04 12:32     ` Wolfgang Bumiller
2023-08-01 10:34 ` [pbs-devel] [PATCH v2 proxmox-backup 2/2] fix: #4761: introduce overwrite bitflags for fine grained overwrites Christian Ebner
2023-08-04 12:30   ` Wolfgang Bumiller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7jplu7x73x2ebk4owsu2dpibgykjqgccbzdmc4w4loerpiks5k@wlihalt5h7c7 \
    --to=w.bumiller@proxmox.com \
    --cc=c.ebner@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal