Leaked source code of windows server 2003
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.

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