1N/A# File/Copy.pm. Written in 1994 by Aaron Sherman <ajs@ajs.com>. This
1N/A# source code has been placed in the public domain by the author.
1N/A# Please be kind and preserve the documentation.
1N/A#
1N/A# Additions copyright 1996 by Charles Bailey. Permission is granted
1N/A# to distribute the revised code under the same terms as Perl itself.
1N/A
1N/Apackage File::Copy;
1N/A
1N/Ause 5.006;
1N/Ause strict;
1N/Ause warnings;
1N/Ause Carp;
1N/Ause File::Spec;
1N/Ause Config;
1N/Aour(@ISA, @EXPORT, @EXPORT_OK, $VERSION, $Too_Big, $Syscopy_is_copy);
1N/Asub copy;
1N/Asub syscopy;
1N/Asub cp;
1N/Asub mv;
1N/A
1N/A# Note that this module implements only *part* of the API defined by
1N/A# the File/Copy.pm module of the File-Tools-2.0 package. However, that
1N/A# package has not yet been updated to work with Perl 5.004, and so it
1N/A# would be a Bad Thing for the CPAN module to grab it and replace this
1N/A# module. Therefore, we set this module's version higher than 2.0.
1N/A$VERSION = '2.07';
1N/A
1N/Arequire Exporter;
1N/A@ISA = qw(Exporter);
1N/A@EXPORT = qw(copy move);
1N/A@EXPORT_OK = qw(cp mv);
1N/A
1N/A$Too_Big = 1024 * 1024 * 2;
1N/A
1N/Amy $macfiles;
1N/Aif ($^O eq 'MacOS') {
1N/A $macfiles = eval { require Mac::MoreFiles };
1N/A warn 'Mac::MoreFiles could not be loaded; using non-native syscopy'
1N/A if $@ && $^W;
1N/A}
1N/A
1N/Asub _catname {
1N/A my($from, $to) = @_;
1N/A if (not defined &basename) {
1N/A require File::Basename;
1N/A import File::Basename 'basename';
1N/A }
1N/A
1N/A if ($^O eq 'MacOS') {
1N/A # a partial dir name that's valid only in the cwd (e.g. 'tmp')
1N/A $to = ':' . $to if $to !~ /:/;
1N/A }
1N/A
1N/A return File::Spec->catfile($to, basename($from));
1N/A}
1N/A
1N/Asub copy {
1N/A croak("Usage: copy(FROM, TO [, BUFFERSIZE]) ")
1N/A unless(@_ == 2 || @_ == 3);
1N/A
1N/A my $from = shift;
1N/A my $to = shift;
1N/A
1N/A my $from_a_handle = (ref($from)
1N/A ? (ref($from) eq 'GLOB'
1N/A || UNIVERSAL::isa($from, 'GLOB')
1N/A || UNIVERSAL::isa($from, 'IO::Handle'))
1N/A : (ref(\$from) eq 'GLOB'));
1N/A my $to_a_handle = (ref($to)
1N/A ? (ref($to) eq 'GLOB'
1N/A || UNIVERSAL::isa($to, 'GLOB')
1N/A || UNIVERSAL::isa($to, 'IO::Handle'))
1N/A : (ref(\$to) eq 'GLOB'));
1N/A
1N/A if ($from eq $to) { # works for references, too
1N/A croak("'$from' and '$to' are identical (not copied)");
1N/A }
1N/A
1N/A if ((($Config{d_symlink} && $Config{d_readlink}) || $Config{d_link}) &&
1N/A !($^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'vms')) {
1N/A my @fs = stat($from);
1N/A if (@fs) {
1N/A my @ts = stat($to);
1N/A if (@ts && $fs[0] == $ts[0] && $fs[1] == $ts[1]) {
1N/A croak("'$from' and '$to' are identical (not copied)");
1N/A }
1N/A }
1N/A }
1N/A
1N/A if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) {
1N/A $to = _catname($from, $to);
1N/A }
1N/A
1N/A if (defined &syscopy && !$Syscopy_is_copy
1N/A && !$to_a_handle
1N/A && !($from_a_handle && $^O eq 'os2' ) # OS/2 cannot handle handles
1N/A && !($from_a_handle && $^O eq 'mpeix') # and neither can MPE/iX.
1N/A && !($from_a_handle && $^O eq 'MSWin32')
1N/A && !($from_a_handle && $^O eq 'MacOS')
1N/A && !($from_a_handle && $^O eq 'NetWare')
1N/A )
1N/A {
1N/A return syscopy($from, $to);
1N/A }
1N/A
1N/A my $closefrom = 0;
1N/A my $closeto = 0;
1N/A my ($size, $status, $r, $buf);
1N/A local($\) = '';
1N/A
1N/A my $from_h;
1N/A if ($from_a_handle) {
1N/A $from_h = $from;
1N/A } else {
1N/A $from = _protect($from) if $from =~ /^\s/s;
1N/A $from_h = \do { local *FH };
1N/A open($from_h, "< $from\0") or goto fail_open1;
1N/A binmode $from_h or die "($!,$^E)";
1N/A $closefrom = 1;
1N/A }
1N/A
1N/A my $to_h;
1N/A if ($to_a_handle) {
1N/A $to_h = $to;
1N/A } else {
1N/A $to = _protect($to) if $to =~ /^\s/s;
1N/A $to_h = \do { local *FH };
1N/A open($to_h,"> $to\0") or goto fail_open2;
1N/A binmode $to_h or die "($!,$^E)";
1N/A $closeto = 1;
1N/A }
1N/A
1N/A if (@_) {
1N/A $size = shift(@_) + 0;
1N/A croak("Bad buffer size for copy: $size\n") unless ($size > 0);
1N/A } else {
1N/A $size = tied(*$from_h) ? 0 : -s $from_h || 0;
1N/A $size = 1024 if ($size < 512);
1N/A $size = $Too_Big if ($size > $Too_Big);
1N/A }
1N/A
1N/A $! = 0;
1N/A for (;;) {
1N/A my ($r, $w, $t);
1N/A defined($r = sysread($from_h, $buf, $size))
1N/A or goto fail_inner;
1N/A last unless $r;
1N/A for ($w = 0; $w < $r; $w += $t) {
1N/A $t = syswrite($to_h, $buf, $r - $w, $w)
1N/A or goto fail_inner;
1N/A }
1N/A }
1N/A
1N/A close($to_h) || goto fail_open2 if $closeto;
1N/A close($from_h) || goto fail_open1 if $closefrom;
1N/A
1N/A # Use this idiom to avoid uninitialized value warning.
1N/A return 1;
1N/A
1N/A # All of these contortions try to preserve error messages...
1N/A fail_inner:
1N/A if ($closeto) {
1N/A $status = $!;
1N/A $! = 0;
1N/A close $to_h;
1N/A $! = $status unless $!;
1N/A }
1N/A fail_open2:
1N/A if ($closefrom) {
1N/A $status = $!;
1N/A $! = 0;
1N/A close $from_h;
1N/A $! = $status unless $!;
1N/A }
1N/A fail_open1:
1N/A return 0;
1N/A}
1N/A
1N/Asub move {
1N/A my($from,$to) = @_;
1N/A my($fromsz,$tosz1,$tomt1,$tosz2,$tomt2,$sts,$ossts);
1N/A
1N/A if (-d $to && ! -d $from) {
1N/A $to = _catname($from, $to);
1N/A }
1N/A
1N/A ($tosz1,$tomt1) = (stat($to))[7,9];
1N/A $fromsz = -s $from;
1N/A if ($^O eq 'os2' and defined $tosz1 and defined $fromsz) {
1N/A # will not rename with overwrite
1N/A unlink $to;
1N/A }
1N/A return 1 if rename $from, $to;
1N/A
1N/A # Did rename return an error even though it succeeded, because $to
1N/A # is on a remote NFS file system, and NFS lost the server's ack?
1N/A return 1 if defined($fromsz) && !-e $from && # $from disappeared
1N/A (($tosz2,$tomt2) = (stat($to))[7,9]) && # $to's there
1N/A ($tosz1 != $tosz2 or $tomt1 != $tomt2) && # and changed
1N/A $tosz2 == $fromsz; # it's all there
1N/A
1N/A ($tosz1,$tomt1) = (stat($to))[7,9]; # just in case rename did something
1N/A return 1 if copy($from,$to) && unlink($from);
1N/A ($sts,$ossts) = ($! + 0, $^E + 0);
1N/A
1N/A ($tosz2,$tomt2) = ((stat($to))[7,9],0,0) if defined $tomt1;
1N/A unlink($to) if !defined($tomt1) or $tomt1 != $tomt2 or $tosz1 != $tosz2;
1N/A ($!,$^E) = ($sts,$ossts);
1N/A return 0;
1N/A}
1N/A
1N/A*cp = \&copy;
1N/A*mv = \&move;
1N/A
1N/A
1N/Aif ($^O eq 'MacOS') {
1N/A *_protect = sub { MacPerl::MakeFSSpec($_[0]) };
1N/A} else {
1N/A *_protect = sub { "./$_[0]" };
1N/A}
1N/A
1N/A# &syscopy is an XSUB under OS/2
1N/Aunless (defined &syscopy) {
1N/A if ($^O eq 'VMS') {
1N/A *syscopy = \&rmscopy;
1N/A } elsif ($^O eq 'mpeix') {
1N/A *syscopy = sub {
1N/A return 0 unless @_ == 2;
1N/A # Use the MPE cp program in order to
1N/A # preserve MPE file attributes.
1N/A return system('/bin/cp', '-f', $_[0], $_[1]) == 0;
1N/A };
1N/A } elsif ($^O eq 'MSWin32') {
1N/A *syscopy = sub {
1N/A return 0 unless @_ == 2;
1N/A return Win32::CopyFile(@_, 1);
1N/A };
1N/A } elsif ($macfiles) {
1N/A *syscopy = sub {
1N/A my($from, $to) = @_;
1N/A my($dir, $toname);
1N/A
1N/A return 0 unless -e $from;
1N/A
1N/A if ($to =~ /(.*:)([^:]+):?$/) {
1N/A ($dir, $toname) = ($1, $2);
1N/A } else {
1N/A ($dir, $toname) = (":", $to);
1N/A }
1N/A
1N/A unlink($to);
1N/A Mac::MoreFiles::FSpFileCopy($from, $dir, $toname, 1);
1N/A };
1N/A } else {
1N/A $Syscopy_is_copy = 1;
1N/A *syscopy = \&copy;
1N/A }
1N/A}
1N/A
1N/A1;
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AFile::Copy - Copy files or filehandles
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A use File::Copy;
1N/A
1N/A copy("file1","file2") or die "Copy failed: $!";
1N/A copy("Copy.pm",\*STDOUT);
1N/A move("/dev1/fileA","/dev2/fileB");
1N/A
1N/A use POSIX;
1N/A use File::Copy cp;
1N/A
1N/A $n = FileHandle->new("/a/file","r");
1N/A cp($n,"x");'
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThe File::Copy module provides two basic functions, C<copy> and
1N/AC<move>, which are useful for getting the contents of a file from
1N/Aone place to another.
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AThe C<copy> function takes two
1N/Aparameters: a file to copy from and a file to copy to. Either
1N/Aargument may be a string, a FileHandle reference or a FileHandle
1N/Aglob. Obviously, if the first argument is a filehandle of some
1N/Asort, it will be read from, and if it is a file I<name> it will
1N/Abe opened for reading. Likewise, the second argument will be
1N/Awritten to (and created if need be). Trying to copy a file on top
1N/Aof itself is a fatal error.
1N/A
1N/AB<Note that passing in
1N/Afiles as handles instead of names may lead to loss of information
1N/Aon some operating systems; it is recommended that you use file
1N/Anames whenever possible.> Files are opened in binary mode where
1N/Aapplicable. To get a consistent behaviour when copying from a
1N/Afilehandle to a file, use C<binmode> on the filehandle.
1N/A
1N/AAn optional third parameter can be used to specify the buffer
1N/Asize used for copying. This is the number of bytes from the
1N/Afirst file, that wil be held in memory at any given time, before
1N/Abeing written to the second file. The default buffer size depends
1N/Aupon the file, but will generally be the whole file (up to 2Mb), or
1N/A1k for filehandles that do not reference files (eg. sockets).
1N/A
1N/AYou may use the syntax C<use File::Copy "cp"> to get at the
1N/A"cp" alias for this function. The syntax is I<exactly> the same.
1N/A
1N/A=item *
1N/A
1N/AThe C<move> function also takes two parameters: the current name
1N/Aand the intended name of the file to be moved. If the destination
1N/Aalready exists and is a directory, and the source is not a
1N/Adirectory, then the source file will be renamed into the directory
1N/Aspecified by the destination.
1N/A
1N/AIf possible, move() will simply rename the file. Otherwise, it copies
1N/Athe file to the new location and deletes the original. If an error occurs
1N/Aduring this copy-and-delete process, you may be left with a (possibly partial)
1N/Acopy of the file under the destination name.
1N/A
1N/AYou may use the "mv" alias for this function in the same way that
1N/Ayou may use the "cp" alias for C<copy>.
1N/A
1N/A=back
1N/A
1N/AFile::Copy also provides the C<syscopy> routine, which copies the
1N/Afile specified in the first parameter to the file specified in the
1N/Asecond parameter, preserving OS-specific attributes and file
1N/Astructure. For Unix systems, this is equivalent to the simple
1N/AC<copy> routine, which doesn't preserve OS-specific attributes. For
1N/AVMS systems, this calls the C<rmscopy> routine (see below). For OS/2
1N/Asystems, this calls the C<syscopy> XSUB directly. For Win32 systems,
1N/Athis calls C<Win32::CopyFile>.
1N/A
1N/AOn Mac OS (Classic), C<syscopy> calls C<Mac::MoreFiles::FSpFileCopy>,
1N/Aif available.
1N/A
1N/A=head2 Special behaviour if C<syscopy> is defined (OS/2, VMS and Win32)
1N/A
1N/AIf both arguments to C<copy> are not file handles,
1N/Athen C<copy> will perform a "system copy" of
1N/Athe input file to a new output file, in order to preserve file
1N/Aattributes, indexed file structure, I<etc.> The buffer size
1N/Aparameter is ignored. If either argument to C<copy> is a
1N/Ahandle to an opened file, then data is copied using Perl
1N/Aoperators, and no effort is made to preserve file attributes
1N/Aor record structure.
1N/A
1N/AThe system copy routine may also be called directly under VMS and OS/2
1N/Aas C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which
1N/Ais the routine that does the actual work for syscopy).
1N/A
1N/A=over 4
1N/A
1N/A=item rmscopy($from,$to[,$date_flag])
1N/A
1N/AThe first and second arguments may be strings, typeglobs, typeglob
1N/Areferences, or objects inheriting from IO::Handle;
1N/Athey are used in all cases to obtain the
1N/AI<filespec> of the input and output files, respectively. The
1N/Aname and type of the input file are used as defaults for the
1N/Aoutput file, if necessary.
1N/A
1N/AA new version of the output file is always created, which
1N/Ainherits the structure and RMS attributes of the input file,
1N/Aexcept for owner and protections (and possibly timestamps;
1N/Asee below). All data from the input file is copied to the
1N/Aoutput file; if either of the first two parameters to C<rmscopy>
1N/Ais a file handle, its position is unchanged. (Note that this
1N/Ameans a file handle pointing to the output file will be
1N/Aassociated with an old version of that file after C<rmscopy>
1N/Areturns, not the newly created version.)
1N/A
1N/AThe third parameter is an integer flag, which tells C<rmscopy>
1N/Ahow to handle timestamps. If it is E<lt> 0, none of the input file's
1N/Atimestamps are propagated to the output file. If it is E<gt> 0, then
1N/Ait is interpreted as a bitmask: if bit 0 (the LSB) is set, then
1N/Atimestamps other than the revision date are propagated; if bit 1
1N/Ais set, the revision date is propagated. If the third parameter
1N/Ato C<rmscopy> is 0, then it behaves much like the DCL COPY command:
1N/Aif the name or type of the output file was explicitly specified,
1N/Athen no timestamps are propagated, but if they were taken implicitly
1N/Afrom the input filespec, then all timestamps other than the
1N/Arevision date are propagated. If this parameter is not supplied,
1N/Ait defaults to 0.
1N/A
1N/ALike C<copy>, C<rmscopy> returns 1 on success. If an error occurs,
1N/Ait sets C<$!>, deletes the output file, and returns 0.
1N/A
1N/A=back
1N/A
1N/A=head1 RETURN
1N/A
1N/AAll functions return 1 on success, 0 on failure.
1N/A$! will be set if an error was encountered.
1N/A
1N/A=head1 NOTES
1N/A
1N/A=over 4
1N/A
1N/A=item *
1N/A
1N/AOn Mac OS (Classic), the path separator is ':', not '/', and the
1N/Acurrent directory is denoted as ':', not '.'. You should be careful
1N/Aabout specifying relative pathnames. While a full path always begins
1N/Awith a volume name, a relative pathname should always begin with a
1N/A':'. If specifying a volume name only, a trailing ':' is required.
1N/A
1N/AE.g.
1N/A
1N/A copy("file1", "tmp"); # creates the file 'tmp' in the current directory
1N/A copy("file1", ":tmp:"); # creates :tmp:file1
1N/A copy("file1", ":tmp"); # same as above
1N/A copy("file1", "tmp"); # same as above, if 'tmp' is a directory (but don't do
1N/A # that, since it may cause confusion, see example #1)
1N/A copy("file1", "tmp:file1"); # error, since 'tmp:' is not a volume
1N/A copy("file1", ":tmp:file1"); # ok, partial path
1N/A copy("file1", "DataHD:"); # creates DataHD:file1
1N/A
1N/A move("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one
1N/A # volume to another
1N/A
1N/A=back
1N/A
1N/A=head1 AUTHOR
1N/A
1N/AFile::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995,
1N/Aand updated by Charles Bailey I<E<lt>bailey@newman.upenn.eduE<gt>> in 1996.
1N/A
1N/A=cut
1N/A