Convert-Pheno

 view release on metacpan or  search on metacpan

lib/Convert/Pheno/IO/Atomic.pm  view on Meta::CPAN

            my $mode = $stat[2] & 07777;
            chmod $mode, $staged
              or die "Could not preserve permissions for staged output <$staged>: $!\n";
        }
    }

    return 1 if rename $staged, $target;
    my $rename_error = $!;

    # Windows cannot replace an existing path with rename(). Keep a recoverable
    # backup until the fully written staged file has taken its place.
    if ( $^O eq 'MSWin32' && -e $target ) {
        my $backup = create_staged_path($target);
        unlink $backup
          or die "Could not prepare backup path for <$target>: $!\n";
        rename $target, $backup
          or die "Could not preserve existing output <$target>: $!\n";

        if ( rename $staged, $target ) {
            warn "Could not remove output backup <$backup>: $!\n"
              unless unlink $backup;
            return 1;
        }

        my $replacement_error = $!;
        rename $backup, $target
          or die "Could not replace <$target> ($replacement_error) or restore it ($!)\n";
        die "Could not replace output <$target>: $replacement_error\n";
    }

    die "Could not replace output <$target>: $rename_error\n";
}

sub write_atomically {
    my ( $target, $writer ) = @_;
    die "Atomic output writer must be a code reference\n"



( run in 0.718 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )