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 | ###########################################################################
|
---|
25 |
|
---|
26 | package util;
|
---|
27 |
|
---|
28 | use File::Copy;
|
---|
29 | use File::Basename;
|
---|
30 |
|
---|
31 | use strict;
|
---|
32 |
|
---|
33 |
|
---|
34 | # removes files (but not directories)
|
---|
35 | sub rm {
|
---|
36 | my (@files) = @_;
|
---|
37 |
|
---|
38 | my @filefiles = ();
|
---|
39 |
|
---|
40 | # make sure the files we want to delete exist
|
---|
41 | # and are regular files
|
---|
42 | foreach my $file (@files) {
|
---|
43 | if (!-e $file) {
|
---|
44 | print STDERR "util::rm $file does not exist\n";
|
---|
45 | } elsif ((!-f $file) && (!-l $file)) {
|
---|
46 | print STDERR "util::rm $file is not a regular (or symbolic) file\n";
|
---|
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 |
|
---|
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 = ();
|
---|
66 |
|
---|
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 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | # recursive removal
|
---|
82 | sub filtered_rm_r {
|
---|
83 | my ($files,$file_accept_re,$file_reject_re) = @_;
|
---|
84 |
|
---|
85 | my @files_array = (ref $files eq "ARRAY") ? @$files : ($files);
|
---|
86 |
|
---|
87 | # recursively remove the files
|
---|
88 | foreach my $file (@files_array) {
|
---|
89 | $file =~ s/[\/\\]+$//; # remove trailing slashes
|
---|
90 |
|
---|
91 | if (!-e $file) {
|
---|
92 | print STDERR "util::filtered_rm_r $file does not exist\n";
|
---|
93 |
|
---|
94 | } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
|
---|
95 | # get the contents of this directory
|
---|
96 | if (!opendir (INDIR, $file)) {
|
---|
97 | print STDERR "util::filtered_rm_r could not open directory $file\n";
|
---|
98 | } else {
|
---|
99 | my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
|
---|
100 | closedir (INDIR);
|
---|
101 |
|
---|
102 | # remove all the files in this directory
|
---|
103 | map {$_="$file/$_";} @filedir;
|
---|
104 | &filtered_rm_r (\@filedir,$file_accept_re,$file_reject_re);
|
---|
105 |
|
---|
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 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | } else {
|
---|
114 | next if (defined $file_reject_re && ($file =~ m/$file_reject_re/));
|
---|
115 |
|
---|
116 | if ((!defined $file_accept_re) || ($file =~ m/$file_accept_re/)) {
|
---|
117 | # remove this file
|
---|
118 | &rm ($file);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
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 |
|
---|
138 | # moves a file or a group of files
|
---|
139 | sub mv {
|
---|
140 | my $dest = pop (@_);
|
---|
141 | my (@srcfiles) = @_;
|
---|
142 |
|
---|
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
|
---|
158 | foreach my $file (@srcfiles) {
|
---|
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 |
|
---|
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
|
---|
193 | foreach my $file (@srcfiles) {
|
---|
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 |
|
---|
210 |
|
---|
211 | # recursively copies a file or group of files
|
---|
212 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
213 | # destination must be a directory - to copy one file to
|
---|
214 | # another use cp instead
|
---|
215 | sub cp_r {
|
---|
216 | my $dest = pop (@_);
|
---|
217 | my (@srcfiles) = @_;
|
---|
218 |
|
---|
219 | # a few sanity checks
|
---|
220 | if (scalar (@srcfiles) == 0) {
|
---|
221 | print STDERR "util::cp_r no destination directory given\n";
|
---|
222 | return;
|
---|
223 | } elsif (-f $dest) {
|
---|
224 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
225 | return;
|
---|
226 | }
|
---|
227 |
|
---|
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 |
|
---|
235 | # copy the files
|
---|
236 | foreach my $file (@srcfiles) {
|
---|
237 |
|
---|
238 | if (!-e $file) {
|
---|
239 | print STDERR "util::cp_r $file does not exist\n";
|
---|
240 |
|
---|
241 | } elsif (-d $file) {
|
---|
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);
|
---|
248 |
|
---|
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 {
|
---|
253 | my @filedir = readdir (INDIR);
|
---|
254 | closedir (INDIR);
|
---|
255 | foreach my $f (@filedir) {
|
---|
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 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | } else {
|
---|
264 | &cp($file, $dest);
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
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) = @_;
|
---|
275 |
|
---|
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 |
|
---|
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) = @_;
|
---|
331 |
|
---|
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 |
|
---|
386 | sub mk_dir {
|
---|
387 | my ($dir) = @_;
|
---|
388 |
|
---|
389 | my $store_umask = umask(0002);
|
---|
390 | my $mkdir_ok = mkdir ($dir, 0777);
|
---|
391 | umask($store_umask);
|
---|
392 |
|
---|
393 | if (!$mkdir_ok)
|
---|
394 | {
|
---|
395 | print STDERR "util::mk_dir could not create directory $dir\n";
|
---|
396 | return;
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
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.
|
---|
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;
|
---|
414 | foreach my $dirname (split ("/", $dir)) {
|
---|
415 | $dirsofar .= "/" unless $first;
|
---|
416 | $first = 0;
|
---|
417 |
|
---|
418 | $dirsofar .= $dirname;
|
---|
419 |
|
---|
420 | next if $dirname =~ /^(|[a-z]:)$/i;
|
---|
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 | }
|
---|
432 | }
|
---|
433 | }
|
---|
434 |
|
---|
435 | # make hard link to file if supported by OS, otherwise copy the file
|
---|
436 | sub hard_link {
|
---|
437 | my ($src, $dest, $verbosity) = @_;
|
---|
438 |
|
---|
439 | # remove trailing slashes from source and destination files
|
---|
440 | $src =~ s/[\\\/]+$//;
|
---|
441 | $dest =~ s/[\\\/]+$//;
|
---|
442 |
|
---|
443 | ## print STDERR "**** src = ", unicode::debug_unicode_string($src),"\n";
|
---|
444 | # a few sanity checks
|
---|
445 | if (-e $dest) {
|
---|
446 | # destination file already exists
|
---|
447 | return;
|
---|
448 | }
|
---|
449 | elsif (!-e $src) {
|
---|
450 | print STDERR "util::hard_link source file \"$src\" does not exist\n";
|
---|
451 | return 1;
|
---|
452 | }
|
---|
453 | elsif (-d $src) {
|
---|
454 | print STDERR "util::hard_link source \"$src\" is a directory\n";
|
---|
455 | return 1;
|
---|
456 | }
|
---|
457 |
|
---|
458 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
459 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
460 |
|
---|
461 |
|
---|
462 | if (!link($src, $dest)) {
|
---|
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 | }
|
---|
467 | &File::Copy::copy ($src, $dest);
|
---|
468 | }
|
---|
469 | return 0;
|
---|
470 | }
|
---|
471 |
|
---|
472 | # make soft link to file if supported by OS, otherwise copy file
|
---|
473 | sub soft_link {
|
---|
474 | my ($src, $dest, $ensure_paths_absolute) = @_;
|
---|
475 |
|
---|
476 | # remove trailing slashes from source and destination files
|
---|
477 | $src =~ s/[\\\/]+$//;
|
---|
478 | $dest =~ s/[\\\/]+$//;
|
---|
479 |
|
---|
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 |
|
---|
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 | }
|
---|
499 |
|
---|
500 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
501 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
502 |
|
---|
503 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
504 | # symlink not supported on windows
|
---|
505 | &File::Copy::copy ($src, $dest);
|
---|
506 |
|
---|
507 | } elsif (!eval {symlink($src, $dest)}) {
|
---|
508 | print STDERR "util::soft_link: unable to create soft link.\n";
|
---|
509 | return 0;
|
---|
510 | }
|
---|
511 |
|
---|
512 | return 1;
|
---|
513 | }
|
---|
514 |
|
---|
515 |
|
---|
516 |
|
---|
517 |
|
---|
518 | # updates a copy of a directory in some other part of the filesystem
|
---|
519 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
520 | # both $fromdir and $todir should be absolute paths
|
---|
521 | sub cachedir {
|
---|
522 | my ($fromdir, $todir, $verbosity) = @_;
|
---|
523 | $verbosity = 1 unless defined $verbosity;
|
---|
524 |
|
---|
525 | # use / for the directory separator, remove duplicate and
|
---|
526 | # trailing slashes
|
---|
527 | $fromdir=~s/[\\\/]+/\//g;
|
---|
528 | $fromdir=~s/[\\\/]+$//;
|
---|
529 | $todir=~s/[\\\/]+/\//g;
|
---|
530 | $todir=~s/[\\\/]+$//;
|
---|
531 |
|
---|
532 | &mk_all_dir ($todir);
|
---|
533 |
|
---|
534 | # get the directories in ascending order
|
---|
535 | if (!opendir (FROMDIR, $fromdir)) {
|
---|
536 | print STDERR "util::cachedir could not read directory $fromdir\n";
|
---|
537 | return;
|
---|
538 | }
|
---|
539 | my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
|
---|
540 | closedir (FROMDIR);
|
---|
541 |
|
---|
542 | if (!opendir (TODIR, $todir)) {
|
---|
543 | print STDERR "util::cacedir could not read directory $todir\n";
|
---|
544 | return;
|
---|
545 | }
|
---|
546 | my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
|
---|
547 | closedir (TODIR);
|
---|
548 |
|
---|
549 | my $fromi = 0;
|
---|
550 | my $toi = 0;
|
---|
551 |
|
---|
552 | while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
|
---|
553 | # print "fromi: $fromi toi: $toi\n";
|
---|
554 |
|
---|
555 | # see if we should delete a file/directory
|
---|
556 | # this should happen if the file/directory
|
---|
557 | # is not in the from list or if its a different
|
---|
558 | # size, or has an older timestamp
|
---|
559 | if ($toi < scalar(@todir)) {
|
---|
560 | if (($fromi >= scalar(@fromdir)) ||
|
---|
561 | ($todir[$toi] lt $fromdir[$fromi] ||
|
---|
562 | ($todir[$toi] eq $fromdir[$fromi] &&
|
---|
563 | &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
|
---|
564 | $verbosity)))) {
|
---|
565 |
|
---|
566 | # the files are different
|
---|
567 | &rm_r("$todir/$todir[$toi]");
|
---|
568 | splice(@todir, $toi, 1); # $toi stays the same
|
---|
569 |
|
---|
570 | } elsif ($todir[$toi] eq $fromdir[$fromi]) {
|
---|
571 | # the files are the same
|
---|
572 | # if it is a directory, check its contents
|
---|
573 | if (-d "$todir/$todir[$toi]") {
|
---|
574 | &cachedir ("$fromdir/$fromdir[$fromi]",
|
---|
575 | "$todir/$todir[$toi]", $verbosity);
|
---|
576 | }
|
---|
577 |
|
---|
578 | $toi++;
|
---|
579 | $fromi++;
|
---|
580 | next;
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | # see if we should insert a file/directory
|
---|
585 | # we should insert a file/directory if there
|
---|
586 | # is no tofiles left or if the tofile does not exist
|
---|
587 | if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
|
---|
588 | $todir[$toi] gt $fromdir[$fromi])) {
|
---|
589 | &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
|
---|
590 | splice (@todir, $toi, 0, $fromdir[$fromi]);
|
---|
591 |
|
---|
592 | $toi++;
|
---|
593 | $fromi++;
|
---|
594 | }
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | # this function returns -1 if either file is not found
|
---|
599 | # assumes that $file1 and $file2 are absolute file names or
|
---|
600 | # in the current directory
|
---|
601 | # $file2 is allowed to be newer than $file1
|
---|
602 | sub differentfiles {
|
---|
603 | my ($file1, $file2, $verbosity) = @_;
|
---|
604 | $verbosity = 1 unless defined $verbosity;
|
---|
605 |
|
---|
606 | $file1 =~ s/\/+$//;
|
---|
607 | $file2 =~ s/\/+$//;
|
---|
608 |
|
---|
609 | my ($file1name) = $file1 =~ /\/([^\/]*)$/;
|
---|
610 | my ($file2name) = $file2 =~ /\/([^\/]*)$/;
|
---|
611 |
|
---|
612 | return -1 unless (-e $file1 && -e $file2);
|
---|
613 | if ($file1name ne $file2name) {
|
---|
614 | print STDERR "filenames are not the same\n" if ($verbosity >= 2);
|
---|
615 | return 1;
|
---|
616 | }
|
---|
617 |
|
---|
618 | my @file1stat = stat ($file1);
|
---|
619 | my @file2stat = stat ($file2);
|
---|
620 |
|
---|
621 | if (-d $file1) {
|
---|
622 | if (! -d $file2) {
|
---|
623 | print STDERR "one file is a directory\n" if ($verbosity >= 2);
|
---|
624 | return 1;
|
---|
625 | }
|
---|
626 | return 0;
|
---|
627 | }
|
---|
628 |
|
---|
629 | # both must be regular files
|
---|
630 | unless (-f $file1 && -f $file2) {
|
---|
631 | print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
|
---|
632 | return 1;
|
---|
633 | }
|
---|
634 |
|
---|
635 | # the size of the files must be the same
|
---|
636 | if ($file1stat[7] != $file2stat[7]) {
|
---|
637 | print STDERR "different sized files\n" if ($verbosity >= 2);
|
---|
638 | return 1;
|
---|
639 | }
|
---|
640 |
|
---|
641 | # the second file cannot be older than the first
|
---|
642 | if ($file1stat[9] > $file2stat[9]) {
|
---|
643 | print STDERR "file is older\n" if ($verbosity >= 2);
|
---|
644 | return 1;
|
---|
645 | }
|
---|
646 |
|
---|
647 | return 0;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | sub get_tmp_filename
|
---|
652 | {
|
---|
653 | my $file_ext = shift(@_) || undef;
|
---|
654 |
|
---|
655 | my $opt_dot_file_ext = "";
|
---|
656 | if (defined $file_ext) {
|
---|
657 | if ($file_ext !~ m/\./) {
|
---|
658 | # no dot, so needs one added in at start
|
---|
659 | $opt_dot_file_ext = ".$file_ext"
|
---|
660 | }
|
---|
661 | else {
|
---|
662 | # allow for "extensions" such as _metadata.txt to be handled
|
---|
663 | # gracefully
|
---|
664 | $opt_dot_file_ext = $file_ext;
|
---|
665 | }
|
---|
666 | }
|
---|
667 |
|
---|
668 | my $tmpdir = filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
669 | &mk_all_dir ($tmpdir) unless -e $tmpdir;
|
---|
670 |
|
---|
671 | my $count = 1000;
|
---|
672 | my $rand = int(rand $count);
|
---|
673 | my $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
674 |
|
---|
675 | while (-e $full_tmp_filename) {
|
---|
676 | $rand = int(rand $count);
|
---|
677 | $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
678 | $count++;
|
---|
679 | }
|
---|
680 |
|
---|
681 | return $full_tmp_filename;
|
---|
682 | }
|
---|
683 |
|
---|
684 | sub get_timestamped_tmp_folder
|
---|
685 | {
|
---|
686 |
|
---|
687 | my $tmp_dirname;
|
---|
688 | if(defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
689 | $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
|
---|
690 | } elsif(defined $ENV{'GSDLHOME'}) {
|
---|
691 | $tmp_dirname = $ENV{'GSDLHOME'};
|
---|
692 | } else {
|
---|
693 | return undef;
|
---|
694 | }
|
---|
695 |
|
---|
696 | $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
|
---|
697 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
698 |
|
---|
699 | # add the timestamp into the path otherwise we can run into problems
|
---|
700 | # if documents have the same name
|
---|
701 | my $timestamp = time;
|
---|
702 | my $time_tmp_dirname = &util::filename_cat($tmp_dirname, $timestamp);
|
---|
703 | $tmp_dirname = $time_tmp_dirname;
|
---|
704 | my $i = 1;
|
---|
705 | while (-e $tmp_dirname) {
|
---|
706 | $tmp_dirname = "$time_tmp_dirname$i";
|
---|
707 | $i++;
|
---|
708 | }
|
---|
709 | &util::mk_dir($tmp_dirname);
|
---|
710 |
|
---|
711 | return $tmp_dirname;
|
---|
712 | }
|
---|
713 |
|
---|
714 | sub get_timestamped_tmp_filename_in_collection
|
---|
715 | {
|
---|
716 |
|
---|
717 | my ($input_filename, $output_ext) = @_;
|
---|
718 | # derive tmp filename from input filename
|
---|
719 | my ($tailname, $dirname, $suffix)
|
---|
720 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
721 |
|
---|
722 | # softlink to collection tmp dir
|
---|
723 | my $tmp_dirname = &util::get_timestamped_tmp_folder();
|
---|
724 | $tmp_dirname = $dirname unless defined $tmp_dirname;
|
---|
725 |
|
---|
726 | # following two steps copied from ConvertBinaryFile
|
---|
727 | # do we need them?? can't use them as is, as they use plugin methods.
|
---|
728 |
|
---|
729 | #$tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
|
---|
730 |
|
---|
731 | # URLEncode this since htmls with images where the html filename is utf8 don't seem
|
---|
732 | # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
|
---|
733 | # files on the filesystem.
|
---|
734 | #$tailname = &util::rename_file($tailname, $self->{'file_rename_method'}, "without_suffix");
|
---|
735 | if (defined $output_ext) {
|
---|
736 | $output_ext = ".$output_ext"; # add the dot
|
---|
737 | } else {
|
---|
738 | $output_ext = $suffix;
|
---|
739 | }
|
---|
740 | $output_ext= lc($output_ext);
|
---|
741 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$output_ext");
|
---|
742 |
|
---|
743 | return $tmp_filename;
|
---|
744 | }
|
---|
745 |
|
---|
746 | sub get_toplevel_tmp_dir
|
---|
747 | {
|
---|
748 | return filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
749 | }
|
---|
750 |
|
---|
751 |
|
---|
752 | sub filename_to_regex {
|
---|
753 | my $filename = shift (@_);
|
---|
754 |
|
---|
755 | # need to put single backslash back to double so that regex works
|
---|
756 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
757 | $filename =~ s/\\/\\\\/g;
|
---|
758 | }
|
---|
759 | return $filename;
|
---|
760 | }
|
---|
761 |
|
---|
762 | sub filename_cat {
|
---|
763 | my $first_file = shift(@_);
|
---|
764 | my (@filenames) = @_;
|
---|
765 |
|
---|
766 | # Useful for debugging
|
---|
767 | # -- might make sense to call caller(0) rather than (1)??
|
---|
768 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
769 | # print STDERR "Calling method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
770 |
|
---|
771 | # If first_file is not null or empty, then add it back into the list
|
---|
772 | if (defined $first_file && $first_file =~ /\S/) {
|
---|
773 | unshift(@filenames, $first_file);
|
---|
774 | }
|
---|
775 |
|
---|
776 | my $filename = join("/", @filenames);
|
---|
777 |
|
---|
778 | # remove duplicate slashes and remove the last slash
|
---|
779 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
780 | $filename =~ s/[\\\/]+/\\/g;
|
---|
781 | } else {
|
---|
782 | $filename =~ s/[\/]+/\//g;
|
---|
783 | # DB: want a filename abc\de.html to remain like this
|
---|
784 | }
|
---|
785 | $filename =~ s/[\\\/]$//;
|
---|
786 |
|
---|
787 | return $filename;
|
---|
788 | }
|
---|
789 |
|
---|
790 |
|
---|
791 | sub pathname_cat {
|
---|
792 | my $first_path = shift(@_);
|
---|
793 | my (@pathnames) = @_;
|
---|
794 |
|
---|
795 | # If first_path is not null or empty, then add it back into the list
|
---|
796 | if (defined $first_path && $first_path =~ /\S/) {
|
---|
797 | unshift(@pathnames, $first_path);
|
---|
798 | }
|
---|
799 |
|
---|
800 | my $join_char;
|
---|
801 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
802 | $join_char = ";";
|
---|
803 | } else {
|
---|
804 | $join_char = ":";
|
---|
805 | }
|
---|
806 |
|
---|
807 | my $pathname = join($join_char, @pathnames);
|
---|
808 |
|
---|
809 | # remove duplicate slashes
|
---|
810 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
811 | $pathname =~ s/[\\\/]+/\\/g;
|
---|
812 | } else {
|
---|
813 | $pathname =~ s/[\/]+/\//g;
|
---|
814 | # DB: want a pathname abc\de.html to remain like this
|
---|
815 | }
|
---|
816 |
|
---|
817 | return $pathname;
|
---|
818 | }
|
---|
819 |
|
---|
820 |
|
---|
821 | sub tidy_up_oid {
|
---|
822 | my ($OID) = @_;
|
---|
823 | if ($OID =~ /\./) {
|
---|
824 | print STDERR "Warning, identifier $OID contains periods (.), removing them\n";
|
---|
825 | $OID =~ s/\.//g; #remove any periods
|
---|
826 | }
|
---|
827 | if ($OID =~ /^\s.*\s$/) {
|
---|
828 | print STDERR "Warning, identifier $OID starts or ends with whitespace. Removing it\n";
|
---|
829 | # remove starting and trailing whitespace
|
---|
830 | $OID =~ s/^\s+//;
|
---|
831 | $OID =~ s/\s+$//;
|
---|
832 | }
|
---|
833 | if ($OID =~ /^[\d]*$/) {
|
---|
834 | print STDERR "Warning, identifier $OID contains only digits. Prepending 'D'.\n";
|
---|
835 | $OID = "D" . $OID;
|
---|
836 | }
|
---|
837 |
|
---|
838 | return $OID;
|
---|
839 | }
|
---|
840 | sub envvar_prepend {
|
---|
841 | my ($var,$val) = @_;
|
---|
842 |
|
---|
843 | # do not prepend any value/path that's already in the environment variable
|
---|
844 | if ($ENV{'GSDLOS'} =~ /^windows$/i)
|
---|
845 | {
|
---|
846 | my $escaped_val = $val;
|
---|
847 | $escaped_val =~ s/\\/\\\\/g; # escape any Windows backslashes for upcoming regex
|
---|
848 | if (!defined($ENV{$var})) {
|
---|
849 | $ENV{$var} = "$val";
|
---|
850 | }
|
---|
851 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
852 | $ENV{$var} = "$val;".$ENV{$var};
|
---|
853 | }
|
---|
854 | }
|
---|
855 | else {
|
---|
856 | if (!defined($ENV{$var})) {
|
---|
857 | $ENV{$var} = "$val";
|
---|
858 | }
|
---|
859 | elsif($ENV{$var} !~ m/$val/) {
|
---|
860 | $ENV{$var} = "$val:".$ENV{$var};
|
---|
861 | }
|
---|
862 | }
|
---|
863 | }
|
---|
864 |
|
---|
865 | sub envvar_append {
|
---|
866 | my ($var,$val) = @_;
|
---|
867 |
|
---|
868 | # do not append any value/path that's already in the environment variable
|
---|
869 | if ($ENV{'GSDLOS'} =~ /^windows$/i)
|
---|
870 | {
|
---|
871 | my $escaped_val = $val;
|
---|
872 | $escaped_val =~ s/\\/\\\\/g; # escape any Windows backslashes for upcoming regex
|
---|
873 | if (!defined($ENV{$var})) {
|
---|
874 | $ENV{$var} = "$val";
|
---|
875 | }
|
---|
876 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
877 | $ENV{$var} .= ";$val";
|
---|
878 | }
|
---|
879 | }
|
---|
880 | else {
|
---|
881 | if (!defined($ENV{$var})) {
|
---|
882 | $ENV{$var} = "$val";
|
---|
883 | }
|
---|
884 | elsif($ENV{$var} !~ m/$val/) {
|
---|
885 | $ENV{$var} .= ":$val";
|
---|
886 | }
|
---|
887 | }
|
---|
888 | }
|
---|
889 |
|
---|
890 |
|
---|
891 | # splits a filename into a prefix and a tail extension using the tail_re, or
|
---|
892 | # if that fails, splits on the file_extension . (dot)
|
---|
893 | sub get_prefix_and_tail_by_regex {
|
---|
894 |
|
---|
895 | my ($filename,$tail_re) = @_;
|
---|
896 |
|
---|
897 | my ($file_prefix,$file_ext) = ($filename =~ m/^(.*?)($tail_re)$/);
|
---|
898 | if ((!defined $file_prefix) || (!defined $file_ext)) {
|
---|
899 | ($file_prefix,$file_ext) = ($filename =~ m/^(.*)(\..*?)$/);
|
---|
900 | }
|
---|
901 |
|
---|
902 | return ($file_prefix,$file_ext);
|
---|
903 | }
|
---|
904 |
|
---|
905 | # get full path and file only path from a base_dir (which may be empty) and
|
---|
906 | # file (which may contain directories)
|
---|
907 | sub get_full_filenames {
|
---|
908 | my ($base_dir, $file) = @_;
|
---|
909 |
|
---|
910 | my $filename_full_path = $file;
|
---|
911 | # add on directory if present
|
---|
912 | $filename_full_path = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\S/;
|
---|
913 |
|
---|
914 | my $filename_no_path = $file;
|
---|
915 |
|
---|
916 | # remove directory if present
|
---|
917 | $filename_no_path =~ s/^.*[\/\\]//;
|
---|
918 | return ($filename_full_path, $filename_no_path);
|
---|
919 | }
|
---|
920 |
|
---|
921 | # returns the path of a file without the filename -- ie. the directory the file is in
|
---|
922 | sub filename_head {
|
---|
923 | my $filename = shift(@_);
|
---|
924 |
|
---|
925 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
926 | $filename =~ s/[^\\\\]*$//;
|
---|
927 | }
|
---|
928 | else {
|
---|
929 | $filename =~ s/[^\\\/]*$//;
|
---|
930 | }
|
---|
931 |
|
---|
932 | return $filename;
|
---|
933 | }
|
---|
934 |
|
---|
935 |
|
---|
936 | # returns 1 if filename1 and filename2 point to the same
|
---|
937 | # file or directory
|
---|
938 | sub filenames_equal {
|
---|
939 | my ($filename1, $filename2) = @_;
|
---|
940 |
|
---|
941 | # use filename_cat to clean up trailing slashes and
|
---|
942 | # multiple slashes
|
---|
943 | $filename1 = filename_cat ($filename1);
|
---|
944 | $filename2 = filename_cat ($filename2);
|
---|
945 |
|
---|
946 | # filenames not case sensitive on windows
|
---|
947 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
948 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
949 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
950 | }
|
---|
951 | return 1 if $filename1 eq $filename2;
|
---|
952 | return 0;
|
---|
953 | }
|
---|
954 |
|
---|
955 | sub filename_within_collection
|
---|
956 | {
|
---|
957 | my ($filename) = @_;
|
---|
958 |
|
---|
959 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
960 |
|
---|
961 | if (defined $collect_dir) {
|
---|
962 | my $dirsep = &util::get_dirsep();
|
---|
963 | if ($collect_dir !~ m/$dirsep$/) {
|
---|
964 | $collect_dir .= $dirsep;
|
---|
965 | }
|
---|
966 |
|
---|
967 | $collect_dir =~ s/\\/\\\\/g; # escape DOS style file separator
|
---|
968 |
|
---|
969 | # if from within GSDLCOLLECTDIR, then remove directory prefix
|
---|
970 | # so source_filename is realative to it. This is done to aid
|
---|
971 | # portability, i.e. the collection can be moved to somewhere
|
---|
972 | # else on the file system and the archives directory will still
|
---|
973 | # work. This is needed, for example in the applet version of
|
---|
974 | # GLI where GSDLHOME/collect on the server will be different to
|
---|
975 | # the collect directory of the remove user. Of course,
|
---|
976 | # GSDLCOLLECTDIR subsequently needs to be put back on to turn
|
---|
977 | # it back into a full pathname.
|
---|
978 |
|
---|
979 | if ($filename =~ /^$collect_dir(.*)$/) {
|
---|
980 | $filename = $1;
|
---|
981 | }
|
---|
982 | }
|
---|
983 |
|
---|
984 | return $filename;
|
---|
985 | }
|
---|
986 |
|
---|
987 | sub filename_is_absolute
|
---|
988 | {
|
---|
989 | my ($filename) = @_;
|
---|
990 |
|
---|
991 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
992 | return ($filename =~ m/^(\w:)?\\/);
|
---|
993 | }
|
---|
994 | else {
|
---|
995 | return ($filename =~ m/^\//);
|
---|
996 | }
|
---|
997 | }
|
---|
998 |
|
---|
999 |
|
---|
1000 | ## @method make_absolute()
|
---|
1001 | #
|
---|
1002 | # Ensure the given file path is absolute in respect to the given base path.
|
---|
1003 | #
|
---|
1004 | # @param $base_dir A string denoting the base path the given dir must be
|
---|
1005 | # absolute to.
|
---|
1006 | # @param $dir The directory to be made absolute as a string. Note that the
|
---|
1007 | # dir may already be absolute, in which case it will remain
|
---|
1008 | # unchanged.
|
---|
1009 | # @return The now absolute form of the directory as a string.
|
---|
1010 | #
|
---|
1011 | # @author John Thompson, DL Consulting Ltd.
|
---|
1012 | # @copy 2006 DL Consulting Ltd.
|
---|
1013 | #
|
---|
1014 | #used in buildcol.pl, doesn't work for all cases --kjdon
|
---|
1015 | sub make_absolute {
|
---|
1016 |
|
---|
1017 | my ($base_dir, $dir) = @_;
|
---|
1018 | ### print STDERR "dir = $dir\n";
|
---|
1019 | $dir =~ s/[\\\/]+/\//g;
|
---|
1020 | $dir = $base_dir . "/$dir" unless ($dir =~ m|^(\w:)?/|);
|
---|
1021 | $dir =~ s|^/tmp_mnt||;
|
---|
1022 | 1 while($dir =~ s|/[^/]*/\.\./|/|g);
|
---|
1023 | $dir =~ s|/[.][.]?/|/|g;
|
---|
1024 | $dir =~ tr|/|/|s;
|
---|
1025 | ### print STDERR "dir = $dir\n";
|
---|
1026 |
|
---|
1027 | return $dir;
|
---|
1028 | }
|
---|
1029 | ## make_absolute() ##
|
---|
1030 |
|
---|
1031 | sub get_dirsep {
|
---|
1032 |
|
---|
1033 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1034 | return "\\";
|
---|
1035 | } else {
|
---|
1036 | return "\/";
|
---|
1037 | }
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 | sub get_os_dirsep {
|
---|
1041 |
|
---|
1042 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1043 | return "\\\\";
|
---|
1044 | } else {
|
---|
1045 | return "\\\/";
|
---|
1046 | }
|
---|
1047 | }
|
---|
1048 |
|
---|
1049 | sub get_re_dirsep {
|
---|
1050 |
|
---|
1051 | return "\\\\|\\\/";
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 |
|
---|
1055 | sub get_dirsep_tail {
|
---|
1056 | my ($filename) = @_;
|
---|
1057 |
|
---|
1058 | # returns last part of directory or filename
|
---|
1059 | # On unix e.g. a/b.d => b.d
|
---|
1060 | # a/b/c => c
|
---|
1061 |
|
---|
1062 | my $dirsep = get_re_dirsep();
|
---|
1063 | my @dirs = split (/$dirsep/, $filename);
|
---|
1064 | my $tail = pop @dirs;
|
---|
1065 |
|
---|
1066 | # - caused problems under windows
|
---|
1067 | #my ($tail) = ($filename =~ m/^(?:.*?$dirsep)?(.*?)$/);
|
---|
1068 |
|
---|
1069 | return $tail;
|
---|
1070 | }
|
---|
1071 |
|
---|
1072 |
|
---|
1073 | # if this is running on windows we want binaries to end in
|
---|
1074 | # .exe, otherwise they don't have to end in any extension
|
---|
1075 | sub get_os_exe {
|
---|
1076 | return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
1077 | return "";
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 |
|
---|
1081 | # test to see whether this is a big or little endian machine
|
---|
1082 | sub is_little_endian
|
---|
1083 | {
|
---|
1084 | # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
|
---|
1085 | # 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
|
---|
1086 | # Otherwise, it's little endian
|
---|
1087 |
|
---|
1088 | #return 0 if $^O =~ /^darwin$/i;
|
---|
1089 | #return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
|
---|
1090 |
|
---|
1091 | # Going back to stating exactly whether the machine is little endian
|
---|
1092 | # or big endian, without any special case for Macs. Since for rata it comes
|
---|
1093 | # back with little endian and for shuttle with bigendian.
|
---|
1094 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 |
|
---|
1098 | # will return the collection name if successful, "" otherwise
|
---|
1099 | sub use_collection {
|
---|
1100 | my ($collection, $collectdir) = @_;
|
---|
1101 |
|
---|
1102 | if (!defined $collectdir || $collectdir eq "") {
|
---|
1103 | $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | # get and check the collection
|
---|
1107 | if (!defined($collection) || $collection eq "") {
|
---|
1108 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
1109 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
1110 | } else {
|
---|
1111 | print STDOUT "No collection specified\n";
|
---|
1112 | return "";
|
---|
1113 | }
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | if ($collection eq "modelcol") {
|
---|
1117 | print STDOUT "You can't use modelcol.\n";
|
---|
1118 | return "";
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
1122 | # are defined
|
---|
1123 | $ENV{'GSDLCOLLECTION'} = $collection;
|
---|
1124 | $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
|
---|
1125 |
|
---|
1126 | # make sure this collection exists
|
---|
1127 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
1128 | print STDOUT "Invalid collection ($collection).\n";
|
---|
1129 | return "";
|
---|
1130 | }
|
---|
1131 |
|
---|
1132 | # everything is ready to go
|
---|
1133 | return $collection;
|
---|
1134 | }
|
---|
1135 |
|
---|
1136 | sub get_current_collection_name {
|
---|
1137 | return $ENV{'GSDLCOLLECTION'};
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 |
|
---|
1141 | # will return the collection name if successful, "" otherwise.
|
---|
1142 | # Like use_collection (above) but for greenstone 3 (taking account of site level)
|
---|
1143 |
|
---|
1144 | sub use_site_collection {
|
---|
1145 | my ($site, $collection, $collectdir) = @_;
|
---|
1146 |
|
---|
1147 | if (!defined $collectdir || $collectdir eq "") {
|
---|
1148 | die "GSDL3HOME not set.\n" unless defined $ENV{'GSDL3HOME'};
|
---|
1149 | $collectdir = &filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect");
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | # collectdir explicitly set by this point (using $site variable if required).
|
---|
1153 | # Can call "old" gsdl2 use_collection now.
|
---|
1154 |
|
---|
1155 | return use_collection($collection,$collectdir);
|
---|
1156 | }
|
---|
1157 |
|
---|
1158 |
|
---|
1159 |
|
---|
1160 | sub locate_config_file
|
---|
1161 | {
|
---|
1162 | my ($file) = @_;
|
---|
1163 |
|
---|
1164 | my $locations = locate_config_files($file);
|
---|
1165 |
|
---|
1166 | return shift @$locations; # returns undef if 'locations' is empty
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 |
|
---|
1170 | sub locate_config_files
|
---|
1171 | {
|
---|
1172 | my ($file) = @_;
|
---|
1173 |
|
---|
1174 | my @locations = ();
|
---|
1175 |
|
---|
1176 | if (-e $file) {
|
---|
1177 | # Clearly specified (most likely full filename)
|
---|
1178 | # No need to hunt in 'etc' directories, return value unchanged
|
---|
1179 | push(@locations,$file);
|
---|
1180 | }
|
---|
1181 | else {
|
---|
1182 | # Check for collection specific one before looking in global GSDL 'etc'
|
---|
1183 | if (defined $ENV{'GSDLCOLLECTDIR'} && $ENV{'GSDLCOLLECTDIR'} ne "") {
|
---|
1184 | my $test_collect_etc_filename
|
---|
1185 | = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $file);
|
---|
1186 |
|
---|
1187 | if (-e $test_collect_etc_filename) {
|
---|
1188 | push(@locations,$test_collect_etc_filename);
|
---|
1189 | }
|
---|
1190 | }
|
---|
1191 | my $test_main_etc_filename
|
---|
1192 | = &util::filename_cat($ENV{'GSDLHOME'},"etc", $file);
|
---|
1193 | if (-e $test_main_etc_filename) {
|
---|
1194 | push(@locations,$test_main_etc_filename);
|
---|
1195 | }
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 | return \@locations;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 |
|
---|
1202 | sub hyperlink_text
|
---|
1203 | {
|
---|
1204 | my ($text) = @_;
|
---|
1205 |
|
---|
1206 | $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
|
---|
1207 | $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
|
---|
1208 |
|
---|
1209 | return $text;
|
---|
1210 | }
|
---|
1211 |
|
---|
1212 |
|
---|
1213 | # A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
|
---|
1214 | # Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
|
---|
1215 | sub is_dir_empty
|
---|
1216 | {
|
---|
1217 | my ($path) = @_;
|
---|
1218 | opendir DIR, $path;
|
---|
1219 | while(my $entry = readdir DIR) {
|
---|
1220 | next if($entry =~ /^\.\.?$/);
|
---|
1221 | closedir DIR;
|
---|
1222 | return 0;
|
---|
1223 | }
|
---|
1224 | closedir DIR;
|
---|
1225 | return 1;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | # Returns the given filename converted using either URL encoding or base64
|
---|
1229 | # encoding, as specified by $rename_method. If the given filename has no suffix
|
---|
1230 | # (if it is just the tailname), then $no_suffix should be some defined value.
|
---|
1231 | # rename_method can be url, none, base64
|
---|
1232 | sub rename_file {
|
---|
1233 | my ($filename, $rename_method, $no_suffix) = @_;
|
---|
1234 |
|
---|
1235 | if(!$filename) { # undefined or empty string
|
---|
1236 | return $filename;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | if (!$rename_method) {
|
---|
1240 | print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
|
---|
1241 | # Debugging information
|
---|
1242 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
1243 | # print STDERR "Called from method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
1244 | $rename_method = "url";
|
---|
1245 | } elsif($rename_method eq "none") {
|
---|
1246 | return $filename; # would have already been renamed
|
---|
1247 | }
|
---|
1248 |
|
---|
1249 | # No longer replace spaces with underscores, since underscores mess with incremental rebuild
|
---|
1250 | ### Replace spaces with underscore. Do this first else it can go wrong below when getting tailname
|
---|
1251 | ###$filename =~ s/ /_/g;
|
---|
1252 |
|
---|
1253 | my ($tailname,$dirname,$suffix);
|
---|
1254 | if($no_suffix) { # given a tailname, no suffix
|
---|
1255 | ($tailname,$dirname) = File::Basename::fileparse($filename);
|
---|
1256 | }
|
---|
1257 | else {
|
---|
1258 | ($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.(?:[^\\.]+?)\$");
|
---|
1259 | }
|
---|
1260 | $suffix = "" if !$suffix;
|
---|
1261 |
|
---|
1262 | if ($rename_method eq "url") {
|
---|
1263 | $tailname = &unicode::url_encode($tailname);
|
---|
1264 | }
|
---|
1265 | elsif ($rename_method eq "base64") {
|
---|
1266 | $tailname = &unicode::base64_encode($tailname);
|
---|
1267 | $tailname =~ s/\s*//sg; # for some reason it adds spaces not just at end but also in middle
|
---|
1268 | }
|
---|
1269 |
|
---|
1270 | $filename = "$tailname$suffix";
|
---|
1271 | $filename = "$dirname$filename" if ($dirname ne "./" && $dirname ne ".\\");
|
---|
1272 |
|
---|
1273 | return $filename;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files
|
---|
1278 | sub rename_ldb_or_bdb_file {
|
---|
1279 | my ($filename_no_ext) = @_;
|
---|
1280 |
|
---|
1281 | my $new_filename = "$filename_no_ext.gdb";
|
---|
1282 | return if (-f $new_filename); # if the file has the right extension, don't need to do anything
|
---|
1283 | # try ldb
|
---|
1284 | my $old_filename = "$filename_no_ext.ldb";
|
---|
1285 |
|
---|
1286 | if (-f $old_filename) {
|
---|
1287 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
1288 | rename ($old_filename, $new_filename)
|
---|
1289 | || print STDERR "Rename failed: $!\n";
|
---|
1290 | return;
|
---|
1291 | }
|
---|
1292 | # try bdb
|
---|
1293 | $old_filename = "$filename_no_ext.bdb";
|
---|
1294 | if (-f $old_filename) {
|
---|
1295 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
1296 | rename ($old_filename, $new_filename)
|
---|
1297 | || print STDERR "Rename failed: $!\n";
|
---|
1298 | return;
|
---|
1299 | }
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 |
|
---|
1303 | # Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file.
|
---|
1304 | # By default, /greenstone3 for GS3 or /greenstone for GS2.
|
---|
1305 | sub get_greenstone_url_prefix() {
|
---|
1306 | # if already set on a previous occasion, just return that
|
---|
1307 | # (Don't want to keep repeating this: cost of re-opening and scanning files.)
|
---|
1308 | return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
|
---|
1309 |
|
---|
1310 | my ($configfile, $urlprefix, $defaultUrlprefix);
|
---|
1311 | my @propertynames = ();
|
---|
1312 |
|
---|
1313 | if($ENV{'GSDL3SRCHOME'}) {
|
---|
1314 | $defaultUrlprefix = "/greenstone3";
|
---|
1315 | $configfile = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
|
---|
1316 | push(@propertynames, qw/path\s*\=/);
|
---|
1317 | } else {
|
---|
1318 | $defaultUrlprefix = "/greenstone";
|
---|
1319 | $configfile = &util::filename_cat($ENV{'GSDLHOME'}, "cgi-bin", "gsdlsite.cfg");
|
---|
1320 | push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other
|
---|
1321 | }
|
---|
1322 |
|
---|
1323 | $urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
|
---|
1324 |
|
---|
1325 | if(!$urlprefix) { # no values found for URL prefix, use default values
|
---|
1326 | $urlprefix = $defaultUrlprefix;
|
---|
1327 | } else {
|
---|
1328 | #gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
|
---|
1329 | $urlprefix =~ s/^\///; # remove the starting slash
|
---|
1330 | my @dirs = split(/(\\|\/)/, $urlprefix);
|
---|
1331 | $urlprefix = shift(@dirs);
|
---|
1332 |
|
---|
1333 | if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
|
---|
1334 | $urlprefix = "/$urlprefix";
|
---|
1335 | }
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | # set for the future
|
---|
1339 | $ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
|
---|
1340 | # print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
|
---|
1341 | return $urlprefix;
|
---|
1342 | }
|
---|
1343 |
|
---|
1344 |
|
---|
1345 | # Given a config file (xml or java properties file) and a list/array of regular expressions
|
---|
1346 | # that represent property names to match on, this function will return the value for the 1st
|
---|
1347 | # matching property name. If the return value is undefined, no matching property was found.
|
---|
1348 | sub extract_propvalue_from_file() {
|
---|
1349 | my ($configfile, $propertynames) = @_;
|
---|
1350 |
|
---|
1351 | my $value;
|
---|
1352 | unless(open(FIN, "<$configfile")) {
|
---|
1353 | print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
|
---|
1354 | return $value; # not initialised
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | # Read the entire file at once, as one single line, then close it
|
---|
1358 | my $filecontents;
|
---|
1359 | {
|
---|
1360 | local $/ = undef;
|
---|
1361 | $filecontents = <FIN>;
|
---|
1362 | }
|
---|
1363 | close(FIN);
|
---|
1364 |
|
---|
1365 | foreach my $regex (@$propertynames) {
|
---|
1366 | ($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
|
---|
1367 | if($value) {
|
---|
1368 | $value =~ s/^\"//; # remove any startquotes
|
---|
1369 | $value =~ s/\".*$//; # remove the 1st endquotes (if any) followed by any xml
|
---|
1370 | last; # found value for a matching property, break from loop
|
---|
1371 | }
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | return $value;
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | # Subroutine that sources setup.bash, given GSDLHOME and GSDLOS and
|
---|
1378 | # given that perllib is in @INC in order to invoke this subroutine.
|
---|
1379 | # Call as follows -- after setting up INC to include perllib and
|
---|
1380 | # after setting up GSDLHOME and GSDLOS:
|
---|
1381 | #
|
---|
1382 | # require util;
|
---|
1383 | # &util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
|
---|
1384 | #
|
---|
1385 | sub setup_greenstone_env() {
|
---|
1386 | my ($GSDLHOME, $GSDLOS) = @_;
|
---|
1387 |
|
---|
1388 | #my %env_map = ();
|
---|
1389 | # Get the localised ENV settings of running a localised source setup.bash
|
---|
1390 | # and put it into the ENV here. Need to clear GSDLHOME before running setup
|
---|
1391 | #my $perl_command = "(cd $GSDLHOME; export GSDLHOME=; . ./setup.bash > /dev/null; env)";
|
---|
1392 | my $perl_command = "(cd $GSDLHOME; /bin/bash -c \"export GSDLHOME=; source setup.bash > /dev/null; env\")";
|
---|
1393 | if($GSDLOS =~ m/windows/i) {
|
---|
1394 | #$perl_command = "cmd /C \"cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set\"";
|
---|
1395 | $perl_command = "(cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set)";
|
---|
1396 | }
|
---|
1397 | if (!open(PIN, "$perl_command |")) {
|
---|
1398 | print STDERR ("Unable to execute command: $perl_command. $!\n");
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | while (defined (my $perl_output_line = <PIN>)) {
|
---|
1402 | my($key,$value) = ($perl_output_line =~ m/^([^=]*)[=](.*)$/);
|
---|
1403 | #$env_map{$key}=$value;
|
---|
1404 | $ENV{$key}=$value;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | # If any keys in $ENV don't occur in Greenstone's localised env
|
---|
1408 | # (stored in $env_map), delete those entries from $ENV
|
---|
1409 | #foreach $key (keys %ENV) {
|
---|
1410 | # if(!defined $env_map{$key}) {
|
---|
1411 | # print STDOUT "**** DELETING ENV KEY: $key\tVALUE: $ENV{'$key'}\n";
|
---|
1412 | # delete $ENV{$key}; # del $ENV(key, value) pair
|
---|
1413 | # }
|
---|
1414 | #}
|
---|
1415 | #undef %env_map;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | 1;
|
---|