/// Write the file to its destination with the chosen permissions.
Write the file to its destination with the chosen permissions.
Write the file to its destination with the chosen permissions .
pub fn replace_with_perms(self, permissions: Permissions) -> Result<()> {
        let subdir = self.subdir();
        let fd = self.fd.as_fd();
        let procself_fd = rustix::io::proc_self_fd()?;
        let fdnum = rustix::path::DecInt::from_fd(&fd);
        let mut attempts = 0u32;
        let tempname = loop {
            attempts = attempts.saturating_add(1);
            if attempts == u32::MAX {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::AlreadyExists,
                    "too many temporary files exist",
                ));
            }
            let name = new_name();
            match rustix::fs::linkat(
                &procself_fd,
                fdnum.as_c_str(),
                subdir,
                &name,
                AtFlags::SYMLINK_FOLLOW,
            ) {
                Ok(()) => break name,
                Err(e) if e == rustix::io::Error::EXIST => continue,
                Err(e) => return Err(e.into()),
            }
        };
        self.fd.set_permissions(permissions)?;
        // We could avoid the error here if we infallibly constructed the uuid as CString.
        let tempname = rustix::ffi::ZString::new(tempname)?;
        // TODO: panic-safe cleanup of the tempfile.  But I think the only case where we could panic is OOM
        match rustix::fs::renameat(subdir, &tempname, subdir, self.name) {
            Ok(()) => Ok(()),
            Err(e) => {
                // Clean up our temporary file, but ignore errors doing so because
                // the real error was probably from the rename(), and we don't want to mask it.
                let _ = rustix::fs::unlinkat(subdir, tempname, AtFlags::empty());
                Err(e.into())
            }
        }
    }
pub fn replace_with_perms ( self , permissions : Permissions ) -> Result < ( ) > { let subdir = self . subdir ( ) ; let fd = self . fd . as_fd ( ) ; let procself_fd = rustix :: io :: proc_self_fd ( ) ? ; let fdnum = rustix :: path :: DecInt :: from_fd ( & fd ) ; let mut attempts = 0u32 ; let tempname = loop { attempts = attempts . saturating_add ( 1 ) ; if attempts == u32 :: MAX { return Err ( std :: io :: Error :: new ( std :: io :: ErrorKind :: AlreadyExists , " " , ) ) ; } let name = new_name ( ) ; match rustix :: fs :: linkat ( & procself_fd , fdnum . as_c_str ( ) , subdir , & name , AtFlags :: SYMLINK_FOLLOW , ) { Ok ( ( ) ) => break name , Err ( e ) if e == rustix :: io :: Error :: EXIST => continue , Err ( e ) => return Err ( e . into ( ) ) , } } ; self . fd . set_permissions ( permissions ) ? ; // We could avoid the error here if we infallibly constructed the uuid as CString. let tempname = rustix :: ffi :: ZString :: new ( tempname ) ? ; // TODO: panic-safe cleanup of the tempfile.  But I think the only case where we could panic is OOM match rustix :: fs :: renameat ( subdir , & tempname , subdir , self . name ) { Ok ( ( ) ) => Ok ( ( ) ) , Err ( e ) => { // Clean up our temporary file, but ignore errors doing so because // the real error was probably from the rename(), and we don't want to mask it. let _ = rustix :: fs :: unlinkat ( subdir , tempname , AtFlags :: empty ( ) ) ; Err ( e . into ( ) ) } } }
Write the file to its destination with the chosen permissions.
Write the file to its destination with the chosen permissions .
self
permissions
Permissions