[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # util.pm -- various useful utilities
|
---|
| 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
[4] | 25 |
|
---|
| 26 | package util;
|
---|
| 27 |
|
---|
[14362] | 28 | BEGIN {
|
---|
| 29 | die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
|
---|
| 30 | die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
|
---|
| 31 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
|
---|
| 32 | unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
|
---|
| 33 | }
|
---|
[4] | 34 | use File::Copy;
|
---|
[619] | 35 | use File::Basename;
|
---|
[4] | 36 |
|
---|
| 37 | # removes files (but not directories)
|
---|
| 38 | sub rm {
|
---|
| 39 | my (@files) = @_;
|
---|
| 40 | my @filefiles = ();
|
---|
| 41 |
|
---|
| 42 | # make sure the files we want to delete exist
|
---|
| 43 | # and are regular files
|
---|
[10046] | 44 | foreach my $file (@files) {
|
---|
[4] | 45 | if (!-e $file) {
|
---|
| 46 | print STDERR "util::rm $file does not exist\n";
|
---|
[721] | 47 | } elsif ((!-f $file) && (!-l $file)) {
|
---|
| 48 | print STDERR "util::rm $file is not a regular (or symbolic) file\n";
|
---|
[4] | 49 | } else {
|
---|
| 50 | push (@filefiles, $file);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | # remove the files
|
---|
| 55 | my $numremoved = unlink @filefiles;
|
---|
| 56 |
|
---|
| 57 | # check to make sure all of them were removed
|
---|
| 58 | if ($numremoved != scalar(@filefiles)) {
|
---|
| 59 | print STDERR "util::rm Not all files were removed\n";
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 |
|
---|
[10211] | 64 |
|
---|
[4] | 65 | # recursive removal
|
---|
[10211] | 66 | sub filtered_rm_r {
|
---|
| 67 | my ($files,$file_accept_re,$file_reject_re) = @_;
|
---|
[4] | 68 |
|
---|
[10211] | 69 | my @files_array = (ref $files eq "ARRAY") ? @$files : ($files);
|
---|
| 70 |
|
---|
[4] | 71 | # recursively remove the files
|
---|
[10211] | 72 | foreach my $file (@files_array) {
|
---|
[4] | 73 | $file =~ s/[\/\\]+$//; # remove trailing slashes
|
---|
| 74 |
|
---|
| 75 | if (!-e $file) {
|
---|
[10211] | 76 | print STDERR "util::filtered_rm_r $file does not exist\n";
|
---|
[4] | 77 |
|
---|
[721] | 78 | } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
|
---|
[4] | 79 | # get the contents of this directory
|
---|
| 80 | if (!opendir (INDIR, $file)) {
|
---|
[10211] | 81 | print STDERR "util::filtered_rm_r could not open directory $file\n";
|
---|
[4] | 82 | } else {
|
---|
| 83 | my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
|
---|
| 84 | closedir (INDIR);
|
---|
[10211] | 85 |
|
---|
[4] | 86 | # remove all the files in this directory
|
---|
[10211] | 87 | map {$_="$file/$_";} @filedir;
|
---|
| 88 | &filtered_rm_r (\@filedir,$file_accept_re,$file_reject_re);
|
---|
[4] | 89 |
|
---|
[10211] | 90 | if (!defined $file_accept_re && !defined $file_reject_re) {
|
---|
| 91 | # remove this directory
|
---|
| 92 | if (!rmdir $file) {
|
---|
| 93 | print STDERR "util::filtered_rm_r couldn't remove directory $file\n";
|
---|
| 94 | }
|
---|
[4] | 95 | }
|
---|
| 96 | }
|
---|
[10211] | 97 | } else {
|
---|
| 98 | next if (defined $file_reject_re && ($file =~ m/$file_reject_re/));
|
---|
[4] | 99 |
|
---|
[10211] | 100 | if ((!defined $file_accept_re) || ($file =~ m/$file_accept_re/)) {
|
---|
| 101 | # remove this file
|
---|
| 102 | &rm ($file);
|
---|
| 103 | }
|
---|
[4] | 104 | }
|
---|
| 105 | }
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[10211] | 108 |
|
---|
| 109 | # recursive removal
|
---|
| 110 | sub rm_r {
|
---|
| 111 | my (@files) = @_;
|
---|
| 112 |
|
---|
| 113 | # use the more general (but reterospectively written function
|
---|
| 114 | # filtered_rm_r function()
|
---|
| 115 |
|
---|
| 116 | filtered_rm_r(\@files,undef,undef); # no accept or reject expressions
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 |
|
---|
[721] | 122 | # moves a file or a group of files
|
---|
| 123 | sub mv {
|
---|
| 124 | my $dest = pop (@_);
|
---|
| 125 | my (@srcfiles) = @_;
|
---|
[4] | 126 |
|
---|
[721] | 127 | # remove trailing slashes from source and destination files
|
---|
| 128 | $dest =~ s/[\\\/]+$//;
|
---|
| 129 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
| 130 |
|
---|
| 131 | # a few sanity checks
|
---|
| 132 | if (scalar (@srcfiles) == 0) {
|
---|
| 133 | print STDERR "util::mv no destination directory given\n";
|
---|
| 134 | return;
|
---|
| 135 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
| 136 | print STDERR "util::mv if multiple source files are given the ".
|
---|
| 137 | "destination must be a directory\n";
|
---|
| 138 | return;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | # move the files
|
---|
[8716] | 142 | foreach my $file (@srcfiles) {
|
---|
[721] | 143 | my $tempdest = $dest;
|
---|
| 144 | if (-d $tempdest) {
|
---|
| 145 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
| 146 | $tempdest .= "/$filename";
|
---|
| 147 | }
|
---|
| 148 | if (!-e $file) {
|
---|
| 149 | print STDERR "util::mv $file does not exist\n";
|
---|
| 150 | } else {
|
---|
| 151 | rename ($file, $tempdest);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 |
|
---|
[4] | 157 | # copies a file or a group of files
|
---|
| 158 | sub cp {
|
---|
| 159 | my $dest = pop (@_);
|
---|
| 160 | my (@srcfiles) = @_;
|
---|
| 161 |
|
---|
| 162 | # remove trailing slashes from source and destination files
|
---|
| 163 | $dest =~ s/[\\\/]+$//;
|
---|
| 164 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
| 165 |
|
---|
| 166 | # a few sanity checks
|
---|
| 167 | if (scalar (@srcfiles) == 0) {
|
---|
| 168 | print STDERR "util::cp no destination directory given\n";
|
---|
| 169 | return;
|
---|
| 170 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
| 171 | print STDERR "util::cp if multiple source files are given the ".
|
---|
| 172 | "destination must be a directory\n";
|
---|
| 173 | return;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | # copy the files
|
---|
[8716] | 177 | foreach my $file (@srcfiles) {
|
---|
[4] | 178 | my $tempdest = $dest;
|
---|
| 179 | if (-d $tempdest) {
|
---|
| 180 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
| 181 | $tempdest .= "/$filename";
|
---|
| 182 | }
|
---|
| 183 | if (!-e $file) {
|
---|
| 184 | print STDERR "util::cp $file does not exist\n";
|
---|
| 185 | } elsif (!-f $file) {
|
---|
| 186 | print STDERR "util::cp $file is not a plain file\n";
|
---|
| 187 | } else {
|
---|
| 188 | &File::Copy::copy ($file, $tempdest);
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 |
|
---|
[721] | 194 |
|
---|
[4] | 195 | # recursively copies a file or group of files
|
---|
[1454] | 196 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
| 197 | # destination must be a directory - to copy one file to
|
---|
| 198 | # another use cp instead
|
---|
[4] | 199 | sub cp_r {
|
---|
| 200 | my $dest = pop (@_);
|
---|
| 201 | my (@srcfiles) = @_;
|
---|
| 202 |
|
---|
| 203 | # a few sanity checks
|
---|
| 204 | if (scalar (@srcfiles) == 0) {
|
---|
[1454] | 205 | print STDERR "util::cp_r no destination directory given\n";
|
---|
[4] | 206 | return;
|
---|
[1454] | 207 | } elsif (-f $dest) {
|
---|
| 208 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
[4] | 209 | return;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[1454] | 212 | # create destination directory if it doesn't exist already
|
---|
| 213 | if (! -d $dest) {
|
---|
| 214 | my $store_umask = umask(0002);
|
---|
| 215 | mkdir ($dest, 0777);
|
---|
| 216 | umask($store_umask);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
[4] | 219 | # copy the files
|
---|
[8716] | 220 | foreach my $file (@srcfiles) {
|
---|
[4] | 221 |
|
---|
| 222 | if (!-e $file) {
|
---|
[1454] | 223 | print STDERR "util::cp_r $file does not exist\n";
|
---|
[4] | 224 |
|
---|
| 225 | } elsif (-d $file) {
|
---|
[1586] | 226 | # make the new directory
|
---|
| 227 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
| 228 | $dest = &util::filename_cat ($dest, $filename);
|
---|
| 229 | my $store_umask = umask(0002);
|
---|
| 230 | mkdir ($dest, 0777);
|
---|
| 231 | umask($store_umask);
|
---|
[836] | 232 |
|
---|
[4] | 233 | # get the contents of this directory
|
---|
| 234 | if (!opendir (INDIR, $file)) {
|
---|
| 235 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
| 236 | } else {
|
---|
[1454] | 237 | my @filedir = readdir (INDIR);
|
---|
[4] | 238 | closedir (INDIR);
|
---|
[8716] | 239 | foreach my $f (@filedir) {
|
---|
[1454] | 240 | next if $f =~ /^\.\.?$/;
|
---|
| 241 | # copy all the files in this directory
|
---|
| 242 | my $ff = &util::filename_cat ($file, $f);
|
---|
| 243 | &cp_r ($ff, $dest);
|
---|
| 244 | }
|
---|
[4] | 245 | }
|
---|
| 246 |
|
---|
| 247 | } else {
|
---|
[1454] | 248 | &cp($file, $dest);
|
---|
[4] | 249 | }
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 |
|
---|
[11179] | 253 | # copies a directory and its contents, excluding subdirectories, into a new directory
|
---|
| 254 | sub cp_r_toplevel {
|
---|
| 255 | my $dest = pop (@_);
|
---|
| 256 | my (@srcfiles) = @_;
|
---|
[4] | 257 |
|
---|
[11179] | 258 | # a few sanity checks
|
---|
| 259 | if (scalar (@srcfiles) == 0) {
|
---|
| 260 | print STDERR "util::cp_r no destination directory given\n";
|
---|
| 261 | return;
|
---|
| 262 | } elsif (-f $dest) {
|
---|
| 263 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
| 264 | return;
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | # create destination directory if it doesn't exist already
|
---|
| 268 | if (! -d $dest) {
|
---|
| 269 | my $store_umask = umask(0002);
|
---|
| 270 | mkdir ($dest, 0777);
|
---|
| 271 | umask($store_umask);
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | # copy the files
|
---|
| 275 | foreach my $file (@srcfiles) {
|
---|
| 276 |
|
---|
| 277 | if (!-e $file) {
|
---|
| 278 | print STDERR "util::cp_r $file does not exist\n";
|
---|
| 279 |
|
---|
| 280 | } elsif (-d $file) {
|
---|
| 281 | # make the new directory
|
---|
| 282 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
| 283 | $dest = &util::filename_cat ($dest, $filename);
|
---|
| 284 | my $store_umask = umask(0002);
|
---|
| 285 | mkdir ($dest, 0777);
|
---|
| 286 | umask($store_umask);
|
---|
| 287 |
|
---|
| 288 | # get the contents of this directory
|
---|
| 289 | if (!opendir (INDIR, $file)) {
|
---|
| 290 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
| 291 | } else {
|
---|
| 292 | my @filedir = readdir (INDIR);
|
---|
| 293 | closedir (INDIR);
|
---|
| 294 | foreach my $f (@filedir) {
|
---|
| 295 | next if $f =~ /^\.\.?$/;
|
---|
| 296 |
|
---|
| 297 | # copy all the files in this directory, but not directories
|
---|
| 298 | my $ff = &util::filename_cat ($file, $f);
|
---|
| 299 | if (-f $ff) {
|
---|
| 300 | &cp($ff, $dest);
|
---|
| 301 | #&cp_r ($ff, $dest);
|
---|
| 302 | }
|
---|
| 303 | }
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | } else {
|
---|
| 307 | &cp($file, $dest);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[721] | 312 | sub mk_dir {
|
---|
| 313 | my ($dir) = @_;
|
---|
| 314 |
|
---|
[836] | 315 | my $store_umask = umask(0002);
|
---|
| 316 | my $mkdir_ok = mkdir ($dir, 0777);
|
---|
| 317 | umask($store_umask);
|
---|
| 318 |
|
---|
| 319 | if (!$mkdir_ok)
|
---|
| 320 | {
|
---|
[721] | 321 | print STDERR "util::mk_dir could not create directory $dir\n";
|
---|
| 322 | return;
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
| 325 |
|
---|
[1046] | 326 | # in case anyone cares - I did some testing (using perls Benchmark module)
|
---|
| 327 | # on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
|
---|
| 328 | # slightly faster (surprisingly) - Stefan.
|
---|
[4] | 329 | sub mk_all_dir {
|
---|
| 330 | my ($dir) = @_;
|
---|
| 331 |
|
---|
| 332 | # use / for the directory separator, remove duplicate and
|
---|
| 333 | # trailing slashes
|
---|
| 334 | $dir=~s/[\\\/]+/\//g;
|
---|
| 335 | $dir=~s/[\\\/]+$//;
|
---|
| 336 |
|
---|
| 337 | # make sure the cache directory exists
|
---|
| 338 | my $dirsofar = "";
|
---|
| 339 | my $first = 1;
|
---|
[8716] | 340 | foreach my $dirname (split ("/", $dir)) {
|
---|
[4] | 341 | $dirsofar .= "/" unless $first;
|
---|
| 342 | $first = 0;
|
---|
| 343 |
|
---|
| 344 | $dirsofar .= $dirname;
|
---|
| 345 |
|
---|
| 346 | next if $dirname =~ /^(|[a-z]:)$/i;
|
---|
[836] | 347 | if (!-e $dirsofar)
|
---|
| 348 | {
|
---|
| 349 | my $store_umask = umask(0002);
|
---|
| 350 | my $mkdir_ok = mkdir ($dirsofar, 0777);
|
---|
| 351 | umask($store_umask);
|
---|
| 352 | if (!$mkdir_ok)
|
---|
| 353 | {
|
---|
| 354 | print STDERR "util::mk_all_dir could not create directory $dirsofar\n";
|
---|
| 355 | return;
|
---|
| 356 | }
|
---|
| 357 | }
|
---|
[4] | 358 | }
|
---|
| 359 | }
|
---|
| 360 |
|
---|
[619] | 361 | # make hard link to file if supported by OS, otherwise copy the file
|
---|
| 362 | sub hard_link {
|
---|
[983] | 363 | my ($src, $dest) = @_;
|
---|
[4] | 364 |
|
---|
[619] | 365 | # remove trailing slashes from source and destination files
|
---|
| 366 | $src =~ s/[\\\/]+$//;
|
---|
| 367 | $dest =~ s/[\\\/]+$//;
|
---|
| 368 |
|
---|
| 369 | # a few sanity checks
|
---|
[812] | 370 | if (-e $dest) {
|
---|
| 371 | # destination file already exists
|
---|
| 372 | return;
|
---|
| 373 | }
|
---|
| 374 | elsif (!-e $src) {
|
---|
[619] | 375 | print STDERR "util::hard_link source file $src does not exist\n";
|
---|
[3628] | 376 | return 1;
|
---|
[619] | 377 | }
|
---|
| 378 | elsif (-d $src) {
|
---|
| 379 | print STDERR "util::hard_link source $src is a directory\n";
|
---|
[3628] | 380 | return 1;
|
---|
[619] | 381 | }
|
---|
| 382 |
|
---|
| 383 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
| 384 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
| 385 |
|
---|
[5494] | 386 | # link not supported on windows 9x
|
---|
| 387 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && (Win32::FsType() !~ /^ntfs$/i)) {
|
---|
[14362] | 388 | print STDERR "util::hard_link: win32: using copy for hard link.\n";
|
---|
| 389 | &File::Copy::copy ($src, $dest);
|
---|
| 390 | } else {
|
---|
| 391 | require Win32::Hardlink;
|
---|
| 392 | if (!link($src, $dest)) {
|
---|
| 393 | print STDERR "util::hard_link: unable to create hard link. ";
|
---|
| 394 | print STDERR " Attempting to copy file: $src -> $dest\n";
|
---|
| 395 | &File::Copy::copy ($src, $dest);
|
---|
| 396 | }
|
---|
[619] | 397 | }
|
---|
[3628] | 398 | return 0;
|
---|
[619] | 399 | }
|
---|
| 400 |
|
---|
[2193] | 401 | # make soft link to file if supported by OS, otherwise copy file
|
---|
[721] | 402 | sub soft_link {
|
---|
[983] | 403 | my ($src, $dest) = @_;
|
---|
[619] | 404 |
|
---|
[721] | 405 | # remove trailing slashes from source and destination files
|
---|
| 406 | $src =~ s/[\\\/]+$//;
|
---|
| 407 | $dest =~ s/[\\\/]+$//;
|
---|
[619] | 408 |
|
---|
[721] | 409 | # a few sanity checks
|
---|
| 410 | if (!-e $src) {
|
---|
| 411 | print STDERR "util::soft_link source file $src does not exist\n";
|
---|
| 412 | return 0;
|
---|
| 413 | }
|
---|
[619] | 414 |
|
---|
[721] | 415 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
| 416 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
[14221] | 417 |
|
---|
| 418 | # symlink not supported on windows
|
---|
[2193] | 419 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
[14221] | 420 | if ( (Win32::FsType() =~ /^ntfs$/i) &&
|
---|
| 421 | ($src =~ /^.:\\.*/) && ($dest =~ /^.:\\.*/) &&
|
---|
| 422 | (substr($src,0,3) eq substr($dest,0,3)) ) {
|
---|
| 423 | # if filesystem is NTFS and both source and destination is on the same local drive,
|
---|
| 424 | # use hardlink instead of symlink
|
---|
[14362] | 425 | #print STDERR "util::soft_link: win32: using hard link instead of soft ink.\n";
|
---|
| 426 | hard_link($src,$dest);
|
---|
[14221] | 427 | } else {
|
---|
| 428 | #print STDERR "util::soft_link: win32: using copy for soft link.\n ";
|
---|
| 429 | &File::Copy::copy ($src, $dest);
|
---|
| 430 | }
|
---|
[2193] | 431 |
|
---|
| 432 | } elsif (!eval {symlink($src, $dest)}) {
|
---|
[2974] | 433 | print STDERR "util::soft_link: unable to create soft link.\n";
|
---|
[721] | 434 | return 0;
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | return 1;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 |
|
---|
| 441 |
|
---|
| 442 |
|
---|
[4] | 443 | # updates a copy of a directory in some other part of the filesystem
|
---|
| 444 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
| 445 | # both $fromdir and $todir should be absolute paths
|
---|
| 446 | sub cachedir {
|
---|
| 447 | my ($fromdir, $todir, $verbosity) = @_;
|
---|
| 448 | $verbosity = 1 unless defined $verbosity;
|
---|
| 449 |
|
---|
| 450 | # use / for the directory separator, remove duplicate and
|
---|
| 451 | # trailing slashes
|
---|
| 452 | $fromdir=~s/[\\\/]+/\//g;
|
---|
| 453 | $fromdir=~s/[\\\/]+$//;
|
---|
| 454 | $todir=~s/[\\\/]+/\//g;
|
---|
| 455 | $todir=~s/[\\\/]+$//;
|
---|
| 456 |
|
---|
| 457 | &mk_all_dir ($todir);
|
---|
| 458 |
|
---|
| 459 | # get the directories in ascending order
|
---|
| 460 | if (!opendir (FROMDIR, $fromdir)) {
|
---|
| 461 | print STDERR "util::cachedir could not read directory $fromdir\n";
|
---|
| 462 | return;
|
---|
| 463 | }
|
---|
| 464 | my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
|
---|
| 465 | closedir (FROMDIR);
|
---|
| 466 |
|
---|
| 467 | if (!opendir (TODIR, $todir)) {
|
---|
| 468 | print STDERR "util::cacedir could not read directory $todir\n";
|
---|
| 469 | return;
|
---|
| 470 | }
|
---|
| 471 | my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
|
---|
| 472 | closedir (TODIR);
|
---|
| 473 |
|
---|
| 474 | my $fromi = 0;
|
---|
| 475 | my $toi = 0;
|
---|
| 476 |
|
---|
| 477 | while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
|
---|
| 478 | # print "fromi: $fromi toi: $toi\n";
|
---|
| 479 |
|
---|
| 480 | # see if we should delete a file/directory
|
---|
| 481 | # this should happen if the file/directory
|
---|
| 482 | # is not in the from list or if its a different
|
---|
| 483 | # size, or has an older timestamp
|
---|
| 484 | if ($toi < scalar(@todir)) {
|
---|
| 485 | if (($fromi >= scalar(@fromdir)) ||
|
---|
| 486 | ($todir[$toi] lt $fromdir[$fromi] ||
|
---|
| 487 | ($todir[$toi] eq $fromdir[$fromi] &&
|
---|
| 488 | &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
|
---|
| 489 | $verbosity)))) {
|
---|
| 490 |
|
---|
| 491 | # the files are different
|
---|
| 492 | &rm_r("$todir/$todir[$toi]");
|
---|
| 493 | splice(@todir, $toi, 1); # $toi stays the same
|
---|
| 494 |
|
---|
| 495 | } elsif ($todir[$toi] eq $fromdir[$fromi]) {
|
---|
| 496 | # the files are the same
|
---|
| 497 | # if it is a directory, check its contents
|
---|
| 498 | if (-d "$todir/$todir[$toi]") {
|
---|
| 499 | &cachedir ("$fromdir/$fromdir[$fromi]",
|
---|
| 500 | "$todir/$todir[$toi]", $verbosity);
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | $toi++;
|
---|
| 504 | $fromi++;
|
---|
| 505 | next;
|
---|
| 506 | }
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | # see if we should insert a file/directory
|
---|
| 510 | # we should insert a file/directory if there
|
---|
| 511 | # is no tofiles left or if the tofile does not exist
|
---|
| 512 | if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
|
---|
| 513 | $todir[$toi] gt $fromdir[$fromi])) {
|
---|
| 514 | &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
|
---|
| 515 | splice (@todir, $toi, 0, $fromdir[$fromi]);
|
---|
| 516 |
|
---|
| 517 | $toi++;
|
---|
| 518 | $fromi++;
|
---|
| 519 | }
|
---|
| 520 | }
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | # this function returns -1 if either file is not found
|
---|
| 524 | # assumes that $file1 and $file2 are absolute file names or
|
---|
| 525 | # in the current directory
|
---|
| 526 | # $file2 is allowed to be newer than $file1
|
---|
| 527 | sub differentfiles {
|
---|
| 528 | my ($file1, $file2, $verbosity) = @_;
|
---|
| 529 | $verbosity = 1 unless defined $verbosity;
|
---|
| 530 |
|
---|
| 531 | $file1 =~ s/\/+$//;
|
---|
| 532 | $file2 =~ s/\/+$//;
|
---|
| 533 |
|
---|
| 534 | my ($file1name) = $file1 =~ /\/([^\/]*)$/;
|
---|
| 535 | my ($file2name) = $file2 =~ /\/([^\/]*)$/;
|
---|
| 536 |
|
---|
| 537 | return -1 unless (-e $file1 && -e $file2);
|
---|
| 538 | if ($file1name ne $file2name) {
|
---|
| 539 | print STDERR "filenames are not the same\n" if ($verbosity >= 2);
|
---|
| 540 | return 1;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[8716] | 543 | my @file1stat = stat ($file1);
|
---|
| 544 | my @file2stat = stat ($file2);
|
---|
[4] | 545 |
|
---|
| 546 | if (-d $file1) {
|
---|
| 547 | if (! -d $file2) {
|
---|
| 548 | print STDERR "one file is a directory\n" if ($verbosity >= 2);
|
---|
| 549 | return 1;
|
---|
| 550 | }
|
---|
| 551 | return 0;
|
---|
| 552 | }
|
---|
| 553 |
|
---|
| 554 | # both must be regular files
|
---|
| 555 | unless (-f $file1 && -f $file2) {
|
---|
| 556 | print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
|
---|
| 557 | return 1;
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | # the size of the files must be the same
|
---|
| 561 | if ($file1stat[7] != $file2stat[7]) {
|
---|
| 562 | print STDERR "different sized files\n" if ($verbosity >= 2);
|
---|
| 563 | return 1;
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | # the second file cannot be older than the first
|
---|
| 567 | if ($file1stat[9] > $file2stat[9]) {
|
---|
| 568 | print STDERR "file is older\n" if ($verbosity >= 2);
|
---|
| 569 | return 1;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | return 0;
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 |
|
---|
| 576 | sub get_tmp_filename {
|
---|
[2795] | 577 | my $tmpdir = filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
[4] | 578 | &mk_all_dir ($tmpdir) unless -e $tmpdir;
|
---|
| 579 |
|
---|
| 580 | my $count = 1000;
|
---|
| 581 | my $rand = int(rand $count);
|
---|
[2795] | 582 | while (-e &filename_cat($tmpdir, "F$rand")) {
|
---|
[4] | 583 | $rand = int(rand $count);
|
---|
| 584 | $count++;
|
---|
| 585 | }
|
---|
| 586 |
|
---|
[2795] | 587 | return filename_cat($tmpdir, "F$rand");
|
---|
[4] | 588 | }
|
---|
| 589 |
|
---|
| 590 |
|
---|
| 591 | sub filename_cat {
|
---|
[7507] | 592 | my $first_file = shift(@_);
|
---|
[4] | 593 | my (@filenames) = @_;
|
---|
[10146] | 594 |
|
---|
| 595 | # Check for empty first filename
|
---|
| 596 | if ($first_file =~ /\S/) {
|
---|
[7507] | 597 | unshift(@filenames, $first_file);
|
---|
| 598 | }
|
---|
| 599 |
|
---|
[4] | 600 | my $filename = join("/", @filenames);
|
---|
| 601 |
|
---|
| 602 | # remove duplicate slashes and remove the last slash
|
---|
[488] | 603 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 604 | $filename =~ s/[\\\/]+/\\/g;
|
---|
| 605 | } else {
|
---|
[836] | 606 | $filename =~ s/[\/]+/\//g;
|
---|
| 607 | # DB: want a filename abc\de.html to remain like this
|
---|
[488] | 608 | }
|
---|
| 609 | $filename =~ s/[\\\/]$//;
|
---|
[4] | 610 |
|
---|
| 611 | return $filename;
|
---|
| 612 | }
|
---|
| 613 |
|
---|
[8682] | 614 |
|
---|
[10212] | 615 | sub envvar_prepend {
|
---|
| 616 | my ($var,$val) = @_;
|
---|
| 617 |
|
---|
| 618 | my $current_val = $ENV{$var};
|
---|
| 619 |
|
---|
| 620 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 621 | $ENV{$var} .= "$val;$current_val";
|
---|
| 622 | }
|
---|
| 623 | else {
|
---|
| 624 | $ENV{$var} .= "$val:$current_val";
|
---|
| 625 | }
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | sub envvar_append {
|
---|
| 629 | my ($var,$val) = @_;
|
---|
| 630 |
|
---|
| 631 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 632 | $ENV{$var} .= ";$val";
|
---|
| 633 | }
|
---|
| 634 | else {
|
---|
| 635 | $ENV{$var} .= ":$val";
|
---|
| 636 | }
|
---|
| 637 | }
|
---|
| 638 |
|
---|
| 639 |
|
---|
[8682] | 640 | # returns the path of a file without the filename -- ie. the directory the file is in
|
---|
| 641 | sub filename_head {
|
---|
| 642 | my $filename = shift(@_);
|
---|
| 643 |
|
---|
| 644 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 645 | $filename =~ s/[^\\\\]*$//;
|
---|
| 646 | }
|
---|
| 647 | else {
|
---|
| 648 | $filename =~ s/[^\\\/]*$//;
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | return $filename;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 |
|
---|
[1454] | 655 | # returns 1 if filename1 and filename2 point to the same
|
---|
| 656 | # file or directory
|
---|
| 657 | sub filenames_equal {
|
---|
| 658 | my ($filename1, $filename2) = @_;
|
---|
| 659 |
|
---|
| 660 | # use filename_cat to clean up trailing slashes and
|
---|
| 661 | # multiple slashes
|
---|
| 662 | $filename1 = filename_cat ($filename1);
|
---|
[2516] | 663 | $filename2 = filename_cat ($filename2);
|
---|
[1454] | 664 |
|
---|
| 665 | # filenames not case sensitive on windows
|
---|
| 666 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 667 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
| 668 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
| 669 | }
|
---|
| 670 | return 1 if $filename1 eq $filename2;
|
---|
| 671 | return 0;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
[10281] | 674 | sub filename_within_collection
|
---|
| 675 | {
|
---|
| 676 | my ($filename) = @_;
|
---|
| 677 |
|
---|
| 678 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 679 |
|
---|
| 680 | if (defined $collect_dir) {
|
---|
| 681 | my $dirsep = &util::get_dirsep();
|
---|
| 682 | if ($collect_dir !~ m/$dirsep$/) {
|
---|
| 683 | $collect_dir .= $dirsep;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 | $collect_dir =~ s/\\/\\\\/g; # escape DOS style file separator
|
---|
| 687 |
|
---|
| 688 | if ($filename =~ /^$collect_dir(.*)$/) {
|
---|
| 689 | $filename = $1;
|
---|
| 690 | }
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | return $filename;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 |
|
---|
[7929] | 697 | sub get_dirsep {
|
---|
| 698 |
|
---|
| 699 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 700 | return "\\";
|
---|
| 701 | } else {
|
---|
| 702 | return "\/";
|
---|
| 703 | }
|
---|
| 704 | }
|
---|
| 705 |
|
---|
[619] | 706 | sub get_os_dirsep {
|
---|
[4] | 707 |
|
---|
[619] | 708 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 709 | return "\\\\";
|
---|
| 710 | } else {
|
---|
| 711 | return "\\\/";
|
---|
| 712 | }
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | sub get_re_dirsep {
|
---|
| 716 |
|
---|
| 717 | return "\\\\|\\\/";
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 |
|
---|
[4] | 721 | # if this is running on windows we want binaries to end in
|
---|
| 722 | # .exe, otherwise they don't have to end in any extension
|
---|
| 723 | sub get_os_exe {
|
---|
| 724 | return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
| 725 | return "";
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 |
|
---|
[86] | 729 | # test to see whether this is a big or little endian machine
|
---|
| 730 | sub is_little_endian {
|
---|
[14175] | 731 | # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
|
---|
| 732 | # What we do here is, if it is a Macintosh machine (i.e. the Darwin operating system), regardless it is running on the IBM power-pc cpu or it is the x86 Intel-based chip with a power-pc emulator running on top of it, it requires the big-endian data format in the gdbm database file, we make the file extension .bdb; otherwise it's .ldb extension.
|
---|
| 733 |
|
---|
| 734 | #return 0 if $^O =~ /^darwin$/i;
|
---|
| 735 | return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
|
---|
| 736 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
[86] | 737 | }
|
---|
[4] | 738 |
|
---|
[86] | 739 |
|
---|
[135] | 740 | # will return the collection name if successful, "" otherwise
|
---|
| 741 | sub use_collection {
|
---|
[1454] | 742 | my ($collection, $collectdir) = @_;
|
---|
[135] | 743 |
|
---|
[1454] | 744 | if (!defined $collectdir || $collectdir eq "") {
|
---|
| 745 | $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
| 746 | }
|
---|
| 747 |
|
---|
[135] | 748 | # get and check the collection
|
---|
| 749 | if (!defined($collection) || $collection eq "") {
|
---|
| 750 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
| 751 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
| 752 | } else {
|
---|
[2359] | 753 | print STDOUT "No collection specified\n";
|
---|
[135] | 754 | return "";
|
---|
| 755 | }
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | if ($collection eq "modelcol") {
|
---|
[2359] | 759 | print STDOUT "You can't use modelcol.\n";
|
---|
[135] | 760 | return "";
|
---|
| 761 | }
|
---|
| 762 |
|
---|
| 763 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
| 764 | # are defined
|
---|
| 765 | $ENV{'GSDLCOLLECTION'} = $collection unless defined $ENV{'GSDLCOLLECTION'};
|
---|
[1454] | 766 | $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
|
---|
[135] | 767 |
|
---|
| 768 | # make sure this collection exists
|
---|
| 769 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
[2359] | 770 | print STDOUT "Invalid collection ($collection).\n";
|
---|
[135] | 771 | return "";
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | # everything is ready to go
|
---|
| 775 | return $collection;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
[9955] | 778 | sub hyperlink_text
|
---|
| 779 | {
|
---|
| 780 | my ($text) = @_;
|
---|
| 781 |
|
---|
| 782 | $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
|
---|
| 783 | $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
|
---|
| 784 |
|
---|
| 785 | return $text;
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 |
|
---|
[4] | 789 | 1;
|
---|