1 # Copyright (C) 2001, 2003, 2004, 2006, 2008, 2009, 2010 Free Software
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Written by Akim Demaille <akim@freefriends.org>.
19 ###############################################################
20 # The main copy of this file is in Automake's CVS repository. #
21 # Updates should be sent to automake-patches@gnu.org. #
22 ###############################################################
24 package Automake::XFile;
28 Automake::XFile - supply object methods for filehandles with error handling
34 $fh = new Automake::XFile;
36 # No need to check $FH: we died if open failed.
39 # No need to check the return value of close: we died if it failed.
41 $fh = new Automake::XFile "> file";
42 # No need to check $FH: we died if new failed.
46 $fh = new Automake::XFile "file", "r";
47 # No need to check $FH: we died if new failed.
50 undef $fh; # automatically closes the file and checks for errors.
52 $fh = new Automake::XFile "file", O_WRONLY | O_APPEND;
53 # No need to check $FH: we died if new failed.
59 undef $fh; # automatically closes the file and checks for errors.
65 C<Automake::XFile> inherits from C<IO::File>. It provides the method
66 C<name> returning the file name. It provides dying versions of the
67 methods C<close>, C<lock> (corresponding to C<flock>), C<new>,
68 C<open>, C<seek>, and C<truncate>. It also overrides the C<getline>
69 and C<getlines> methods to translate C<\r\n> to C<\n>.
75 use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD @ISA);
80 use Automake::ChannelDefs;
81 use Automake::Channels qw(msg);
82 use Automake::FileUtils;
87 @ISA = qw(IO::File Exporter DynaLoader);
91 @EXPORT = @IO::File::EXPORT;
94 # Make all Fcntl O_XXX and LOCK_XXX constants available for importing
96 my @O = grep /^(LOCK|O)_/, @Fcntl::EXPORT, @Fcntl::EXPORT_OK;
97 Fcntl->import (@O); # first we import what we want to export
105 =item C<$fh = new Automake::XFile ([$expr, ...]>
107 Constructor a new XFile object. Additional arguments
108 are passed to C<open>, if any.
115 my $class = ref $type || $type || "Automake::XFile";
116 my $fh = $class->SUPER::new ();
124 =item C<$fh-E<gt>open ([$file, ...])>
126 Open a file, passing C<$file> and further arguments to C<IO::File::open>.
127 Die if opening fails. Store the name of the file. Use binmode for writing.
136 # WARNING: Gross hack: $FH is a typeglob: use its hash slot to store
137 # the `name' of the file we are opening. See the example with
138 # io_socket_timeout in IO::Socket for more, and read Graham's
139 # comment in IO::Handle.
140 ${*$fh}{'autom4te_xfile_file'} = "$file";
142 if (!$fh->SUPER::open (@_))
144 fatal "cannot open $file: $!";
147 # In case we're running under MSWindows, don't write with CRLF.
148 # (This circumvents a bug in at least Cygwin bash where the shell
149 # parsing fails on lines ending with the continuation character '\'
151 binmode $fh if $file =~ /^\s*>/;
154 =item C<$fh-E<gt>close>
156 Close the file, handling errors.
163 if (!$fh->SUPER::close (@_))
165 my $file = $fh->name;
166 Automake::FileUtils::handle_exec_errors $file
168 fatal "cannot close $file: $!";
172 =item C<$line = $fh-E<gt>getline>
174 Read and return a line from the file. Ensure C<\r\n> is translated to
175 C<\n> on input files.
179 # Some Win32/perl installations fail to translate \r\n to \n on input
180 # so we do that here.
183 local $_ = $_[0]->SUPER::getline;
184 # Perform a _global_ replacement: $_ may can contains many lines
185 # in slurp mode ($/ = undef).
186 s/\015\012/\n/gs if defined $_;
190 =item C<@lines = $fh-E<gt>getlines>
192 Slurp lines from the files.
200 push @res, $line while $line = $_[0]->getline;
204 =item C<$name = $fh-E<gt>name>
206 Return the name of the file.
213 return ${*$fh}{'autom4te_xfile_file'};
216 =item C<$fh-E<gt>lock>
218 Lock the file using C<flock>. If locking fails for reasons other than
219 C<flock> being unsupported, then error out if C<$ENV{'MAKEFLAGS'}> indicates
220 that we are spawned from a parallel C<make>.
226 my ($fh, $mode) = @_;
227 # Cannot use @_ here.
229 # Unless explicitly configured otherwise, Perl implements its `flock' with the
230 # first of flock(2), fcntl(2), or lockf(3) that works. These can fail on
231 # NFS-backed files, with ENOLCK (GNU/Linux) or EOPNOTSUPP (FreeBSD); we
232 # usually ignore these errors. If $ENV{MAKEFLAGS} suggests that a parallel
233 # invocation of `make' has invoked the tool we serve, report all locking
234 # failures and abort.
236 # On Unicos, flock(2) and fcntl(2) over NFS hang indefinitely when `lockd' is
237 # not running. NetBSD NFS clients silently grant all locks. We do not
238 # attempt to defend against these dangers.
240 # -j is for parallel BSD make, -P is for parallel HP-UX make.
241 if (!flock ($fh, $mode))
243 my $make_j = (exists $ENV{'MAKEFLAGS'}
244 && " -$ENV{'MAKEFLAGS'}" =~ / (-[BdeikrRsSw]*[jP]|--[jP]|---?jobs)/);
245 my $note = "\nforgo `make -j' or use a file system that supports locks";
246 my $file = $fh->name;
248 msg ($make_j ? 'fatal' : 'unsupported',
249 "cannot lock $file with mode $mode: $!" . ($make_j ? $note : ""))
250 if $make_j || !($!{ENOLCK} || $!{EOPNOTSUPP});
254 =item C<$fh-E<gt>seek ($position, [$whence])>
256 Seek file to C<$position>. Die if seeking fails.
263 # Cannot use @_ here.
264 if (!seek ($fh, $_[0], $_[1]))
266 my $file = $fh->name;
267 fatal "cannot rewind $file with @_: $!";
271 =item C<$fh-E<gt>truncate ($len)>
273 Truncate the file to length C<$len>. Die on failure.
280 if (!truncate ($fh, $len))
282 my $file = $fh->name;
283 fatal "cannot truncate $file at $len: $!";
292 L<perlop/"I/O Operators">,
299 Derived from IO::File.pm by Akim Demaille E<lt>F<akim@freefriends.org>E<gt>.
305 ### Setup "GNU" style for perl-mode and cperl-mode.
307 ## perl-indent-level: 2
308 ## perl-continued-statement-offset: 2
309 ## perl-continued-brace-offset: 0
310 ## perl-brace-offset: 0
311 ## perl-brace-imaginary-offset: 0
312 ## perl-label-offset: -2
313 ## cperl-indent-level: 2
314 ## cperl-brace-offset: 0
315 ## cperl-continued-brace-offset: 0
316 ## cperl-label-offset: -2
317 ## cperl-extra-newline-before-brace: t
318 ## cperl-merge-trailing-else: nil
319 ## cperl-continued-statement-offset: 2