123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- package Automake::XFile;
- use 5.006;
- use strict;
- use vars qw($VERSION @EXPORT @EXPORT_OK $AUTOLOAD @ISA);
- use Carp;
- use Errno;
- use IO::File;
- use File::Basename;
- use Automake::ChannelDefs;
- use Automake::Channels qw(msg);
- use Automake::FileUtils;
- require Exporter;
- require DynaLoader;
- @ISA = qw(IO::File Exporter DynaLoader);
- $VERSION = "1.2";
- @EXPORT = @IO::File::EXPORT;
- eval {
-
- require Fcntl;
- my @O = grep /^(LOCK|O)_/, @Fcntl::EXPORT, @Fcntl::EXPORT_OK;
- Fcntl->import (@O);
- push (@EXPORT, @O);
- };
- sub new
- {
- my $type = shift;
- my $class = ref $type || $type || "Automake::XFile";
- my $fh = $class->SUPER::new ();
- if (@_)
- {
- $fh->open (@_);
- }
- $fh;
- }
- sub open
- {
- my $fh = shift;
- my ($file, $mode) = @_;
-
-
-
-
- ${*$fh}{'autom4te_xfile_file'} = "$file";
- if (!$fh->SUPER::open (@_))
- {
- fatal "cannot open $file: $!";
- }
-
-
-
-
-
-
-
-
- binmode $fh
- if (defined $mode && $mode =~ /^[+>wa]/ or $file =~ /^\s*>/);
- }
- sub close
- {
- my $fh = shift;
- if (!$fh->SUPER::close (@_))
- {
- my $file = $fh->name;
- Automake::FileUtils::handle_exec_errors $file
- unless $!;
- fatal "cannot close $file: $!";
- }
- }
- sub getline
- {
- local $_ = $_[0]->SUPER::getline;
-
-
- s/\015\012/\n/gs if defined $_;
- return $_;
- }
- sub getlines
- {
- my @res = ();
- my $line;
- push @res, $line while $line = $_[0]->getline;
- return @res;
- }
- sub name
- {
- my $fh = shift;
- return ${*$fh}{'autom4te_xfile_file'};
- }
- sub lock
- {
- my ($fh, $mode) = @_;
-
-
-
-
-
-
-
-
-
-
-
-
-
- if (!flock ($fh, $mode))
- {
- my $make_j = (exists $ENV{'MAKEFLAGS'}
- && " -$ENV{'MAKEFLAGS'}" =~ / (-[BdeikrRsSw]*[jP]|--[jP]|---?jobs)/);
- my $note = "\nforgo \"make -j\" or use a file system that supports locks";
- my $file = $fh->name;
- msg ($make_j ? 'fatal' : 'unsupported',
- "cannot lock $file with mode $mode: $!" . ($make_j ? $note : ""))
- if $make_j || !($!{ENOLCK} || $!{EOPNOTSUPP});
- }
- }
- sub seek
- {
- my $fh = shift;
-
- if (!seek ($fh, $_[0], $_[1]))
- {
- my $file = $fh->name;
- fatal "cannot rewind $file with @_: $!";
- }
- }
- sub truncate
- {
- my ($fh, $len) = @_;
- if (!truncate ($fh, $len))
- {
- my $file = $fh->name;
- fatal "cannot truncate $file at $len: $!";
- }
- }
- 1;
|