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