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