[537] | 1 | ###########################################################################
|
---|
| 2 | #
|
---|
| 3 | # util.pm -- various useful utilities
|
---|
| 4 | # A component of the Greenstone digital library software
|
---|
| 5 | # from the New Zealand Digital Library Project at the
|
---|
| 6 | # University of Waikato, New Zealand.
|
---|
| 7 | #
|
---|
| 8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
| 9 | #
|
---|
| 10 | # This program is free software; you can redistribute it and/or modify
|
---|
| 11 | # it under the terms of the GNU General Public License as published by
|
---|
| 12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
| 13 | # (at your option) any later version.
|
---|
| 14 | #
|
---|
| 15 | # This program is distributed in the hope that it will be useful,
|
---|
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 18 | # GNU General Public License for more details.
|
---|
| 19 | #
|
---|
| 20 | # You should have received a copy of the GNU General Public License
|
---|
| 21 | # along with this program; if not, write to the Free Software
|
---|
| 22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 23 | #
|
---|
| 24 | ###########################################################################
|
---|
[4] | 25 |
|
---|
| 26 | package util;
|
---|
| 27 |
|
---|
| 28 | use File::Copy;
|
---|
[619] | 29 | use File::Basename;
|
---|
[4] | 30 |
|
---|
| 31 |
|
---|
| 32 | # removes files (but not directories)
|
---|
| 33 | sub rm {
|
---|
| 34 | my (@files) = @_;
|
---|
| 35 | my @filefiles = ();
|
---|
| 36 |
|
---|
| 37 | # make sure the files we want to delete exist
|
---|
| 38 | # and are regular files
|
---|
| 39 | foreach $file (@files) {
|
---|
| 40 | if (!-e $file) {
|
---|
| 41 | print STDERR "util::rm $file does not exist\n";
|
---|
[721] | 42 | } elsif ((!-f $file) && (!-l $file)) {
|
---|
| 43 | print STDERR "util::rm $file is not a regular (or symbolic) file\n";
|
---|
[4] | 44 | } else {
|
---|
| 45 | push (@filefiles, $file);
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | # remove the files
|
---|
| 50 | my $numremoved = unlink @filefiles;
|
---|
| 51 |
|
---|
| 52 | # check to make sure all of them were removed
|
---|
| 53 | if ($numremoved != scalar(@filefiles)) {
|
---|
| 54 | print STDERR "util::rm Not all files were removed\n";
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
| 59 | # recursive removal
|
---|
| 60 | sub rm_r {
|
---|
| 61 | my (@files) = @_;
|
---|
| 62 |
|
---|
| 63 | # recursively remove the files
|
---|
| 64 | foreach $file (@files) {
|
---|
| 65 | $file =~ s/[\/\\]+$//; # remove trailing slashes
|
---|
| 66 |
|
---|
| 67 | if (!-e $file) {
|
---|
| 68 | print STDERR "util::rm_r $file does not exist\n";
|
---|
| 69 |
|
---|
[721] | 70 | } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
|
---|
[4] | 71 | # get the contents of this directory
|
---|
| 72 | if (!opendir (INDIR, $file)) {
|
---|
| 73 | print STDERR "util::rm_r could not open directory $file\n";
|
---|
| 74 | } else {
|
---|
| 75 | my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
|
---|
| 76 | closedir (INDIR);
|
---|
| 77 |
|
---|
| 78 | # remove all the files in this directory
|
---|
| 79 | &rm_r (map {$_="$file/$_";} @filedir);
|
---|
| 80 |
|
---|
| 81 | # remove this directory
|
---|
| 82 | if (!rmdir $file) {
|
---|
| 83 | print STDERR "util::rm_r couldn't remove directory $file\n";
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | } else {
|
---|
| 88 | # remove this file
|
---|
| 89 | &rm ($file);
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[721] | 94 | # moves a file or a group of files
|
---|
| 95 | sub mv {
|
---|
| 96 | my $dest = pop (@_);
|
---|
| 97 | my (@srcfiles) = @_;
|
---|
[4] | 98 |
|
---|
[721] | 99 | # remove trailing slashes from source and destination files
|
---|
| 100 | $dest =~ s/[\\\/]+$//;
|
---|
| 101 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
| 102 |
|
---|
| 103 | # a few sanity checks
|
---|
| 104 | if (scalar (@srcfiles) == 0) {
|
---|
| 105 | print STDERR "util::mv no destination directory given\n";
|
---|
| 106 | return;
|
---|
| 107 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
| 108 | print STDERR "util::mv if multiple source files are given the ".
|
---|
| 109 | "destination must be a directory\n";
|
---|
| 110 | return;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | # move the files
|
---|
| 114 | foreach $file (@srcfiles) {
|
---|
| 115 | my $tempdest = $dest;
|
---|
| 116 | if (-d $tempdest) {
|
---|
| 117 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
| 118 | $tempdest .= "/$filename";
|
---|
| 119 | }
|
---|
| 120 | if (!-e $file) {
|
---|
| 121 | print STDERR "util::mv $file does not exist\n";
|
---|
| 122 | } else {
|
---|
| 123 | rename ($file, $tempdest);
|
---|
| 124 | }
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
[4] | 129 | # copies a file or a group of files
|
---|
| 130 | sub cp {
|
---|
| 131 | my $dest = pop (@_);
|
---|
| 132 | my (@srcfiles) = @_;
|
---|
| 133 |
|
---|
| 134 | # remove trailing slashes from source and destination files
|
---|
| 135 | $dest =~ s/[\\\/]+$//;
|
---|
| 136 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
| 137 |
|
---|
| 138 | # a few sanity checks
|
---|
| 139 | if (scalar (@srcfiles) == 0) {
|
---|
| 140 | print STDERR "util::cp no destination directory given\n";
|
---|
| 141 | return;
|
---|
| 142 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
| 143 | print STDERR "util::cp if multiple source files are given the ".
|
---|
| 144 | "destination must be a directory\n";
|
---|
| 145 | return;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | # copy the files
|
---|
| 149 | foreach $file (@srcfiles) {
|
---|
| 150 | my $tempdest = $dest;
|
---|
| 151 | if (-d $tempdest) {
|
---|
| 152 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
| 153 | $tempdest .= "/$filename";
|
---|
| 154 | }
|
---|
| 155 | if (!-e $file) {
|
---|
| 156 | print STDERR "util::cp $file does not exist\n";
|
---|
| 157 | } elsif (!-f $file) {
|
---|
| 158 | print STDERR "util::cp $file is not a plain file\n";
|
---|
| 159 | } else {
|
---|
| 160 | &File::Copy::copy ($file, $tempdest);
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 |
|
---|
[721] | 166 |
|
---|
[4] | 167 | # recursively copies a file or group of files
|
---|
[1454] | 168 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
| 169 | # destination must be a directory - to copy one file to
|
---|
| 170 | # another use cp instead
|
---|
[4] | 171 | sub cp_r {
|
---|
| 172 | my $dest = pop (@_);
|
---|
| 173 | my (@srcfiles) = @_;
|
---|
| 174 |
|
---|
| 175 | # a few sanity checks
|
---|
| 176 | if (scalar (@srcfiles) == 0) {
|
---|
[1454] | 177 | print STDERR "util::cp_r no destination directory given\n";
|
---|
[4] | 178 | return;
|
---|
[1454] | 179 | } elsif (-f $dest) {
|
---|
| 180 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
[4] | 181 | return;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[1454] | 184 | # create destination directory if it doesn't exist already
|
---|
| 185 | if (! -d $dest) {
|
---|
| 186 | my $store_umask = umask(0002);
|
---|
| 187 | mkdir ($dest, 0777);
|
---|
| 188 | umask($store_umask);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[4] | 191 | # copy the files
|
---|
| 192 | foreach $file (@srcfiles) {
|
---|
| 193 |
|
---|
| 194 | if (!-e $file) {
|
---|
[1454] | 195 | print STDERR "util::cp_r $file does not exist\n";
|
---|
[4] | 196 |
|
---|
| 197 | } elsif (-d $file) {
|
---|
[1586] | 198 | # make the new directory
|
---|
| 199 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
| 200 | $dest = &util::filename_cat ($dest, $filename);
|
---|
| 201 | my $store_umask = umask(0002);
|
---|
| 202 | mkdir ($dest, 0777);
|
---|
| 203 | umask($store_umask);
|
---|
[836] | 204 |
|
---|
[4] | 205 | # get the contents of this directory
|
---|
| 206 | if (!opendir (INDIR, $file)) {
|
---|
| 207 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
| 208 | } else {
|
---|
[1454] | 209 | my @filedir = readdir (INDIR);
|
---|
[4] | 210 | closedir (INDIR);
|
---|
[1454] | 211 | foreach $f (@filedir) {
|
---|
| 212 | next if $f =~ /^\.\.?$/;
|
---|
| 213 | # copy all the files in this directory
|
---|
| 214 | my $ff = &util::filename_cat ($file, $f);
|
---|
| 215 | &cp_r ($ff, $dest);
|
---|
| 216 | }
|
---|
[4] | 217 | }
|
---|
| 218 |
|
---|
| 219 | } else {
|
---|
[1454] | 220 | &cp($file, $dest);
|
---|
[4] | 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
[721] | 226 | sub mk_dir {
|
---|
| 227 | my ($dir) = @_;
|
---|
| 228 |
|
---|
[836] | 229 | my $store_umask = umask(0002);
|
---|
| 230 | my $mkdir_ok = mkdir ($dir, 0777);
|
---|
| 231 | umask($store_umask);
|
---|
| 232 |
|
---|
| 233 | if (!$mkdir_ok)
|
---|
| 234 | {
|
---|
[721] | 235 | print STDERR "util::mk_dir could not create directory $dir\n";
|
---|
| 236 | return;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[1046] | 240 | # in case anyone cares - I did some testing (using perls Benchmark module)
|
---|
| 241 | # on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
|
---|
| 242 | # slightly faster (surprisingly) - Stefan.
|
---|
[4] | 243 | sub mk_all_dir {
|
---|
| 244 | my ($dir) = @_;
|
---|
| 245 |
|
---|
| 246 | # use / for the directory separator, remove duplicate and
|
---|
| 247 | # trailing slashes
|
---|
| 248 | $dir=~s/[\\\/]+/\//g;
|
---|
| 249 | $dir=~s/[\\\/]+$//;
|
---|
| 250 |
|
---|
| 251 | # make sure the cache directory exists
|
---|
| 252 | my $dirsofar = "";
|
---|
| 253 | my $first = 1;
|
---|
| 254 | foreach $dirname (split ("/", $dir)) {
|
---|
| 255 | $dirsofar .= "/" unless $first;
|
---|
| 256 | $first = 0;
|
---|
| 257 |
|
---|
| 258 | $dirsofar .= $dirname;
|
---|
| 259 |
|
---|
| 260 | next if $dirname =~ /^(|[a-z]:)$/i;
|
---|
[836] | 261 | if (!-e $dirsofar)
|
---|
| 262 | {
|
---|
| 263 | my $store_umask = umask(0002);
|
---|
| 264 | my $mkdir_ok = mkdir ($dirsofar, 0777);
|
---|
| 265 | umask($store_umask);
|
---|
| 266 | if (!$mkdir_ok)
|
---|
| 267 | {
|
---|
| 268 | print STDERR "util::mk_all_dir could not create directory $dirsofar\n";
|
---|
| 269 | return;
|
---|
| 270 | }
|
---|
| 271 | }
|
---|
[4] | 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
[619] | 275 | # make hard link to file if supported by OS, otherwise copy the file
|
---|
| 276 | sub hard_link {
|
---|
[983] | 277 | my ($src, $dest) = @_;
|
---|
[4] | 278 |
|
---|
[619] | 279 | # remove trailing slashes from source and destination files
|
---|
| 280 | $src =~ s/[\\\/]+$//;
|
---|
| 281 | $dest =~ s/[\\\/]+$//;
|
---|
| 282 |
|
---|
| 283 | # a few sanity checks
|
---|
[812] | 284 | if (-e $dest) {
|
---|
| 285 | # destination file already exists
|
---|
| 286 | return;
|
---|
| 287 | }
|
---|
| 288 | elsif (!-e $src) {
|
---|
[619] | 289 | print STDERR "util::hard_link source file $src does not exist\n";
|
---|
| 290 | return;
|
---|
| 291 | }
|
---|
| 292 | elsif (-d $src) {
|
---|
| 293 | print STDERR "util::hard_link source $src is a directory\n";
|
---|
| 294 | return;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
| 298 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
| 299 |
|
---|
[983] | 300 | # link not supported on wondows
|
---|
| 301 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 302 | &File::Copy::copy ($src, $dest);
|
---|
| 303 |
|
---|
| 304 | } elsif (!link($src, $dest)) {
|
---|
[619] | 305 | print STDERR "util::hard_link: unable to create hard link. ";
|
---|
| 306 | print STDERR " Attempting to copy file: $src -> $dest\n";
|
---|
| 307 | &File::Copy::copy ($src, $dest);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[721] | 311 | # make soft link to file if supported by OS, otherwise return error
|
---|
| 312 | sub soft_link {
|
---|
[983] | 313 | my ($src, $dest) = @_;
|
---|
[619] | 314 |
|
---|
[721] | 315 | # remove trailing slashes from source and destination files
|
---|
| 316 | $src =~ s/[\\\/]+$//;
|
---|
| 317 | $dest =~ s/[\\\/]+$//;
|
---|
[619] | 318 |
|
---|
[721] | 319 | # a few sanity checks
|
---|
| 320 | if (!-e $src) {
|
---|
| 321 | print STDERR "util::soft_link source file $src does not exist\n";
|
---|
| 322 | return 0;
|
---|
| 323 | }
|
---|
[619] | 324 |
|
---|
[721] | 325 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
| 326 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
| 327 |
|
---|
[983] | 328 | if (!symlink($src, $dest))
|
---|
[721] | 329 | {
|
---|
| 330 | print STDERR "util::soft_link: unable to create soft link.";
|
---|
| 331 | return 0;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | return 1;
|
---|
| 335 | }
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 |
|
---|
| 339 |
|
---|
[4] | 340 | # updates a copy of a directory in some other part of the filesystem
|
---|
| 341 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
| 342 | # both $fromdir and $todir should be absolute paths
|
---|
| 343 | sub cachedir {
|
---|
| 344 | my ($fromdir, $todir, $verbosity) = @_;
|
---|
| 345 | $verbosity = 1 unless defined $verbosity;
|
---|
| 346 |
|
---|
| 347 | # use / for the directory separator, remove duplicate and
|
---|
| 348 | # trailing slashes
|
---|
| 349 | $fromdir=~s/[\\\/]+/\//g;
|
---|
| 350 | $fromdir=~s/[\\\/]+$//;
|
---|
| 351 | $todir=~s/[\\\/]+/\//g;
|
---|
| 352 | $todir=~s/[\\\/]+$//;
|
---|
| 353 |
|
---|
| 354 | &mk_all_dir ($todir);
|
---|
| 355 |
|
---|
| 356 | # get the directories in ascending order
|
---|
| 357 | if (!opendir (FROMDIR, $fromdir)) {
|
---|
| 358 | print STDERR "util::cachedir could not read directory $fromdir\n";
|
---|
| 359 | return;
|
---|
| 360 | }
|
---|
| 361 | my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
|
---|
| 362 | closedir (FROMDIR);
|
---|
| 363 |
|
---|
| 364 | if (!opendir (TODIR, $todir)) {
|
---|
| 365 | print STDERR "util::cacedir could not read directory $todir\n";
|
---|
| 366 | return;
|
---|
| 367 | }
|
---|
| 368 | my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
|
---|
| 369 | closedir (TODIR);
|
---|
| 370 |
|
---|
| 371 | my $fromi = 0;
|
---|
| 372 | my $toi = 0;
|
---|
| 373 |
|
---|
| 374 | while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
|
---|
| 375 | # print "fromi: $fromi toi: $toi\n";
|
---|
| 376 |
|
---|
| 377 | # see if we should delete a file/directory
|
---|
| 378 | # this should happen if the file/directory
|
---|
| 379 | # is not in the from list or if its a different
|
---|
| 380 | # size, or has an older timestamp
|
---|
| 381 | if ($toi < scalar(@todir)) {
|
---|
| 382 | if (($fromi >= scalar(@fromdir)) ||
|
---|
| 383 | ($todir[$toi] lt $fromdir[$fromi] ||
|
---|
| 384 | ($todir[$toi] eq $fromdir[$fromi] &&
|
---|
| 385 | &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
|
---|
| 386 | $verbosity)))) {
|
---|
| 387 |
|
---|
| 388 | # the files are different
|
---|
| 389 | &rm_r("$todir/$todir[$toi]");
|
---|
| 390 | splice(@todir, $toi, 1); # $toi stays the same
|
---|
| 391 |
|
---|
| 392 | } elsif ($todir[$toi] eq $fromdir[$fromi]) {
|
---|
| 393 | # the files are the same
|
---|
| 394 | # if it is a directory, check its contents
|
---|
| 395 | if (-d "$todir/$todir[$toi]") {
|
---|
| 396 | &cachedir ("$fromdir/$fromdir[$fromi]",
|
---|
| 397 | "$todir/$todir[$toi]", $verbosity);
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | $toi++;
|
---|
| 401 | $fromi++;
|
---|
| 402 | next;
|
---|
| 403 | }
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | # see if we should insert a file/directory
|
---|
| 407 | # we should insert a file/directory if there
|
---|
| 408 | # is no tofiles left or if the tofile does not exist
|
---|
| 409 | if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
|
---|
| 410 | $todir[$toi] gt $fromdir[$fromi])) {
|
---|
| 411 | &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
|
---|
| 412 | splice (@todir, $toi, 0, $fromdir[$fromi]);
|
---|
| 413 |
|
---|
| 414 | $toi++;
|
---|
| 415 | $fromi++;
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | # this function returns -1 if either file is not found
|
---|
| 421 | # assumes that $file1 and $file2 are absolute file names or
|
---|
| 422 | # in the current directory
|
---|
| 423 | # $file2 is allowed to be newer than $file1
|
---|
| 424 | sub differentfiles {
|
---|
| 425 | my ($file1, $file2, $verbosity) = @_;
|
---|
| 426 | $verbosity = 1 unless defined $verbosity;
|
---|
| 427 |
|
---|
| 428 | $file1 =~ s/\/+$//;
|
---|
| 429 | $file2 =~ s/\/+$//;
|
---|
| 430 |
|
---|
| 431 | my ($file1name) = $file1 =~ /\/([^\/]*)$/;
|
---|
| 432 | my ($file2name) = $file2 =~ /\/([^\/]*)$/;
|
---|
| 433 |
|
---|
| 434 | return -1 unless (-e $file1 && -e $file2);
|
---|
| 435 | if ($file1name ne $file2name) {
|
---|
| 436 | print STDERR "filenames are not the same\n" if ($verbosity >= 2);
|
---|
| 437 | return 1;
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | @file1stat = stat ($file1);
|
---|
| 441 | @file2stat = stat ($file2);
|
---|
| 442 |
|
---|
| 443 | if (-d $file1) {
|
---|
| 444 | if (! -d $file2) {
|
---|
| 445 | print STDERR "one file is a directory\n" if ($verbosity >= 2);
|
---|
| 446 | return 1;
|
---|
| 447 | }
|
---|
| 448 | return 0;
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | # both must be regular files
|
---|
| 452 | unless (-f $file1 && -f $file2) {
|
---|
| 453 | print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
|
---|
| 454 | return 1;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
| 457 | # the size of the files must be the same
|
---|
| 458 | if ($file1stat[7] != $file2stat[7]) {
|
---|
| 459 | print STDERR "different sized files\n" if ($verbosity >= 2);
|
---|
| 460 | return 1;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | # the second file cannot be older than the first
|
---|
| 464 | if ($file1stat[9] > $file2stat[9]) {
|
---|
| 465 | print STDERR "file is older\n" if ($verbosity >= 2);
|
---|
| 466 | return 1;
|
---|
| 467 | }
|
---|
| 468 |
|
---|
| 469 | return 0;
|
---|
| 470 | }
|
---|
| 471 |
|
---|
| 472 |
|
---|
| 473 | sub get_tmp_filename {
|
---|
| 474 | my $tmpdir = "$ENV{'GSDLHOME'}/tmp";
|
---|
| 475 | &mk_all_dir ($tmpdir) unless -e $tmpdir;
|
---|
| 476 |
|
---|
| 477 | my $count = 1000;
|
---|
| 478 | my $rand = int(rand $count);
|
---|
| 479 | while (-e "$tmpdir/F$rand") {
|
---|
| 480 | $rand = int(rand $count);
|
---|
| 481 | $count++;
|
---|
| 482 | }
|
---|
| 483 |
|
---|
| 484 | return "$tmpdir/F$rand";
|
---|
| 485 | }
|
---|
| 486 |
|
---|
| 487 |
|
---|
| 488 | sub filename_cat {
|
---|
| 489 | my (@filenames) = @_;
|
---|
| 490 | my $filename = join("/", @filenames);
|
---|
| 491 |
|
---|
| 492 | # remove duplicate slashes and remove the last slash
|
---|
[488] | 493 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 494 | $filename =~ s/[\\\/]+/\\/g;
|
---|
| 495 | } else {
|
---|
[836] | 496 | $filename =~ s/[\/]+/\//g;
|
---|
| 497 | # DB: want a filename abc\de.html to remain like this
|
---|
[488] | 498 | }
|
---|
| 499 | $filename =~ s/[\\\/]$//;
|
---|
[4] | 500 |
|
---|
| 501 | return $filename;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
[1454] | 504 | # returns 1 if filename1 and filename2 point to the same
|
---|
| 505 | # file or directory
|
---|
| 506 | sub filenames_equal {
|
---|
| 507 | my ($filename1, $filename2) = @_;
|
---|
| 508 |
|
---|
| 509 | # use filename_cat to clean up trailing slashes and
|
---|
| 510 | # multiple slashes
|
---|
| 511 | $filename1 = filename_cat ($filename1);
|
---|
| 512 | $filename1 = filename_cat ($filename1);
|
---|
| 513 |
|
---|
| 514 | # filenames not case sensitive on windows
|
---|
| 515 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 516 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
| 517 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
| 518 | }
|
---|
| 519 | return 1 if $filename1 eq $filename2;
|
---|
| 520 | return 0;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
[619] | 523 | sub get_os_dirsep {
|
---|
[4] | 524 |
|
---|
[619] | 525 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
| 526 | return "\\\\";
|
---|
| 527 | } else {
|
---|
| 528 | return "\\\/";
|
---|
| 529 | }
|
---|
| 530 | }
|
---|
| 531 |
|
---|
| 532 | sub get_re_dirsep {
|
---|
| 533 |
|
---|
| 534 | return "\\\\|\\\/";
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 |
|
---|
[4] | 538 | # if this is running on windows we want binaries to end in
|
---|
| 539 | # .exe, otherwise they don't have to end in any extension
|
---|
| 540 | sub get_os_exe {
|
---|
| 541 | return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
| 542 | return "";
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 |
|
---|
[86] | 546 | # test to see whether this is a big or little endian machine
|
---|
| 547 | sub is_little_endian {
|
---|
| 548 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
| 549 | }
|
---|
[4] | 550 |
|
---|
[86] | 551 |
|
---|
[135] | 552 | # will return the collection name if successful, "" otherwise
|
---|
| 553 | sub use_collection {
|
---|
[1454] | 554 | my ($collection, $collectdir) = @_;
|
---|
[135] | 555 |
|
---|
[1454] | 556 | if (!defined $collectdir || $collectdir eq "") {
|
---|
| 557 | $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
| 558 | }
|
---|
| 559 |
|
---|
[135] | 560 | # get and check the collection
|
---|
| 561 | if (!defined($collection) || $collection eq "") {
|
---|
| 562 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
| 563 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
| 564 | } else {
|
---|
| 565 | print STDERR "No collection specified\n";
|
---|
| 566 | return "";
|
---|
| 567 | }
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | if ($collection eq "modelcol") {
|
---|
| 571 | print STDERR "You can't use modelcol.\n";
|
---|
| 572 | return "";
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
| 576 | # are defined
|
---|
| 577 | $ENV{'GSDLCOLLECTION'} = $collection unless defined $ENV{'GSDLCOLLECTION'};
|
---|
[1454] | 578 | $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
|
---|
[135] | 579 |
|
---|
| 580 | # make sure this collection exists
|
---|
| 581 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
| 582 | print STDERR "Invalid collection ($collection).\n";
|
---|
| 583 | return "";
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | # everything is ready to go
|
---|
| 587 | return $collection;
|
---|
| 588 | }
|
---|
| 589 |
|
---|
[4] | 590 | 1;
|
---|