| 1 | #+############################################################################## | ||||||||||||||||||||
| 2 | # # | ||||||||||||||||||||
| 3 | # File: No/Worries/File.pm # | ||||||||||||||||||||
| 4 | # # | ||||||||||||||||||||
| 5 | # Description: file handling without worries # | ||||||||||||||||||||
| 6 | # # | ||||||||||||||||||||
| 7 | #-############################################################################## | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # | ||||||||||||||||||||
| 10 | # module definition | ||||||||||||||||||||
| 11 | # | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | package No::Worries::File; | ||||||||||||||||||||
| 14 | 16 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 16 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 16 | our $VERSION = "1.7"; | |||||||||||||||||||
| 17 | 16 | our $REVISION = sprintf("%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # | ||||||||||||||||||||
| 20 | # used modules | ||||||||||||||||||||
| 21 | # | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 16 | ▶ | use No::Worries qw($_IntegerRegexp); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 16 | ▶ | use No::Worries::Die qw(dief); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 25 | 16 | ▶ | use No::Worries::Dir qw(dir_ensure dir_parent); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 26 | 16 | ▶ | use No::Worries::Export qw(export_control); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 27 | 16 | ▶ | use No::Worries::Proc qw(proc_run); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 28 | 16 | ▶ | use Params::Validate qw(validate :types); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # | ||||||||||||||||||||
| 31 | # global variables | ||||||||||||||||||||
| 32 | # | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 16 | our($DefaultBufSize); | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # | ||||||||||||||||||||
| 37 | # open() helper | ||||||||||||||||||||
| 38 | # | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 48 | ▶ | sub _open ($$$) { | ||||||||||||||||||
|
Subroutine
_open: called CC=7 SCAR=19.5
| |||||||||||||||||||||
| 41 | 48 | my($path, $mode, $opt) = @_; | |||||||||||||||||||
| 42 | 48 | my($fh); | |||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 48 | ▶ | return($opt->{handle}) if $opt->{handle}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | ## no critic 'InputOutput::RequireBriefOpen' | ||||||||||||||||||||
| 46 | 46 | ▶ | open($fh, $mode, $path) or dief("cannot open(%s): %s", $path, $!); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 47 | 45 | ▶ | if ($opt->{binmode}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 48 | binmode($fh, $opt->{binmode}) | ||||||||||||||||||||
| 49 | 2 | ▶ | or dief("cannot binmode(%s, %s): %s", $path, $opt->{binmode}, $!); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | } elsif ($opt->{binary}) { | ||||||||||||||||||||
| 51 | 2 | ▶ | binmode($fh) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | or dief("cannot binmode(%s): %s", $path, $!); | ||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | 45 | return($fh); | |||||||||||||||||||
| 55 | } | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | # | ||||||||||||||||||||
| 58 | # sysread() helper | ||||||||||||||||||||
| 59 | # | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 22 | ▶ | sub _read ($$$$) { | ||||||||||||||||||
|
Subroutine
_read: called CC=11 SCAR=24.0
| |||||||||||||||||||||
| 62 | 22 | my($path, $fh, $data, $bufsize) = @_; | |||||||||||||||||||
| 63 | 22 | my($done, $ref, $result); | |||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 22 | $done = -1; | |||||||||||||||||||
| 66 | 22 | ▶ | $ref = $data ? ref($data) : ""; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 67 | 22 | ▶ | if ($ref eq "SCALAR") { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 68 | # by reference | ||||||||||||||||||||
| 69 | 1 | ${ $data } = ""; | |||||||||||||||||||
| 70 | 1 | ▶ | while ($done) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 71 | 2 | $done = sysread($fh, ${ $data }, $bufsize, length(${ $data })); | |||||||||||||||||||
| 72 | 2 | ▶ | dief("cannot sysread(%s): %s", $path, $!) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 73 | unless defined($done); | ||||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | 1 | $result = $data; | |||||||||||||||||||
| 76 | } elsif ($ref eq "CODE") { | ||||||||||||||||||||
| 77 | # by code | ||||||||||||||||||||
| 78 | 1 | ▶ | while ($done) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | 2 | $result = ""; | |||||||||||||||||||
| 80 | 2 | $done = sysread($fh, $result, $bufsize); | |||||||||||||||||||
| 81 | 2 | ▶ | dief("cannot sysread(%s): %s", $path, $!) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | unless defined($done); | ||||||||||||||||||||
| 83 | 2 | ▶ | $data->($result) if $done; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | 1 | $result = $data->(""); | |||||||||||||||||||
| 86 | } else { | ||||||||||||||||||||
| 87 | # normal | ||||||||||||||||||||
| 88 | 20 | $result = ""; | |||||||||||||||||||
| 89 | 20 | ▶ | while ($done) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 90 | 39 | $done = sysread($fh, $result, $bufsize, length($result)); | |||||||||||||||||||
| 91 | 39 | ▶ | dief("cannot sysread(%s): %s", $path, $!) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | unless defined($done); | ||||||||||||||||||||
| 93 | } | ||||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | 22 | return(\$result); | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # | ||||||||||||||||||||
| 99 | # syswrite() helper | ||||||||||||||||||||
| 100 | # | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 25 | ▶ | sub _write ($$$$) { | ||||||||||||||||||
|
Subroutine
_write: called CC=10 SCAR=23.1
| |||||||||||||||||||||
| 103 | 25 | my($path, $fh, $data, $bufsize) = @_; | |||||||||||||||||||
| 104 | 25 | my($ref, $offset, $length, $done, $chunk); | |||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 25 | $offset = 0; | |||||||||||||||||||
| 107 | 25 | $ref = ref($data); | |||||||||||||||||||
| 108 | 25 | ▶ | if ($ref eq "SCALAR") { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 109 | # by reference | ||||||||||||||||||||
| 110 | 2 | $length = length(${ $data }); | |||||||||||||||||||
| 111 | 2 | ▶ | while ($length) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 2 | $done = syswrite($fh, ${ $data }, $bufsize, $offset); | |||||||||||||||||||
| 113 | 2 | ▶ | dief("cannot syswrite(%s): %s", $path, $!) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | unless defined($done); | ||||||||||||||||||||
| 115 | 2 | $length -= $done; | |||||||||||||||||||
| 116 | 2 | $offset += $done; | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | } elsif ($ref eq "CODE") { | ||||||||||||||||||||
| 119 | # by code | ||||||||||||||||||||
| 120 | 1 | while (1) { | |||||||||||||||||||
| 121 | 4 | $chunk = $data->(); | |||||||||||||||||||
| 122 | 4 | $length = length($chunk); | |||||||||||||||||||
| 123 | 4 | ▶ | last unless $length; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 124 | 3 | $offset = 0; | |||||||||||||||||||
| 125 | 3 | ▶ | while ($length) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 126 | 3 | $done = syswrite($fh, $chunk, $bufsize, $offset); | |||||||||||||||||||
| 127 | 3 | ▶ | dief("cannot syswrite(%s): %s", $path, $!) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | unless defined($done); | ||||||||||||||||||||
| 129 | 3 | $length -= $done; | |||||||||||||||||||
| 130 | 3 | $offset += $done; | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | } else { | ||||||||||||||||||||
| 134 | # normal | ||||||||||||||||||||
| 135 | 22 | $length = length($data); | |||||||||||||||||||
| 136 | 22 | ▶ | while ($length) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 21 | $done = syswrite($fh, $data, $bufsize, $offset); | |||||||||||||||||||
| 138 | 21 | ▶ | dief("cannot syswrite(%s): %s", $path, $!) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 139 | unless defined($done); | ||||||||||||||||||||
| 140 | 21 | $length -= $done; | |||||||||||||||||||
| 141 | 21 | $offset += $done; | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | # | ||||||||||||||||||||
| 147 | # common read/write options | ||||||||||||||||||||
| 148 | # | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 16 | my %file_rw_options = ( | |||||||||||||||||||
| 151 | binary => { optional => 1, type => BOOLEAN }, | ||||||||||||||||||||
| 152 | binmode => { optional => 1, type => SCALAR }, | ||||||||||||||||||||
| 153 | bufsize => { optional => 1, type => SCALAR, regex => $_IntegerRegexp }, | ||||||||||||||||||||
| 154 | handle => { optional => 1, type => HANDLE }, | ||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||
| 156 | |||||||||||||||||||||
| 157 | # | ||||||||||||||||||||
| 158 | # read from a file | ||||||||||||||||||||
| 159 | # | ||||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 16 | my %file_read_options = (%file_rw_options, | |||||||||||||||||||
| 162 | data => { optional => 1, type => SCALARREF | CODEREF }, | ||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | 23 | ▶ | sub file_read ($@) { | ||||||||||||||||||
|
Subroutine
file_read: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||
| 166 | 23 | my($path, %option, $fh, $result); | |||||||||||||||||||
| 167 | |||||||||||||||||||||
| 168 | 23 | $path = shift(@_); | |||||||||||||||||||
| 169 | 23 | ▶ | %option = validate(@_, \%file_read_options) if @_; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 170 | 23 | ▶ | $option{bufsize} ||= $DefaultBufSize; | ||||||||||||||||||
|
Condition33%1/3
$option{"bufsize"} ||= $No::Worries::File::DefaultBufSize
MC/DC0%0/2
$option{"bufsize"} ||= $No::Worries::File::DefaultBufSize
$option{"bufsize"}
$No::Worries::File::DefaultBufSize
Truth table33%1/3
A ||= B
A= $option{"bufsize"}B= $No::Worries::File::DefaultBufSize
| |||||||||||||||||||||
| 171 | 23 | $fh = _open($path, "<", \%option); | |||||||||||||||||||
| 172 | 22 | $result = _read($path, $fh, $option{data}, $option{bufsize}); | |||||||||||||||||||
| 173 | 22 | ▶ | close($fh) or dief("cannot close(%s): %s", $path, $!); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | 22 | return(${ $result }); | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | # | ||||||||||||||||||||
| 178 | # write to a file | ||||||||||||||||||||
| 179 | # | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | 16 | my %file_write_options = (%file_rw_options, | |||||||||||||||||||
| 182 | data => { optional => 0, type => SCALAR | SCALARREF | CODEREF }, | ||||||||||||||||||||
| 183 | ); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 25 | ▶ | sub file_write ($@) { | ||||||||||||||||||
|
Subroutine
file_write: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||
| 186 | 25 | my($path, %option, $fh); | |||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 25 | $path = shift(@_); | |||||||||||||||||||
| 189 | 25 | %option = validate(@_, \%file_write_options); | |||||||||||||||||||
| 190 | 25 | ▶ | $option{bufsize} ||= $DefaultBufSize; | ||||||||||||||||||
|
Condition33%1/3
$option{"bufsize"} ||= $No::Worries::File::DefaultBufSize
MC/DC0%0/2
$option{"bufsize"} ||= $No::Worries::File::DefaultBufSize
$option{"bufsize"}
$No::Worries::File::DefaultBufSize
Truth table33%1/3
A ||= B
A= $option{"bufsize"}B= $No::Worries::File::DefaultBufSize
| |||||||||||||||||||||
| 191 | 25 | $fh = _open($path, ">", \%option); | |||||||||||||||||||
| 192 | 25 | _write($path, $fh, $option{data}, $option{bufsize}); | |||||||||||||||||||
| 193 | 25 | ▶ | close($fh) or dief("cannot close(%s): %s", $path, $!); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | # | ||||||||||||||||||||
| 197 | # update a file (high level wrapper for text files) | ||||||||||||||||||||
| 198 | # | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | 16 | my %file_update_options = ( | |||||||||||||||||||
| 201 | data => { optional => 0, type => SCALAR }, | ||||||||||||||||||||
| 202 | diff => { optional => 1, type => BOOLEAN }, | ||||||||||||||||||||
| 203 | noaction => { optional => 1, type => BOOLEAN }, | ||||||||||||||||||||
| 204 | silent => { optional => 1, type => BOOLEAN }, | ||||||||||||||||||||
| 205 | ); | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | 0 | ▶ | sub file_update ($@) { | ||||||||||||||||||
|
Subroutine
file_update: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||||||||
| 208 | 0 | my($path, %option, $data, $fh); | |||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | 0 | $path = shift(@_); | |||||||||||||||||||
| 211 | 0 | %option = validate(@_, \%file_update_options); | |||||||||||||||||||
| 212 | 0 | ▶ | if (-f $path) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 213 | 0 | $data = file_read($path); | |||||||||||||||||||
| 214 | 0 | ▶ | if ($data eq $option{data}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 215 | printf("did not update %s (already up-to-date)\n", $path) | ||||||||||||||||||||
| 216 | 0 | ▶ | unless $option{silent}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 217 | } else { | ||||||||||||||||||||
| 218 | proc_run( | ||||||||||||||||||||
| 219 | command => [ qw(diff -u), $path, "-" ], | ||||||||||||||||||||
| 220 | stdin => \$option{data}, | ||||||||||||||||||||
| 221 | 0 | ▶ | ) if $option{diff}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 222 | 0 | ▶ | if ($option{noaction}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 223 | printf("did not update %s (noaction)\n", $path) | ||||||||||||||||||||
| 224 | 0 | ▶ | unless $option{silent}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 225 | } else { | ||||||||||||||||||||
| 226 | 0 | file_write($path, data => $option{data}); | |||||||||||||||||||
| 227 | printf("updated %s\n", $path) | ||||||||||||||||||||
| 228 | 0 | ▶ | unless $option{silent}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||
| 232 | proc_run( | ||||||||||||||||||||
| 233 | command => [ qw(diff -u /dev/null), "-" ], | ||||||||||||||||||||
| 234 | stdin => \$option{data}, | ||||||||||||||||||||
| 235 | 0 | ▶ | ) if $option{diff}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 236 | 0 | ▶ | if ($option{noaction}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 237 | printf("did not create %s (noaction)\n", $path) | ||||||||||||||||||||
| 238 | 0 | ▶ | unless $option{silent}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||
| 240 | 0 | dir_ensure(dir_parent($path)); | |||||||||||||||||||
| 241 | 0 | file_write($path, data => $option{data}); | |||||||||||||||||||
| 242 | printf("created %s\n", $path) | ||||||||||||||||||||
| 243 | 0 | ▶ | unless $option{silent}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 244 | } | ||||||||||||||||||||
| 245 | } | ||||||||||||||||||||
| 246 | } | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | # | ||||||||||||||||||||
| 249 | # module initialization | ||||||||||||||||||||
| 250 | # | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | 16 | $DefaultBufSize = 1_048_576; # 1MB | |||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | # | ||||||||||||||||||||
| 255 | # export control | ||||||||||||||||||||
| 256 | # | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | 16 | ▶ | sub import : method { | ||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 259 | 16 | my($pkg, %exported); | |||||||||||||||||||
| 260 | |||||||||||||||||||||
| 261 | 16 | $pkg = shift(@_); | |||||||||||||||||||
| 262 | 16 | grep($exported{$_}++, map("file_$_", qw(read write update))); | |||||||||||||||||||
| 263 | 16 | export_control(scalar(caller()), $pkg, \%exported, @_); | |||||||||||||||||||
| 264 | } | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | 16 | 1; | |||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | __DATA__ | ||||||||||||||||||||