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.

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