Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

342 lines
11 KiB

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