| 1 | package File::Xcopy; | ||
| 2 | |||
| 3 | use 5.005_64; | ||
| 4 | use strict; | ||
| 5 | use vars qw($AUTOLOAD); | ||
| 6 | use Carp; | ||
| 7 | our(@ISA, @EXPORT, @EXPORT_OK, $VERSION, %EXPORT_TAGS); | ||
| 8 | $VERSION = '0.12'; | ||
| 9 | |||
| 10 | |||
| 11 | # require Exporter; | ||
| 12 | # @ISA = qw(Exporter); | ||
| 13 | @EXPORT = qw(); | ||
| 14 | @EXPORT_OK = qw(xcp xmv xcopy xmove find_files list_files output | ||
| 15 | fmtTime format_number get_stat file_stat execute syscopy | ||
| 16 | ); | ||
| 17 | %EXPORT_TAGS = ( | ||
| 18 | all => [@EXPORT_OK] | ||
| 19 | ); | ||
| 20 | |||
| 21 | use File::Find; | ||
| 22 | use IO::File; | ||
| 23 | use File::Basename; | ||
| 24 | |||
| 25 | # use Fax::DataFax::Subs qw(:echo_msg disp_param); | ||
| 26 | |||
| 27 | sub xcopy; | ||
| 28 | sub xmove; | ||
| 29 | sub xcp; | ||
| 30 | sub xmv; | ||
| 31 | sub syscopy; | ||
| 32 | |||
| 33 | =head1 NAME | ||
| 34 | |||
| 35 | File::Xcopy - copy files after comparing them. | ||
| 36 | |||
| 37 | =head1 SYNOPSIS | ||
| 38 | |||
| 39 | use File::Xcopy; | ||
| 40 | my $fx = new File::Xcopy; | ||
| 41 | $fx->from_dir("/from/dir"); | ||
| 42 | $fx->to_dir("/to/dir"); | ||
| 43 | $fx->fn_pat('(\.pl|\.txt)$'); # files with pl & txt extensions | ||
| 44 | $fx->param('s',1); # search recursively to sub dirs | ||
| 45 | $fx->param('verbose',1); # search recursively to sub dirs | ||
| 46 | $fx->param('log_file','/my/log/file.log'); | ||
| 47 | my ($sr, $rr) = $fx->get_stat; | ||
| 48 | $fx->xcopy; # or | ||
| 49 | $fx->execute('copy'); | ||
| 50 | |||
| 51 | # the same with short name | ||
| 52 | $fx->xcp("from_dir", "to_dir", "file_name_pattern"); | ||
| 53 | |||
| 54 | =head1 DESCRIPTION | ||
| 55 | |||
| 56 | The File::Xcopy module provides two basic functions, C<xcopy> and | ||
| 57 | C<xmove>, which are useful for coping and/or moving a file or | ||
| 58 | files in a directory from one place to another. It mimics some of | ||
| 59 | behaviours of C<xcopy> in DOS but with more functions and options. | ||
| 60 | |||
| 61 | |||
| 62 | The differences between C<xcopy> and C<copy> are | ||
| 63 | |||
| 64 | =over 4 | ||
| 65 | |||
| 66 | =item * | ||
| 67 | |||
| 68 | C<xcopy> searches files based on file name pattern if the | ||
| 69 | pattern is specified. | ||
| 70 | |||
| 71 | =item * | ||
| 72 | |||
| 73 | C<xcopy> compares the timestamp and size of a file before it copies. | ||
| 74 | |||
| 75 | =item * | ||
| 76 | |||
| 77 | C<xcopy> takes different actions if you tell it to. | ||
| 78 | |||
| 79 | =back | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | { # Encapsulated class data | ||
| 84 | my %_attr_data = # default accessibility | ||
| 85 | ( | ||
| 86 | _from_dir =>['$','read/write',''], # directory 1 | ||
| 87 | _to_dir =>['$','read/write',''], # directory 2 | ||
| 88 | _fn_pat =>['$','read/write',''], # file name pattern | ||
| 89 | _action =>['$','read/write',''], # action | ||
| 90 | _param =>['%','read/write',{}], # dynamic parameters | ||
| 91 | ); | ||
| 92 | sub _accessible { | ||
| 93 | my ($self, $attr, $mode) = @_; | ||
| 94 | if (exists $_attr_data{$attr}) { | ||
| 95 | return $_attr_data{$attr}[1] =~ /$mode/; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | # classwide default value for a specified object attributes | ||
| 99 | sub _default_for { | ||
| 100 | my ($self, $attr) = @_; | ||
| 101 | if (exists $_attr_data{$attr}) { | ||
| 102 | return $_attr_data{$attr}[2]; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | # list of names of all specified object attributes | ||
| 106 | |||
| 107 | sub _standard_keys { | ||
| 108 | my $self = shift; | ||
| 109 | # ($self->SUPER::_standard_keys, keys %_attr_data); | ||
| 110 | (keys %_attr_data); | ||
| 111 | } | ||
| 112 | sub _accs_type { | ||
| 113 | my ($self, $attr) = @_; | ||
| 114 | if (exists $_attr_data{$attr}) { | ||
| 115 | return $_attr_data{$attr}[0]; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 120 | =head2 The Constructor new(%arg) | ||
| 121 | |||
| 122 | Without any input, i.e., new(), the constructor generates an empty | ||
| 123 | object with default values for its parameters. | ||
| 124 | |||
| 125 | If any argument is provided, the constructor expects them in | ||
| 126 | the name and value pairs, i.e., in a hash array. | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub new { | ||
| 131 | my $caller = shift; | ||
| 132 | my $caller_is_obj = ref($caller); | ||
| 133 | my $class = $caller_is_obj || $caller; | ||
| 134 | my $self = bless {}, $class; | ||
| 135 | my %arg = @_; # convert rest of inputs into hash array | ||
| 136 | # print join "|", $caller, $caller_is_obj, $class, $self, "\n"; | ||
| 137 | foreach my $attrname ( $self->_standard_keys() ) { | ||
| 138 | my ($argname) = ($attrname =~ /^_(.*)/); | ||
| 139 | # print "attrname = $attrname: argname = $argname\n"; | ||
| 140 | if (exists $arg{$argname}) { | ||
| 141 | $self->{$attrname} = $arg{$argname}; | ||
| 142 | } elsif ($caller_is_obj) { | ||
| 143 | $self->{$attrname} = $caller->{$attrname}; | ||
| 144 | } else { | ||
| 145 | $self->{$attrname} = $self->_default_for($attrname); | ||
| 146 | } | ||
| 147 | # print $attrname, " = ", $self->{$attrname}, "\n"; | ||
| 148 | } | ||
| 149 | # $self->debug(5); | ||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | # implement other get_... and set_... method (create as neccessary) | ||
| 155 | sub AUTOLOAD { | ||
| 156 | no strict "refs"; | ||
| 157 | my ($self, $v1, $v2) = @_; | ||
| 158 | (my $sub = $AUTOLOAD) =~ s/.*:://; | ||
| 159 | my $m = $sub; | ||
| 160 | (my $attr = $sub) =~ s/(get_|set_)//; | ||
| 161 | $attr = "_$attr"; | ||
| 162 | # print join "|", $self, $v1, $v2, $sub, $attr,"\n"; | ||
| 163 | my $type = $self->_accs_type($attr); | ||
| 164 | croak "ERR: No such method: $AUTOLOAD.\n" if !$type; | ||
| 165 | my $v = ""; | ||
| 166 | my $msg = "WARN: no permission to change"; | ||
| 167 | if ($type eq '$') { # scalar method | ||
| 168 | $v = "\n"; | ||
| 169 | $v .= " my \$s = shift;\n"; | ||
| 170 | $v .= " croak \"ERR: Too many args to $m.\" if \@_ > 1;\n"; | ||
| 171 | if ($self->_accessible($attr, 'write')) { | ||
| 172 | $v .= " \@_ ? (\$s->{$attr}=shift) : "; | ||
| 173 | $v .= "return \$s->{$attr};\n"; | ||
| 174 | } else { | ||
| 175 | $v .= " \@_ ? (carp \"$msg $m.\n\") : "; | ||
| 176 | $v .= "return \$s->{$attr};\n"; | ||
| 177 | } | ||
| 178 | } elsif ($type eq '@') { # array method | ||
| 179 | $v = "\n"; | ||
| 180 | $v .= " my \$s = shift;\n"; | ||
| 181 | $v .= " my \$a = \$s->{$attr}; # get array ref\n"; | ||
| 182 | $v .= " if (\@_ && (ref(\$_[0]) eq 'ARRAY' "; | ||
| 183 | $v .= "|| \$_[0] =~ /.*=ARRAY/)) {\n"; | ||
| 184 | $v .= " \$s->{$attr} = shift; return;\n }\n"; | ||
| 185 | $v .= " my \$i; # array index\n"; | ||
| 186 | $v .= " \@_ ? (\$i=shift) : return \$a;\n"; | ||
| 187 | $v .= " croak \"ERR: Too many args to $m.\" if \@_ > 1;\n"; | ||
| 188 | if ($self->_accessible($attr, 'write')) { | ||
| 189 | $v .= " \@_ ? (\${\$a}[\$i]=shift) : "; | ||
| 190 | $v .= "return \${\$a}[\$i];\n"; | ||
| 191 | } else { | ||
| 192 | $v .= " \@_ ? (carp \"$msg $m.\n\") : "; | ||
| 193 | $v .= "return \${\$a}[\$i];\n"; | ||
| 194 | } | ||
| 195 | } else { # assume hash method: type = '%' | ||
| 196 | $v = "\n"; | ||
| 197 | $v .= " my \$s = shift;\n"; | ||
| 198 | $v .= " my \$a = \$s->{$attr}; # get hash array ref\n"; | ||
| 199 | $v .= " if (\@_ && (ref(\$_[0]) eq 'HASH' "; | ||
| 200 | $v .= " || \$_[0] =~ /.*=HASH/)) {\n"; | ||
| 201 | $v .= " \$s->{$attr} = shift; return;\n }\n"; | ||
| 202 | $v .= " my \$k; # hash array key\n"; | ||
| 203 | $v .= " \@_ ? (\$k=shift) : return \$a;\n"; | ||
| 204 | $v .= " croak \"ERR: Too many args to $m.\" if \@_ > 1;\n"; | ||
| 205 | if ($self->_accessible($attr, 'write')) { | ||
| 206 | $v .= " \@_ ? (\${\$a}{\$k}=shift) : "; | ||
| 207 | $v .= "return \${\$a}{\$k};\n"; | ||
| 208 | } else { | ||
| 209 | $v .= " \@_ ? (carp \"$msg $m.\n\") : "; | ||
| 210 | $v .= "return \${\$a}{\$k};\n"; | ||
| 211 | } | ||
| 212 | } | ||
| 213 | # $self->echoMSG("sub $m {$v}\n",100); | ||
| 214 | *{$sub} = eval "sub {$v}"; | ||
| 215 | goto &$sub; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub DESTROY { | ||
| 219 | my ($self) = @_; | ||
| 220 | # clean up base classes | ||
| 221 | return if !@ISA; | ||
| 222 | foreach my $parent (@ISA) { | ||
| 223 | next if $self::DESTROY{$parent}++; | ||
| 224 | my $destructor = $parent->can("DESTROY"); | ||
| 225 | $self->$destructor() if $destructor; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | =head3 xcopy($from, $to, $pat, $par) | ||
| 231 | |||
| 232 | Input variables: | ||
| 233 | |||
| 234 | $from - a source file or directory | ||
| 235 | $to - a target directory or file name | ||
| 236 | $pat - file name match pattern, default to {.+} | ||
| 237 | $par - parameter array | ||
| 238 | log_file - log file name with full path | ||
| 239 | |||
| 240 | |||
| 241 | Variables used or routines called: | ||
| 242 | |||
| 243 | get_stat - get file stats | ||
| 244 | output - output the stats | ||
| 245 | execute - execute a action | ||
| 246 | |||
| 247 | How to use: | ||
| 248 | |||
| 249 | use File::Xcopy; | ||
| 250 | my $obj = File::Xcopy->new; | ||
| 251 | # copy all the files with .txt extension if they exists in /tgt/dir | ||
| 252 | $obj->xcopy('/src/files', '/tgt/dir', '\.txt$'); | ||
| 253 | |||
| 254 | use File:Xcopy qw(xcopy); | ||
| 255 | xcopy('/src/files', '/tgt/dir', '\.txt$'); | ||
| 256 | |||
| 257 | Return: ($n, $m). | ||
| 258 | |||
| 259 | $n - number of files copied or moved. | ||
| 260 | $m - total number of files matched | ||
| 261 | |||
| 262 | =cut | ||
| 263 | |||
| 264 | sub xcopy { | ||
| 265 | my $self = shift; | ||
| 266 | my $class = ref($self)||$self; | ||
| 267 | my($from,$to, $pat, $par) = @_; | ||
| 268 | $self->action('copy'); | ||
| 269 | my ($sr, $rr) = $self->get_stat(@_); | ||
| 270 | return $self->execute; | ||
| 271 | } | ||
| 272 | |||
| 273 | =head3 syscopy($from, $to) | ||
| 274 | |||
| 275 | Input variables: | ||
| 276 | |||
| 277 | $from - a source file or directory | ||
| 278 | $to - a target directory or file name | ||
| 279 | |||
| 280 | Variables used or routines called: | ||
| 281 | |||
| 282 | |||
| 283 | How to use: | ||
| 284 | |||
| 285 | use File::Xcopy; | ||
| 286 | syscopy('/src/file_a', '/tgt/dir/file_b'); # copy to a file | ||
| 287 | syscopy('/src/file_a', '/tgt/dir'); # copy to a dir | ||
| 288 | syscopy('/src/dir_a', '/tgt/dir_b'); # copy a dir to a dir | ||
| 289 | |||
| 290 | Return: none | ||
| 291 | |||
| 292 | =cut | ||
| 293 | |||
| 294 | sub syscopy { | ||
| 295 | my $self = shift; | ||
| 296 | my $class = ref($self)||$self; | ||
| 297 | my ($from, $to) = @_; | ||
| 298 | |||
| 299 | my @arg = (); | ||
| 300 | $arg[0] = '/bin/cp'; | ||
| 301 | if ($^O eq 'VMS') { | ||
| 302 | return &rmscopy(@_); | ||
| 303 | } elsif ($^O eq 'MacOS') { | ||
| 304 | return &maccopy(@_); | ||
| 305 | } elsif ($^O eq 'MSWin32') { | ||
| 306 | $arg[0] = 'copy'; | ||
| 307 | $from =~ s{/}{\\}g; | ||
| 308 | $to =~ s{/}{\\}g; | ||
| 309 | push @arg, "\"$from\"", "\"$to\""; | ||
| 310 | } else { | ||
| 311 | push @arg, '-p', '-f', $from, $to; | ||
| 312 | } | ||
| 313 | # print "ARG: @arg\n"; | ||
| 314 | my $rcode = system(@arg); | ||
| 315 | # MSWin32 returns 256: $rcode>>8 = 1 | ||
| 316 | # Unix returns 0: | ||
| 317 | my $r = ""; | ||
| 318 | if ($^O eq 'MSWin32') { | ||
| 319 | $r = $rcode>>8; | ||
| 320 | } else { | ||
| 321 | $r = ($rcode==0)?1:0; | ||
| 322 | } | ||
| 323 | return $r; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub maccopy { | ||
| 327 | require Mac::MoreFiles; | ||
| 328 | my($from, $to) = @_; | ||
| 329 | my($dir, $toname); | ||
| 330 | return 0 unless -e $from; | ||
| 331 | if ($to =~ /(.*:)([^:]+):?$/) { | ||
| 332 | ($dir, $toname) = ($1, $2); | ||
| 333 | } else { | ||
| 334 | ($dir, $toname) = (":", $to); | ||
| 335 | } | ||
| 336 | unlink($to); | ||
| 337 | return Mac::MoreFiles::FSpFileCopy($from, $dir, $toname, 1); | ||
| 338 | } | ||
| 339 | |||
| 340 | =head3 xmove($from, $to, $pat, $par) | ||
| 341 | |||
| 342 | Input variables: | ||
| 343 | |||
| 344 | $from - a source file or directory | ||
| 345 | $to - a target directory or file name | ||
| 346 | $pat - file name match pattern, default to {.+} | ||
| 347 | $par - parameter array | ||
| 348 | log_file - log file name with full path | ||
| 349 | |||
| 350 | |||
| 351 | Variables used or routines called: | ||
| 352 | |||
| 353 | get_stat - get file stats | ||
| 354 | output - output the stats | ||
| 355 | execute - execute a action | ||
| 356 | |||
| 357 | How to use: | ||
| 358 | |||
| 359 | use File::Xcopy; | ||
| 360 | my $obj = File::Xcopy->new; | ||
| 361 | # move the files with .txt extension if they exists in /tgt/dir | ||
| 362 | $obj->xmove('/src/files', '/tgt/dir', '\.txt$'); | ||
| 363 | |||
| 364 | Return: ($n, $m). | ||
| 365 | |||
| 366 | $n - number of files copied or moved. | ||
| 367 | $m - total number of files matched | ||
| 368 | |||
| 369 | =cut | ||
| 370 | |||
| 371 | sub xmove { | ||
| 372 | my $self = shift; | ||
| 373 | my $class = ref($self)||$self; | ||
| 374 | my($from,$to, $pat, $par) = @_; | ||
| 375 | $self->action('move'); | ||
| 376 | my ($sr, $rr) = $self->get_stat(@_); | ||
| 377 | return $self->execute; | ||
| 378 | }; | ||
| 379 | |||
| 380 | *xcp = \&xcopy; | ||
| 381 | *xmv = \&xmove; | ||
| 382 | |||
| 383 | =head3 execute ($act) | ||
| 384 | |||
| 385 | Input variables: | ||
| 386 | |||
| 387 | $act - action: | ||
| 388 | report|test - test run | ||
| 389 | copy|CP - copy files from source to target only if | ||
| 390 | 1) the files do not exist or | ||
| 391 | 2) newer than the existing ones | ||
| 392 | This is default. | ||
| 393 | overwrite|OW - copy files from source to target only if | ||
| 394 | 1) the files exist and | ||
| 395 | 2) no matter is older or newer | ||
| 396 | move|MV - same as in copy except it removes from source the | ||
| 397 | following files: | ||
| 398 | 1) files are exactly the same (size and time stamp) | ||
| 399 | 2) files are copied successfully | ||
| 400 | update|UD - copy files only if | ||
| 401 | 1) the file exists in the target and | ||
| 402 | 2) newer in time stamp | ||
| 403 | |||
| 404 | Variables used or routines called: None | ||
| 405 | |||
| 406 | How to use: | ||
| 407 | |||
| 408 | use File::Xcopy; | ||
| 409 | my $obj = File::Xcopy->new; | ||
| 410 | # update all the files with .txt extension if they exists in /tgt/dir | ||
| 411 | $obj->get_stat('/src/files', '/tgt/dir', '\.txt$'); | ||
| 412 | my ($n, $m) = $obj->execute('overwrite'); | ||
| 413 | |||
| 414 | Return: ($n, $m). | ||
| 415 | |||
| 416 | $n - number of files copied or moved. | ||
| 417 | $m - total number of files matched | ||
| 418 | |||
| 419 | =cut | ||
| 420 | |||
| 421 | sub execute { | ||
| 422 | my $self = shift; | ||
| 423 | my ($act) = @_; | ||
| 424 | $act = $self->action if ! $act; | ||
| 425 | $act = 'test' if ! $act; | ||
| 426 | my $sr = $self->param('stat_ar'); | ||
| 427 | my $rr = $self->param('file_ar'); | ||
| 428 | croak "ERR: please run get_stat first.\n" if ! $rr; | ||
| 429 | $self->action($act); | ||
| 430 | my $par = $self->param; | ||
| 431 | my $vbm = ${$par}{verbose}; | ||
| 432 | my $fdir = $self->from_dir; | ||
| 433 | my $tdir = $self->to_dir; | ||
| 434 | my ($n, $m, $tp, $f1, $f2) = (0,0,"","",""); | ||
| 435 | my $tm_bit = 1; | ||
| 436 | foreach my $f (sort keys %{$rr}) { | ||
| 437 | ++$m; | ||
| 438 | $tp = ${$rr}{$f}{type}; | ||
| 439 | # skip if the file only exists in to_dir | ||
| 440 | next if ($tp =~ /^OLD/); | ||
| 441 | my $f3 = $f; $f3 =~ s{^\.\/}{}; | ||
| 442 | $f1 = join '/', $fdir, $f3; | ||
| 443 | $f2 = join '/', $tdir, $f3; | ||
| 444 | next if -d $f1; # skip the sub-dirs | ||
| 445 | if (! -f $f1) { | ||
| 446 | carp "WARN: 1 - could not find $f1\n"; | ||
| 447 | next; | ||
| 448 | } | ||
| 449 | my $td2 = dirname($f2); | ||
| 450 | if (!-d $td2) { | ||
| 451 | # we need to get original mask and imply here | ||
| 452 | mkdir $td2; | ||
| 453 | } | ||
| 454 | if ($act =~ /^c/i) { # copy | ||
| 455 | if ($tp =~ /^(NEW|EX1|EX2)/) { | ||
| 456 | if ($self->syscopy($f1, $f2)) { | ||
| 457 | ++$n; | ||
| 458 | print "$f1 copied to $f2\n" if $vbm; | ||
| 459 | } else { | ||
| 460 | carp "ERR: could not copy $f1: $!\n"; | ||
| 461 | } | ||
| 462 | } else { | ||
| 463 | print "copying $f1 to $f2: skipped.\n" if $vbm; | ||
| 464 | } | ||
| 465 | } elsif ($act =~ /^m/i) { # move | ||
| 466 | if ($tp =~ /^(NEW|EX1|EX2)/) { | ||
| 467 | if ($self->syscopy($f1, $f2)) { | ||
| 468 | ++$n; | ||
| 469 | unlink $f1; | ||
| 470 | print "$f1 moved to $f2\n" if $vbm; | ||
| 471 | } else { | ||
| 472 | carp "ERR: could not move $f1: $!\n"; | ||
| 473 | } | ||
| 474 | } else { | ||
| 475 | print "moving $f1 to $f2: skipped.\n" if $vbm; | ||
| 476 | } | ||
| 477 | } elsif ($act =~ /^u/i) { # update | ||
| 478 | if ($tp =~ /^(EX1|EX2)/) { | ||
| 479 | if ($self->syscopy($f1, $f2)) { | ||
| 480 | ++$n; | ||
| 481 | print "$f1 updated to $f2\n" if $vbm; | ||
| 482 | } else { | ||
| 483 | carp "ERR: could not update $f2: $!\n"; | ||
| 484 | } | ||
| 485 | } else { | ||
| 486 | print "updating $f1 to $f2: skipped.\n" if $vbm; | ||
| 487 | } | ||
| 488 | } elsif ($act =~ /^o/i) { # overwirte | ||
| 489 | if ($tp =~ /^(EX0|EX1|EX2)/) { | ||
| 490 | if ($self->syscopy($f1, $f2)) { | ||
| 491 | ++$n; | ||
| 492 | print "$f1 overwritten $f2\n" if $vbm; | ||
| 493 | } else { | ||
| 494 | carp "ERR: could not overwrite $f2: $!\n"; | ||
| 495 | } | ||
| 496 | } else { | ||
| 497 | print "overwriting $f1 to $f2: skipped.\n" if $vbm; | ||
| 498 | } | ||
| 499 | } else { | ||
| 500 | carp "WARN: $f - do not know what to do.\n"; | ||
| 501 | } | ||
| 502 | } | ||
| 503 | $self->output($sr,$rr,"",$par); | ||
| 504 | return ($n ,$m); | ||
| 505 | } | ||
| 506 | |||
| 507 | |||
| 508 | =head3 get_stat($from, $to, $pat, $par) | ||
| 509 | |||
| 510 | Input variables: | ||
| 511 | |||
| 512 | $from - a source file or directory | ||
| 513 | $to - a target directory or file name | ||
| 514 | $pat - file name match pattern, default to {.+} | ||
| 515 | $par - parameter array | ||
| 516 | log_file - log file name with full path | ||
| 517 | |||
| 518 | |||
| 519 | I currently only implemented /S paramter. Here is an example on how to | ||
| 520 | use the module: | ||
| 521 | |||
| 522 | package main; | ||
| 523 | my $self = bless {}, "main"; | ||
| 524 | |||
| 525 | use File::Xcopy; | ||
| 526 | use Debug::EchoMessage; | ||
| 527 | |||
| 528 | my $xcp = File::Xcopy->new; | ||
| 529 | my $fm = '/opt/from/dir'; | ||
| 530 | my $to = '/opt/to/dir'; | ||
| 531 | my %p = (s=>1); # or $xcp->param('s',1); | ||
| 532 | my ($a, $b) = $xcp->get_stat($fm, $to, '\.sql$', \%p); | ||
| 533 | # $self->disp_param($a); | ||
| 534 | # $self->disp_param($b); | ||
| 535 | $xcp->output($a,$b); | ||
| 536 | |||
| 537 | $xcp->param('verbose',1); | ||
| 538 | my ($n, $m) = $xcp->execute('cp'); | ||
| 539 | # $self->disp_param($xcp->param()); | ||
| 540 | |||
| 541 | print "Total number of files matched: $m\n"; | ||
| 542 | print "Number of files copied: $n\n"; | ||
| 543 | |||
| 544 | I will implement the following parameters gradually: | ||
| 545 | |||
| 546 | source Specifies the file(s) to copy. | ||
| 547 | destination Specifies the location and/or name of new files. | ||
| 548 | /A Copies only files with the archive attribute set, | ||
| 549 | doesn't change the attribute. | ||
| 550 | /M Copies only files with the archive attribute set, | ||
| 551 | turns off the archive attribute. | ||
| 552 | /D:m-d-y Copies files changed on or after the specified date. | ||
| 553 | If no date is given, copies only those files whose | ||
| 554 | source time is newer than the destination time. | ||
| 555 | /EXCLUDE:file1[+file2][+file3]... | ||
| 556 | Specifies a list of files containing strings. | ||
| 557 | When any of the strings match any part of the absolute | ||
| 558 | path of the file to be copied, that file will be | ||
| 559 | excluded from being copied. For example, specifying a | ||
| 560 | string like \obj\ or .obj will exclude all files | ||
| 561 | underneath the directory obj or all files with the | ||
| 562 | .obj extension respectively. | ||
| 563 | /P Prompts you before creating each destination file. | ||
| 564 | /S Copies directories and subdirectories except empty ones. | ||
| 565 | /E Copies directories and subdirectories, including empty | ||
| 566 | ones. Same as /S /E. May be used to modify /T. | ||
| 567 | /V Verifies each new file. | ||
| 568 | /W Prompts you to press a key before copying. | ||
| 569 | /C Continues copying even if errors occur. | ||
| 570 | /I If destination does not exist and copying more than one | ||
| 571 | file, | ||
| 572 | assumes that destination must be a directory. | ||
| 573 | /Q Does not display file names while copying. | ||
| 574 | /F Displays full source and destination file names while | ||
| 575 | copying. | ||
| 576 | /L Displays files that would be copied. | ||
| 577 | /H Copies hidden and system files also. | ||
| 578 | /R Overwrites read-only files. | ||
| 579 | /T Creates directory structure, but does not copy files. | ||
| 580 | Does not include empty directories or subdirectories. | ||
| 581 | /T /E includes empty directories and subdirectories. | ||
| 582 | /U Copies only files that already exist in destination. | ||
| 583 | /K Copies attributes. Normal Xcopy will reset read-only | ||
| 584 | attributes. | ||
| 585 | /N Copies using the generated short names. | ||
| 586 | /O Copies file ownership and ACL information. | ||
| 587 | /X Copies file audit settings (implies /O). | ||
| 588 | /Y Suppresses prompting to confirm you want to overwrite an | ||
| 589 | existing destination file. | ||
| 590 | /-Y Causes prompting to confirm you want to overwrite an | ||
| 591 | existing destination file. | ||
| 592 | /Z Copies networked files in restartable mode. | ||
| 593 | |||
| 594 | Variables used or routines called: | ||
| 595 | |||
| 596 | from_dir - get from_dir | ||
| 597 | to_dir - get to_dir | ||
| 598 | fn_pat - get file name pattern | ||
| 599 | param - get parameters | ||
| 600 | find_files - get a list of files from a dir and its sub dirs | ||
| 601 | list_files - get a list of files from a dir | ||
| 602 | file_stat - get file stats | ||
| 603 | fmtTime - format time | ||
| 604 | |||
| 605 | |||
| 606 | How to use: | ||
| 607 | |||
| 608 | use File::Xcopy; | ||
| 609 | my $obj = File::Xcopy->new; | ||
| 610 | # get stat for all the files with .txt extension | ||
| 611 | # if they exists in /tgt/dir | ||
| 612 | $obj->get_stat('/src/files', '/tgt/dir', '\.txt$'); | ||
| 613 | |||
| 614 | use File:Xcopy qw(xcopy); | ||
| 615 | xcopy('/src/files', '/tgt/dir', 'OW', '\.txt$'); | ||
| 616 | |||
| 617 | Return: ($sr, $rr). | ||
| 618 | |||
| 619 | $sr - statistic hash array ref with the following keys: | ||
| 620 | OK - the files are the same in size and time stamp | ||
| 621 | txt - "The Same size and time" | ||
| 622 | cnt - count of files | ||
| 623 | szt - total bytes of all files in the category | ||
| 624 | NO - the files are different either in size or time | ||
| 625 | txt - "Different size or time" | ||
| 626 | cnt - count of files | ||
| 627 | szt - total bytes of all files in the category | ||
| 628 | OLD{txt|cnt|szt} - "File does not exist in FROM folder" | ||
| 629 | NEW{txt|cnt|szt} - "File does not exist in TO folder" | ||
| 630 | EX0{txt|cnt|szt} - "File is older or the same" | ||
| 631 | EX1{txt|cnt|szt} - "File is newer and its size bigger" | ||
| 632 | EX2{txt|cnt|szt} - "File is newer and its size smaller" | ||
| 633 | STAT | ||
| 634 | max_size - largest file in all the selected files | ||
| 635 | min_size - smallest file in all the selected files. | ||
| 636 | max_time - time stamp of the most recent file | ||
| 637 | min_time - time stamp of the oldest file | ||
| 638 | |||
| 639 | The sum of {OK} and {NO} is equal to the sum of {EX0}, {EX1} and | ||
| 640 | {EX2}. | ||
| 641 | |||
| 642 | $rr - result hash array ref with the following keys {$f}{$itm}: | ||
| 643 | {$f} - file name relative to from_dir or to_dir | ||
| 644 | file - file name without dir parts | ||
| 645 | pdir - parent directory | ||
| 646 | prop - file stat array | ||
| 647 | rdir - relative file name to the $dir | ||
| 648 | path - full path of the file | ||
| 649 | type - file status: NEW, OLD, EX1, or EX2 | ||
| 650 | f_pdir - parent dir for from_dir | ||
| 651 | f_size - file size in bytes from from_dir | ||
| 652 | f_time - file time stamp from from_dir | ||
| 653 | t_pdir - parent dir for to_dir | ||
| 654 | t_size - file size in bytes from to_dir | ||
| 655 | t_time - file time stamp from to_dir | ||
| 656 | tmdiff - time difference in seconds between the file | ||
| 657 | in from_dir and to_dir | ||
| 658 | szdiff - size difference in bytes between the file | ||
| 659 | in from_dir and to_dir | ||
| 660 | action - suggested action: CP, OW, SK | ||
| 661 | |||
| 662 | The method also sets the two parameters: stat_ar, file_ar and you can | ||
| 663 | get it using this method: | ||
| 664 | |||
| 665 | my $sr = $self->param('stat_ar'); | ||
| 666 | my $rr = $self->param('file_ar'); | ||
| 667 | |||
| 668 | =cut | ||
| 669 | |||
| 670 | sub get_stat { | ||
| 671 | my $self = shift; | ||
| 672 | my $class = ref($self)||$self; | ||
| 673 | my($from,$to, $pat, $par) = @_; | ||
| 674 | $from = $self->from_dir if ! $from; | ||
| 675 | $to = $self->to_dir if ! $to; | ||
| 676 | $pat = $self->fn_pat if ! $pat; | ||
| 677 | $par = $self->param if ! $par; | ||
| 678 | croak "ERR: source dir or file not specified.\n" if ! $from; | ||
| 679 | croak "ERR: target dir not specified.\n" if ! $to; | ||
| 680 | croak "ERR: could not find src dir - $from.\n" if ! -d $from; | ||
| 681 | croak "ERR: could not find tgt dir - $to.\n" if ! -d $to ; | ||
| 682 | $self->from_dir($from); | ||
| 683 | $self->to_dir($to); | ||
| 684 | $self->fn_pat($pat); | ||
| 685 | my ($re, $n, $m, $t); | ||
| 686 | if ($pat) { $re = qr {$pat}; } else { $re = qr {.+}; } | ||
| 687 | # $$re = qr {^lib_df51t5.*(\.pl|\.txt)$}; | ||
| 688 | my $far = bless [], $class; # from array ref | ||
| 689 | my $tar = bless [], $class; # to array ref | ||
| 690 | # get file name list | ||
| 691 | if ($par && exists ${$par}{s}) { # search sub-dir as well | ||
| 692 | $far = $self->find_files($from, $re); | ||
| 693 | $tar = $self->find_files($to, $re); | ||
| 694 | } else { # only files in $from | ||
| 695 | $far = $self->list_files($from, $re); | ||
| 696 | $tar = $self->list_files($to, $re); | ||
| 697 | } | ||
| 698 | # convert array into hash | ||
| 699 | my $fhr = $self->file_stat($from, $far); | ||
| 700 | my $thr = $self->file_stat($to, $tar); | ||
| 701 | my %r = (); | ||
| 702 | my %s = ( OK=>{txt=>"The Same size and time"}, | ||
| 703 | NO=>{txt=>"Different size or time"}, | ||
| 704 | OLD=>{txt=>"File does not exist in FROM folder"}, | ||
| 705 | NEW=>{txt=>"File does not exist in TO folder"}, | ||
| 706 | EX0=>{txt=>"File is older or the same"}, | ||
| 707 | EX1=>{txt=>"File is newer and its size bigger"}, | ||
| 708 | EX2=>{txt=>"File is newer and its size smaller"}, | ||
| 709 | STAT=>{max_size=>0, min_size=>99999999999, | ||
| 710 | max_time=>0, min_time=>99999999999}, | ||
| 711 | ); | ||
| 712 | foreach my $f (keys %{$fhr}) { | ||
| 713 | $s{STAT}{max_size} = ($s{STAT}{max_size}<${$fhr}{$f}{size}) ? | ||
| 714 | ${$fhr}{$f}{size} : $s{STAT}{max_size}; | ||
| 715 | $s{STAT}{min_size} = ($s{STAT}{min_size}>${$fhr}{$f}{size}) ? | ||
| 716 | ${$fhr}{$f}{size} : $s{STAT}{min_size}; | ||
| 717 | $s{STAT}{max_time} = ($s{STAT}{max_time}<${$fhr}{$f}{time}) ? | ||
| 718 | ${$fhr}{$f}{time} : $s{STAT}{max_time}; | ||
| 719 | $s{STAT}{min_time} = ($s{STAT}{min_time}>${$fhr}{$f}{time}) ? | ||
| 720 | ${$fhr}{$f}{time} : $s{STAT}{min_time}; | ||
| 721 | $r{$f} = {file=>${$fhr}{$f}{file}, f_pdir=>${$fhr}{$f}{pdir}, | ||
| 722 | f_size=>${$fhr}{$f}{size}, | ||
| 723 | f_time=>$self->fmtTime(${$fhr}{$f}{time})}; | ||
| 724 | if (! exists ${$thr}{$f}) { | ||
| 725 | ++$s{NEW}{cnt}; | ||
| 726 | $s{NEW}{szt} += ${$fhr}{$f}{size}; | ||
| 727 | $r{$f}{t_pdir}=$to; $r{$f}{t_size}=""; | ||
| 728 | $r{$f}{t_time}=""; $r{$f}{tmdiff}=""; | ||
| 729 | $r{$f}{szdiff}=""; | ||
| 730 | $r{$f}{action}="CP"; | ||
| 731 | $r{$f}{type} = 'NEW'; | ||
| 732 | next; | ||
| 733 | } | ||
| 734 | $r{$f}{t_pdir}=${$thr}{$f}{pdir}; | ||
| 735 | $r{$f}{t_size}=${$thr}{$f}{size}; | ||
| 736 | $r{$f}{t_time}=$self->fmtTime(${$thr}{$f}{time}); | ||
| 737 | $r{$f}{tmdiff}=${$thr}{$f}{time}-${$fhr}{$f}{time}; | ||
| 738 | $r{$f}{szdiff}=${$thr}{$f}{size}-${$fhr}{$f}{size}; | ||
| 739 | if (${$fhr}{$f}{size} == ${$thr}{$f}{size} && | ||
| 740 | ${$fhr}{$f}{time} == ${$thr}{$f}{time} ) { | ||
| 741 | ++$s{OK}{cnt}; | ||
| 742 | $s{OK}{szt} += ${$fhr}{$f}{size}; | ||
| 743 | $r{$f}{action}="no action"; | ||
| 744 | $r{$f}{type} = 'OK'; | ||
| 745 | next; | ||
| 746 | } | ||
| 747 | $s{NO}{szt} += ${$fhr}{$f}{size}; | ||
| 748 | $r{$f}{type} = 'NO'; | ||
| 749 | ++$s{NO}{cnt}; | ||
| 750 | if (${$fhr}{$f}{time}>${$thr}{$f}{time}) { | ||
| 751 | if (${$fhr}{$f}{time}>${$thr}{$f}{time}) { | ||
| 752 | ++$s{EX1}{cnt}; | ||
| 753 | $s{EX1}{szt} += ${$fhr}{$f}{size}; | ||
| 754 | $r{$f}{type} = 'EX1'; | ||
| 755 | } else { | ||
| 756 | ++$s{EX2}{cnt}; | ||
| 757 | $s{EX2}{szt} += ${$fhr}{$f}{size}; | ||
| 758 | $r{$f}{type} = 'EX2'; | ||
| 759 | } | ||
| 760 | $r{$f}{action}="OW"; | ||
| 761 | } else { | ||
| 762 | $r{$f}{action}="SK"; | ||
| 763 | ++$s{EX0}{cnt}; | ||
| 764 | $s{EX0}{szt} += ${$fhr}{$f}{size}; | ||
| 765 | $r{$f}{type} = 'EX0'; | ||
| 766 | } | ||
| 767 | } | ||
| 768 | foreach my $f (keys %{$thr}) { | ||
| 769 | $s{STAT}{max_size} = ($s{STAT}{max_size}<${$thr}{$f}{size}) ? | ||
| 770 | ${$thr}{$f}{size} : $s{STAT}{max_size}; | ||
| 771 | $s{STAT}{min_size} = ($s{STAT}{min_size}>${$thr}{$f}{size}) ? | ||
| 772 | ${$thr}{$f}{size} : $s{STAT}{min_size}; | ||
| 773 | $s{STAT}{max_time} = ($s{STAT}{max_time}<${$thr}{$f}{time}) ? | ||
| 774 | ${$thr}{$f}{time} : $s{STAT}{max_time}; | ||
| 775 | $s{STAT}{min_time} = ($s{STAT}{min_time}>${$thr}{$f}{time}) ? | ||
| 776 | ${$thr}{$f}{time} : $s{STAT}{min_time}; | ||
| 777 | next if (exists ${$fhr}{$f}); | ||
| 778 | ++$s{OLD}{cnt}; | ||
| 779 | $s{OLD}{szt} += ${$thr}{$f}{size}; | ||
| 780 | $r{$f} = {file=>${$thr}{$f}{file}, | ||
| 781 | f_pdir=>"", f_size=>"", f_time=>"", | ||
| 782 | t_pdir=>${$thr}{$f}{pdir}, | ||
| 783 | t_size=>${$thr}{$f}{size}, | ||
| 784 | t_time=>$self->fmtTime(${$thr}{$f}{time}), | ||
| 785 | tmdiff=>"", szdiff=>"", | ||
| 786 | action=>"NA", type =>'OLD' | ||
| 787 | }; | ||
| 788 | } | ||
| 789 | $s{STAT}{tmdiff}=$s{STAT}{max_time}-$s{STAT}{min_time}; | ||
| 790 | $s{STAT}{szdiff}=$s{STAT}{max_size}-$s{STAT}{min_size}; | ||
| 791 | $s{STAT}{max_time}=$self->fmtTime($s{STAT}{max_time}); | ||
| 792 | $s{STAT}{min_time}=$self->fmtTime($s{STAT}{min_time}); | ||
| 793 | |||
| 794 | $self->param('stat_ar', \%s); | ||
| 795 | $self->param('file_ar', \%r); | ||
| 796 | |||
| 797 | # $self->disp_param(\%s); | ||
| 798 | |||
| 799 | return (\%s, \%r); | ||
| 800 | } | ||
| 801 | |||
| 802 | =head2 output($sr,$rr, $out, $par) | ||
| 803 | |||
| 804 | Input variables: | ||
| 805 | |||
| 806 | $sr - statistic hash array ref from xcopy | ||
| 807 | $rr - result hash array ref containing all the files and their | ||
| 808 | properties. | ||
| 809 | $out - output file name. If specified, the log_file will not be used. | ||
| 810 | $par - array ref containing parameters such as | ||
| 811 | log_file - log file name | ||
| 812 | |||
| 813 | Variables used or routines called: | ||
| 814 | |||
| 815 | from_dir - get from_dir | ||
| 816 | to_dir - get to_dir | ||
| 817 | fn_pat - get file name pattern | ||
| 818 | param - get parameters | ||
| 819 | action - get action name | ||
| 820 | format_number - format time or size numbers | ||
| 821 | |||
| 822 | How to use: | ||
| 823 | |||
| 824 | use File::Xcopy; | ||
| 825 | my $fc = File::Xcopy->new; | ||
| 826 | my ($s, $r) = $fc->get_stat($fdir, $tdir, 'pdf$') | ||
| 827 | $fc->output($s, $r); | ||
| 828 | |||
| 829 | Return: None. | ||
| 830 | |||
| 831 | |||
| 832 | If $out or log_file parameter is provided, then the result will be | ||
| 833 | outputed to it. | ||
| 834 | |||
| 835 | =cut | ||
| 836 | |||
| 837 | sub output { | ||
| 838 | my $self = shift; | ||
| 839 | my ($sr, $rr, $out, $par) = @_; | ||
| 840 | my $fh = ""; | ||
| 841 | if ($out) { | ||
| 842 | $fh = new IO::File "$out", O_WRONLY|O_APPEND; | ||
| 843 | } | ||
| 844 | $fh = *STDOUT if (!$fh && (!$par || ! exists ${$par}{log_file})); | ||
| 845 | if (!$fh && -f ${$par}{log_file}) { | ||
| 846 | $fh = new IO::File "${$par}{log_file}", O_WRONLY|O_APPEND; | ||
| 847 | } | ||
| 848 | my $fdir = $self->from_dir; | ||
| 849 | my $tdir = $self->to_dir; | ||
| 850 | my $fpat = $self->fn_pat; | ||
| 851 | my $act = $self->action; | ||
| 852 | my $fmt = "# %35s: %9s:%6.2f\%:%10s\n"; | ||
| 853 | my $ft1 = "# %15s: max %15s min %15s diff %10s\n"; | ||
| 854 | my $t = ""; | ||
| 855 | if (exists ${$par}{log_file}) { | ||
| 856 | $t .= "# Xcopy Log File: ${$par}{log_file}\n" | ||
| 857 | } else { | ||
| 858 | $t .= "# Xcopy Log Output\n" | ||
| 859 | } | ||
| 860 | $t .= "# Date: " . localtime(time) . "\n"; | ||
| 861 | $t .= "# Input parameters: \n"; | ||
| 862 | $t .= "# From dir: $fdir\n# To dir: $tdir\n"; | ||
| 863 | $t .= "# File Pat: $fpat\n# Action : $act\n"; | ||
| 864 | $t .= "# File statistics: category: "; | ||
| 865 | $t .= " count: pct: total size\n"; | ||
| 866 | my $n = ${$sr}{NEW}{cnt}+${$sr}{EX0}{cnt}+${$sr}{EX1}{cnt} | ||
| 867 | +${$sr}{EX2}{cnt} +${$sr}{OLD}{cnt}; | ||
| 868 | $n = ($n)?$n:1; | ||
| 869 | my $m = ${$sr}{NEW}{szt}+${$sr}{EX0}{szt}+${$sr}{EX1}{szt} | ||
| 870 | +${$sr}{EX2}{szt} +${$sr}{OLD}{szt}; | ||
| 871 | $t .= sprintf $fmt, ${$sr}{OK}{txt}, ${$sr}{OK}{cnt}, | ||
| 872 | 100*${$sr}{OK}{cnt}/$n, | ||
| 873 | $self->format_number(${$sr}{OK}{szt}); | ||
| 874 | $t .= sprintf $fmt, ${$sr}{NO}{txt}, ${$sr}{NO}{cnt}, | ||
| 875 | 100*${$sr}{NO}{cnt}/$n, | ||
| 876 | $self->format_number(${$sr}{NO}{szt}); | ||
| 877 | $t .= sprintf $fmt, ${$sr}{NEW}{txt}, ${$sr}{NEW}{cnt}, | ||
| 878 | 100*${$sr}{NEW}{cnt}/$n, | ||
| 879 | $self->format_number(${$sr}{NEW}{szt}); | ||
| 880 | $t .= sprintf $fmt, ${$sr}{EX0}{txt}, ${$sr}{EX0}{cnt}, | ||
| 881 | 100*${$sr}{EX0}{cnt}/$n, | ||
| 882 | $self->format_number(${$sr}{EX1}{szt}); | ||
| 883 | $t .= sprintf $fmt, ${$sr}{EX1}{txt}, ${$sr}{EX1}{cnt}, | ||
| 884 | 100*${$sr}{EX1}{cnt}/$n, | ||
| 885 | $self->format_number(${$sr}{EX1}{szt}); | ||
| 886 | $t .= sprintf $fmt, ${$sr}{EX2}{txt}, ${$sr}{EX2}{cnt}, | ||
| 887 | 100*${$sr}{EX2}{cnt}/$n, | ||
| 888 | $self->format_number(${$sr}{EX2}{szt}); | ||
| 889 | $t .= sprintf $fmt, ${$sr}{OLD}{txt}, ${$sr}{OLD}{cnt}, | ||
| 890 | 100*${$sr}{OLD}{cnt}/$n, | ||
| 891 | $self->format_number(${$sr}{OLD}{szt}); | ||
| 892 | $t .= "# " . ("-"x35) . ": ---------:-------:----------\n"; | ||
| 893 | $t .= sprintf $fmt, "Totals", $n, 100, $self->format_number($m); | ||
| 894 | $t .= "#\n"; | ||
| 895 | $t .= sprintf $ft1, "File size", ${$sr}{STAT}{max_size}, | ||
| 896 | ${$sr}{STAT}{min_size}, | ||
| 897 | $self->format_number(${$sr}{STAT}{szdiff},'time'); | ||
| 898 | $t .= sprintf $ft1, "File time", ${$sr}{STAT}{max_time}, | ||
| 899 | ${$sr}{STAT}{min_time}, | ||
| 900 | $self->format_number(${$sr}{STAT}{tmdiff},'time'); | ||
| 901 | print $fh $t; | ||
| 902 | $t = "#\n"; | ||
| 903 | # action:f_time:t_time:tmdiff:f_size:t_size:szdiff:file_name | ||
| 904 | $t .= "#action| from_time| to_time| tmdiff|"; | ||
| 905 | $t .= " from_size| to_size| szdiff|file_name"; | ||
| 906 | print $fh "$t\n"; | ||
| 907 | my $ft2 = "%2s|%15s|%15s|%10s|%10s|%10s|%10s|%-30s\n"; | ||
| 908 | foreach my $f (sort keys %{$rr}) { | ||
| 909 | $t = sprintf $ft2, ${$rr}{$f}{action}, | ||
| 910 | ${$rr}{$f}{f_time}, ${$rr}{$f}{t_time}, | ||
| 911 | $self->format_number(${$rr}{$f}{tmdiff},'time'), | ||
| 912 | $self->format_number(${$rr}{$f}{f_size}), | ||
| 913 | $self->format_number(${$rr}{$f}{t_size}), | ||
| 914 | $self->format_number(${$rr}{$f}{szdiff}), | ||
| 915 | $f; | ||
| 916 | print $fh $t; | ||
| 917 | } | ||
| 918 | undef $fh; | ||
| 919 | } | ||
| 920 | |||
| 921 | =head2 format_number($n,$t) | ||
| 922 | |||
| 923 | Input variables: | ||
| 924 | |||
| 925 | $n - a numeric number | ||
| 926 | $t - number type: | ||
| 927 | size - in bytes or | ||
| 928 | time - in seconds | ||
| 929 | |||
| 930 | Variables used or routines called: None. | ||
| 931 | |||
| 932 | How to use: | ||
| 933 | |||
| 934 | use File::Xcopy; | ||
| 935 | my $fc = File::Xcopy->new; | ||
| 936 | # convert bytes to KB, MB or GB | ||
| 937 | my $n1 = $self->format_number(10000000); # $n1 = 9.537MB | ||
| 938 | # convert seconds to DDD:HH:MM:SS | ||
| 939 | my $n2 = $self->format_number(1000000,'time'); # $n2 = 11D13:46:40 | ||
| 940 | |||
| 941 | Return: formated time difference in DDDHH:MM:SS or size in GB, MB or | ||
| 942 | KB. | ||
| 943 | |||
| 944 | =cut | ||
| 945 | |||
| 946 | sub format_number { | ||
| 947 | my $self = shift; | ||
| 948 | my ($n, $t) = @_; | ||
| 949 | # $n - number | ||
| 950 | # $t - type: size or time | ||
| 951 | # | ||
| 952 | return "" if $n =~ /^$/; | ||
| 953 | $t = 'size' if ! $t; | ||
| 954 | my ($r,$s) = ("",0); | ||
| 955 | my $kb = 1024; | ||
| 956 | my $mb = 1024*$kb; | ||
| 957 | my $gb = 1024*$mb; | ||
| 958 | my $mi = 60; | ||
| 959 | my $hh = 60*$mi; | ||
| 960 | my $dd = 24*$hh; | ||
| 961 | if ($t =~ /^s/i) { | ||
| 962 | return (sprintf "%5.3fGB", $n/$gb) if $n>$gb; | ||
| 963 | return (sprintf "%5.3fMB", $n/$mb) if $n>$mb; | ||
| 964 | return (sprintf "%5.3fKB", $n/$kb) if $n>$kb; | ||
| 965 | return "$n Bytes"; | ||
| 966 | } else { | ||
| 967 | $s = abs($n); | ||
| 968 | if ($s>$dd) { | ||
| 969 | $r = sprintf "%5dD", $s/$dd; | ||
| 970 | $s = $s%$dd; | ||
| 971 | } | ||
| 972 | if ($s>$hh) { | ||
| 973 | $r .= sprintf "%02d:", $s/$hh; | ||
| 974 | $s = $s%$hh; | ||
| 975 | } | ||
| 976 | if ($s>$mi) { | ||
| 977 | $r .= sprintf "%02d:", $s/$mi; | ||
| 978 | $s = $s%$mi; | ||
| 979 | } | ||
| 980 | $r .= sprintf "%02d", $s; | ||
| 981 | $r = "-$r" if ($n<0); | ||
| 982 | } | ||
| 983 | return $r; | ||
| 984 | } | ||
| 985 | |||
| 986 | =head2 find_files($dir,$re) | ||
| 987 | |||
| 988 | Input variables: | ||
| 989 | |||
| 990 | $dir - directory name in which files and sub-dirs will be searched | ||
| 991 | $re - file name pattern to be matched. | ||
| 992 | |||
| 993 | Variables used or routines called: None. | ||
| 994 | |||
| 995 | How to use: | ||
| 996 | |||
| 997 | use File::Xcopy; | ||
| 998 | my $fc = File::Xcopu->new; | ||
| 999 | # find all the pdf files and stored in the array ref $ar | ||
| 1000 | my $ar = $fc->find_files('/my/src/dir', '\.pdf$'); | ||
| 1001 | |||
| 1002 | Return: $ar - array ref and can be accessed as ${$ar}[$i]{$itm}, | ||
| 1003 | where $i is sequence number, and $itm are | ||
| 1004 | |||
| 1005 | file - file name without dir | ||
| 1006 | pdir - parent dir for the file | ||
| 1007 | path - full path for the file | ||
| 1008 | |||
| 1009 | This method resursively finds all the matched files in the directory | ||
| 1010 | and its sub-directories. It uses C<finddepth> method from | ||
| 1011 | File::Find(1) module. | ||
| 1012 | |||
| 1013 | =cut | ||
| 1014 | |||
| 1015 | sub find_files { | ||
| 1016 | my $self = shift; | ||
| 1017 | my $cls = ref($self)||$self; | ||
| 1018 | my ($dir, $re) = @_; | ||
| 1019 | my $ar = bless [], $cls; | ||
| 1020 | my $sub = sub { | ||
| 1021 | (/$re/) | ||
| 1022 | && (push @{$ar}, {file=>$_, pdir=>$File::Find::dir, | ||
| 1023 | path=>$File::Find::name}); | ||
| 1024 | }; | ||
| 1025 | finddepth($sub, $dir); | ||
| 1026 | return $ar; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | =head2 list_files($dir,$re) | ||
| 1030 | |||
| 1031 | Input variables: | ||
| 1032 | |||
| 1033 | $dir - directory name in which files will be searched | ||
| 1034 | $re - file name pattern to be matched. | ||
| 1035 | |||
| 1036 | Variables used or routines called: None. | ||
| 1037 | |||
| 1038 | How to use: | ||
| 1039 | |||
| 1040 | use File::Xcopy; | ||
| 1041 | my $fc = File::Xcopu->new; | ||
| 1042 | # find all the pdf files and stored in the array ref $ar | ||
| 1043 | my $ar = $fc->list_files('/my/src/dir', '\.pdf$'); | ||
| 1044 | |||
| 1045 | Return: $ar - array ref and can be accessed as ${$ar}[$i]{$itm}, | ||
| 1046 | where $i is sequence number, and $itm are | ||
| 1047 | |||
| 1048 | file - file name without dir | ||
| 1049 | pdir - parent dir for the file | ||
| 1050 | path - full path for the file | ||
| 1051 | |||
| 1052 | This method only finds the matched files in the directory and will not | ||
| 1053 | search sub directories. It uses C<readdir> to get file names. | ||
| 1054 | |||
| 1055 | =cut | ||
| 1056 | |||
| 1057 | sub list_files { | ||
| 1058 | my $self = shift; | ||
| 1059 | my $cls = ref($self)||$self; | ||
| 1060 | my $ar = bless [], $cls; | ||
| 1061 | my ($dir, $re) = @_; | ||
| 1062 | opendir DD, $dir or croak "ERR: open dir - $dir: $!\n"; | ||
| 1063 | my @a = grep $re , readdir DD; | ||
| 1064 | closedir DD; | ||
| 1065 | foreach my $f (@a) { | ||
| 1066 | push @{$ar}, {file=>$f, pdir=>$dir, rdir=>$f, | ||
| 1067 | path=>"$dir/$f"}; | ||
| 1068 | } | ||
| 1069 | return $ar; | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | =head2 file_stat($dir,$ar) | ||
| 1073 | |||
| 1074 | Input variables: | ||
| 1075 | |||
| 1076 | $dir - directory name in which files will be searched | ||
| 1077 | $ar - array ref returned from C<find_files> or C<list_files> | ||
| 1078 | method. | ||
| 1079 | |||
| 1080 | Variables used or routines called: None. | ||
| 1081 | |||
| 1082 | How to use: | ||
| 1083 | |||
| 1084 | use File::Xcopy; | ||
| 1085 | my $fc = File::Xcopu->new; | ||
| 1086 | # find all the pdf files and stored in the array ref $ar | ||
| 1087 | my $ar = $fc->find_files('/my/src/dir', '\.pdf$'); | ||
| 1088 | my $br = $fc->file_stat('/my/src/dir', $ar); | ||
| 1089 | |||
| 1090 | Return: $br - hash array ref and can be accessed as ${$ar}{$k}{$itm}, | ||
| 1091 | where $k is C<rdir> and the $itm are | ||
| 1092 | |||
| 1093 | size - file size in bytes | ||
| 1094 | time - modification time in Perl time | ||
| 1095 | file - file name | ||
| 1096 | pdir - parent directory | ||
| 1097 | |||
| 1098 | |||
| 1099 | This method also adds the following elements additional to 'file', | ||
| 1100 | 'pdir', and 'path' in the $ar array: | ||
| 1101 | |||
| 1102 | prop - file stat array | ||
| 1103 | rdir - relative file name to the $dir | ||
| 1104 | |||
| 1105 | The following lists the elements in the stat array: | ||
| 1106 | |||
| 1107 | file stat array - ${$far}[$i]{prop}: | ||
| 1108 | 0 dev device number of filesystem | ||
| 1109 | 1 ino inode number | ||
| 1110 | 2 mode file mode (type and permissions) | ||
| 1111 | 3 nlink number of (hard) links to the file | ||
| 1112 | 4 uid numeric user ID of file's owner | ||
| 1113 | 5 gid numeric group ID of file's owner | ||
| 1114 | 6 rdev the device identifier (special files only) | ||
| 1115 | 7 size total size of file, in bytes | ||
| 1116 | 8 atime last access time in seconds since the epoch | ||
| 1117 | 9 mtime last modify time in seconds since the epoch | ||
| 1118 | 10 ctime inode change time (NOT creation time!) in seconds | ||
| 1119 | sinc e the epoch | ||
| 1120 | 11 blksize preferred block size for file system I/O | ||
| 1121 | 12 blocks actual number of blocks allocated | ||
| 1122 | |||
| 1123 | This method converts the array into a hash array and add additional | ||
| 1124 | elements to the input array as well. | ||
| 1125 | |||
| 1126 | =cut | ||
| 1127 | |||
| 1128 | sub file_stat { | ||
| 1129 | my $s = shift; | ||
| 1130 | my $c = ref($s)||$s; | ||
| 1131 | my ($dir, $ar) = @_; | ||
| 1132 | |||
| 1133 | my $br = bless {}, $c; | ||
| 1134 | my ($k, $fsz, $mtm); | ||
| 1135 | for my $i (0..$#{$ar}) { | ||
| 1136 | $k = ${$ar}[$i]{path}; | ||
| 1137 | ${$ar}[$i]{prop} = [stat $k]; | ||
| 1138 | $k =~ s{$dir}{\.}; | ||
| 1139 | ${$ar}[$i]{rdir} = $k; | ||
| 1140 | $fsz = ${$ar}[$i]{prop}[7]; | ||
| 1141 | $mtm = ${$ar}[$i]{prop}[9]; | ||
| 1142 | ${$br}{$k} = {file=>${$ar}[$i]{file}, size=>$fsz, time=>$mtm, | ||
| 1143 | pdir=>${$ar}[$i]{pdir}}; | ||
| 1144 | } | ||
| 1145 | return $br; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | |||
| 1149 | =head3 fmtTime($ptm, $otp) | ||
| 1150 | |||
| 1151 | Input variables: | ||
| 1152 | |||
| 1153 | $ptm - Perl time | ||
| 1154 | $otp - output type: default - YYYYMMDD.hhmmss | ||
| 1155 | 1 - YYYY/MM/DD hh:mm:ss | ||
| 1156 | 5 - MM/DD/YYYY hh:mm:ss | ||
| 1157 | 11 - Wed Mar 31 08:59:27 1999 | ||
| 1158 | |||
| 1159 | Variables used or routines called: None | ||
| 1160 | |||
| 1161 | How to use: | ||
| 1162 | |||
| 1163 | # return current time in YYYYMMDD.hhmmss | ||
| 1164 | my $t1 = $self->fmtTime; | ||
| 1165 | # return current time in YYYY/MM/DD hh:mm:ss | ||
| 1166 | my $t2 = $self->fmtTime(time,1); | ||
| 1167 | |||
| 1168 | Return: date and time in the format specified. | ||
| 1169 | |||
| 1170 | =cut | ||
| 1171 | |||
| 1172 | sub fmtTime { | ||
| 1173 | my $self = shift; | ||
| 1174 | my ($ptm,$otp) = @_; | ||
| 1175 | my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst,$r); | ||
| 1176 | # | ||
| 1177 | # Input variables: | ||
| 1178 | # $ptm - Perl time | ||
| 1179 | # $otp - output type: default - YYYYMMDD.hhmmss | ||
| 1180 | # 1 - YYYY/MM/DD hh:mm:ss | ||
| 1181 | # 5 - MM/DD/YYYY hh:mm:ss | ||
| 1182 | # 11 - Wed Mar 31 08:59:27 1999 | ||
| 1183 | # Local variables: | ||
| 1184 | # $sec - seconds (0~59) | ||
| 1185 | # $min - minutes (0~59) | ||
| 1186 | # $hour - hours (0~23) | ||
| 1187 | # $mday - day in month (1~31) | ||
| 1188 | # $mon - months (0~11) | ||
| 1189 | # $year - year in YY | ||
| 1190 | # $wday - day in a week (0~6: S M T W T F S) | ||
| 1191 | # $yday - day in a year (1~366) | ||
| 1192 | # $isdst - | ||
| 1193 | # Global variables used: None | ||
| 1194 | # Global variables modified: None | ||
| 1195 | # Calls-To: | ||
| 1196 | # &cvtYY2YYYY($year) | ||
| 1197 | # Return: a formated time. | ||
| 1198 | # Purpose: format perl time to readable time. | ||
| 1199 | # | ||
| 1200 | if (!$ptm) { $ptm = time } | ||
| 1201 | if (!$otp) { $otp = 0 } | ||
| 1202 | ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = | ||
| 1203 | localtime($ptm); | ||
| 1204 | $year = ($year<31) ? $year+2000 : $year+1900; | ||
| 1205 | if ($otp==1) { # output format: YYYY/MM/DD hh:mm:ss | ||
| 1206 | $r = sprintf "%04d/%02d/%02d %02d:%02d:%02d", $year, $mon+1, | ||
| 1207 | $mday, $hour, $min, $sec; | ||
| 1208 | } elsif ($otp==2) { # output format: YYYYMMDD_hhmmss | ||
| 1209 | $r = sprintf "%04d%02d%02d_%02d%02d%02d", $year, $mon+1, | ||
| 1210 | $mday, $hour, $min, $sec; | ||
| 1211 | } elsif ($otp==5) { # output format: MM/DD/YYYY hh:mm:ss | ||
| 1212 | $r = sprintf "%02d/%02d/%04d %02d:%02d:%02d", $mon+1, | ||
| 1213 | $mday, $year, $hour, $min, $sec; | ||
| 1214 | } elsif ($otp==11) { | ||
| 1215 | $r = scalar localtime($ptm); | ||
| 1216 | } else { # output format: YYYYMMDD.hhmmss | ||
| 1217 | $r = sprintf "%04d%02d%02d.%02d%02d%02d", $year, $mon+1, | ||
| 1218 | $mday, $hour, $min, $sec; | ||
| 1219 | } | ||
| 1220 | return $r; | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | |||
| 1224 | 1; | ||
| 1225 | |||
| 1226 | __END__ |