[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 |
|
---|
[23362] | 28 | use strict;
|
---|
| 29 |
|
---|
| 30 | use Encode;
|
---|
[4] | 31 | use File::Copy;
|
---|
[619] | 32 | use File::Basename;
|
---|
[24362] | 33 | # Config for getting the perlpath in the recommended way, though it uses paths that are
|
---|
| 34 | # hard-coded into the Config file that's generated upon configuring and compiling perl.
|
---|
| 35 | # $^X works better in some cases to return the path to perl used to launch the script,
|
---|
| 36 | # but if launched with plain "perl" (no full-path), that will be just what it returns.
|
---|
| 37 | use Config;
|
---|
[4] | 38 |
|
---|
| 39 | # removes files (but not directories)
|
---|
| 40 | sub rm {
|
---|
| 41 | my (@files) = @_;
|
---|
[18469] | 42 |
|
---|
[4] | 43 | my @filefiles = ();
|
---|
| 44 |
|
---|
| 45 | # make sure the files we want to delete exist
|
---|
| 46 | # and are regular files
|
---|
[10046] | 47 | foreach my $file (@files) {
|
---|
[4] | 48 | if (!-e $file) {
|
---|
| 49 | print STDERR "util::rm $file does not exist\n";
|
---|
[721] | 50 | } elsif ((!-f $file) && (!-l $file)) {
|
---|
| 51 | print STDERR "util::rm $file is not a regular (or symbolic) file\n";
|
---|
[4] | 52 | } else {
|
---|
| 53 | push (@filefiles, $file);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | # remove the files
|
---|
| 58 | my $numremoved = unlink @filefiles;
|
---|
| 59 |
|
---|
| 60 | # check to make sure all of them were removed
|
---|
| 61 | if ($numremoved != scalar(@filefiles)) {
|
---|
| 62 | print STDERR "util::rm Not all files were removed\n";
|
---|
| 63 | }
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[23249] | 66 | # removes files (but not directories) - can rename this to the default
|
---|
| 67 | # "rm" subroutine when debugging the deletion of individual files.
|
---|
| 68 | sub rm_debug {
|
---|
| 69 | my (@files) = @_;
|
---|
| 70 | my @filefiles = ();
|
---|
[4] | 71 |
|
---|
[23249] | 72 | # make sure the files we want to delete exist
|
---|
| 73 | # and are regular files
|
---|
| 74 | foreach my $file (@files) {
|
---|
| 75 | if (!-e $file) {
|
---|
| 76 | print STDERR "util::rm $file does not exist\n";
|
---|
| 77 | } elsif ((!-f $file) && (!-l $file)) {
|
---|
| 78 | print STDERR "util::rm $file is not a regular (or symbolic) file\n";
|
---|
| 79 | } else { # debug message
|
---|
| 80 | unlink($file) or warn "Could not delete file $file: $!\n";
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[10211] | 84 |
|
---|
[23249] | 85 |
|
---|
[4] | 86 | # recursive removal
|
---|
[10211] | 87 | sub filtered_rm_r {
|
---|
| 88 | my ($files,$file_accept_re,$file_reject_re) = @_;
|
---|
[4] | 89 |
|
---|
[24291] | 90 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(2);
|
---|
| 91 | # my ($lcfilename) = ($cfilename =~ m/([^\\\/]*)$/);
|
---|
| 92 | # print STDERR "** Calling method (2): $lcfilename:$cline $cpackage->$csubr\n";
|
---|
| 93 |
|
---|
[10211] | 94 | my @files_array = (ref $files eq "ARRAY") ? @$files : ($files);
|
---|
| 95 |
|
---|
[4] | 96 | # recursively remove the files
|
---|
[10211] | 97 | foreach my $file (@files_array) {
|
---|
[4] | 98 | $file =~ s/[\/\\]+$//; # remove trailing slashes
|
---|
| 99 |
|
---|
| 100 | if (!-e $file) {
|
---|
[10211] | 101 | print STDERR "util::filtered_rm_r $file does not exist\n";
|
---|
[4] | 102 |
|
---|
[721] | 103 | } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
|
---|
[4] | 104 | # get the contents of this directory
|
---|
| 105 | if (!opendir (INDIR, $file)) {
|
---|
[10211] | 106 | print STDERR "util::filtered_rm_r could not open directory $file\n";
|
---|
[4] | 107 | } else {
|
---|
| 108 | my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
|
---|
| 109 | closedir (INDIR);
|
---|
[10211] | 110 |
|
---|
[4] | 111 | # remove all the files in this directory
|
---|
[10211] | 112 | map {$_="$file/$_";} @filedir;
|
---|
| 113 | &filtered_rm_r (\@filedir,$file_accept_re,$file_reject_re);
|
---|
[4] | 114 |
|
---|
[10211] | 115 | if (!defined $file_accept_re && !defined $file_reject_re) {
|
---|
| 116 | # remove this directory
|
---|
| 117 | if (!rmdir $file) {
|
---|
| 118 | print STDERR "util::filtered_rm_r couldn't remove directory $file\n";
|
---|
| 119 | }
|
---|
[4] | 120 | }
|
---|
| 121 | }
|
---|
[10211] | 122 | } else {
|
---|
| 123 | next if (defined $file_reject_re && ($file =~ m/$file_reject_re/));
|
---|
[4] | 124 |
|
---|
[10211] | 125 | if ((!defined $file_accept_re) || ($file =~ m/$file_accept_re/)) {
|
---|
| 126 | # remove this file
|
---|
| 127 | &rm ($file);
|
---|
| 128 | }
|
---|
[4] | 129 | }
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[10211] | 133 |
|
---|
| 134 | # recursive removal
|
---|
| 135 | sub rm_r {
|
---|
| 136 | my (@files) = @_;
|
---|
| 137 |
|
---|
| 138 | # use the more general (but reterospectively written function
|
---|
| 139 | # filtered_rm_r function()
|
---|
| 140 |
|
---|
| 141 | filtered_rm_r(\@files,undef,undef); # no accept or reject expressions
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 |
|
---|
[721] | 147 | # moves a file or a group of files
|
---|
| 148 | sub mv {
|
---|
| 149 | my $dest = pop (@_);
|
---|
| 150 | my (@srcfiles) = @_;
|
---|
[4] | 151 |
|
---|
[721] | 152 | # remove trailing slashes from source and destination files
|
---|
| 153 | $dest =~ s/[\\\/]+$//;
|
---|
| 154 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
| 155 |
|
---|
| 156 | # a few sanity checks
|
---|
| 157 | if (scalar (@srcfiles) == 0) {
|
---|
| 158 | print STDERR "util::mv no destination directory given\n";
|
---|
| 159 | return;
|
---|
| 160 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
| 161 | print STDERR "util::mv if multiple source files are given the ".
|
---|
| 162 | "destination must be a directory\n";
|
---|
| 163 | return;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | # move the files
|
---|
[8716] | 167 | foreach my $file (@srcfiles) {
|
---|
[721] | 168 | my $tempdest = $dest;
|
---|
| 169 | if (-d $tempdest) {
|
---|
| 170 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
| 171 | $tempdest .= "/$filename";
|
---|
| 172 | }
|
---|
| 173 | if (!-e $file) {
|
---|
| 174 | print STDERR "util::mv $file does not exist\n";
|
---|
| 175 | } else {
|
---|
| 176 | rename ($file, $tempdest);
|
---|
| 177 | }
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 |
|
---|
[4] | 182 | # copies a file or a group of files
|
---|
| 183 | sub cp {
|
---|
| 184 | my $dest = pop (@_);
|
---|
| 185 | my (@srcfiles) = @_;
|
---|
| 186 |
|
---|
| 187 | # remove trailing slashes from source and destination files
|
---|
| 188 | $dest =~ s/[\\\/]+$//;
|
---|
| 189 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
| 190 |
|
---|
| 191 | # a few sanity checks
|
---|
| 192 | if (scalar (@srcfiles) == 0) {
|
---|
| 193 | print STDERR "util::cp no destination directory given\n";
|
---|
| 194 | return;
|
---|
| 195 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
| 196 | print STDERR "util::cp if multiple source files are given the ".
|
---|
| 197 | "destination must be a directory\n";
|
---|
| 198 | return;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | # copy the files
|
---|
[8716] | 202 | foreach my $file (@srcfiles) {
|
---|
[4] | 203 | my $tempdest = $dest;
|
---|
| 204 | if (-d $tempdest) {
|
---|
| 205 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
| 206 | $tempdest .= "/$filename";
|
---|
| 207 | }
|
---|
| 208 | if (!-e $file) {
|
---|
| 209 | print STDERR "util::cp $file does not exist\n";
|
---|
| 210 | } elsif (!-f $file) {
|
---|
| 211 | print STDERR "util::cp $file is not a plain file\n";
|
---|
| 212 | } else {
|
---|
| 213 | &File::Copy::copy ($file, $tempdest);
|
---|
| 214 | }
|
---|
| 215 | }
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 |
|
---|
[721] | 219 |
|
---|
[4] | 220 | # recursively copies a file or group of files
|
---|
[1454] | 221 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
| 222 | # destination must be a directory - to copy one file to
|
---|
| 223 | # another use cp instead
|
---|
[4] | 224 | sub cp_r {
|
---|
| 225 | my $dest = pop (@_);
|
---|
| 226 | my (@srcfiles) = @_;
|
---|
| 227 |
|
---|
| 228 | # a few sanity checks
|
---|
| 229 | if (scalar (@srcfiles) == 0) {
|
---|
[1454] | 230 | print STDERR "util::cp_r no destination directory given\n";
|
---|
[4] | 231 | return;
|
---|
[1454] | 232 | } elsif (-f $dest) {
|
---|
| 233 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
[4] | 234 | return;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[1454] | 237 | # create destination directory if it doesn't exist already
|
---|
| 238 | if (! -d $dest) {
|
---|
| 239 | my $store_umask = umask(0002);
|
---|
| 240 | mkdir ($dest, 0777);
|
---|
| 241 | umask($store_umask);
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[4] | 244 | # copy the files
|
---|
[8716] | 245 | foreach my $file (@srcfiles) {
|
---|
[4] | 246 |
|
---|
| 247 | if (!-e $file) {
|
---|
[1454] | 248 | print STDERR "util::cp_r $file does not exist\n";
|
---|
[4] | 249 |
|
---|
| 250 | } elsif (-d $file) {
|
---|
[1586] | 251 | # make the new directory
|
---|
| 252 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
| 253 | $dest = &util::filename_cat ($dest, $filename);
|
---|
| 254 | my $store_umask = umask(0002);
|
---|
| 255 | mkdir ($dest, 0777);
|
---|
| 256 | umask($store_umask);
|
---|
[836] | 257 |
|
---|
[4] | 258 | # get the contents of this directory
|
---|
| 259 | if (!opendir (INDIR, $file)) {
|
---|
| 260 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
| 261 | } else {
|
---|
[1454] | 262 | my @filedir = readdir (INDIR);
|
---|
[4] | 263 | closedir (INDIR);
|
---|
[8716] | 264 | foreach my $f (@filedir) {
|
---|
[1454] | 265 | next if $f =~ /^\.\.?$/;
|
---|
| 266 | # copy all the files in this directory
|
---|
| 267 | my $ff = &util::filename_cat ($file, $f);
|
---|
| 268 | &cp_r ($ff, $dest);
|
---|
| 269 | }
|
---|
[4] | 270 | }
|
---|
| 271 |
|
---|
| 272 | } else {
|
---|
[1454] | 273 | &cp($file, $dest);
|
---|
[4] | 274 | }
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
[21762] | 277 | # recursively copies a file or group of files
|
---|
| 278 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
| 279 | # destination must be a directory - to copy one file to
|
---|
| 280 | # another use cp instead
|
---|
| 281 | sub cp_r_nosvn {
|
---|
| 282 | my $dest = pop (@_);
|
---|
| 283 | my (@srcfiles) = @_;
|
---|
[4] | 284 |
|
---|
[21762] | 285 | # a few sanity checks
|
---|
| 286 | if (scalar (@srcfiles) == 0) {
|
---|
| 287 | print STDERR "util::cp_r no destination directory given\n";
|
---|
| 288 | return;
|
---|
| 289 | } elsif (-f $dest) {
|
---|
| 290 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
| 291 | return;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | # create destination directory if it doesn't exist already
|
---|
| 295 | if (! -d $dest) {
|
---|
| 296 | my $store_umask = umask(0002);
|
---|
| 297 | mkdir ($dest, 0777);
|
---|
| 298 | umask($store_umask);
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | # copy the files
|
---|
| 302 | foreach my $file (@srcfiles) {
|
---|
| 303 |
|
---|
| 304 | if (!-e $file) {
|
---|
| 305 | print STDERR "util::cp_r $file does not exist\n";
|
---|
| 306 |
|
---|
| 307 | } elsif (-d $file) {
|
---|
| 308 | # make the new directory
|
---|
| 309 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
| 310 | $dest = &util::filename_cat ($dest, $filename);
|
---|
| 311 | my $store_umask = umask(0002);
|
---|
| 312 | mkdir ($dest, 0777);
|
---|
| 313 | umask($store_umask);
|
---|
| 314 |
|
---|
| 315 | # get the contents of this directory
|
---|
| 316 | if (!opendir (INDIR, $file)) {
|
---|
| 317 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
| 318 | } else {
|
---|
| 319 | my @filedir = readdir (INDIR);
|
---|
| 320 | closedir (INDIR);
|
---|
| 321 | foreach my $f (@filedir) {
|
---|
| 322 | next if $f =~ /^\.\.?$/;
|
---|
| 323 | next if $f =~ /^\.svn$/;
|
---|
| 324 | # copy all the files in this directory
|
---|
| 325 | my $ff = &util::filename_cat ($file, $f);
|
---|
| 326 | &cp_r ($ff, $dest);
|
---|
| 327 | }
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | } else {
|
---|
| 331 | &cp($file, $dest);
|
---|
| 332 | }
|
---|
| 333 | }
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[11179] | 336 | # copies a directory and its contents, excluding subdirectories, into a new directory
|
---|
| 337 | sub cp_r_toplevel {
|
---|
| 338 | my $dest = pop (@_);
|
---|
| 339 | my (@srcfiles) = @_;
|
---|
[4] | 340 |
|
---|
[11179] | 341 | # a few sanity checks
|
---|
| 342 | if (scalar (@srcfiles) == 0) {
|
---|
| 343 | print STDERR "util::cp_r no destination directory given\n";
|
---|
| 344 | return;
|
---|
| 345 | } elsif (-f $dest) {
|
---|
| 346 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
| 347 | return;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | # create destination directory if it doesn't exist already
|
---|
| 351 | if (! -d $dest) {
|
---|
| 352 | my $store_umask = umask(0002);
|
---|
| 353 | mkdir ($dest, 0777);
|
---|
| 354 | umask($store_umask);
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | # copy the files
|
---|
| 358 | foreach my $file (@srcfiles) {
|
---|
| 359 |
|
---|
| 360 | if (!-e $file) {
|
---|
| 361 | print STDERR "util::cp_r $file does not exist\n";
|
---|
| 362 |
|
---|
| 363 | } elsif (-d $file) {
|
---|
| 364 | # make the new directory
|
---|
| 365 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
| 366 | $dest = &util::filename_cat ($dest, $filename);
|
---|
| 367 | my $store_umask = umask(0002);
|
---|
| 368 | mkdir ($dest, 0777);
|
---|
| 369 | umask($store_umask);
|
---|
| 370 |
|
---|
| 371 | # get the contents of this directory
|
---|
| 372 | if (!opendir (INDIR, $file)) {
|
---|
| 373 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
| 374 | } else {
|
---|
| 375 | my @filedir = readdir (INDIR);
|
---|
| 376 | closedir (INDIR);
|
---|
| 377 | foreach my $f (@filedir) {
|
---|
| 378 | next if $f =~ /^\.\.?$/;
|
---|
| 379 |
|
---|
| 380 | # copy all the files in this directory, but not directories
|
---|
| 381 | my $ff = &util::filename_cat ($file, $f);
|
---|
| 382 | if (-f $ff) {
|
---|
| 383 | &cp($ff, $dest);
|
---|
| 384 | #&cp_r ($ff, $dest);
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | } else {
|
---|
| 390 | &cp($file, $dest);
|
---|
| 391 | }
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[721] | 395 | sub mk_dir {
|
---|
| 396 | my ($dir) = @_;
|
---|
| 397 |
|
---|
[836] | 398 | my $store_umask = umask(0002);
|
---|
| 399 | my $mkdir_ok = mkdir ($dir, 0777);
|
---|
| 400 | umask($store_umask);
|
---|
| 401 |
|
---|
| 402 | if (!$mkdir_ok)
|
---|
| 403 | {
|
---|
[721] | 404 | print STDERR "util::mk_dir could not create directory $dir\n";
|
---|
| 405 | return;
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[1046] | 409 | # in case anyone cares - I did some testing (using perls Benchmark module)
|
---|
| 410 | # on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
|
---|
| 411 | # slightly faster (surprisingly) - Stefan.
|
---|
[4] | 412 | sub mk_all_dir {
|
---|
| 413 | my ($dir) = @_;
|
---|
| 414 |
|
---|
| 415 | # use / for the directory separator, remove duplicate and
|
---|
| 416 | # trailing slashes
|
---|
| 417 | $dir=~s/[\\\/]+/\//g;
|
---|
| 418 | $dir=~s/[\\\/]+$//;
|
---|
| 419 |
|
---|
| 420 | # make sure the cache directory exists
|
---|
| 421 | my $dirsofar = "";
|
---|
| 422 | my $first = 1;
|
---|
[8716] | 423 | foreach my $dirname (split ("/", $dir)) {
|
---|
[4] | 424 | $dirsofar .= "/" unless $first;
|
---|
| 425 | $first = 0;
|
---|
| 426 |
|
---|
| 427 | $dirsofar .= $dirname;
|
---|
| 428 |
|
---|
| 429 | next if $dirname =~ /^(|[a-z]:)$/i;
|
---|
[836] | 430 | if (!-e $dirsofar)
|
---|
| 431 | {
|
---|
| 432 | my $store_umask = umask(0002);
|
---|
| 433 | my $mkdir_ok = mkdir ($dirsofar, 0777);
|
---|
| 434 | umask($store_umask);
|
---|
| 435 | if (!$mkdir_ok)
|
---|
| 436 | {
|
---|
| 437 | print STDERR "util::mk_all_dir could not create directory $dirsofar\n";
|
---|
| 438 | return;
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
[4] | 441 | }
|
---|
| 442 | }
|
---|
| 443 |
|
---|
[619] | 444 | # make hard link to file if supported by OS, otherwise copy the file
|
---|
| 445 | sub hard_link {
|
---|
[18463] | 446 | my ($src, $dest, $verbosity) = @_;
|
---|
[4] | 447 |
|
---|
[619] | 448 | # remove trailing slashes from source and destination files
|
---|
| 449 | $src =~ s/[\\\/]+$//;
|
---|
| 450 | $dest =~ s/[\\\/]+$//;
|
---|
| 451 |
|
---|
[23307] | 452 | ## print STDERR "**** src = ", unicode::debug_unicode_string($src),"\n";
|
---|
[619] | 453 | # a few sanity checks
|
---|
[812] | 454 | if (-e $dest) {
|
---|
| 455 | # destination file already exists
|
---|
| 456 | return;
|
---|
| 457 | }
|
---|
| 458 | elsif (!-e $src) {
|
---|
[23307] | 459 | print STDERR "util::hard_link source file \"$src\" does not exist\n";
|
---|
[3628] | 460 | return 1;
|
---|
[619] | 461 | }
|
---|
| 462 | elsif (-d $src) {
|
---|
[23307] | 463 | print STDERR "util::hard_link source \"$src\" is a directory\n";
|
---|
[3628] | 464 | return 1;
|
---|
[619] | 465 | }
|
---|
| 466 |
|
---|
| 467 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
| 468 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
| 469 |
|
---|
[14365] | 470 |
|
---|
[22119] | 471 | if (!link($src, $dest)) {
|
---|
[18463] | 472 | if ((!defined $verbosity) || ($verbosity>2)) {
|
---|
| 473 | print STDERR "util::hard_link: unable to create hard link. ";
|
---|
| 474 | print STDERR " Copying file: $src -> $dest\n";
|
---|
| 475 | }
|
---|
[14365] | 476 | &File::Copy::copy ($src, $dest);
|
---|
[619] | 477 | }
|
---|
[3628] | 478 | return 0;
|
---|
[619] | 479 | }
|
---|
| 480 |
|
---|
[2193] | 481 | # make soft link to file if supported by OS, otherwise copy file
|
---|
[721] | 482 | sub soft_link {
|
---|
[15165] | 483 | my ($src, $dest, $ensure_paths_absolute) = @_;
|
---|
[619] | 484 |
|
---|
[721] | 485 | # remove trailing slashes from source and destination files
|
---|
| 486 | $src =~ s/[\\\/]+$//;
|
---|
| 487 | $dest =~ s/[\\\/]+$//;
|
---|
[619] | 488 |
|
---|
[15165] | 489 | # Ensure file paths are absolute IF requested to do so
|
---|
| 490 | # Soft_linking didn't work for relative paths
|
---|
| 491 | if(defined $ensure_paths_absolute && $ensure_paths_absolute) {
|
---|
| 492 | # We need to ensure that the src file is the absolute path
|
---|
| 493 | # See http://perldoc.perl.org/File/Spec.html
|
---|
| 494 | if(!File::Spec->file_name_is_absolute( $src )) { # it's relative
|
---|
| 495 | $src = File::Spec->rel2abs($src); # make absolute
|
---|
| 496 | }
|
---|
| 497 | # Might as well ensure that the destination file's absolute path is used
|
---|
| 498 | if(!File::Spec->file_name_is_absolute( $dest )) {
|
---|
| 499 | $dest = File::Spec->rel2abs($dest); # make absolute
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[721] | 503 | # a few sanity checks
|
---|
| 504 | if (!-e $src) {
|
---|
| 505 | print STDERR "util::soft_link source file $src does not exist\n";
|
---|
| 506 | return 0;
|
---|
| 507 | }
|
---|
[619] | 508 |
|
---|
[721] | 509 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
| 510 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
[14365] | 511 |
|
---|
[2193] | 512 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
[23484] | 513 |
|
---|
[14365] | 514 | # symlink not supported on windows
|
---|
| 515 | &File::Copy::copy ($src, $dest);
|
---|
[2193] | 516 |
|
---|
| 517 | } elsif (!eval {symlink($src, $dest)}) {
|
---|
[2974] | 518 | print STDERR "util::soft_link: unable to create soft link.\n";
|
---|
[721] | 519 | return 0;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
| 522 | return 1;
|
---|
| 523 | }
|
---|
| 524 |
|
---|
[23362] | 525 | # Primarily for filenames generated by processing
|
---|
| 526 | # content of HTML files (which are mapped to UTF-8 internally)
|
---|
| 527 | #
|
---|
| 528 | # To turn this into an octet string that really exists on the file
|
---|
| 529 | # system:
|
---|
| 530 | # 1. don't need to do anything special for Unix-based systems
|
---|
| 531 | # (as underlying file system is byte-code)
|
---|
| 532 | # 2. need to map to short DOS filenames for Windows
|
---|
[721] | 533 |
|
---|
[23362] | 534 | sub utf8_to_real_filename
|
---|
| 535 | {
|
---|
| 536 | my ($utf8_filename) = @_;
|
---|
[721] | 537 |
|
---|
[23362] | 538 | my $real_filename;
|
---|
[721] | 539 |
|
---|
[23362] | 540 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
| 541 | require Win32;
|
---|
[23388] | 542 |
|
---|
| 543 | print STDERR "***** utf8 filename = $utf8_filename\n\n\n";
|
---|
| 544 |
|
---|
[23362] | 545 | my $unicode_filename = decode("utf8",$utf8_filename);
|
---|
| 546 | $real_filename = Win32::GetShortPathName($unicode_filename);
|
---|
| 547 | }
|
---|
| 548 | else {
|
---|
| 549 | $real_filename = $utf8_filename;
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | return $real_filename;
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 |
|
---|
| 556 | sub fd_exists
|
---|
| 557 | {
|
---|
| 558 | my $filename_full_path = shift @_;
|
---|
| 559 | my $test_op = shift @_ || "-e";
|
---|
| 560 |
|
---|
| 561 | # By default tests for existance of file or directory (-e)
|
---|
| 562 | # Can be made more specific by providing second parameter (e.g. -f or -d)
|
---|
| 563 |
|
---|
| 564 | my $exists = 0;
|
---|
| 565 |
|
---|
| 566 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
| 567 | require Win32;
|
---|
| 568 | my $filename_short_path = Win32::GetShortPathName($filename_full_path);
|
---|
| 569 | if (!defined $filename_short_path) {
|
---|
| 570 | # Was probably still in UTF8 form (not what is needed on Windows)
|
---|
| 571 | my $unicode_filename_full_path = eval "decode(\"utf8\",\$filename_full_path)";
|
---|
| 572 | if (defined $unicode_filename_full_path) {
|
---|
| 573 | $filename_short_path = Win32::GetShortPathName($unicode_filename_full_path);
|
---|
| 574 | }
|
---|
| 575 | }
|
---|
| 576 | $filename_full_path = $filename_short_path;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | if (defined $filename_full_path) {
|
---|
| 580 | $exists = eval "($test_op \$filename_full_path)";
|
---|
| 581 | }
|
---|
| 582 |
|
---|
| 583 | return $exists;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | sub file_exists
|
---|
| 587 | {
|
---|
| 588 | my ($filename_full_path) = @_;
|
---|
| 589 |
|
---|
| 590 | return fd_exists($filename_full_path,"-f");
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | sub dir_exists
|
---|
| 594 | {
|
---|
| 595 | my ($filename_full_path) = @_;
|
---|
| 596 |
|
---|
| 597 | return fd_exists($filename_full_path,"-d");
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 |
|
---|
| 601 |
|
---|
[4] | 602 | # updates a copy of a directory in some other part of the filesystem
|
---|
| 603 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
| 604 | # both $fromdir and $todir should be absolute paths
|
---|
| 605 | sub cachedir {
|
---|
| 606 | my ($fromdir, $todir, $verbosity) = @_;
|
---|
| 607 | $verbosity = 1 unless defined $verbosity;
|
---|
| 608 |
|
---|
| 609 | # use / for the directory separator, remove duplicate and
|
---|
| 610 | # trailing slashes
|
---|
| 611 | $fromdir=~s/[\\\/]+/\//g;
|
---|
| 612 | $fromdir=~s/[\\\/]+$//;
|
---|
| 613 | $todir=~s/[\\\/]+/\//g;
|
---|
| 614 | $todir=~s/[\\\/]+$//;
|
---|
| 615 |
|
---|
| 616 | &mk_all_dir ($todir);
|
---|
| 617 |
|
---|
| 618 | # get the directories in ascending order
|
---|
| 619 | if (!opendir (FROMDIR, $fromdir)) {
|
---|
| 620 | print STDERR "util::cachedir could not read directory $fromdir\n";
|
---|
| 621 | return;
|
---|
| 622 | }
|
---|
| 623 | my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
|
---|
| 624 | closedir (FROMDIR);
|
---|
| 625 |
|
---|
| 626 | if (!opendir (TODIR, $todir)) {
|
---|
| 627 | print STDERR "util::cacedir could not read directory $todir\n";
|
---|
| 628 | return;
|
---|
| 629 | }
|
---|
| 630 | my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
|
---|
| 631 | closedir (TODIR);
|
---|
| 632 |
|
---|
| 633 | my $fromi = 0;
|
---|
| 634 | my $toi = 0;
|
---|
| 635 |
|
---|
| 636 | while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
|
---|
| 637 | # print "fromi: $fromi toi: $toi\n";
|
---|
| 638 |
|
---|
| 639 | # see if we should delete a file/directory
|
---|
| 640 | # this should happen if the file/directory
|
---|
| 641 | # is not in the from list or if its a different
|
---|
| 642 | # size, or has an older timestamp
|
---|
| 643 | if ($toi < scalar(@todir)) {
|
---|
| 644 | if (($fromi >= scalar(@fromdir)) ||
|
---|
| 645 | ($todir[$toi] lt $fromdir[$fromi] ||
|
---|
| 646 | ($todir[$toi] eq $fromdir[$fromi] &&
|
---|
| 647 | &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
|
---|
| 648 | $verbosity)))) {
|
---|
| 649 |
|
---|
| 650 | # the files are different
|
---|
| 651 | &rm_r("$todir/$todir[$toi]");
|
---|
| 652 | splice(@todir, $toi, 1); # $toi stays the same
|
---|
| 653 |
|
---|
| 654 | } elsif ($todir[$toi] eq $fromdir[$fromi]) {
|
---|
| 655 | # the files are the same
|
---|
| 656 | # if it is a directory, check its contents
|
---|
| 657 | if (-d "$todir/$todir[$toi]") {
|
---|
| 658 | &cachedir ("$fromdir/$fromdir[$fromi]",
|
---|
| 659 | "$todir/$todir[$toi]", $verbosity);
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | $toi++;
|
---|
| 663 | $fromi++;
|
---|
| 664 | next;
|
---|
| 665 | }
|
---|
| 666 | }
|
---|
| 667 |
|
---|
| 668 | # see if we should insert a file/directory
|
---|
| 669 | # we should insert a file/directory if there
|
---|
| 670 | # is no tofiles left or if the tofile does not exist
|
---|
| 671 | if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
|
---|
| 672 | $todir[$toi] gt $fromdir[$fromi])) {
|
---|
| 673 | &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
|
---|
| 674 | splice (@todir, $toi, 0, $fromdir[$fromi]);
|
---|
| 675 |
|
---|
| 676 | $toi++;
|
---|
| 677 | $fromi++;
|
---|
| 678 | }
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | # this function returns -1 if either file is not found
|
---|
| 683 | # assumes that $file1 and $file2 are absolute file names or
|
---|
| 684 | # in the current directory
|
---|
| 685 | # $file2 is allowed to be newer than $file1
|
---|
| 686 | sub differentfiles {
|
---|
| 687 | my ($file1, $file2, $verbosity) = @_;
|
---|
| 688 | $verbosity = 1 unless defined $verbosity;
|
---|
| 689 |
|
---|
| 690 | $file1 =~ s/\/+$//;
|
---|
| 691 | $file2 =~ s/\/+$//;
|
---|
| 692 |
|
---|
| 693 | my ($file1name) = $file1 =~ /\/([^\/]*)$/;
|
---|
| 694 | my ($file2name) = $file2 =~ /\/([^\/]*)$/;
|
---|
| 695 |
|
---|
| 696 | return -1 unless (-e $file1 && -e $file2);
|
---|
| 697 | if ($file1name ne $file2name) {
|
---|
| 698 | print STDERR "filenames are not the same\n" if ($verbosity >= 2);
|
---|
| 699 | return 1;
|
---|
| 700 | }
|
---|
| 701 |
|
---|
[8716] | 702 | my @file1stat = stat ($file1);
|
---|
| 703 | my @file2stat = stat ($file2);
|
---|
[4] | 704 |
|
---|
| 705 | if (-d $file1) {
|
---|
| 706 | if (! -d $file2) {
|
---|
| 707 | print STDERR "one file is a directory\n" if ($verbosity >= 2);
|
---|
| 708 | return 1;
|
---|
| 709 | }
|
---|
| 710 | return 0;
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | # both must be regular files
|
---|
| 714 | unless (-f $file1 && -f $file2) {
|
---|
| 715 | print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
|
---|
| 716 | return 1;
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | # the size of the files must be the same
|
---|
| 720 | if ($file1stat[7] != $file2stat[7]) {
|
---|
| 721 | print STDERR "different sized files\n" if ($verbosity >= 2);
|
---|
| 722 | return 1;
|
---|
| 723 | }
|
---|
| 724 |
|
---|
| 725 | # the second file cannot be older than the first
|
---|
| 726 | if ($file1stat[9] > $file2stat[9]) {
|
---|
| 727 | print STDERR "file is older\n" if ($verbosity >= 2);
|
---|
| 728 | return 1;
|
---|
| 729 | }
|
---|
| 730 |
|
---|
| 731 | return 0;
|
---|
| 732 | }
|
---|
| 733 |
|
---|
| 734 |
|
---|
[16266] | 735 | sub get_tmp_filename
|
---|
| 736 | {
|
---|
| 737 | my $file_ext = shift(@_) || undef;
|
---|
| 738 |
|
---|
[22438] | 739 | my $opt_dot_file_ext = "";
|
---|
| 740 | if (defined $file_ext) {
|
---|
| 741 | if ($file_ext !~ m/\./) {
|
---|
| 742 | # no dot, so needs one added in at start
|
---|
| 743 | $opt_dot_file_ext = ".$file_ext"
|
---|
| 744 | }
|
---|
| 745 | else {
|
---|
| 746 | # allow for "extensions" such as _metadata.txt to be handled
|
---|
| 747 | # gracefully
|
---|
| 748 | $opt_dot_file_ext = $file_ext;
|
---|
| 749 | }
|
---|
| 750 | }
|
---|
[16266] | 751 |
|
---|
[2795] | 752 | my $tmpdir = filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
[4] | 753 | &mk_all_dir ($tmpdir) unless -e $tmpdir;
|
---|
| 754 |
|
---|
| 755 | my $count = 1000;
|
---|
| 756 | my $rand = int(rand $count);
|
---|
[16266] | 757 | my $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
| 758 |
|
---|
| 759 | while (-e $full_tmp_filename) {
|
---|
[4] | 760 | $rand = int(rand $count);
|
---|
[16266] | 761 | $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
[4] | 762 | $count++;
|
---|
| 763 | }
|
---|
[16266] | 764 |
|
---|
| 765 | return $full_tmp_filename;
|
---|
[4] | 766 | }
|
---|
| 767 |
|
---|
[22886] | 768 | sub get_timestamped_tmp_folder
|
---|
[22873] | 769 | {
|
---|
| 770 |
|
---|
[22886] | 771 | my $tmp_dirname;
|
---|
[22873] | 772 | if(defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
| 773 | $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 774 | } elsif(defined $ENV{'GSDLHOME'}) {
|
---|
| 775 | $tmp_dirname = $ENV{'GSDLHOME'};
|
---|
[22886] | 776 | } else {
|
---|
| 777 | return undef;
|
---|
[22873] | 778 | }
|
---|
| 779 |
|
---|
| 780 | $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
|
---|
| 781 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
| 782 |
|
---|
| 783 | # add the timestamp into the path otherwise we can run into problems
|
---|
| 784 | # if documents have the same name
|
---|
| 785 | my $timestamp = time;
|
---|
| 786 | my $time_tmp_dirname = &util::filename_cat($tmp_dirname, $timestamp);
|
---|
| 787 | $tmp_dirname = $time_tmp_dirname;
|
---|
| 788 | my $i = 1;
|
---|
| 789 | while (-e $tmp_dirname) {
|
---|
| 790 | $tmp_dirname = "$time_tmp_dirname$i";
|
---|
| 791 | $i++;
|
---|
| 792 | }
|
---|
| 793 | &util::mk_dir($tmp_dirname);
|
---|
| 794 |
|
---|
[22886] | 795 | return $tmp_dirname;
|
---|
| 796 | }
|
---|
[22873] | 797 |
|
---|
[22886] | 798 | sub get_timestamped_tmp_filename_in_collection
|
---|
| 799 | {
|
---|
| 800 |
|
---|
| 801 | my ($input_filename, $output_ext) = @_;
|
---|
| 802 | # derive tmp filename from input filename
|
---|
| 803 | my ($tailname, $dirname, $suffix)
|
---|
| 804 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
| 805 |
|
---|
| 806 | # softlink to collection tmp dir
|
---|
| 807 | my $tmp_dirname = &util::get_timestamped_tmp_folder();
|
---|
| 808 | $tmp_dirname = $dirname unless defined $tmp_dirname;
|
---|
| 809 |
|
---|
[22873] | 810 | # following two steps copied from ConvertBinaryFile
|
---|
[22886] | 811 | # do we need them?? can't use them as is, as they use plugin methods.
|
---|
| 812 |
|
---|
[22873] | 813 | #$tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
|
---|
| 814 |
|
---|
| 815 | # URLEncode this since htmls with images where the html filename is utf8 don't seem
|
---|
| 816 | # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
|
---|
| 817 | # files on the filesystem.
|
---|
| 818 | #$tailname = &util::rename_file($tailname, $self->{'file_rename_method'}, "without_suffix");
|
---|
| 819 | if (defined $output_ext) {
|
---|
| 820 | $output_ext = ".$output_ext"; # add the dot
|
---|
| 821 | } else {
|
---|
| 822 | $output_ext = $suffix;
|
---|
| 823 | }
|
---|
| 824 | $output_ext= lc($output_ext);
|
---|
| 825 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$output_ext");
|
---|
| 826 |
|
---|
| 827 | return $tmp_filename;
|
---|
| 828 | }
|
---|
| 829 |
|
---|
[21218] | 830 | sub get_toplevel_tmp_dir
|
---|
| 831 | {
|
---|
| 832 | return filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 |
|
---|
[17512] | 836 | sub filename_to_regex {
|
---|
| 837 | my $filename = shift (@_);
|
---|
[4] | 838 |
|
---|
[24971] | 839 | # need to make single backslashes double so that regex works
|
---|
[24832] | 840 | $filename =~ s/\\/\\\\/g; # if ($ENV{'GSDLOS'} =~ /^windows$/i);
|
---|
[24829] | 841 |
|
---|
[24832] | 842 | # note that the first part of a substitution is a regex, so RE chars need to be escaped,
|
---|
| 843 | # the second part of a substitution is not a regex, so for e.g. full-stop can be specified literally
|
---|
[24829] | 844 | $filename =~ s/\./\\./g; # in case there are extensions/other full stops, escape them
|
---|
| 845 | $filename =~ s@\(@\\(@g; # escape brackets
|
---|
| 846 | $filename =~ s@\)@\\)@g; # escape brackets
|
---|
[24932] | 847 | $filename =~ s@\[@\\[@g; # escape brackets
|
---|
| 848 | $filename =~ s@\]@\\]@g; # escape brackets
|
---|
[24829] | 849 |
|
---|
[17512] | 850 | return $filename;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
[24829] | 853 | sub unregex_filename {
|
---|
| 854 | my $filename = shift (@_);
|
---|
| 855 |
|
---|
| 856 | # need to put doubled backslashes for regex back to single
|
---|
| 857 | $filename =~ s/\\\./\./g; # remove RE syntax for .
|
---|
| 858 | $filename =~ s@\\\(@(@g; # remove RE syntax for ( => "\(" turns into "("
|
---|
| 859 | $filename =~ s@\\\)@)@g; # remove RE syntax for ) => "\)" turns into ")"
|
---|
[24932] | 860 | $filename =~ s@\\\[@[@g; # remove RE syntax for [ => "\[" turns into "["
|
---|
| 861 | $filename =~ s@\\\]@]@g; # remove RE syntax for ] => "\]" turns into "]"
|
---|
[24940] | 862 |
|
---|
| 863 | # \\ goes to \
|
---|
| 864 | # This is the last step in reverse mirroring the order of steps in filename_to_regex()
|
---|
| 865 | $filename =~ s/\\\\/\\/g; # remove RE syntax for \
|
---|
[24829] | 866 | return $filename;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
[4] | 869 | sub filename_cat {
|
---|
[7507] | 870 | my $first_file = shift(@_);
|
---|
[4] | 871 | my (@filenames) = @_;
|
---|
[10146] | 872 |
|
---|
[16266] | 873 | # Useful for debugging
|
---|
| 874 | # -- might make sense to call caller(0) rather than (1)??
|
---|
| 875 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
[22856] | 876 | # print STDERR "Calling method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
[18913] | 877 |
|
---|
| 878 | # If first_file is not null or empty, then add it back into the list
|
---|
| 879 | if (defined $first_file && $first_file =~ /\S/) {
|
---|
[7507] | 880 | unshift(@filenames, $first_file);
|
---|
| 881 | }
|
---|
| 882 |
|
---|
[4] | 883 | my $filename = join("/", @filenames);
|
---|
| 884 |
|
---|
| 885 | # remove duplicate slashes and remove the last slash
|
---|
[488] | 886 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 887 | $filename =~ s/[\\\/]+/\\/g;
|
---|
| 888 | } else {
|
---|
[836] | 889 | $filename =~ s/[\/]+/\//g;
|
---|
| 890 | # DB: want a filename abc\de.html to remain like this
|
---|
[488] | 891 | }
|
---|
| 892 | $filename =~ s/[\\\/]$//;
|
---|
[4] | 893 |
|
---|
| 894 | return $filename;
|
---|
| 895 | }
|
---|
| 896 |
|
---|
[21413] | 897 |
|
---|
| 898 | sub pathname_cat {
|
---|
| 899 | my $first_path = shift(@_);
|
---|
| 900 | my (@pathnames) = @_;
|
---|
| 901 |
|
---|
| 902 | # If first_path is not null or empty, then add it back into the list
|
---|
| 903 | if (defined $first_path && $first_path =~ /\S/) {
|
---|
| 904 | unshift(@pathnames, $first_path);
|
---|
| 905 | }
|
---|
| 906 |
|
---|
[21425] | 907 | my $join_char;
|
---|
[21413] | 908 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 909 | $join_char = ";";
|
---|
| 910 | } else {
|
---|
| 911 | $join_char = ":";
|
---|
| 912 | }
|
---|
| 913 |
|
---|
| 914 | my $pathname = join($join_char, @pathnames);
|
---|
| 915 |
|
---|
| 916 | # remove duplicate slashes
|
---|
| 917 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 918 | $pathname =~ s/[\\\/]+/\\/g;
|
---|
| 919 | } else {
|
---|
| 920 | $pathname =~ s/[\/]+/\//g;
|
---|
| 921 | # DB: want a pathname abc\de.html to remain like this
|
---|
| 922 | }
|
---|
| 923 |
|
---|
| 924 | return $pathname;
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 |
|
---|
[19616] | 928 | sub tidy_up_oid {
|
---|
| 929 | my ($OID) = @_;
|
---|
| 930 | if ($OID =~ /\./) {
|
---|
| 931 | print STDERR "Warning, identifier $OID contains periods (.), removing them\n";
|
---|
| 932 | $OID =~ s/\.//g; #remove any periods
|
---|
| 933 | }
|
---|
| 934 | if ($OID =~ /^\s.*\s$/) {
|
---|
| 935 | print STDERR "Warning, identifier $OID starts or ends with whitespace. Removing it\n";
|
---|
| 936 | # remove starting and trailing whitespace
|
---|
| 937 | $OID =~ s/^\s+//;
|
---|
| 938 | $OID =~ s/\s+$//;
|
---|
| 939 | }
|
---|
| 940 | if ($OID =~ /^[\d]*$/) {
|
---|
| 941 | print STDERR "Warning, identifier $OID contains only digits. Prepending 'D'.\n";
|
---|
| 942 | $OID = "D" . $OID;
|
---|
| 943 | }
|
---|
| 944 |
|
---|
| 945 | return $OID;
|
---|
| 946 | }
|
---|
[10212] | 947 | sub envvar_prepend {
|
---|
| 948 | my ($var,$val) = @_;
|
---|
| 949 |
|
---|
[16404] | 950 | # do not prepend any value/path that's already in the environment variable
|
---|
[24832] | 951 |
|
---|
| 952 | my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
|
---|
| 953 | if (!defined($ENV{$var})) {
|
---|
| 954 | $ENV{$var} = "$val";
|
---|
[16442] | 955 | }
|
---|
[24832] | 956 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
| 957 | $ENV{$var} = "$val;".$ENV{$var};
|
---|
[10212] | 958 | }
|
---|
| 959 | }
|
---|
| 960 |
|
---|
| 961 | sub envvar_append {
|
---|
| 962 | my ($var,$val) = @_;
|
---|
[24832] | 963 |
|
---|
[16404] | 964 | # do not append any value/path that's already in the environment variable
|
---|
[24832] | 965 |
|
---|
| 966 | my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
|
---|
| 967 | if (!defined($ENV{$var})) {
|
---|
| 968 | $ENV{$var} = "$val";
|
---|
[16442] | 969 | }
|
---|
[24832] | 970 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
| 971 | $ENV{$var} .= ";$val";
|
---|
| 972 | }
|
---|
[10212] | 973 | }
|
---|
| 974 |
|
---|
[16442] | 975 |
|
---|
[16380] | 976 | # splits a filename into a prefix and a tail extension using the tail_re, or
|
---|
| 977 | # if that fails, splits on the file_extension . (dot)
|
---|
| 978 | sub get_prefix_and_tail_by_regex {
|
---|
[10212] | 979 |
|
---|
[16380] | 980 | my ($filename,$tail_re) = @_;
|
---|
| 981 |
|
---|
| 982 | my ($file_prefix,$file_ext) = ($filename =~ m/^(.*?)($tail_re)$/);
|
---|
| 983 | if ((!defined $file_prefix) || (!defined $file_ext)) {
|
---|
| 984 | ($file_prefix,$file_ext) = ($filename =~ m/^(.*)(\..*?)$/);
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | return ($file_prefix,$file_ext);
|
---|
| 988 | }
|
---|
| 989 |
|
---|
| 990 | # get full path and file only path from a base_dir (which may be empty) and
|
---|
| 991 | # file (which may contain directories)
|
---|
| 992 | sub get_full_filenames {
|
---|
| 993 | my ($base_dir, $file) = @_;
|
---|
| 994 |
|
---|
| 995 | my $filename_full_path = $file;
|
---|
| 996 | # add on directory if present
|
---|
| 997 | $filename_full_path = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\S/;
|
---|
| 998 |
|
---|
| 999 | my $filename_no_path = $file;
|
---|
| 1000 |
|
---|
| 1001 | # remove directory if present
|
---|
| 1002 | $filename_no_path =~ s/^.*[\/\\]//;
|
---|
| 1003 | return ($filename_full_path, $filename_no_path);
|
---|
| 1004 | }
|
---|
| 1005 |
|
---|
[8682] | 1006 | # returns the path of a file without the filename -- ie. the directory the file is in
|
---|
| 1007 | sub filename_head {
|
---|
| 1008 | my $filename = shift(@_);
|
---|
| 1009 |
|
---|
| 1010 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 1011 | $filename =~ s/[^\\\\]*$//;
|
---|
| 1012 | }
|
---|
| 1013 | else {
|
---|
| 1014 | $filename =~ s/[^\\\/]*$//;
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | return $filename;
|
---|
| 1018 | }
|
---|
| 1019 |
|
---|
| 1020 |
|
---|
[23362] | 1021 |
|
---|
[1454] | 1022 | # returns 1 if filename1 and filename2 point to the same
|
---|
| 1023 | # file or directory
|
---|
| 1024 | sub filenames_equal {
|
---|
| 1025 | my ($filename1, $filename2) = @_;
|
---|
| 1026 |
|
---|
| 1027 | # use filename_cat to clean up trailing slashes and
|
---|
| 1028 | # multiple slashes
|
---|
| 1029 | $filename1 = filename_cat ($filename1);
|
---|
[2516] | 1030 | $filename2 = filename_cat ($filename2);
|
---|
[1454] | 1031 |
|
---|
| 1032 | # filenames not case sensitive on windows
|
---|
| 1033 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 1034 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
| 1035 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
| 1036 | }
|
---|
| 1037 | return 1 if $filename1 eq $filename2;
|
---|
| 1038 | return 0;
|
---|
| 1039 | }
|
---|
| 1040 |
|
---|
[24932] | 1041 | # If filename is relative to within_dir, returns the relative path of filename to that directory
|
---|
| 1042 | # with slashes in the filename returned as they were in the original (absolute) filename.
|
---|
[23362] | 1043 | sub filename_within_directory
|
---|
| 1044 | {
|
---|
| 1045 | my ($filename,$within_dir) = @_;
|
---|
| 1046 |
|
---|
[23371] | 1047 | if ($within_dir !~ m/[\/\\]$/) {
|
---|
| 1048 | my $dirsep = &util::get_dirsep();
|
---|
[23362] | 1049 | $within_dir .= $dirsep;
|
---|
| 1050 | }
|
---|
| 1051 |
|
---|
[24829] | 1052 | $within_dir = &filename_to_regex($within_dir); # escape DOS style file separator and brackets
|
---|
[23362] | 1053 | if ($filename =~ m/^$within_dir(.*)$/) {
|
---|
| 1054 | $filename = $1;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | return $filename;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
[24932] | 1060 | # If filename is relative to within_dir, returns the relative path of filename to that directory in URL format.
|
---|
| 1061 | # Filename and within_dir can be any type of slashes, but will be compared as URLs (i.e. unix-style slashes).
|
---|
| 1062 | # The subpath returned will also be a URL type filename.
|
---|
| 1063 | sub filename_within_directory_url_format
|
---|
| 1064 | {
|
---|
| 1065 | my ($filename,$within_dir) = @_;
|
---|
| 1066 |
|
---|
| 1067 | # convert parameters only to / slashes if Windows
|
---|
| 1068 |
|
---|
[24971] | 1069 | my $filename_urlformat = &filepath_to_url_format($filename);
|
---|
| 1070 | my $within_dir_urlformat = &filepath_to_url_format($within_dir);
|
---|
| 1071 |
|
---|
[24932] | 1072 | #if ($within_dir_urlformat !~ m/\/$/) {
|
---|
| 1073 | # make sure directory ends with a slash
|
---|
| 1074 | #$within_dir_urlformat .= "/";
|
---|
| 1075 | #}
|
---|
| 1076 |
|
---|
| 1077 | my $within_dir_urlformat_re = &filename_to_regex($within_dir_urlformat); # escape any special RE characters, such as brackets
|
---|
| 1078 |
|
---|
| 1079 | #print STDERR "@@@@@ $filename_urlformat =~ $within_dir_urlformat_re\n";
|
---|
| 1080 |
|
---|
| 1081 | # dir prefix may or may not end with a slash (this is discarded when extracting the sub-filepath)
|
---|
| 1082 | if ($filename_urlformat =~ m/^$within_dir_urlformat_re(?:\/)*(.*)$/) {
|
---|
| 1083 | $filename_urlformat = $1;
|
---|
| 1084 | }
|
---|
| 1085 |
|
---|
| 1086 | return $filename_urlformat;
|
---|
| 1087 | }
|
---|
| 1088 |
|
---|
[24971] | 1089 | # Convert parameter to use / slashes if Windows (if on Linux leave any \ as is,
|
---|
| 1090 | # since on Linux it doesn't represent a file separator but an escape char).
|
---|
| 1091 | sub filepath_to_url_format
|
---|
| 1092 | {
|
---|
| 1093 | my ($filepath) = @_;
|
---|
| 1094 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 1095 | # Only need to worry about Windows, as Unix style directories already in url-format
|
---|
| 1096 | # Convert Windows style \ => /
|
---|
| 1097 | $filepath =~ s@\\@/@g;
|
---|
| 1098 | }
|
---|
| 1099 | return $filepath;
|
---|
| 1100 | }
|
---|
[24932] | 1101 |
|
---|
[24971] | 1102 |
|
---|
[10281] | 1103 | sub filename_within_collection
|
---|
| 1104 | {
|
---|
| 1105 | my ($filename) = @_;
|
---|
| 1106 |
|
---|
| 1107 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
| 1108 |
|
---|
| 1109 | if (defined $collect_dir) {
|
---|
[23362] | 1110 |
|
---|
[15875] | 1111 | # if from within GSDLCOLLECTDIR, then remove directory prefix
|
---|
| 1112 | # so source_filename is realative to it. This is done to aid
|
---|
| 1113 | # portability, i.e. the collection can be moved to somewhere
|
---|
| 1114 | # else on the file system and the archives directory will still
|
---|
| 1115 | # work. This is needed, for example in the applet version of
|
---|
| 1116 | # GLI where GSDLHOME/collect on the server will be different to
|
---|
| 1117 | # the collect directory of the remove user. Of course,
|
---|
| 1118 | # GSDLCOLLECTDIR subsequently needs to be put back on to turn
|
---|
| 1119 | # it back into a full pathname.
|
---|
[23362] | 1120 |
|
---|
| 1121 | $filename = filename_within_directory($filename,$collect_dir);
|
---|
[10281] | 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | return $filename;
|
---|
| 1125 | }
|
---|
| 1126 |
|
---|
[23362] | 1127 | sub prettyprint_file
|
---|
| 1128 | {
|
---|
[23484] | 1129 | my ($base_dir,$file,$gli) = @_;
|
---|
[23362] | 1130 |
|
---|
| 1131 | my $filename_full_path = &util::filename_cat($base_dir,$file);
|
---|
| 1132 |
|
---|
| 1133 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
| 1134 | require Win32;
|
---|
| 1135 |
|
---|
| 1136 | # For some reason base_dir in the form c:/a/b/c
|
---|
| 1137 | # This leads to confusion later on, so turn it back into
|
---|
| 1138 | # the more usual Windows form
|
---|
| 1139 | $base_dir =~ s/\//\\/g;
|
---|
| 1140 | my $long_base_dir = Win32::GetLongPathName($base_dir);
|
---|
| 1141 | my $long_full_path = Win32::GetLongPathName($filename_full_path);
|
---|
| 1142 |
|
---|
| 1143 | $file = filename_within_directory($long_full_path,$long_base_dir);
|
---|
[23484] | 1144 | $file = encode("utf8",$file) if ($gli);
|
---|
[23362] | 1145 | }
|
---|
| 1146 |
|
---|
| 1147 | return $file;
|
---|
| 1148 | }
|
---|
| 1149 |
|
---|
| 1150 |
|
---|
| 1151 | sub upgrade_if_dos_filename
|
---|
| 1152 | {
|
---|
[23371] | 1153 | my ($filename_full_path,$and_encode) = @_;
|
---|
[23362] | 1154 |
|
---|
| 1155 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
| 1156 | # Ensure any DOS-like filename, such as test~1.txt, has been upgraded
|
---|
| 1157 | # to its long (Windows) version
|
---|
[23416] | 1158 | my $long_filename = Win32::GetLongPathName($filename_full_path);
|
---|
| 1159 | if (defined $long_filename) {
|
---|
| 1160 | $filename_full_path = $long_filename;
|
---|
| 1161 | }
|
---|
[23362] | 1162 | # Make sure initial drive letter is lower-case (to fit in with rest of Greenstone)
|
---|
[23483] | 1163 | $filename_full_path =~ s/^(.):/\u$1:/;
|
---|
[23371] | 1164 | if ((defined $and_encode) && ($and_encode)) {
|
---|
| 1165 | $filename_full_path = encode("utf8",$filename_full_path);
|
---|
| 1166 | }
|
---|
[23362] | 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | return $filename_full_path;
|
---|
| 1170 | }
|
---|
| 1171 |
|
---|
| 1172 |
|
---|
[23388] | 1173 | sub downgrade_if_dos_filename
|
---|
| 1174 | {
|
---|
| 1175 | my ($filename_full_path) = @_;
|
---|
| 1176 |
|
---|
| 1177 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
| 1178 | require Win32;
|
---|
| 1179 |
|
---|
| 1180 | # Ensure the given long Windows filename is in a form that can
|
---|
| 1181 | # be opened by Perl => convert it to a short DOS-like filename
|
---|
| 1182 |
|
---|
[23414] | 1183 | my $short_filename = Win32::GetShortPathName($filename_full_path);
|
---|
| 1184 | if (defined $short_filename) {
|
---|
| 1185 | $filename_full_path = $short_filename;
|
---|
| 1186 | }
|
---|
[23416] | 1187 | # Make sure initial drive letter is lower-case (to fit in
|
---|
| 1188 | # with rest of Greenstone)
|
---|
[23483] | 1189 | $filename_full_path =~ s/^(.):/\u$1:/;
|
---|
[23388] | 1190 | }
|
---|
| 1191 |
|
---|
| 1192 | return $filename_full_path;
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
[23561] | 1195 | sub block_filename
|
---|
| 1196 | {
|
---|
| 1197 | my ($block_hash,$filename) = @_;
|
---|
| 1198 |
|
---|
| 1199 | if ($ENV{'GSDLOS'} =~ m/^windows$/) {
|
---|
| 1200 |
|
---|
| 1201 | # lower case the entire thing, eg for cover.jpg when its actually cover.JPG
|
---|
| 1202 | my $lower_filename = lc($filename);
|
---|
| 1203 | $block_hash->{'file_blocks'}->{$lower_filename} = 1;
|
---|
| 1204 | # my $lower_drive = $filename;
|
---|
| 1205 | # $lower_drive =~ s/^([A-Z]):/\l$1:/i;
|
---|
| 1206 |
|
---|
| 1207 | # my $upper_drive = $filename;
|
---|
| 1208 | # $upper_drive =~ s/^([A-Z]):/\u$1:/i;
|
---|
| 1209 | #
|
---|
| 1210 | # $block_hash->{'file_blocks'}->{$lower_drive} = 1;
|
---|
| 1211 | # $block_hash->{'file_blocks'}->{$upper_drive} = 1;
|
---|
| 1212 | }
|
---|
| 1213 | else {
|
---|
| 1214 | $block_hash->{'file_blocks'}->{$filename} = 1;
|
---|
| 1215 | }
|
---|
| 1216 | }
|
---|
[23388] | 1217 |
|
---|
[23561] | 1218 |
|
---|
[18441] | 1219 | sub filename_is_absolute
|
---|
| 1220 | {
|
---|
| 1221 | my ($filename) = @_;
|
---|
| 1222 |
|
---|
| 1223 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 1224 | return ($filename =~ m/^(\w:)?\\/);
|
---|
| 1225 | }
|
---|
| 1226 | else {
|
---|
| 1227 | return ($filename =~ m/^\//);
|
---|
| 1228 | }
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 |
|
---|
[17572] | 1232 | ## @method make_absolute()
|
---|
| 1233 | #
|
---|
| 1234 | # Ensure the given file path is absolute in respect to the given base path.
|
---|
| 1235 | #
|
---|
| 1236 | # @param $base_dir A string denoting the base path the given dir must be
|
---|
| 1237 | # absolute to.
|
---|
| 1238 | # @param $dir The directory to be made absolute as a string. Note that the
|
---|
| 1239 | # dir may already be absolute, in which case it will remain
|
---|
| 1240 | # unchanged.
|
---|
| 1241 | # @return The now absolute form of the directory as a string.
|
---|
| 1242 | #
|
---|
| 1243 | # @author John Thompson, DL Consulting Ltd.
|
---|
| 1244 | # @copy 2006 DL Consulting Ltd.
|
---|
| 1245 | #
|
---|
| 1246 | #used in buildcol.pl, doesn't work for all cases --kjdon
|
---|
| 1247 | sub make_absolute {
|
---|
| 1248 |
|
---|
| 1249 | my ($base_dir, $dir) = @_;
|
---|
[18441] | 1250 | ### print STDERR "dir = $dir\n";
|
---|
[17572] | 1251 | $dir =~ s/[\\\/]+/\//g;
|
---|
| 1252 | $dir = $base_dir . "/$dir" unless ($dir =~ m|^(\w:)?/|);
|
---|
| 1253 | $dir =~ s|^/tmp_mnt||;
|
---|
| 1254 | 1 while($dir =~ s|/[^/]*/\.\./|/|g);
|
---|
| 1255 | $dir =~ s|/[.][.]?/|/|g;
|
---|
| 1256 | $dir =~ tr|/|/|s;
|
---|
[18441] | 1257 | ### print STDERR "dir = $dir\n";
|
---|
[17572] | 1258 |
|
---|
| 1259 | return $dir;
|
---|
| 1260 | }
|
---|
| 1261 | ## make_absolute() ##
|
---|
[10281] | 1262 |
|
---|
[7929] | 1263 | sub get_dirsep {
|
---|
| 1264 |
|
---|
| 1265 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 1266 | return "\\";
|
---|
| 1267 | } else {
|
---|
| 1268 | return "\/";
|
---|
| 1269 | }
|
---|
| 1270 | }
|
---|
| 1271 |
|
---|
[619] | 1272 | sub get_os_dirsep {
|
---|
[4] | 1273 |
|
---|
[619] | 1274 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 1275 | return "\\\\";
|
---|
| 1276 | } else {
|
---|
| 1277 | return "\\\/";
|
---|
| 1278 | }
|
---|
| 1279 | }
|
---|
| 1280 |
|
---|
| 1281 | sub get_re_dirsep {
|
---|
| 1282 |
|
---|
| 1283 | return "\\\\|\\\/";
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
| 1286 |
|
---|
[15003] | 1287 | sub get_dirsep_tail {
|
---|
| 1288 | my ($filename) = @_;
|
---|
| 1289 |
|
---|
| 1290 | # returns last part of directory or filename
|
---|
| 1291 | # On unix e.g. a/b.d => b.d
|
---|
| 1292 | # a/b/c => c
|
---|
| 1293 |
|
---|
[15088] | 1294 | my $dirsep = get_re_dirsep();
|
---|
| 1295 | my @dirs = split (/$dirsep/, $filename);
|
---|
| 1296 | my $tail = pop @dirs;
|
---|
[15003] | 1297 |
|
---|
[15088] | 1298 | # - caused problems under windows
|
---|
| 1299 | #my ($tail) = ($filename =~ m/^(?:.*?$dirsep)?(.*?)$/);
|
---|
| 1300 |
|
---|
[15003] | 1301 | return $tail;
|
---|
| 1302 | }
|
---|
| 1303 |
|
---|
| 1304 |
|
---|
[4] | 1305 | # if this is running on windows we want binaries to end in
|
---|
| 1306 | # .exe, otherwise they don't have to end in any extension
|
---|
| 1307 | sub get_os_exe {
|
---|
| 1308 | return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
| 1309 | return "";
|
---|
| 1310 | }
|
---|
| 1311 |
|
---|
| 1312 |
|
---|
[86] | 1313 | # test to see whether this is a big or little endian machine
|
---|
[15713] | 1314 | sub is_little_endian
|
---|
| 1315 | {
|
---|
| 1316 | # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
|
---|
| 1317 | # If it is a Macintosh machine (i.e. the Darwin operating system), regardless if it's running on the IBM power-pc cpu or the x86 Intel-based chip with a power-pc emulator running on top of it, it's big-endian
|
---|
| 1318 | # Otherwise, it's little endian
|
---|
| 1319 |
|
---|
| 1320 | #return 0 if $^O =~ /^darwin$/i;
|
---|
[17714] | 1321 | #return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
|
---|
| 1322 |
|
---|
| 1323 | # Going back to stating exactly whether the machine is little endian
|
---|
| 1324 | # or big endian, without any special case for Macs. Since for rata it comes
|
---|
| 1325 | # back with little endian and for shuttle with bigendian.
|
---|
[15713] | 1326 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
[86] | 1327 | }
|
---|
[4] | 1328 |
|
---|
[86] | 1329 |
|
---|
[135] | 1330 | # will return the collection name if successful, "" otherwise
|
---|
| 1331 | sub use_collection {
|
---|
[1454] | 1332 | my ($collection, $collectdir) = @_;
|
---|
[135] | 1333 |
|
---|
[1454] | 1334 | if (!defined $collectdir || $collectdir eq "") {
|
---|
| 1335 | $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
| 1336 | }
|
---|
| 1337 |
|
---|
[135] | 1338 | # get and check the collection
|
---|
| 1339 | if (!defined($collection) || $collection eq "") {
|
---|
| 1340 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
| 1341 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
| 1342 | } else {
|
---|
[2359] | 1343 | print STDOUT "No collection specified\n";
|
---|
[135] | 1344 | return "";
|
---|
| 1345 | }
|
---|
| 1346 | }
|
---|
| 1347 |
|
---|
| 1348 | if ($collection eq "modelcol") {
|
---|
[2359] | 1349 | print STDOUT "You can't use modelcol.\n";
|
---|
[135] | 1350 | return "";
|
---|
| 1351 | }
|
---|
| 1352 |
|
---|
| 1353 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
| 1354 | # are defined
|
---|
[17204] | 1355 | $ENV{'GSDLCOLLECTION'} = $collection;
|
---|
[1454] | 1356 | $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
|
---|
[135] | 1357 |
|
---|
| 1358 | # make sure this collection exists
|
---|
| 1359 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
[2359] | 1360 | print STDOUT "Invalid collection ($collection).\n";
|
---|
[135] | 1361 | return "";
|
---|
| 1362 | }
|
---|
| 1363 |
|
---|
| 1364 | # everything is ready to go
|
---|
| 1365 | return $collection;
|
---|
| 1366 | }
|
---|
| 1367 |
|
---|
[21207] | 1368 | sub get_current_collection_name {
|
---|
| 1369 | return $ENV{'GSDLCOLLECTION'};
|
---|
| 1370 | }
|
---|
[14926] | 1371 |
|
---|
| 1372 |
|
---|
| 1373 | # will return the collection name if successful, "" otherwise.
|
---|
| 1374 | # Like use_collection (above) but for greenstone 3 (taking account of site level)
|
---|
| 1375 |
|
---|
| 1376 | sub use_site_collection {
|
---|
| 1377 | my ($site, $collection, $collectdir) = @_;
|
---|
| 1378 |
|
---|
| 1379 | if (!defined $collectdir || $collectdir eq "") {
|
---|
| 1380 | die "GSDL3HOME not set.\n" unless defined $ENV{'GSDL3HOME'};
|
---|
| 1381 | $collectdir = &filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect");
|
---|
| 1382 | }
|
---|
| 1383 |
|
---|
| 1384 | # collectdir explicitly set by this point (using $site variable if required).
|
---|
| 1385 | # Can call "old" gsdl2 use_collection now.
|
---|
| 1386 |
|
---|
| 1387 | return use_collection($collection,$collectdir);
|
---|
| 1388 | }
|
---|
| 1389 |
|
---|
| 1390 |
|
---|
| 1391 |
|
---|
[15018] | 1392 | sub locate_config_file
|
---|
| 1393 | {
|
---|
| 1394 | my ($file) = @_;
|
---|
| 1395 |
|
---|
| 1396 | my $locations = locate_config_files($file);
|
---|
| 1397 |
|
---|
| 1398 | return shift @$locations; # returns undef if 'locations' is empty
|
---|
| 1399 | }
|
---|
| 1400 |
|
---|
| 1401 |
|
---|
| 1402 | sub locate_config_files
|
---|
| 1403 | {
|
---|
| 1404 | my ($file) = @_;
|
---|
| 1405 |
|
---|
| 1406 | my @locations = ();
|
---|
| 1407 |
|
---|
| 1408 | if (-e $file) {
|
---|
| 1409 | # Clearly specified (most likely full filename)
|
---|
| 1410 | # No need to hunt in 'etc' directories, return value unchanged
|
---|
| 1411 | push(@locations,$file);
|
---|
| 1412 | }
|
---|
| 1413 | else {
|
---|
| 1414 | # Check for collection specific one before looking in global GSDL 'etc'
|
---|
[16969] | 1415 | if (defined $ENV{'GSDLCOLLECTDIR'} && $ENV{'GSDLCOLLECTDIR'} ne "") {
|
---|
| 1416 | my $test_collect_etc_filename
|
---|
| 1417 | = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $file);
|
---|
| 1418 |
|
---|
| 1419 | if (-e $test_collect_etc_filename) {
|
---|
| 1420 | push(@locations,$test_collect_etc_filename);
|
---|
| 1421 | }
|
---|
[15018] | 1422 | }
|
---|
| 1423 | my $test_main_etc_filename
|
---|
| 1424 | = &util::filename_cat($ENV{'GSDLHOME'},"etc", $file);
|
---|
| 1425 | if (-e $test_main_etc_filename) {
|
---|
| 1426 | push(@locations,$test_main_etc_filename);
|
---|
| 1427 | }
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
| 1430 | return \@locations;
|
---|
| 1431 | }
|
---|
| 1432 |
|
---|
| 1433 |
|
---|
[9955] | 1434 | sub hyperlink_text
|
---|
| 1435 | {
|
---|
| 1436 | my ($text) = @_;
|
---|
| 1437 |
|
---|
| 1438 | $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
|
---|
| 1439 | $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
|
---|
| 1440 |
|
---|
| 1441 | return $text;
|
---|
| 1442 | }
|
---|
| 1443 |
|
---|
| 1444 |
|
---|
[16436] | 1445 | # A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
|
---|
| 1446 | # Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
|
---|
| 1447 | sub is_dir_empty
|
---|
| 1448 | {
|
---|
| 1449 | my ($path) = @_;
|
---|
| 1450 | opendir DIR, $path;
|
---|
| 1451 | while(my $entry = readdir DIR) {
|
---|
| 1452 | next if($entry =~ /^\.\.?$/);
|
---|
| 1453 | closedir DIR;
|
---|
| 1454 | return 0;
|
---|
| 1455 | }
|
---|
| 1456 | closedir DIR;
|
---|
| 1457 | return 1;
|
---|
| 1458 | }
|
---|
| 1459 |
|
---|
[18337] | 1460 | # Returns the given filename converted using either URL encoding or base64
|
---|
| 1461 | # encoding, as specified by $rename_method. If the given filename has no suffix
|
---|
[20413] | 1462 | # (if it is just the tailname), then $no_suffix should be some defined value.
|
---|
| 1463 | # rename_method can be url, none, base64
|
---|
[18319] | 1464 | sub rename_file {
|
---|
[18337] | 1465 | my ($filename, $rename_method, $no_suffix) = @_;
|
---|
[18329] | 1466 |
|
---|
[18337] | 1467 | if(!$filename) { # undefined or empty string
|
---|
[18329] | 1468 | return $filename;
|
---|
| 1469 | }
|
---|
[18319] | 1470 |
|
---|
[20413] | 1471 | if (!$rename_method) {
|
---|
| 1472 | print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
|
---|
| 1473 | # Debugging information
|
---|
[22856] | 1474 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
| 1475 | # print STDERR "Called from method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
[20413] | 1476 | $rename_method = "url";
|
---|
| 1477 | } elsif($rename_method eq "none") {
|
---|
| 1478 | return $filename; # would have already been renamed
|
---|
| 1479 | }
|
---|
| 1480 |
|
---|
[19762] | 1481 | # No longer replace spaces with underscores, since underscores mess with incremental rebuild
|
---|
| 1482 | ### Replace spaces with underscore. Do this first else it can go wrong below when getting tailname
|
---|
| 1483 | ###$filename =~ s/ /_/g;
|
---|
[18337] | 1484 |
|
---|
| 1485 | my ($tailname,$dirname,$suffix);
|
---|
| 1486 | if($no_suffix) { # given a tailname, no suffix
|
---|
| 1487 | ($tailname,$dirname) = File::Basename::fileparse($filename);
|
---|
| 1488 | }
|
---|
| 1489 | else {
|
---|
| 1490 | ($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.(?:[^\\.]+?)\$");
|
---|
| 1491 | }
|
---|
[23388] | 1492 | if (!$suffix) {
|
---|
| 1493 | $suffix = "";
|
---|
| 1494 | }
|
---|
| 1495 | else {
|
---|
| 1496 | $suffix = lc($suffix);
|
---|
| 1497 | }
|
---|
[18337] | 1498 |
|
---|
[20413] | 1499 | if ($rename_method eq "url") {
|
---|
[18319] | 1500 | $tailname = &unicode::url_encode($tailname);
|
---|
| 1501 | }
|
---|
| 1502 | elsif ($rename_method eq "base64") {
|
---|
[18341] | 1503 | $tailname = &unicode::base64_encode($tailname);
|
---|
[18319] | 1504 | $tailname =~ s/\s*//sg; # for some reason it adds spaces not just at end but also in middle
|
---|
| 1505 | }
|
---|
[18326] | 1506 |
|
---|
[18319] | 1507 | $filename = "$tailname$suffix";
|
---|
[18326] | 1508 | $filename = "$dirname$filename" if ($dirname ne "./" && $dirname ne ".\\");
|
---|
[18319] | 1509 |
|
---|
| 1510 | return $filename;
|
---|
| 1511 | }
|
---|
| 1512 |
|
---|
[21616] | 1513 |
|
---|
| 1514 | # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files
|
---|
[21664] | 1515 | sub rename_ldb_or_bdb_file {
|
---|
[18657] | 1516 | my ($filename_no_ext) = @_;
|
---|
| 1517 |
|
---|
| 1518 | my $new_filename = "$filename_no_ext.gdb";
|
---|
[21615] | 1519 | return if (-f $new_filename); # if the file has the right extension, don't need to do anything
|
---|
[18657] | 1520 | # try ldb
|
---|
| 1521 | my $old_filename = "$filename_no_ext.ldb";
|
---|
| 1522 |
|
---|
| 1523 | if (-f $old_filename) {
|
---|
[19056] | 1524 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
| 1525 | rename ($old_filename, $new_filename)
|
---|
| 1526 | || print STDERR "Rename failed: $!\n";
|
---|
[18657] | 1527 | return;
|
---|
| 1528 | }
|
---|
| 1529 | # try bdb
|
---|
| 1530 | $old_filename = "$filename_no_ext.bdb";
|
---|
| 1531 | if (-f $old_filename) {
|
---|
[19056] | 1532 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
| 1533 | rename ($old_filename, $new_filename)
|
---|
| 1534 | || print STDERR "Rename failed: $!\n";
|
---|
[18657] | 1535 | return;
|
---|
| 1536 | }
|
---|
| 1537 | }
|
---|
| 1538 |
|
---|
[24874] | 1539 | sub os_dir() {
|
---|
| 1540 |
|
---|
| 1541 | my $gsdlarch = "";
|
---|
| 1542 | if(defined $ENV{'GSDLARCH'}) {
|
---|
| 1543 | $gsdlarch = $ENV{'GSDLARCH'};
|
---|
| 1544 | }
|
---|
| 1545 | return $ENV{'GSDLOS'}.$gsdlarch;
|
---|
| 1546 | }
|
---|
[18657] | 1547 |
|
---|
[21719] | 1548 | # Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file.
|
---|
| 1549 | # By default, /greenstone3 for GS3 or /greenstone for GS2.
|
---|
| 1550 | sub get_greenstone_url_prefix() {
|
---|
| 1551 | # if already set on a previous occasion, just return that
|
---|
| 1552 | # (Don't want to keep repeating this: cost of re-opening and scanning files.)
|
---|
| 1553 | return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
|
---|
[18657] | 1554 |
|
---|
[21719] | 1555 | my ($configfile, $urlprefix, $defaultUrlprefix);
|
---|
| 1556 | my @propertynames = ();
|
---|
| 1557 |
|
---|
| 1558 | if($ENV{'GSDL3SRCHOME'}) {
|
---|
| 1559 | $defaultUrlprefix = "/greenstone3";
|
---|
| 1560 | $configfile = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
|
---|
| 1561 | push(@propertynames, qw/path\s*\=/);
|
---|
| 1562 | } else {
|
---|
| 1563 | $defaultUrlprefix = "/greenstone";
|
---|
[24874] | 1564 | $configfile = &util::filename_cat($ENV{'GSDLHOME'}, "cgi-bin", &os_dir(), "gsdlsite.cfg");
|
---|
[21719] | 1565 | push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other
|
---|
| 1566 | }
|
---|
| 1567 |
|
---|
| 1568 | $urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
|
---|
| 1569 |
|
---|
| 1570 | if(!$urlprefix) { # no values found for URL prefix, use default values
|
---|
| 1571 | $urlprefix = $defaultUrlprefix;
|
---|
| 1572 | } else {
|
---|
| 1573 | #gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
|
---|
| 1574 | $urlprefix =~ s/^\///; # remove the starting slash
|
---|
| 1575 | my @dirs = split(/(\\|\/)/, $urlprefix);
|
---|
| 1576 | $urlprefix = shift(@dirs);
|
---|
| 1577 |
|
---|
| 1578 | if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
|
---|
| 1579 | $urlprefix = "/$urlprefix";
|
---|
| 1580 | }
|
---|
| 1581 | }
|
---|
| 1582 |
|
---|
| 1583 | # set for the future
|
---|
| 1584 | $ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
|
---|
| 1585 | # print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
|
---|
| 1586 | return $urlprefix;
|
---|
| 1587 | }
|
---|
| 1588 |
|
---|
| 1589 |
|
---|
| 1590 | # Given a config file (xml or java properties file) and a list/array of regular expressions
|
---|
| 1591 | # that represent property names to match on, this function will return the value for the 1st
|
---|
| 1592 | # matching property name. If the return value is undefined, no matching property was found.
|
---|
| 1593 | sub extract_propvalue_from_file() {
|
---|
| 1594 | my ($configfile, $propertynames) = @_;
|
---|
| 1595 |
|
---|
| 1596 | my $value;
|
---|
| 1597 | unless(open(FIN, "<$configfile")) {
|
---|
| 1598 | print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
|
---|
| 1599 | return $value; # not initialised
|
---|
| 1600 | }
|
---|
| 1601 |
|
---|
| 1602 | # Read the entire file at once, as one single line, then close it
|
---|
| 1603 | my $filecontents;
|
---|
| 1604 | {
|
---|
| 1605 | local $/ = undef;
|
---|
| 1606 | $filecontents = <FIN>;
|
---|
| 1607 | }
|
---|
| 1608 | close(FIN);
|
---|
| 1609 |
|
---|
| 1610 | foreach my $regex (@$propertynames) {
|
---|
| 1611 | ($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
|
---|
| 1612 | if($value) {
|
---|
| 1613 | $value =~ s/^\"//; # remove any startquotes
|
---|
| 1614 | $value =~ s/\".*$//; # remove the 1st endquotes (if any) followed by any xml
|
---|
| 1615 | last; # found value for a matching property, break from loop
|
---|
| 1616 | }
|
---|
| 1617 | }
|
---|
| 1618 |
|
---|
| 1619 | return $value;
|
---|
| 1620 | }
|
---|
| 1621 |
|
---|
[23306] | 1622 | # Subroutine that sources setup.bash, given GSDLHOME and GSDLOS and
|
---|
| 1623 | # given that perllib is in @INC in order to invoke this subroutine.
|
---|
| 1624 | # Call as follows -- after setting up INC to include perllib and
|
---|
| 1625 | # after setting up GSDLHOME and GSDLOS:
|
---|
| 1626 | #
|
---|
| 1627 | # require util;
|
---|
| 1628 | # &util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
|
---|
| 1629 | #
|
---|
| 1630 | sub setup_greenstone_env() {
|
---|
| 1631 | my ($GSDLHOME, $GSDLOS) = @_;
|
---|
| 1632 |
|
---|
| 1633 | #my %env_map = ();
|
---|
| 1634 | # Get the localised ENV settings of running a localised source setup.bash
|
---|
[23314] | 1635 | # and put it into the ENV here. Need to clear GSDLHOME before running setup
|
---|
| 1636 | #my $perl_command = "(cd $GSDLHOME; export GSDLHOME=; . ./setup.bash > /dev/null; env)";
|
---|
| 1637 | my $perl_command = "(cd $GSDLHOME; /bin/bash -c \"export GSDLHOME=; source setup.bash > /dev/null; env\")";
|
---|
[23306] | 1638 | if($GSDLOS =~ m/windows/i) {
|
---|
[23314] | 1639 | #$perl_command = "cmd /C \"cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set\"";
|
---|
| 1640 | $perl_command = "(cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set)";
|
---|
[23306] | 1641 | }
|
---|
| 1642 | if (!open(PIN, "$perl_command |")) {
|
---|
| 1643 | print STDERR ("Unable to execute command: $perl_command. $!\n");
|
---|
[24563] | 1644 | }
|
---|
[23306] | 1645 |
|
---|
| 1646 | while (defined (my $perl_output_line = <PIN>)) {
|
---|
| 1647 | my($key,$value) = ($perl_output_line =~ m/^([^=]*)[=](.*)$/);
|
---|
| 1648 | #$env_map{$key}=$value;
|
---|
| 1649 | $ENV{$key}=$value;
|
---|
| 1650 | }
|
---|
[24563] | 1651 | close (PIN);
|
---|
| 1652 |
|
---|
[23306] | 1653 | # If any keys in $ENV don't occur in Greenstone's localised env
|
---|
| 1654 | # (stored in $env_map), delete those entries from $ENV
|
---|
| 1655 | #foreach $key (keys %ENV) {
|
---|
| 1656 | # if(!defined $env_map{$key}) {
|
---|
| 1657 | # print STDOUT "**** DELETING ENV KEY: $key\tVALUE: $ENV{'$key'}\n";
|
---|
| 1658 | # delete $ENV{$key}; # del $ENV(key, value) pair
|
---|
| 1659 | # }
|
---|
| 1660 | #}
|
---|
| 1661 | #undef %env_map;
|
---|
| 1662 | }
|
---|
| 1663 |
|
---|
[24362] | 1664 | sub get_perl_exec() {
|
---|
| 1665 | my $perl_exec = $^X; # may return just "perl"
|
---|
| 1666 |
|
---|
| 1667 | if($ENV{'PERLPATH'}) {
|
---|
| 1668 | # OR: # $perl_exec = &util::filename_cat($ENV{'PERLPATH'},"perl");
|
---|
| 1669 | if($ENV{'GSDLOS'} =~ m/windows/) {
|
---|
| 1670 | $perl_exec = "$ENV{'PERLPATH'}\\Perl.exe";
|
---|
| 1671 | } else {
|
---|
| 1672 | $perl_exec = "$ENV{'PERLPATH'}/perl";
|
---|
| 1673 | }
|
---|
| 1674 | } else { # no PERLPATH, use Config{perlpath} else $^X: special variables
|
---|
| 1675 | # containing the full path to the current perl executable we're using
|
---|
| 1676 | $perl_exec = $Config{perlpath}; # configured path for perl
|
---|
| 1677 | if (!-e $perl_exec) { # may not point to location on this machine
|
---|
| 1678 | $perl_exec = $^X; # may return just "perl"
|
---|
| 1679 | if($perl_exec =~ m/^perl/i) { # warn if just perl or Perl.exe
|
---|
| 1680 | print STDERR "**** WARNING: Perl exec found contains no path: $perl_exec";
|
---|
| 1681 | }
|
---|
| 1682 | }
|
---|
| 1683 | }
|
---|
| 1684 |
|
---|
| 1685 | return $perl_exec;
|
---|
| 1686 | }
|
---|
| 1687 |
|
---|
| 1688 |
|
---|
[4] | 1689 | 1;
|
---|