home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / File / Copy.pm < prev    next >
Encoding:
Text File  |  1997-10-19  |  10.8 KB  |  359 lines  |  [TEXT/McPL]

  1. # File/Copy.pm. Written in 1994 by Aaron Sherman <ajs@ajs.com>. This
  2. # source code has been placed in the public domain by the author.
  3. # Please be kind and preserve the documentation.
  4. #
  5. # Additions copyright 1996 by Charles Bailey.  Permission is granted
  6. # to distribute the revised code under the same terms as Perl itself.
  7.  
  8. package File::Copy;
  9.  
  10. use strict;
  11. use Carp;
  12. use UNIVERSAL qw(isa);
  13. use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION $Too_Big
  14.         © &syscopy &cp &mv);
  15.  
  16. # Note that this module implements only *part* of the API defined by
  17. # the File/Copy.pm module of the File-Tools-2.0 package.  However, that
  18. # package has not yet been updated to work with Perl 5.004, and so it
  19. # would be a Bad Thing for the CPAN module to grab it and replace this
  20. # module.  Therefore, we set this module's version higher than 2.0.
  21. $VERSION = '2.02';
  22.  
  23. require Exporter;
  24. @ISA = qw(Exporter);
  25. @EXPORT = qw(copy move);
  26. @EXPORT_OK = qw(cp mv);
  27.  
  28. $Too_Big = 1024 * 1024 * 2;
  29.  
  30. sub _catname { #  Will be replaced by File::Spec when it arrives
  31.     my($from, $to) = @_;
  32.     if (not defined &basename) {
  33.     require File::Basename;
  34.     import  File::Basename 'basename';
  35.     }
  36.     if ($^O eq 'VMS')  { $to = VMS::Filespec::vmspath($to) . basename($from); }
  37.     elsif ($^O eq 'MacOS') { $to =~ s/^([^:]+)$/:$1/; $to .= ':' . basename($from); }
  38.     elsif ($to =~ m|\\|)   { $to .= '\\' . basename($from); }
  39.     else                   { $to .= '/' . basename($from); }
  40. }
  41.  
  42. eval($^O eq 'MacOS' ? <<'END_MAC' : <<'END_NOMAC');
  43. sub _protect {
  44.     my($name) = @_;
  45.     MacPerl::MakeFSSpec($name);
  46. }
  47. END_MAC
  48. sub _protect {
  49.     my($name) = @_;
  50.     "./$name";
  51. }
  52. END_NOMAC
  53.  
  54. sub copy {
  55.     croak("Usage: copy(FROM, TO [, BUFFERSIZE]) ")
  56.       unless(@_ == 2 || @_ == 3);
  57.  
  58.     my $from = shift;
  59.     my $to = shift;
  60.  
  61.     my $from_a_handle = (ref($from)
  62.              ? (ref($from) eq 'GLOB'
  63.                 || isa($from, 'GLOB') || isa($from, 'IO::Handle'))
  64.              : (ref(\$from) eq 'GLOB'));
  65.     my $to_a_handle =   (ref($to)
  66.              ? (ref($to) eq 'GLOB'
  67.                 || isa($to, 'GLOB') || isa($to, 'IO::Handle'))
  68.              : (ref(\$to) eq 'GLOB'));
  69.  
  70.     if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) {
  71.     $to = _catname($from, $to);
  72.     }
  73.  
  74.     if (defined &syscopy && \&syscopy != \©
  75.     && !$to_a_handle
  76.     && !($from_a_handle && ($^O eq 'os2' || $^O eq 'MacOS')))    
  77.         # OS/2 and MacOS cannot handle handles
  78.     {
  79.     return syscopy($from, $to);
  80.     }
  81.  
  82.     my $closefrom = 0;
  83.     my $closeto = 0;
  84.     my ($size, $status, $r, $buf);
  85.     local(*FROM, *TO);
  86.     local($\) = '';
  87.  
  88.     if ($from_a_handle) {
  89.     *FROM = *$from{FILEHANDLE};
  90.     } else {
  91.     $from = _protect($from) if $from =~ /^\s/;
  92.     open(FROM, "< $from\0") or goto fail_open1;
  93.     binmode FROM or die "($!,$^E)";
  94.     $closefrom = 1;
  95.     } 
  96.  
  97.     if ($to_a_handle) {
  98.     *TO = *$to{FILEHANDLE};
  99.     } else {        
  100.     $to = _protect($to) if $to =~ /^\s/;
  101.     open(TO,"> $to\0") or goto fail_open2;
  102.     binmode TO or die "($!,$^E)";
  103.     $closeto = 1;
  104.     }  
  105.  
  106.     if (@_) {
  107.     $size = shift(@_) + 0;
  108.     croak("Bad buffer size for copy: $size\n") unless ($size > 0);
  109.     } else {
  110.     $size = -s FROM;
  111.     $size = 1024 if ($size < 512);
  112.     $size = $Too_Big if ($size > $Too_Big);
  113.     }
  114.  
  115.     $! = 0;
  116.     for (;;) {
  117.     my ($r, $w, $t);
  118.     defined($r = sysread(FROM, $buf, $size))
  119.         or goto fail_inner;
  120.     last unless $r;
  121.     for ($w = 0; $w < $r; $w += $t) {
  122.         $t = syswrite(TO, $buf, $r - $w, $w)
  123.         or goto fail_inner;
  124.     }
  125.     }
  126.  
  127.     close(TO) || goto fail_open2 if $closeto;
  128.     close(FROM) || goto fail_open1 if $closefrom;
  129.  
  130.     # Use this idiom to avoid uninitialized value warning.
  131.     return 1;
  132.     
  133.     # All of these contortions try to preserve error messages...
  134.   fail_inner:
  135.     if ($closeto) {
  136.     $status = $!;
  137.     $! = 0;
  138.     close TO;
  139.     $! = $status unless $!;
  140.     }
  141.   fail_open2:
  142.     if ($closefrom) {
  143.     $status = $!;
  144.     $! = 0;
  145.     close FROM;
  146.     $! = $status unless $!;
  147.     }
  148.   fail_open1:
  149.     return 0;
  150. }
  151.  
  152. sub move {
  153.     my($from,$to) = @_;
  154.     my($copied,$fromsz,$tosz1,$tomt1,$tosz2,$tomt2,$sts,$ossts);
  155.  
  156.     if (-d $to && ! -d $from) {
  157.     $to = _catname($from, $to);
  158.     }
  159.  
  160.     ($tosz1,$tomt1) = (stat($to))[7,9];
  161.     $fromsz = -s $from;
  162.     if ($^O eq 'os2' and defined $tosz1 and defined $fromsz) {
  163.       # will not rename with overwrite
  164.       unlink $to;
  165.     }
  166.     return 1 if rename $from, $to;
  167.  
  168.     ($sts,$ossts) = ($! + 0, $^E + 0);
  169.     # Did rename return an error even though it succeeded, because $to
  170.     # is on a remote NFS file system, and NFS lost the server's ack?
  171.     return 1 if defined($fromsz) && !-e $from &&           # $from disappeared
  172.                 (($tosz2,$tomt2) = (stat($to))[7,9]) &&    # $to's there
  173.                 ($tosz1 != $tosz2 or $tomt1 != $tomt2) &&  #   and changed
  174.                 $tosz2 == $fromsz;                         # it's all there
  175.  
  176.     ($tosz1,$tomt1) = (stat($to))[7,9];  # just in case rename did something
  177.     return 1 if ($copied = copy($from,$to)) && unlink($from);
  178.   
  179.     ($tosz2,$tomt2) = ((stat($to))[7,9],0,0) if defined $tomt1;
  180.     unlink($to) if !defined($tomt1) or $tomt1 != $tomt2 or $tosz1 != $tosz2;
  181.     ($!,$^E) = ($sts,$ossts);
  182.     return 0;
  183. }
  184.  
  185. *cp = \©
  186. *mv = \&move;
  187.  
  188. eval <<'MAC_SYSCOPY' if ($^O eq 'MacOS');
  189. use Mac::MoreFiles;
  190.  
  191. sub syscopy {
  192.     my($from,$to) = @_;
  193.     my($dir,$toname);
  194.     
  195.     return 0 unless -e $from;
  196.     
  197.     if ($to =~ /(.*:)([^:]+):?$/) {
  198.         ($dir, $toname) = ($1,$2);
  199.     } else {
  200.         ($dir, $toname) = (":", $to);
  201.     }
  202.     unlink($to);
  203.     FSpFileCopy($from, $dir, $toname, 1); 
  204. }
  205. MAC_SYSCOPY
  206.  
  207. # &syscopy is an XSUB under OS/2 and MacOS
  208. *syscopy = ($^O eq 'VMS' ? \&rmscopy : \©) unless defined &syscopy;
  209.  
  210. 1;
  211.  
  212. __END__
  213.  
  214. =head1 NAME
  215.  
  216. File::Copy - Copy files or filehandles
  217.  
  218. =head1 SYNOPSIS
  219.  
  220.       use File::Copy;
  221.  
  222.     copy("file1","file2");
  223.       copy("Copy.pm",\*STDOUT);'
  224.     move("/dev1/fileA","/dev2/fileB");
  225.  
  226.       use POSIX;
  227.     use File::Copy cp;
  228.  
  229.     $n=FileHandle->new("/dev/null","r");
  230.     cp($n,"x");'
  231.  
  232. =head1 DESCRIPTION
  233.  
  234. The File::Copy module provides two basic functions, C<copy> and
  235. C<move>, which are useful for getting the contents of a file from
  236. one place to another.
  237.  
  238. =over 4
  239.  
  240. =item *
  241.  
  242. The C<copy> function takes two
  243. parameters: a file to copy from and a file to copy to. Either
  244. argument may be a string, a FileHandle reference or a FileHandle
  245. glob. Obviously, if the first argument is a filehandle of some
  246. sort, it will be read from, and if it is a file I<name> it will
  247. be opened for reading. Likewise, the second argument will be
  248. written to (and created if need be).
  249.  
  250. B<Note that passing in
  251. files as handles instead of names may lead to loss of information
  252. on some operating systems; it is recommended that you use file
  253. names whenever possible.>  Files are opened in binary mode where
  254. applicable.  To get a consistent behavour when copying from a
  255. filehandle to a file, use C<binmode> on the filehandle.
  256.  
  257. An optional third parameter can be used to specify the buffer
  258. size used for copying. This is the number of bytes from the
  259. first file, that wil be held in memory at any given time, before
  260. being written to the second file. The default buffer size depends
  261. upon the file, but will generally be the whole file (up to 2Mb), or
  262. 1k for filehandles that do not reference files (eg. sockets).
  263.  
  264. You may use the syntax C<use File::Copy "cp"> to get at the
  265. "cp" alias for this function. The syntax is I<exactly> the same.
  266.  
  267. =item *
  268.  
  269. The C<move> function also takes two parameters: the current name
  270. and the intended name of the file to be moved.  If the destination
  271. already exists and is a directory, and the source is not a
  272. directory, then the source file will be renamed into the directory
  273. specified by the destination.
  274.  
  275. If possible, move() will simply rename the file.  Otherwise, it copies
  276. the file to the new location and deletes the original.  If an error occurs
  277. during this copy-and-delete process, you may be left with a (possibly partial)
  278. copy of the file under the destination name.
  279.  
  280. You may use the "mv" alias for this function in the same way that
  281. you may use the "cp" alias for C<copy>.
  282.  
  283. =back
  284.  
  285. File::Copy also provides the C<syscopy> routine, which copies the
  286. file specified in the first parameter to the file specified in the
  287. second parameter, preserving OS-specific attributes and file
  288. structure.  For Unix systems, this is equivalent to the simple
  289. C<copy> routine.  For VMS systems, this calls the C<rmscopy>
  290. routine (see below).  For OS/2 systems, this calls the C<syscopy>
  291. XSUB directly.
  292.  
  293. =head2 Special behavior if C<syscopy> is defined (VMS and OS/2)
  294.  
  295. If both arguments to C<copy> are not file handles,
  296. then C<copy> will perform a "system copy" of
  297. the input file to a new output file, in order to preserve file
  298. attributes, indexed file structure, I<etc.>  The buffer size
  299. parameter is ignored.  If either argument to C<copy> is a
  300. handle to an opened file, then data is copied using Perl
  301. operators, and no effort is made to preserve file attributes
  302. or record structure.
  303.  
  304. The system copy routine may also be called directly under VMS and OS/2
  305. as C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which
  306. is the routine that does the actual work for syscopy).
  307.  
  308. =over 4
  309.  
  310. =item rmscopy($from,$to[,$date_flag])
  311.  
  312. The first and second arguments may be strings, typeglobs, typeglob
  313. references, or objects inheriting from IO::Handle;
  314. they are used in all cases to obtain the
  315. I<filespec> of the input and output files, respectively.  The
  316. name and type of the input file are used as defaults for the
  317. output file, if necessary.
  318.  
  319. A new version of the output file is always created, which
  320. inherits the structure and RMS attributes of the input file,
  321. except for owner and protections (and possibly timestamps;
  322. see below).  All data from the input file is copied to the
  323. output file; if either of the first two parameters to C<rmscopy>
  324. is a file handle, its position is unchanged.  (Note that this
  325. means a file handle pointing to the output file will be
  326. associated with an old version of that file after C<rmscopy>
  327. returns, not the newly created version.)
  328.  
  329. The third parameter is an integer flag, which tells C<rmscopy>
  330. how to handle timestamps.  If it is E<lt> 0, none of the input file's
  331. timestamps are propagated to the output file.  If it is E<gt> 0, then
  332. it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
  333. timestamps other than the revision date are propagated; if bit 1
  334. is set, the revision date is propagated.  If the third parameter
  335. to C<rmscopy> is 0, then it behaves much like the DCL COPY command:
  336. if the name or type of the output file was explicitly specified,
  337. then no timestamps are propagated, but if they were taken implicitly
  338. from the input filespec, then all timestamps other than the
  339. revision date are propagated.  If this parameter is not supplied,
  340. it defaults to 0.
  341.  
  342. Like C<copy>, C<rmscopy> returns 1 on success.  If an error occurs,
  343. it sets C<$!>, deletes the output file, and returns 0.
  344.  
  345. =back
  346.  
  347. =head1 RETURN
  348.  
  349. All functions return 1 on success, 0 on failure.
  350. $! will be set if an error was encountered.
  351.  
  352. =head1 AUTHOR
  353.  
  354. File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995,
  355. and updated by Charles Bailey I<E<lt>bailey@genetics.upenn.eduE<gt>> in 1996.
  356.  
  357. =cut
  358.  
  359.