WinGit: Do not require the user to accept the license, it's just informational
[msysgit/kusma.git] / lib / perl5 / 5.6.1 / File / Copy.pm
blobc8bd8b0037b598842925170c08abea882eae359e
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.
5 # Additions copyright 1996 by Charles Bailey. Permission is granted
6 # to distribute the revised code under the same terms as Perl itself.
8 package File::Copy;
10 use 5.6.0;
11 use strict;
12 use Carp;
13 use File::Spec;
14 our(@ISA, @EXPORT, @EXPORT_OK, $VERSION, $Too_Big, $Syscopy_is_copy);
15 sub copy;
16 sub syscopy;
17 sub cp;
18 sub mv;
20 # Note that this module implements only *part* of the API defined by
21 # the File/Copy.pm module of the File-Tools-2.0 package. However, that
22 # package has not yet been updated to work with Perl 5.004, and so it
23 # would be a Bad Thing for the CPAN module to grab it and replace this
24 # module. Therefore, we set this module's version higher than 2.0.
25 $VERSION = '2.04';
27 require Exporter;
28 @ISA = qw(Exporter);
29 @EXPORT = qw(copy move);
30 @EXPORT_OK = qw(cp mv);
32 $Too_Big = 1024 * 1024 * 2;
34 sub _catname {
35 my($from, $to) = @_;
36 if (not defined &basename) {
37 require File::Basename;
38 import File::Basename 'basename';
41 if ($^O eq 'MacOS') {
42 # a partial dir name that's valid only in the cwd (e.g. 'tmp')
43 $to = ':' . $to if $to =~ /^[^:]+$/;
46 return File::Spec->catfile($to, basename($from));
49 sub copy {
50 croak("Usage: copy(FROM, TO [, BUFFERSIZE]) ")
51 unless(@_ == 2 || @_ == 3);
53 my $from = shift;
54 my $to = shift;
56 my $from_a_handle = (ref($from)
57 ? (ref($from) eq 'GLOB'
58 || UNIVERSAL::isa($from, 'GLOB')
59 || UNIVERSAL::isa($from, 'IO::Handle'))
60 : (ref(\$from) eq 'GLOB'));
61 my $to_a_handle = (ref($to)
62 ? (ref($to) eq 'GLOB'
63 || UNIVERSAL::isa($to, 'GLOB')
64 || UNIVERSAL::isa($to, 'IO::Handle'))
65 : (ref(\$to) eq 'GLOB'));
67 if (!$from_a_handle && !$to_a_handle && -d $to && ! -d $from) {
68 $to = _catname($from, $to);
71 if (defined &syscopy && !$Syscopy_is_copy
72 && !$to_a_handle
73 && !($from_a_handle && $^O eq 'os2' ) # OS/2 cannot handle handles
74 && !($from_a_handle && $^O eq 'mpeix') # and neither can MPE/iX.
75 && !($from_a_handle && $^O eq 'MSWin32')
76 && !($from_a_handle && $^O eq 'MacOS')
79 return syscopy($from, $to);
82 my $closefrom = 0;
83 my $closeto = 0;
84 my ($size, $status, $r, $buf);
85 local(*FROM, *TO);
86 local($\) = '';
88 if ($from_a_handle) {
89 *FROM = *$from{FILEHANDLE};
90 } else {
91 $from = _protect($from) if $from =~ /^\s/s;
92 open(FROM, "< $from\0") or goto fail_open1;
93 binmode FROM or die "($!,$^E)";
94 $closefrom = 1;
97 if ($to_a_handle) {
98 *TO = *$to{FILEHANDLE};
99 } else {
100 $to = _protect($to) if $to =~ /^\s/s;
101 open(TO,"> $to\0") or goto fail_open2;
102 binmode TO or die "($!,$^E)";
103 $closeto = 1;
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);
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;
127 close(TO) || goto fail_open2 if $closeto;
128 close(FROM) || goto fail_open1 if $closefrom;
130 # Use this idiom to avoid uninitialized value warning.
131 return 1;
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 $!;
141 fail_open2:
142 if ($closefrom) {
143 $status = $!;
144 $! = 0;
145 close FROM;
146 $! = $status unless $!;
148 fail_open1:
149 return 0;
152 sub move {
153 my($from,$to) = @_;
154 my($copied,$fromsz,$tosz1,$tomt1,$tosz2,$tomt2,$sts,$ossts);
156 if (-d $to && ! -d $from) {
157 $to = _catname($from, $to);
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;
166 return 1 if rename $from, $to;
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
176 ($tosz1,$tomt1) = (stat($to))[7,9]; # just in case rename did something
177 return 1 if ($copied = copy($from,$to)) && unlink($from);
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;
185 *cp = \&copy;
186 *mv = \&move;
189 if ($^O eq 'MacOS') {
190 *_protect = sub { MacPerl::MakeFSSpec($_[0]) };
191 } else {
192 *_protect = sub { "./$_[0]" };
195 # &syscopy is an XSUB under OS/2
196 unless (defined &syscopy) {
197 if ($^O eq 'VMS') {
198 *syscopy = \&rmscopy;
199 } elsif ($^O eq 'mpeix') {
200 *syscopy = sub {
201 return 0 unless @_ == 2;
202 # Use the MPE cp program in order to
203 # preserve MPE file attributes.
204 return system('/bin/cp', '-f', $_[0], $_[1]) == 0;
206 } elsif ($^O eq 'MSWin32') {
207 *syscopy = sub {
208 return 0 unless @_ == 2;
209 return Win32::CopyFile(@_, 1);
211 } elsif ($^O eq 'MacOS') {
212 require Mac::MoreFiles;
213 *syscopy = sub {
214 my($from, $to) = @_;
215 my($dir, $toname);
217 return 0 unless -e $from;
219 if ($to =~ /(.*:)([^:]+):?$/) {
220 ($dir, $toname) = ($1, $2);
221 } else {
222 ($dir, $toname) = (":", $to);
225 unlink($to);
226 Mac::MoreFiles::FSpFileCopy($from, $dir, $toname, 1);
228 } else {
229 $Syscopy_is_copy = 1;
230 *syscopy = \&copy;
236 __END__
238 =head1 NAME
240 File::Copy - Copy files or filehandles
242 =head1 SYNOPSIS
244 use File::Copy;
246 copy("file1","file2");
247 copy("Copy.pm",\*STDOUT);'
248 move("/dev1/fileA","/dev2/fileB");
250 use POSIX;
251 use File::Copy cp;
253 $n = FileHandle->new("/a/file","r");
254 cp($n,"x");'
256 =head1 DESCRIPTION
258 The File::Copy module provides two basic functions, C<copy> and
259 C<move>, which are useful for getting the contents of a file from
260 one place to another.
262 =over 4
264 =item *
266 The C<copy> function takes two
267 parameters: a file to copy from and a file to copy to. Either
268 argument may be a string, a FileHandle reference or a FileHandle
269 glob. Obviously, if the first argument is a filehandle of some
270 sort, it will be read from, and if it is a file I<name> it will
271 be opened for reading. Likewise, the second argument will be
272 written to (and created if need be).
274 B<Note that passing in
275 files as handles instead of names may lead to loss of information
276 on some operating systems; it is recommended that you use file
277 names whenever possible.> Files are opened in binary mode where
278 applicable. To get a consistent behaviour when copying from a
279 filehandle to a file, use C<binmode> on the filehandle.
281 An optional third parameter can be used to specify the buffer
282 size used for copying. This is the number of bytes from the
283 first file, that wil be held in memory at any given time, before
284 being written to the second file. The default buffer size depends
285 upon the file, but will generally be the whole file (up to 2Mb), or
286 1k for filehandles that do not reference files (eg. sockets).
288 You may use the syntax C<use File::Copy "cp"> to get at the
289 "cp" alias for this function. The syntax is I<exactly> the same.
291 =item *
293 The C<move> function also takes two parameters: the current name
294 and the intended name of the file to be moved. If the destination
295 already exists and is a directory, and the source is not a
296 directory, then the source file will be renamed into the directory
297 specified by the destination.
299 If possible, move() will simply rename the file. Otherwise, it copies
300 the file to the new location and deletes the original. If an error occurs
301 during this copy-and-delete process, you may be left with a (possibly partial)
302 copy of the file under the destination name.
304 You may use the "mv" alias for this function in the same way that
305 you may use the "cp" alias for C<copy>.
307 =back
309 File::Copy also provides the C<syscopy> routine, which copies the
310 file specified in the first parameter to the file specified in the
311 second parameter, preserving OS-specific attributes and file
312 structure. For Unix systems, this is equivalent to the simple
313 C<copy> routine. For VMS systems, this calls the C<rmscopy>
314 routine (see below). For OS/2 systems, this calls the C<syscopy>
315 XSUB directly. For Win32 systems, this calls C<Win32::CopyFile>.
317 =head2 Special behaviour if C<syscopy> is defined (OS/2, VMS and Win32)
319 If both arguments to C<copy> are not file handles,
320 then C<copy> will perform a "system copy" of
321 the input file to a new output file, in order to preserve file
322 attributes, indexed file structure, I<etc.> The buffer size
323 parameter is ignored. If either argument to C<copy> is a
324 handle to an opened file, then data is copied using Perl
325 operators, and no effort is made to preserve file attributes
326 or record structure.
328 The system copy routine may also be called directly under VMS and OS/2
329 as C<File::Copy::syscopy> (or under VMS as C<File::Copy::rmscopy>, which
330 is the routine that does the actual work for syscopy).
332 =over 4
334 =item rmscopy($from,$to[,$date_flag])
336 The first and second arguments may be strings, typeglobs, typeglob
337 references, or objects inheriting from IO::Handle;
338 they are used in all cases to obtain the
339 I<filespec> of the input and output files, respectively. The
340 name and type of the input file are used as defaults for the
341 output file, if necessary.
343 A new version of the output file is always created, which
344 inherits the structure and RMS attributes of the input file,
345 except for owner and protections (and possibly timestamps;
346 see below). All data from the input file is copied to the
347 output file; if either of the first two parameters to C<rmscopy>
348 is a file handle, its position is unchanged. (Note that this
349 means a file handle pointing to the output file will be
350 associated with an old version of that file after C<rmscopy>
351 returns, not the newly created version.)
353 The third parameter is an integer flag, which tells C<rmscopy>
354 how to handle timestamps. If it is E<lt> 0, none of the input file's
355 timestamps are propagated to the output file. If it is E<gt> 0, then
356 it is interpreted as a bitmask: if bit 0 (the LSB) is set, then
357 timestamps other than the revision date are propagated; if bit 1
358 is set, the revision date is propagated. If the third parameter
359 to C<rmscopy> is 0, then it behaves much like the DCL COPY command:
360 if the name or type of the output file was explicitly specified,
361 then no timestamps are propagated, but if they were taken implicitly
362 from the input filespec, then all timestamps other than the
363 revision date are propagated. If this parameter is not supplied,
364 it defaults to 0.
366 Like C<copy>, C<rmscopy> returns 1 on success. If an error occurs,
367 it sets C<$!>, deletes the output file, and returns 0.
369 =back
371 =head1 RETURN
373 All functions return 1 on success, 0 on failure.
374 $! will be set if an error was encountered.
376 =head1 NOTES
378 =over 4
380 =item *
382 On Mac OS (Classic), the path separator is ':', not '/', and the
383 current directory is denoted as ':', not '.'. You should be careful
384 about specifying relative pathnames. While a full path always begins
385 with a volume name, a relative pathname should always begin with a
386 ':'. If specifying a volume name only, a trailing ':' is required.
388 E.g.
390 copy("file1", "tmp"); # creates the file 'tmp' in the current directory
391 copy("file1", ":tmp:"); # creates :tmp:file1
392 copy("file1", ":tmp"); # same as above
393 copy("file1", "tmp"); # same as above, if 'tmp' is a directory (but don't do
394 # that, since it may cause confusion, see example #1)
395 copy("file1", "tmp:file1"); # error, since 'tmp:' is not a volume
396 copy("file1", ":tmp:file1"); # ok, partial path
397 copy("file1", "DataHD:"); # creates DataHD:file1
399 move("MacintoshHD:fileA", "DataHD:fileB"); # moves (don't copies) files from one
400 # volume to another
402 =back
404 =head1 AUTHOR
406 File::Copy was written by Aaron Sherman I<E<lt>ajs@ajs.comE<gt>> in 1995,
407 and updated by Charles Bailey I<E<lt>bailey@newman.upenn.eduE<gt>> in 1996.
409 =cut