| 1 |
# Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation; either version 2, or (at your option) |
| 6 |
# any later version. |
| 7 |
|
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
|
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 |
|
| 16 |
# Written by Akim Demaille <[email protected]>. |
| 17 |
|
| 18 |
################################################################## |
| 19 |
# The master copy of this file is in Automake's source repository. |
| 20 |
# Please send updates to [email protected]. |
| 21 |
################################################################## |
| 22 |
|
| 23 |
package Autom4te::XFile; |
| 24 |
|
| 25 |
=head1 NAME |
| 26 |
|
| 27 |
Autom4te::XFile - supply object methods for filehandles with error handling |
| 28 |
|
| 29 |
=head1 SYNOPSIS |
| 30 |
|
| 31 |
use Autom4te::XFile; |
| 32 |
|
| 33 |
$fh = new Autom4te::XFile; |
| 34 |
$fh->open ("file", "<"); |
| 35 |
# No need to check $FH: we died if open failed. |
| 36 |
print <$fh>; |
| 37 |
$fh->close; |
| 38 |
# No need to check the return value of close: we died if it failed. |
| 39 |
|
| 40 |
$fh = new Autom4te::XFile "file", ">"; |
| 41 |
# No need to check $FH: we died if new failed. |
| 42 |
print $fh "bar\n"; |
| 43 |
$fh->close; |
| 44 |
|
| 45 |
$fh = new Autom4te::XFile "file", "r"; |
| 46 |
# No need to check $FH: we died if new failed. |
| 47 |
defined $fh |
| 48 |
print <$fh>; |
| 49 |
undef $fh; # automatically closes the file and checks for errors. |
| 50 |
|
| 51 |
$fh = new Autom4te::XFile "file", O_WRONLY | O_APPEND; |
| 52 |
# No need to check $FH: we died if new failed. |
| 53 |
print $fh "corge\n"; |
| 54 |
|
| 55 |
$pos = $fh->getpos; |
| 56 |
$fh->setpos ($pos); |
| 57 |
|
| 58 |
undef $fh; # automatically closes the file and checks for errors. |
| 59 |
|
| 60 |
autoflush STDOUT 1; |
| 61 |
|
| 62 |
=head1 DESCRIPTION |
| 63 |
|
| 64 |
C<Autom4te::XFile> inherits from C<IO::File>. It provides the method |
| 65 |
C<name> returning the file name. It provides dying versions of the |
| 66 |
methods C<close>, C<lock> (corresponding to C<flock>), C<new>, |
| 67 |
C<open>, C<seek>, and C<truncate>. It also overrides the C<getline> |
| 68 |
and C<getlines> methods to translate C<\r\n> to C<\n>. |
| 69 |
|
| 70 |
=cut |
| 71 |
|
| 72 |
use 5.006; use strict; use warnings; |
| 73 |
use Errno; |
| 74 |
use Exporter; |
| 75 |
use IO::File; |
| 76 |
|
| 77 |
use Autom4te::ChannelDefs; |
| 78 |
use Autom4te::Channels qw (msg); |
| 79 |
use Autom4te::FileUtils; |
| 80 |
|
| 81 |
our @ISA = qw(Exporter IO::File); |
| 82 |
our @EXPORT = @IO::File::EXPORT; |
| 83 |
our $VERSION = "1.2"; |
| 84 |
|
| 85 |
eval { |
| 86 |
# Make all Fcntl O_XXX and LOCK_XXX constants available for importing |
| 87 |
require Fcntl; |
| 88 |
my @O = grep /^(LOCK|O)_/, @Fcntl::EXPORT, @Fcntl::EXPORT_OK; |
| 89 |
Fcntl->import (@O); # first we import what we want to export |
| 90 |
push (@EXPORT, @O); |
| 91 |
}; |
| 92 |
|
| 93 |
=head2 Methods |
| 94 |
|
| 95 |
=over |
| 96 |
|
| 97 |
=item C<$fh = new Autom4te::XFile ([$expr, ...]> |
| 98 |
|
| 99 |
Constructor a new XFile object. Additional arguments |
| 100 |
are passed to C<open>, if any. |
| 101 |
|
| 102 |
=cut |
| 103 |
|
| 104 |
sub new |
| 105 |
{ |
| 106 |
my $type = shift; |
| 107 |
my $class = ref $type || $type || "Autom4te::XFile"; |
| 108 |
my $fh = $class->SUPER::new (); |
| 109 |
if (@_) |
| 110 |
{ |
| 111 |
$fh->open (@_); |
| 112 |
} |
| 113 |
$fh; |
| 114 |
} |
| 115 |
|
| 116 |
=item C<$fh-E<gt>open ([$file, ...])> |
| 117 |
|
| 118 |
Open a file, passing C<$file> and further arguments to C<IO::File::open>. |
| 119 |
Die if opening fails. Store the name of the file. Use binmode for writing. |
| 120 |
|
| 121 |
=cut |
| 122 |
|
| 123 |
sub open |
| 124 |
{ |
| 125 |
my $fh = shift; |
| 126 |
my ($file, $mode) = @_; |
| 127 |
|
| 128 |
# WARNING: Gross hack: $FH is a typeglob: use its hash slot to store |
| 129 |
# the 'name' of the file we are opening. See the example with |
| 130 |
# io_socket_timeout in IO::Socket for more, and read Graham's |
| 131 |
# comment in IO::Handle. |
| 132 |
${*$fh}{'autom4te_xfile_file'} = "$file"; |
| 133 |
|
| 134 |
if (!$fh->SUPER::open (@_)) |
| 135 |
{ |
| 136 |
fatal "cannot open $file: $!"; |
| 137 |
} |
| 138 |
|
| 139 |
# In case we're running under MSWindows, don't write with CRLF. |
| 140 |
# (This circumvents a bug in at least Cygwin bash where the shell |
| 141 |
# parsing fails on lines ending with the continuation character '\' |
| 142 |
# and CRLF). |
| 143 |
# Correctly recognize usages like: |
| 144 |
# - open ($file, "w") |
| 145 |
# - open ($file, "+<") |
| 146 |
# - open (" >$file") |
| 147 |
binmode $fh |
| 148 |
if (defined $mode && $mode =~ /^[+>wa]/ or $file =~ /^\s*>/); |
| 149 |
} |
| 150 |
|
| 151 |
=item C<$fh-E<gt>close> |
| 152 |
|
| 153 |
Close the file, handling errors. |
| 154 |
|
| 155 |
=cut |
| 156 |
|
| 157 |
sub close |
| 158 |
{ |
| 159 |
my $fh = shift; |
| 160 |
if (!$fh->SUPER::close (@_)) |
| 161 |
{ |
| 162 |
my $file = $fh->name; |
| 163 |
Autom4te::FileUtils::handle_exec_errors $file |
| 164 |
unless $!; |
| 165 |
fatal "cannot close $file: $!"; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
=item C<$line = $fh-E<gt>getline> |
| 170 |
|
| 171 |
Read and return a line from the file. Ensure C<\r\n> is translated to |
| 172 |
C<\n> on input files. |
| 173 |
|
| 174 |
=cut |
| 175 |
|
| 176 |
# Some native Windows/perl installations fail to translate \r\n to \n on |
| 177 |
# input so we do that here. |
| 178 |
sub getline |
| 179 |
{ |
| 180 |
local $_ = $_[0]->SUPER::getline; |
| 181 |
# Perform a _global_ replacement: $_ may can contains many lines |
| 182 |
# in slurp mode ($/ = undef). |
| 183 |
s/\015\012/\n/gs if defined $_; |
| 184 |
return $_; |
| 185 |
} |
| 186 |
|
| 187 |
=item C<@lines = $fh-E<gt>getlines> |
| 188 |
|
| 189 |
Slurp lines from the files. |
| 190 |
|
| 191 |
=cut |
| 192 |
|
| 193 |
sub getlines |
| 194 |
{ |
| 195 |
my @res = (); |
| 196 |
my $line; |
| 197 |
push @res, $line while $line = $_[0]->getline; |
| 198 |
return @res; |
| 199 |
} |
| 200 |
|
| 201 |
=item C<$name = $fh-E<gt>name> |
| 202 |
|
| 203 |
Return the name of the file. |
| 204 |
|
| 205 |
=cut |
| 206 |
|
| 207 |
sub name |
| 208 |
{ |
| 209 |
my $fh = shift; |
| 210 |
return ${*$fh}{'autom4te_xfile_file'}; |
| 211 |
} |
| 212 |
|
| 213 |
=item C<$fh-E<gt>lock> |
| 214 |
|
| 215 |
Lock the file using C<flock>. If locking fails for reasons other than |
| 216 |
C<flock> being unsupported, then error out if C<$ENV{'MAKEFLAGS'}> indicates |
| 217 |
that we are spawned from a parallel C<make>. |
| 218 |
|
| 219 |
=cut |
| 220 |
|
| 221 |
sub lock |
| 222 |
{ |
| 223 |
my ($fh, $mode) = @_; |
| 224 |
# Cannot use @_ here. |
| 225 |
|
| 226 |
# Unless explicitly configured otherwise, Perl implements its 'flock' with the |
| 227 |
# first of flock(2), fcntl(2), or lockf(3) that works. These can fail on |
| 228 |
# NFS-backed files, with ENOLCK (GNU/Linux) or EOPNOTSUPP (FreeBSD) or |
| 229 |
# EINVAL (OpenIndiana, as per POSIX 1003.1-2017 fcntl spec); we |
| 230 |
# usually ignore these errors. If $ENV{MAKEFLAGS} suggests that a parallel |
| 231 |
# invocation of 'make' has invoked the tool we serve, report all locking |
| 232 |
# failures and abort. |
| 233 |
# |
| 234 |
# On Unicos, flock(2) and fcntl(2) over NFS hang indefinitely when 'lockd' is |
| 235 |
# not running. NetBSD NFS clients silently grant all locks. We do not |
| 236 |
# attempt to defend against these dangers. |
| 237 |
# |
| 238 |
# -j is for parallel BSD make, -P is for parallel HP-UX make. |
| 239 |
if (!flock ($fh, $mode)) |
| 240 |
{ |
| 241 |
my $make_j = (exists $ENV{'MAKEFLAGS'} |
| 242 |
&& " -$ENV{'MAKEFLAGS'}" =~ / (-[BdeikrRsSw]*[jP]|--[jP]|---?jobs)/); |
| 243 |
my $note = "\nforgo \"make -j\" or use a file system that supports locks"; |
| 244 |
my $file = $fh->name; |
| 245 |
|
| 246 |
msg ($make_j ? 'fatal' : 'unsupported', |
| 247 |
"cannot lock $file with mode $mode: $!" . ($make_j ? $note : "")) |
| 248 |
if $make_j || !($!{EINVAL} || $!{ENOLCK} || $!{EOPNOTSUPP}); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
=item C<$fh-E<gt>seek ($position, [$whence])> |
| 253 |
|
| 254 |
Seek file to C<$position>. Die if seeking fails. |
| 255 |
|
| 256 |
=cut |
| 257 |
|
| 258 |
sub seek |
| 259 |
{ |
| 260 |
my $fh = shift; |
| 261 |
# Cannot use @_ here. |
| 262 |
if (!seek ($fh, $_[0], $_[1])) |
| 263 |
{ |
| 264 |
my $file = $fh->name; |
| 265 |
fatal "cannot rewind $file with @_: $!"; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
=item C<$fh-E<gt>truncate ($len)> |
| 270 |
|
| 271 |
Truncate the file to length C<$len>. Die on failure. |
| 272 |
|
| 273 |
=cut |
| 274 |
|
| 275 |
sub truncate |
| 276 |
{ |
| 277 |
my ($fh, $len) = @_; |
| 278 |
if (!truncate ($fh, $len)) |
| 279 |
{ |
| 280 |
my $file = $fh->name; |
| 281 |
fatal "cannot truncate $file at $len: $!"; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
=back |
| 286 |
|
| 287 |
=head1 SEE ALSO |
| 288 |
|
| 289 |
L<perlfunc>, |
| 290 |
L<perlop/"I/O Operators">, |
| 291 |
L<IO::File> |
| 292 |
L<IO::Handle> |
| 293 |
L<IO::Seekable> |
| 294 |
|
| 295 |
=head1 HISTORY |
| 296 |
|
| 297 |
Derived from IO::File.pm by Akim Demaille E<lt>F<[email protected]>E<gt>. |
| 298 |
|
| 299 |
=cut |
| 300 |
|
| 301 |
1; |