/usr/share/perl5/File/NCopy.pm is in libfile-ncopy-perl 0.36-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | package File::NCopy;
use 5.004; # just because I think you should upgrade :)
=head1 NAME
File::NCopy - Deprecated module. Use File::Copy::Recursive instead. Copy file, file. Copy file[s] | dir[s], dir
=head1 SYNOPSIS
use File::NCopy qw(copy);
copy "file","other_file";
copy "file1","file2","file3","directory";
# we want to copy the directory recursively
copy \1,"directory1","directory2";
copy \1,"file1","file2","directory1","file3","directory2","file4",
"directory";
# can also use references to file handles, this is for backward
# compatibility with File::Copy
copy \*FILE1,\*FILE2;
copy \*FILE1,"file";
copy "file1",\*FILE2;
# we don't specify \1 as the first argument because we don't want to
# copy directories recursively
copy "*.c","*.pl","programs";
copy "*", "backup";
use File::NCopy;
# the below are the default config values
$file = File::NCopy->new(
'recursive' => 0,
'preserve' => 0,
'follow_links' => 0,
'force_write' => 0,
'set_permission' => \&File::NCopy::u_chmod,
'file_check' => \&File::NCopy::f_check,
'set_times' => \&File::NCopy::s_times,
);
set_permission will take two file names, the original to get the
file permissions from and the new file to set the file permissions
for.
file_check takes two parameters, the file names to check the file to
copy from and the file to copy to. I am using flock for Unix
systems.
Default for this is \&File::NCopy::f_check. On Unix you can also use
\&File::NCopy::unix_check. This one compares the inode and device
numbers.
set_times is used if the preserve attribute is true. It preserves
the access and modification time of the file and also attempts to
set the owner of the file to the original owner. This can be useful
in a script used by root, though enyone can preserve the access and
modification times. This also takes two arguments. The file to get
the stats from and apply the stats to.
On Unix boxes you shouldn't need to worry. On other system you may
want to supply your own sub references.
$file = File::NCopy->new(recursive => 1);
$file->copy "file","other_file";
$file->copy "directory1","directory2";
$file = File::NCopy->new(u_chmod => \&my_chmod,f_check => \&my_fcheck);
$file->copy "directory1","directory2";
=head1 DESCRIPTION
B<File::NCopy::copy> copies files to directories, or a single file to
another file. You can also use a reference to a file handle if you wish
whem doing a file to file copy. The functionality is very similar to
B<cp>. If the argument is a directory to directory copy and the
recursive flag is set then it is done recursively like B<cp -R>.
In fact it behaves like cp on Unix for the most part.
If called in array context, an array of successful copies is returned,
otherwise the number of successful copies is returned. If passed a file
handle, it's difficult to make sure the file we are copying isn't the
same that we are copying to, since by opening the file in write mode it
gets pooched. To avoid this use file names instead, if at all possible,
especially for the to file. If passed a file handle, it is not closed
when copy returns, files opened by copy are closed.
=over 4
=item B<copy>
Copies a file to another file. Or a file to a directory. Or multiple
files and directories to another directory. Or a directory to another
directory. Wildcard arguments are expanded, except for the last
argument which should not be expanded. The file and directory
permissions are set to the orginating file's permissions and if preserve
is set the access and modification times are also set. If preserve is
set then the uid and gid will also be attempted to be set, though this
may only for for the men in white hats.
In list context it returns all the names of the files/directories that
were successfully copied. In scalar context it returns the number of
successful copies made. A directory argument is considerd a single
successful copy if it manages to copy anything at all. To make a
directory to directory copy the recursive flag must be set.
=item B<cp>
Just calls copy. It's there to be compatible with File::Copy.
=item B<new>
If used then you can treat this as an object oriented module with some
configuration abilities.
=item B<recursive>
If used as an object then you can use this to set the recursive
attribute. It can also be set when instantiating with new. The other
attributes must all be set when instantiating the object. If it isn't
specified then directories are not followed.
=item B<preserve>
Attempt to preserve the last modification and access time as well as
user and group id's. This is a useful feature for sysadmins, though the
access and modification time should always be preservable, the uid and
gid may not.
=item B<follow_links>
If the link is to a directory and this attribute is true then the
directory is followed and recursively copied. Otherwise a link is made
to the root directory the link points to. eg.
/sys/ is a link to /usr/src/sys/ is a link to /usr/src/i386/sys
then the link /sys/ is actually created in the source directory as a
link to /usr/src/i386/sys/ rather than /usr/src/sys/ since if the link
/usr/src/sys/ is removed then we lost the link even though the directory
we originally intended to link to still exists.
=item B<force_write>
Force the writing of a file even if the permissions are read only on it.
=back
=head1 EXAMPLE
See SYNOPSIS.
=head1 BUGS
When following links the target directory might not exactly the same as
the source directory. The reason is that we have to make sure we don't
follow circular or dead links. This is really a feature though the
result may not quite resemble the source dir, the overall content will
be the same. :)
From Ken Healy (Version 0.34)
On Win32, The use of backslash for paths is required.
=head1 AUTHOR
Gabor Egressy B<gabor AT vmunix.com>
Copyright (c) 1998 Gabor Egressy. All rights reserved. All wrongs
reversed. This program is free software; you can redistribute and/or
modify it under the same terms as Perl itself.
Some ideas gleaned from File::Copy by Aaron Sherman & Charles Bailey,
but the code was written from scratch.
Patch at versions 0.33, and 0.34 added by MZSANFORD.
0.35, 0.36 - Alexandr Ciornii (alexchorny AT gmail.com)
=cut
use Cwd ();
use File::Spec;
use strict;
use vars qw(@EXPORT_OK @ISA $VERSION);
@ISA = qw(Exporter);
# we export nothing by default :)
@EXPORT_OK = qw(copy cp);
$VERSION = '0.36';
# this works on Unix
sub u_chmod($$)
{
my ($file_from,$file_to) = @_;
my ($mode) = (stat $file_from)[2];
chmod $mode & 0777,$file_to
unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
1;
}
# this also works on Unix
sub f_check($$)
{
my ($file_from,$file_to) = @_;
# get a shared lock on file to copy from
flock $file_from,5
or return 0;
# try and get an exclusive lock on the file to copy to
flock $file_to,6
or do {
flock $file_from,8;
return 0;
};
flock $file_from,8;
flock $file_to,8;
1;
}
# this also works on Unix, it's not the default but you can easily use
# it by using the module in an object oriented way
# $copy = File::NCopy->new('file_check' => \&File::NCopy::unix_check);
sub unix_check($$)
{
my ($file_from,$file_to) = @_;
my ($fdev,$fino) = (stat $file_from)[0,1];
my ($tdev,$tino) = (stat $file_to)[0,1];
return 0
if $fdev == $tdev && $fino == $tino;
1;
}
sub s_times($$)
{
my ($file_from,$file_to) = @_;
my ($uid,$gid,$atime,$mtime) = (stat $file_from)[4,5,8,9];
utime $atime,$mtime,$file_to
unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
# this may only work for men in white hats; on Unix
chown $uid,$gid,$file_to
unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
1;
}
# all the actual copying is done here, folks ;)
sub _docopy_file_file($$$)
{
my $this = shift;
my ($file_from,$file_to) = @_;
local (*FILE_FROM,*FILE_TO);
my ($was_handle);
# did we get a file handle ?
unless(ref $file_from eq 'GLOB' || ref $file_from eq 'FileHandle') {
open FILE_FROM,"<$file_from"
or do {
print "*** Couldn\'t open from file <$!> ==> $file_from\n"
if $this->{'_debug'};
return 0;
};
}
else {
*FILE_FROM = *$file_from;
}
unless(ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle') {
# we must open in update mode since on some systems exclusive
# locks are only granted to files that are going to be written;
open FILE_TO,"+<$file_to"
or goto NO_FILE; # no file, so file can't be the same :)
}
else {
*FILE_TO = *$file_to;
$was_handle = 1;
}
unless(-t FILE_FROM || -t FILE_TO) {
$this->{'file_check'}->(\*FILE_FROM,\*FILE_TO)
or return 0;
}
NO_FILE:
# files aren't the same; now open for writing unless we got a
# filehandle
if(! $was_handle && ! $this->{test}) {
open FILE_TO,">$file_to"
or chmod 0644, "$file_to"
if $this->{'force_write'};
open FILE_TO,">$file_to"
or do {
print "*** Couldn\'t open to file <$!> ==> $file_to\n"
if $this->{'_debug'};
return 0;
};
}
# and now for the braindead OS's
binmode FILE_FROM unless ($this->{test});
binmode FILE_TO unless ($this->{test});
my $buf = '';
my ($len,$write_n);
# read file and write to new file, recover from write errors and
# read errors; we accept however much we read and try to write it
# 8K is a nice buffer size for most file systems
while(! $this->{test} && 1) {
$len = sysread(FILE_FROM,$buf,8192);
return 0
unless defined $len;
last
unless $len > 0;
while($len) {
$write_n = syswrite(FILE_TO,$buf,$len);
return 0
unless defined $write_n;
$len -= $write_n;
}
}
$this->{'set_permission'}->($file_from,$file_to);
# we only close files we opened
unless ($this->{test}) {
close FILE_FROM
unless ref $file_from eq 'GLOB' || ref $file_from eq 'FileHandle';
close FILE_TO
unless ref $file_to eq 'GLOB' || ref $file_to eq 'FileHandle';
}
# this was moved from above the unless statement per Ken Healy in version 0.34
$this->{'set_times'}->($file_from,$file_to)
if $this->{'preserve'};
print "$file_from ==> $file_to\n"
if $this->{'_debug'};
1;
}
sub get_path($)
{
my $dir = shift;
my $save_dir = Cwd::cwd;
chdir $dir
or return undef;
$dir = Cwd::cwd;
chdir $save_dir;
return $dir;
}
sub _recurse_from_dir($$$);
# we never actually change the directory :)
sub _recurse_from_dir($$$)
{
my $this = shift;
my ($from_dir,$to_dir) = @_;
local (*DIR);
# MZS - v0.39 - Changed from slash to File::Spec;
my $dir_sep = File::Spec->catfile('a','b');
$dir_sep =~ s/^a(.+)b$/$1/;
opendir DIR,$from_dir
or do {
print "*** Couldn\'t opendir <$!> ==> $from_dir\n"
if $this->{'_debug'};
return 0;
};
my @files = readdir DIR
or do {
print "*** Couldn\'t read dir <$!> ==> $from_dir\n"
if $this->{'_debug'};
return 0;
};
closedir DIR;
my $made_dir;
unless(-e $to_dir && ! $this->{test}) {
mkdir $to_dir,0777
or return 0;
$made_dir = 1;
}
my ($retval,$ret,$link,$save_link);
# make sure we don't end up with a recursive, circular link
# this isn't totally foolproof, though it does prevent circular
# links
if($this->{'follow_links'}) {
if(defined($save_link = get_path $from_dir)) {
$this->{'_links'}->{$save_link} = 1;
}
}
for (@files) {
next
if /^\.\.?$/;
if(-f $from_dir . $dir_sep . $_) {
$ret = _docopy_file_file $this, $from_dir . $dir_sep . $_ ,
$to_dir . $dir_sep . $_;
}
elsif(-d "$from_dir$dir_sep$_") {
if($this->{'follow_links'} && -l "$from_dir$dir_sep$_") {
$link = get_path "$from_dir$dir_sep$_";
}
if(! -l "$from_dir$dir_sep$_" || $this->{'follow_links'}
&& defined $link
&& ! exists $this->{'_links'}->{$link}) {
$ret = _recurse_from_dir
$this,$from_dir . $dir_sep . $_ ,$to_dir . $dir_sep . $_;
}
else {
if(defined($link = get_path "$from_dir$dir_sep$_")) {
$ret = symlink $link, "$to_dir$dir_sep$_";
}
}
}
$retval = $retval || $ret;
}
if($made_dir) {
$this->{'set_permission'}->($from_dir,$to_dir);
$this->{'set_times'}->($from_dir,$to_dir)
if $this->{'preserve'};
}
# remove the name so that there can be link to it from other dirs
# that are not subdirs of this one
if($this->{'follow_links'}) {
delete $this->{'_links'}->{$save_link};
}
return $retval;
}
sub _docopy_dir_dir($$$)
{
my $this = shift;
my ($dir_from,$dir_to) = @_;
my ($from_name);
# MZS - v0.39 - Changed from slash to File::Spec;
my $dir_sep = File::Spec->catfile('a','b');
$dir_sep =~ s/^a(.+)b$/$1/;
$dir_from =~ s/$dir_sep$//; # remove trailing slash, if any
if($dir_from =~ tr/$dir_sep//) {
$from_name = substr $dir_from,rindex($dir_from,$dir_sep) + 1;
}
else {
$from_name = $dir_from;
if($from_name =~ /^\.\.?$/) {
$from_name = '';
}
}
unless($dir_to =~ /$dir_sep$/) {
$dir_to .= $dir_sep;
}
$dir_to .= $from_name;
$this->{'_links'} = {};
_recurse_from_dir $this, $dir_from,$dir_to;
}
sub _docopy_file_dir($$$)
{
my $this = shift;
my ($file,$dir) = @_;
my $file_to;
# MZS - v0.39 - Changed from slash to File::Spec;
my $dir_sep = File::Spec->catfile('a','b');
$dir_sep =~ s/^a(.+)b$/$1/;
if($file =~ tr/$dir_sep//) {
$file_to = substr $file,rindex($file,$dir_sep) + 1;
}
else {
$file_to = $file;
}
$dir =~ s/$dir_sep$//; # remove trailing slash
_docopy_file_file $this, $file,$dir.$dir_sep.$file_to;
}
# this just redirects calls, like copy ;)
sub _docopy_files_dir($$@)
{
my $this = shift;
my $copies = shift;
my $dir = pop;
for (@_) {
if(-d $_ && $this->{'recursive'}) {
if ($this->{test}) {
push @$copies, $_;
} else {
_docopy_dir_dir $this, $_, $dir
and push @$copies, $_;
}
}
elsif(-f $_) {
if ($this->{test}) {
push @$copies, $_;
} else {
_docopy_file_dir $this, $_, $dir
and push @$copies, $_;
}
}
}
1;
}
# does glob work on all systems?
sub expand(@)
{
my @args;
return
if @_ < 2;
for (my $i = 0;$i < $#_;++$i) {
push @args,glob $_[$i];
}
push @args,$_[$#_];
@args;
}
sub new(@); #prototype
# this just redirects calls
sub copy(@)
{
my $this;
# were we called through an object reference?
if(ref $_[0] eq 'File::NCopy') {
$this = shift;
}
else {
# no, so let's make one
$this = new File::NCopy;
if(ref $_[0] eq 'SCALAR') {
my $rec = shift;
$this->recursive($$rec);
}
}
my @copies;
my @args = expand @_;
print "passed args ==> ".join(',',map {"'$_'"} @args)."\n"
if $this->{'_debug'};
# one or more files/directories to a directory
if(@args >= 2 && -d $args[$#args]) {
print "Copy to dir started.\n" if ($this->{'_debug'});
_docopy_files_dir $this, \@copies, @args;
}
# file to file
elsif(@args == 2 && -f $args[0]) {
if ($this->{test}) {
push @copies, $args[0];
} else {
_docopy_file_file $this, $args[0],$args[1]
and push @copies, $args[0];
}
}
@copies;
}
sub cp(@) {
return copy @_;
}
# instantiate our object
sub new(@)
{
my $this = shift;
my $conf = {
'test' => 0,
'recursive' => 0,
'preserve' => 0,
'follow_links' => 0,
'force_write' => 0,
'_debug' => 0,
'set_permission' => \&File::NCopy::u_chmod,
'file_check' => \&File::NCopy::f_check,
'set_times' => \&File::NCopy::s_times,
'_links' => {},
};
my $ref;
if(@_ % 2 == 0) {
my %ref = @_;
$ref = \%ref;
}
elsif(ref $_[0] eq 'HASH') {
$ref = shift;
}
if(ref $ref eq 'HASH') {
$conf->{'test'} = abs int $ref->{'test'}
if defined $ref->{'test'};
$conf->{'recursive'} = abs int $ref->{'recursive'}
if defined $ref->{'recursive'};
$conf->{'preserve'} = abs int $ref->{'preserve'}
if defined $ref->{'preserve'};
$conf->{'follow_links'} = abs int $ref->{'follow_links'}
if defined $ref->{'follow_links'};
$conf->{'force_write'} = abs int $ref->{'force_write'}
if defined $ref->{'force_write'};
$conf->{'_debug'} = abs int $ref->{'_debug'}
if defined $ref->{'_debug'};
$conf->{'set_permission'} = $ref->{'set_permission'}
if defined $ref->{'set_permission'}
&& ref $ref->{'set_permission'} eq 'CODE';
$conf->{'file_check'} = $ref->{'file_check'}
if defined $ref->{'file_check'}
&& ref $ref->{'file_check'} eq 'CODE';
$conf->{'set_times'} = $ref->{'set_times'}
if defined $ref->{'set_times'}
&& ref $ref->{'set_times'} eq 'CODE';
}
bless $conf,$this;
}
sub recursive($;$)
{
return
if @_ < 1;
my $this = shift;
return
unless ref $this eq 'File::NCopy';
@_ ? $this->{'recursive'} = abs int shift
: $this->{'recursive'};
}
sub preserve($;$)
{
return
if @_ < 1;
my $this = shift;
return
unless ref $this eq 'File::NCopy';
@_ ? $this->{'preserve'} = abs int shift
: $this->{'preserve'};
}
sub follow_links($;$)
{
return
if @_ < 1;
my $this = shift;
return
unless ref $this eq 'File::NCopy';
@_ ? $this->{'follow_links'} = abs int shift
: $this->{'follow_links'};
}
sub force_write($;$)
{
return
if @_ < 1;
my $this = shift;
return
unless ref $this eq 'File::NCopy';
@_ ? $this->{'force_write'} = abs int shift
: $this->{'force_write'};
}
1;
|