| 1 | # -*- perl -*- | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # Author: Slaven Rezic | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||
| 6 | # Copyright (C) 2017,2018,2021 Slaven Rezic. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 7 | # This package is free software; you can redistribute it and/or | ||||||||||||||||||||||||||||||||||||||||||
| 8 | # modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||
| 10 | # Mail: slaven@rezic.de | ||||||||||||||||||||||||||||||||||||||||||
| 11 | # WWW: http://www.rezic.de/eserte/ | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | package Doit::File; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our $VERSION = '0.024'; | |||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use Doit::Log; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Doit::Util qw(copy_stat new_scope_cleanup); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | sub new { bless {}, shift } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub functions { qw(file_atomic_write) } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
functions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | sub file_atomic_write { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 20 | ▶ | my($doit, $file, $code, %opts) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_atomic_write: called CC=34 SCAR=37.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 20 | ▶ | if (!defined $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | error "File parameter is missing"; | |||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 19 | ▶ | if (!defined $code) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | error "Code parameter is missing"; | |||||||||||||||||||||||||||||||||||||||||
| 34 | } elsif (ref $code ne 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | error "Code parameter should be an anonymous subroutine or subroutine reference"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | 17 | require File::Basename; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 17 | require Cwd; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 17 | my $dest_dir = File::Basename::dirname($file); | |||||||||||||||||||||||||||||||||||||||||
| 41 | 17 | eval { $dest_dir = Cwd::realpath($dest_dir) }; # may fail on some platforms (e.g. Windows) if $dest_dir does not exist | |||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | 17 | ▶ | my $tmp_suffix = delete $opts{tmpsuffix} || '.tmp'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $opts{'tmpsuffix'} || '.tmp'
MC/DC100%1/1
delete $opts{'tmpsuffix'} || '.tmp'
delete $opts{'tmpsuffix'}
Truth table100%2/2
A
A= delete $opts{'tmpsuffix'}
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 17 | my $tmp_dir = delete $opts{tmpdir}; | |||||||||||||||||||||||||||||||||||||||||
| 45 | 17 | ▶ | if (!defined $tmp_dir) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 46 | 14 | ▶ | if (defined $dest_dir && -d $dest_dir) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $dest_dir and -d $dest_dir
MC/DC50%1/2
defined $dest_dir and -d $dest_dir
defined $dest_dir
-d $dest_dir
Truth table66%2/3
A and B
A= defined $dest_dirB= -d $dest_dir
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 13 | $tmp_dir = $dest_dir; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 1 | ▶ | if (eval { require File::Spec; 1 }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | $tmp_dir = File::Spec->tmpdir; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | $tmp_dir = '/tmp'; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 17 | my $mode = delete $opts{mode}; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 17 | my $check_change = delete $opts{check_change}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | 17 | ▶ | error "Unhandled options: " . join(" ", %opts) if %opts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 16 | my($tmp_fh,$tmp_file); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 16 | my(@cleanup_files, @cleanup_fhs); | |||||||||||||||||||||||||||||||||||||||||
| 62 | my $tempfile_scope = new_scope_cleanup { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 16 | ▶ | for my $cleanup_fh (@cleanup_fhs) { # required on Windows, otherwise unlink won't work | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 15 | ▶ | close $cleanup_fh if fileno($cleanup_fh); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 16 | for my $cleanup_file (@cleanup_files) { | |||||||||||||||||||||||||||||||||||||||||
| 67 | 15 | ▶ | unlink $cleanup_file if -e $cleanup_file; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 16 | }; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 16 | ▶ | if ($tmp_dir eq '/dev/full') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | # This is just used for testing error on close() | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | $tmp_file = '/dev/full'; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | open $tmp_fh, '>', $tmp_file | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | or error "Can't write to $tmp_file: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 15 | require File::Temp; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 15 | ($tmp_fh,$tmp_file) = File::Temp::tempfile(SUFFIX => $tmp_suffix, DIR => $tmp_dir, EXLOCK => 0); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 15 | push @cleanup_files, $tmp_file; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 15 | push @cleanup_fhs, $tmp_fh; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 15 | ▶ | if (defined $mode) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | $doit->chmod({quiet => 1}, $mode, $tmp_file); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 13 | $doit->chmod({quiet => 1}, 0666 & ~umask, $tmp_file); | |||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 15 | ▶ | if ($tmp_dir ne $dest_dir) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | my @stat_destdir = stat $dest_dir; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | ▶ | if (@stat_destdir) { # may fail in dry-run mode if $dest_dir is missing | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 2 | ▶ | if ($^O =~ /bsd/ || $^O eq 'darwin' || ($stat_destdir[2] & 02000)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$^O =~ /bsd/ or $^O eq "darwin" or $stat_destdir[2] & 1024
$^O =~ /bsd/ or $^O eq "darwin"
MC/DC33%1/3
$^O =~ /bsd/ or $^O eq "darwin" or $stat_destdir[2] & 1024
$^O =~ /bsd/
$^O eq "darwin"
$stat_destdir[2] & 1024
Truth table50%2/4
A or B or C
A= $^O =~ /bsd/B= $^O eq "darwin"C= $stat_destdir[2] & 1024
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | $doit->chown({quiet => 1 }, undef, $stat_destdir[5], $tmp_file); | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 16 | my $same_fs = do { | |||||||||||||||||||||||||||||||||||||||||
| 95 | 16 | my $tmp_dev = (stat($tmp_file))[0]; | |||||||||||||||||||||||||||||||||||||||||
| 96 | 16 | my $dest_dev = (stat($dest_dir))[0]; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | ▶ | no warnings 'uninitialized'; # $dest_dev may be undefined in dry-run mode | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 16 | $tmp_dev == $dest_dev; | |||||||||||||||||||||||||||||||||||||||||
| 99 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 16 | ▶ | if ($same_fs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 14 | ▶ | if (-e $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 8 | copy_stat $file, $tmp_file, ownership => 1, mode => !defined $mode; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | require File::Copy; # for move() | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 16 | eval { $code->($tmp_fh, $tmp_file) }; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 16 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | error $@; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 15 | ▶ | if ($] < 5.010001) { $! = 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | $tmp_fh->close | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 15 | ▶ | or error "Error while closing temporary file $tmp_file: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 14 | ▶ | if ($] < 5.010001 && $! != 0) { # at least perl 5.8.8 and 5.8.9 are buggy and do not detect errors at close time --- 5.10.1 is correct | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$] < "5.010001" and $! != 0
MC/DC0%0/2
$] < "5.010001" and $! != 0
$] < "5.010001"
$! != 0
Truth table33%1/3
A and B
A= $] < "5.010001"B= $! != 0
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | error "Error while closing temporary file $tmp_file: $!"; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 14 | ▶ | if ($check_change) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | require File::Compare; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | if (File::Compare::compare($tmp_file, $file) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | # unchanged | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 13 | ▶ | if ($same_fs) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 12 | _make_writeable($doit, $file, 'rename'); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 12 | $doit->rename($tmp_file, $file); | |||||||||||||||||||||||||||||||||||||||||
| 132 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 133 | 1 | my @dest_stat; | |||||||||||||||||||||||||||||||||||||||||
| 134 | 1 | ▶ | if (-e $file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | @dest_stat = stat($file) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | or warning "Cannot stat $file: $! (cannot preserve permissions)"; # XXX should this be an error? | ||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | _make_writeable($doit, $file, 'File::Copy::move'); | |||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | $doit->move($tmp_file, $file); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | if (@dest_stat) { # In dry-run mode effectively a noop | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | $dest_stat[2] = $mode if defined $mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | copy_stat [@dest_stat], $file, ownership => 1, mode => 1; | |||||||||||||||||||||||||||||||||||||||||
| 143 | } elsif (defined $mode) { | ||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | $dest_stat[2] = $mode if defined $mode; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | copy_stat [@dest_stat], $file, mode => 1; | |||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 13 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | sub _make_writeable { | ||||||||||||||||||||||||||||||||||||||||||
| 153 | 12 | ▶ | my($doit, $file, $for) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_make_writeable: called CC=5 SCAR=27.0
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 12 | ▶ | return if $for eq 'rename' && !Doit::IS_WIN; # don't need to do anything | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$for eq "rename" and
MC/DC0%0/1
$for eq "rename" and
$for eq "rename"
Truth table50%1/2
A
A= $for eq "rename"
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my @s = stat($file); | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return if !@s; # not stat-able -> file does not exist yet? | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | my $old_mode = $s[2] & 07777; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | return if ($old_mode & 0200); # already writable | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | $doit->chmod(($old_mode | 0200), $file); | |||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | __END__ | ||||||||||||||||||||||||||||||||||||||||||