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 strict;
|
---|
29 |
|
---|
30 | use Encode;
|
---|
31 | use File::Copy;
|
---|
32 | use File::Basename;
|
---|
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 |
|
---|
505 | # symlink not supported on windows
|
---|
506 | &File::Copy::copy ($src, $dest);
|
---|
507 |
|
---|
508 | } elsif (!eval {symlink($src, $dest)}) {
|
---|
509 | print STDERR "util::soft_link: unable to create soft link.\n";
|
---|
510 | return 0;
|
---|
511 | }
|
---|
512 |
|
---|
513 | return 1;
|
---|
514 | }
|
---|
515 |
|
---|
516 | # Primarily for filenames generated by processing
|
---|
517 | # content of HTML files (which are mapped to UTF-8 internally)
|
---|
518 | #
|
---|
519 | # To turn this into an octet string that really exists on the file
|
---|
520 | # system:
|
---|
521 | # 1. don't need to do anything special for Unix-based systems
|
---|
522 | # (as underlying file system is byte-code)
|
---|
523 | # 2. need to map to short DOS filenames for Windows
|
---|
524 |
|
---|
525 | sub utf8_to_real_filename
|
---|
526 | {
|
---|
527 | my ($utf8_filename) = @_;
|
---|
528 |
|
---|
529 | my $real_filename;
|
---|
530 |
|
---|
531 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
532 | require Win32;
|
---|
533 |
|
---|
534 | print STDERR "***** utf8 filename = $utf8_filename\n\n\n";
|
---|
535 |
|
---|
536 | my $unicode_filename = decode("utf8",$utf8_filename);
|
---|
537 | $real_filename = Win32::GetShortPathName($unicode_filename);
|
---|
538 | }
|
---|
539 | else {
|
---|
540 | $real_filename = $utf8_filename;
|
---|
541 | }
|
---|
542 |
|
---|
543 | return $real_filename;
|
---|
544 | }
|
---|
545 |
|
---|
546 |
|
---|
547 | sub fd_exists
|
---|
548 | {
|
---|
549 | my $filename_full_path = shift @_;
|
---|
550 | my $test_op = shift @_ || "-e";
|
---|
551 |
|
---|
552 | # By default tests for existance of file or directory (-e)
|
---|
553 | # Can be made more specific by providing second parameter (e.g. -f or -d)
|
---|
554 |
|
---|
555 | my $exists = 0;
|
---|
556 |
|
---|
557 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
558 | require Win32;
|
---|
559 | my $filename_short_path = Win32::GetShortPathName($filename_full_path);
|
---|
560 | if (!defined $filename_short_path) {
|
---|
561 | # Was probably still in UTF8 form (not what is needed on Windows)
|
---|
562 | my $unicode_filename_full_path = eval "decode(\"utf8\",\$filename_full_path)";
|
---|
563 | if (defined $unicode_filename_full_path) {
|
---|
564 | $filename_short_path = Win32::GetShortPathName($unicode_filename_full_path);
|
---|
565 | }
|
---|
566 | }
|
---|
567 | $filename_full_path = $filename_short_path;
|
---|
568 | }
|
---|
569 |
|
---|
570 | if (defined $filename_full_path) {
|
---|
571 | $exists = eval "($test_op \$filename_full_path)";
|
---|
572 | }
|
---|
573 |
|
---|
574 | return $exists;
|
---|
575 | }
|
---|
576 |
|
---|
577 | sub file_exists
|
---|
578 | {
|
---|
579 | my ($filename_full_path) = @_;
|
---|
580 |
|
---|
581 | return fd_exists($filename_full_path,"-f");
|
---|
582 | }
|
---|
583 |
|
---|
584 | sub dir_exists
|
---|
585 | {
|
---|
586 | my ($filename_full_path) = @_;
|
---|
587 |
|
---|
588 | return fd_exists($filename_full_path,"-d");
|
---|
589 | }
|
---|
590 |
|
---|
591 |
|
---|
592 |
|
---|
593 | # updates a copy of a directory in some other part of the filesystem
|
---|
594 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
595 | # both $fromdir and $todir should be absolute paths
|
---|
596 | sub cachedir {
|
---|
597 | my ($fromdir, $todir, $verbosity) = @_;
|
---|
598 | $verbosity = 1 unless defined $verbosity;
|
---|
599 |
|
---|
600 | # use / for the directory separator, remove duplicate and
|
---|
601 | # trailing slashes
|
---|
602 | $fromdir=~s/[\\\/]+/\//g;
|
---|
603 | $fromdir=~s/[\\\/]+$//;
|
---|
604 | $todir=~s/[\\\/]+/\//g;
|
---|
605 | $todir=~s/[\\\/]+$//;
|
---|
606 |
|
---|
607 | &mk_all_dir ($todir);
|
---|
608 |
|
---|
609 | # get the directories in ascending order
|
---|
610 | if (!opendir (FROMDIR, $fromdir)) {
|
---|
611 | print STDERR "util::cachedir could not read directory $fromdir\n";
|
---|
612 | return;
|
---|
613 | }
|
---|
614 | my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
|
---|
615 | closedir (FROMDIR);
|
---|
616 |
|
---|
617 | if (!opendir (TODIR, $todir)) {
|
---|
618 | print STDERR "util::cacedir could not read directory $todir\n";
|
---|
619 | return;
|
---|
620 | }
|
---|
621 | my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
|
---|
622 | closedir (TODIR);
|
---|
623 |
|
---|
624 | my $fromi = 0;
|
---|
625 | my $toi = 0;
|
---|
626 |
|
---|
627 | while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
|
---|
628 | # print "fromi: $fromi toi: $toi\n";
|
---|
629 |
|
---|
630 | # see if we should delete a file/directory
|
---|
631 | # this should happen if the file/directory
|
---|
632 | # is not in the from list or if its a different
|
---|
633 | # size, or has an older timestamp
|
---|
634 | if ($toi < scalar(@todir)) {
|
---|
635 | if (($fromi >= scalar(@fromdir)) ||
|
---|
636 | ($todir[$toi] lt $fromdir[$fromi] ||
|
---|
637 | ($todir[$toi] eq $fromdir[$fromi] &&
|
---|
638 | &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
|
---|
639 | $verbosity)))) {
|
---|
640 |
|
---|
641 | # the files are different
|
---|
642 | &rm_r("$todir/$todir[$toi]");
|
---|
643 | splice(@todir, $toi, 1); # $toi stays the same
|
---|
644 |
|
---|
645 | } elsif ($todir[$toi] eq $fromdir[$fromi]) {
|
---|
646 | # the files are the same
|
---|
647 | # if it is a directory, check its contents
|
---|
648 | if (-d "$todir/$todir[$toi]") {
|
---|
649 | &cachedir ("$fromdir/$fromdir[$fromi]",
|
---|
650 | "$todir/$todir[$toi]", $verbosity);
|
---|
651 | }
|
---|
652 |
|
---|
653 | $toi++;
|
---|
654 | $fromi++;
|
---|
655 | next;
|
---|
656 | }
|
---|
657 | }
|
---|
658 |
|
---|
659 | # see if we should insert a file/directory
|
---|
660 | # we should insert a file/directory if there
|
---|
661 | # is no tofiles left or if the tofile does not exist
|
---|
662 | if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
|
---|
663 | $todir[$toi] gt $fromdir[$fromi])) {
|
---|
664 | &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
|
---|
665 | splice (@todir, $toi, 0, $fromdir[$fromi]);
|
---|
666 |
|
---|
667 | $toi++;
|
---|
668 | $fromi++;
|
---|
669 | }
|
---|
670 | }
|
---|
671 | }
|
---|
672 |
|
---|
673 | # this function returns -1 if either file is not found
|
---|
674 | # assumes that $file1 and $file2 are absolute file names or
|
---|
675 | # in the current directory
|
---|
676 | # $file2 is allowed to be newer than $file1
|
---|
677 | sub differentfiles {
|
---|
678 | my ($file1, $file2, $verbosity) = @_;
|
---|
679 | $verbosity = 1 unless defined $verbosity;
|
---|
680 |
|
---|
681 | $file1 =~ s/\/+$//;
|
---|
682 | $file2 =~ s/\/+$//;
|
---|
683 |
|
---|
684 | my ($file1name) = $file1 =~ /\/([^\/]*)$/;
|
---|
685 | my ($file2name) = $file2 =~ /\/([^\/]*)$/;
|
---|
686 |
|
---|
687 | return -1 unless (-e $file1 && -e $file2);
|
---|
688 | if ($file1name ne $file2name) {
|
---|
689 | print STDERR "filenames are not the same\n" if ($verbosity >= 2);
|
---|
690 | return 1;
|
---|
691 | }
|
---|
692 |
|
---|
693 | my @file1stat = stat ($file1);
|
---|
694 | my @file2stat = stat ($file2);
|
---|
695 |
|
---|
696 | if (-d $file1) {
|
---|
697 | if (! -d $file2) {
|
---|
698 | print STDERR "one file is a directory\n" if ($verbosity >= 2);
|
---|
699 | return 1;
|
---|
700 | }
|
---|
701 | return 0;
|
---|
702 | }
|
---|
703 |
|
---|
704 | # both must be regular files
|
---|
705 | unless (-f $file1 && -f $file2) {
|
---|
706 | print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
|
---|
707 | return 1;
|
---|
708 | }
|
---|
709 |
|
---|
710 | # the size of the files must be the same
|
---|
711 | if ($file1stat[7] != $file2stat[7]) {
|
---|
712 | print STDERR "different sized files\n" if ($verbosity >= 2);
|
---|
713 | return 1;
|
---|
714 | }
|
---|
715 |
|
---|
716 | # the second file cannot be older than the first
|
---|
717 | if ($file1stat[9] > $file2stat[9]) {
|
---|
718 | print STDERR "file is older\n" if ($verbosity >= 2);
|
---|
719 | return 1;
|
---|
720 | }
|
---|
721 |
|
---|
722 | return 0;
|
---|
723 | }
|
---|
724 |
|
---|
725 |
|
---|
726 | sub get_tmp_filename
|
---|
727 | {
|
---|
728 | my $file_ext = shift(@_) || undef;
|
---|
729 |
|
---|
730 | my $opt_dot_file_ext = "";
|
---|
731 | if (defined $file_ext) {
|
---|
732 | if ($file_ext !~ m/\./) {
|
---|
733 | # no dot, so needs one added in at start
|
---|
734 | $opt_dot_file_ext = ".$file_ext"
|
---|
735 | }
|
---|
736 | else {
|
---|
737 | # allow for "extensions" such as _metadata.txt to be handled
|
---|
738 | # gracefully
|
---|
739 | $opt_dot_file_ext = $file_ext;
|
---|
740 | }
|
---|
741 | }
|
---|
742 |
|
---|
743 | my $tmpdir = filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
744 | &mk_all_dir ($tmpdir) unless -e $tmpdir;
|
---|
745 |
|
---|
746 | my $count = 1000;
|
---|
747 | my $rand = int(rand $count);
|
---|
748 | my $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
749 |
|
---|
750 | while (-e $full_tmp_filename) {
|
---|
751 | $rand = int(rand $count);
|
---|
752 | $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
|
---|
753 | $count++;
|
---|
754 | }
|
---|
755 |
|
---|
756 | return $full_tmp_filename;
|
---|
757 | }
|
---|
758 |
|
---|
759 | sub get_timestamped_tmp_folder
|
---|
760 | {
|
---|
761 |
|
---|
762 | my $tmp_dirname;
|
---|
763 | if(defined $ENV{'GSDLCOLLECTDIR'}) {
|
---|
764 | $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
|
---|
765 | } elsif(defined $ENV{'GSDLHOME'}) {
|
---|
766 | $tmp_dirname = $ENV{'GSDLHOME'};
|
---|
767 | } else {
|
---|
768 | return undef;
|
---|
769 | }
|
---|
770 |
|
---|
771 | $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
|
---|
772 | &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
|
---|
773 |
|
---|
774 | # add the timestamp into the path otherwise we can run into problems
|
---|
775 | # if documents have the same name
|
---|
776 | my $timestamp = time;
|
---|
777 | my $time_tmp_dirname = &util::filename_cat($tmp_dirname, $timestamp);
|
---|
778 | $tmp_dirname = $time_tmp_dirname;
|
---|
779 | my $i = 1;
|
---|
780 | while (-e $tmp_dirname) {
|
---|
781 | $tmp_dirname = "$time_tmp_dirname$i";
|
---|
782 | $i++;
|
---|
783 | }
|
---|
784 | &util::mk_dir($tmp_dirname);
|
---|
785 |
|
---|
786 | return $tmp_dirname;
|
---|
787 | }
|
---|
788 |
|
---|
789 | sub get_timestamped_tmp_filename_in_collection
|
---|
790 | {
|
---|
791 |
|
---|
792 | my ($input_filename, $output_ext) = @_;
|
---|
793 | # derive tmp filename from input filename
|
---|
794 | my ($tailname, $dirname, $suffix)
|
---|
795 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
796 |
|
---|
797 | # softlink to collection tmp dir
|
---|
798 | my $tmp_dirname = &util::get_timestamped_tmp_folder();
|
---|
799 | $tmp_dirname = $dirname unless defined $tmp_dirname;
|
---|
800 |
|
---|
801 | # following two steps copied from ConvertBinaryFile
|
---|
802 | # do we need them?? can't use them as is, as they use plugin methods.
|
---|
803 |
|
---|
804 | #$tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
|
---|
805 |
|
---|
806 | # URLEncode this since htmls with images where the html filename is utf8 don't seem
|
---|
807 | # to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
|
---|
808 | # files on the filesystem.
|
---|
809 | #$tailname = &util::rename_file($tailname, $self->{'file_rename_method'}, "without_suffix");
|
---|
810 | if (defined $output_ext) {
|
---|
811 | $output_ext = ".$output_ext"; # add the dot
|
---|
812 | } else {
|
---|
813 | $output_ext = $suffix;
|
---|
814 | }
|
---|
815 | $output_ext= lc($output_ext);
|
---|
816 | my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$output_ext");
|
---|
817 |
|
---|
818 | return $tmp_filename;
|
---|
819 | }
|
---|
820 |
|
---|
821 | sub get_toplevel_tmp_dir
|
---|
822 | {
|
---|
823 | return filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
824 | }
|
---|
825 |
|
---|
826 |
|
---|
827 | sub filename_to_regex {
|
---|
828 | my $filename = shift (@_);
|
---|
829 |
|
---|
830 | # need to put single backslash back to double so that regex works
|
---|
831 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
832 | $filename =~ s/\\/\\\\/g;
|
---|
833 | }
|
---|
834 | return $filename;
|
---|
835 | }
|
---|
836 |
|
---|
837 | sub filename_cat {
|
---|
838 | my $first_file = shift(@_);
|
---|
839 | my (@filenames) = @_;
|
---|
840 |
|
---|
841 | # Useful for debugging
|
---|
842 | # -- might make sense to call caller(0) rather than (1)??
|
---|
843 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
844 | # print STDERR "Calling method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
845 |
|
---|
846 | # If first_file is not null or empty, then add it back into the list
|
---|
847 | if (defined $first_file && $first_file =~ /\S/) {
|
---|
848 | unshift(@filenames, $first_file);
|
---|
849 | }
|
---|
850 |
|
---|
851 | my $filename = join("/", @filenames);
|
---|
852 |
|
---|
853 | # remove duplicate slashes and remove the last slash
|
---|
854 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
855 | $filename =~ s/[\\\/]+/\\/g;
|
---|
856 | } else {
|
---|
857 | $filename =~ s/[\/]+/\//g;
|
---|
858 | # DB: want a filename abc\de.html to remain like this
|
---|
859 | }
|
---|
860 | $filename =~ s/[\\\/]$//;
|
---|
861 |
|
---|
862 | return $filename;
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 | sub pathname_cat {
|
---|
867 | my $first_path = shift(@_);
|
---|
868 | my (@pathnames) = @_;
|
---|
869 |
|
---|
870 | # If first_path is not null or empty, then add it back into the list
|
---|
871 | if (defined $first_path && $first_path =~ /\S/) {
|
---|
872 | unshift(@pathnames, $first_path);
|
---|
873 | }
|
---|
874 |
|
---|
875 | my $join_char;
|
---|
876 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
877 | $join_char = ";";
|
---|
878 | } else {
|
---|
879 | $join_char = ":";
|
---|
880 | }
|
---|
881 |
|
---|
882 | my $pathname = join($join_char, @pathnames);
|
---|
883 |
|
---|
884 | # remove duplicate slashes
|
---|
885 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
886 | $pathname =~ s/[\\\/]+/\\/g;
|
---|
887 | } else {
|
---|
888 | $pathname =~ s/[\/]+/\//g;
|
---|
889 | # DB: want a pathname abc\de.html to remain like this
|
---|
890 | }
|
---|
891 |
|
---|
892 | return $pathname;
|
---|
893 | }
|
---|
894 |
|
---|
895 |
|
---|
896 | sub tidy_up_oid {
|
---|
897 | my ($OID) = @_;
|
---|
898 | if ($OID =~ /\./) {
|
---|
899 | print STDERR "Warning, identifier $OID contains periods (.), removing them\n";
|
---|
900 | $OID =~ s/\.//g; #remove any periods
|
---|
901 | }
|
---|
902 | if ($OID =~ /^\s.*\s$/) {
|
---|
903 | print STDERR "Warning, identifier $OID starts or ends with whitespace. Removing it\n";
|
---|
904 | # remove starting and trailing whitespace
|
---|
905 | $OID =~ s/^\s+//;
|
---|
906 | $OID =~ s/\s+$//;
|
---|
907 | }
|
---|
908 | if ($OID =~ /^[\d]*$/) {
|
---|
909 | print STDERR "Warning, identifier $OID contains only digits. Prepending 'D'.\n";
|
---|
910 | $OID = "D" . $OID;
|
---|
911 | }
|
---|
912 |
|
---|
913 | return $OID;
|
---|
914 | }
|
---|
915 | sub envvar_prepend {
|
---|
916 | my ($var,$val) = @_;
|
---|
917 |
|
---|
918 | # do not prepend any value/path that's already in the environment variable
|
---|
919 | if ($ENV{'GSDLOS'} =~ /^windows$/i)
|
---|
920 | {
|
---|
921 | my $escaped_val = $val;
|
---|
922 | $escaped_val =~ s/\\/\\\\/g; # escape any Windows backslashes for upcoming regex
|
---|
923 | if (!defined($ENV{$var})) {
|
---|
924 | $ENV{$var} = "$val";
|
---|
925 | }
|
---|
926 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
927 | $ENV{$var} = "$val;".$ENV{$var};
|
---|
928 | }
|
---|
929 | }
|
---|
930 | else {
|
---|
931 | if (!defined($ENV{$var})) {
|
---|
932 | $ENV{$var} = "$val";
|
---|
933 | }
|
---|
934 | elsif($ENV{$var} !~ m/$val/) {
|
---|
935 | $ENV{$var} = "$val:".$ENV{$var};
|
---|
936 | }
|
---|
937 | }
|
---|
938 | }
|
---|
939 |
|
---|
940 | sub envvar_append {
|
---|
941 | my ($var,$val) = @_;
|
---|
942 |
|
---|
943 | # do not append any value/path that's already in the environment variable
|
---|
944 | if ($ENV{'GSDLOS'} =~ /^windows$/i)
|
---|
945 | {
|
---|
946 | my $escaped_val = $val;
|
---|
947 | $escaped_val =~ s/\\/\\\\/g; # escape any Windows backslashes for upcoming regex
|
---|
948 | if (!defined($ENV{$var})) {
|
---|
949 | $ENV{$var} = "$val";
|
---|
950 | }
|
---|
951 | elsif($ENV{$var} !~ m/$escaped_val/) {
|
---|
952 | $ENV{$var} .= ";$val";
|
---|
953 | }
|
---|
954 | }
|
---|
955 | else {
|
---|
956 | if (!defined($ENV{$var})) {
|
---|
957 | $ENV{$var} = "$val";
|
---|
958 | }
|
---|
959 | elsif($ENV{$var} !~ m/$val/) {
|
---|
960 | $ENV{$var} .= ":$val";
|
---|
961 | }
|
---|
962 | }
|
---|
963 | }
|
---|
964 |
|
---|
965 |
|
---|
966 | # splits a filename into a prefix and a tail extension using the tail_re, or
|
---|
967 | # if that fails, splits on the file_extension . (dot)
|
---|
968 | sub get_prefix_and_tail_by_regex {
|
---|
969 |
|
---|
970 | my ($filename,$tail_re) = @_;
|
---|
971 |
|
---|
972 | my ($file_prefix,$file_ext) = ($filename =~ m/^(.*?)($tail_re)$/);
|
---|
973 | if ((!defined $file_prefix) || (!defined $file_ext)) {
|
---|
974 | ($file_prefix,$file_ext) = ($filename =~ m/^(.*)(\..*?)$/);
|
---|
975 | }
|
---|
976 |
|
---|
977 | return ($file_prefix,$file_ext);
|
---|
978 | }
|
---|
979 |
|
---|
980 | # get full path and file only path from a base_dir (which may be empty) and
|
---|
981 | # file (which may contain directories)
|
---|
982 | sub get_full_filenames {
|
---|
983 | my ($base_dir, $file) = @_;
|
---|
984 |
|
---|
985 | my $filename_full_path = $file;
|
---|
986 | # add on directory if present
|
---|
987 | $filename_full_path = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\S/;
|
---|
988 |
|
---|
989 | my $filename_no_path = $file;
|
---|
990 |
|
---|
991 | # remove directory if present
|
---|
992 | $filename_no_path =~ s/^.*[\/\\]//;
|
---|
993 | return ($filename_full_path, $filename_no_path);
|
---|
994 | }
|
---|
995 |
|
---|
996 | # returns the path of a file without the filename -- ie. the directory the file is in
|
---|
997 | sub filename_head {
|
---|
998 | my $filename = shift(@_);
|
---|
999 |
|
---|
1000 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1001 | $filename =~ s/[^\\\\]*$//;
|
---|
1002 | }
|
---|
1003 | else {
|
---|
1004 | $filename =~ s/[^\\\/]*$//;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | return $filename;
|
---|
1008 | }
|
---|
1009 |
|
---|
1010 |
|
---|
1011 |
|
---|
1012 | # returns 1 if filename1 and filename2 point to the same
|
---|
1013 | # file or directory
|
---|
1014 | sub filenames_equal {
|
---|
1015 | my ($filename1, $filename2) = @_;
|
---|
1016 |
|
---|
1017 | # use filename_cat to clean up trailing slashes and
|
---|
1018 | # multiple slashes
|
---|
1019 | $filename1 = filename_cat ($filename1);
|
---|
1020 | $filename2 = filename_cat ($filename2);
|
---|
1021 |
|
---|
1022 | # filenames not case sensitive on windows
|
---|
1023 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1024 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
1025 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
1026 | }
|
---|
1027 | return 1 if $filename1 eq $filename2;
|
---|
1028 | return 0;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 |
|
---|
1032 | sub filename_within_directory
|
---|
1033 | {
|
---|
1034 | my ($filename,$within_dir) = @_;
|
---|
1035 |
|
---|
1036 | if ($within_dir !~ m/[\/\\]$/) {
|
---|
1037 | my $dirsep = &util::get_dirsep();
|
---|
1038 | $within_dir .= $dirsep;
|
---|
1039 | }
|
---|
1040 |
|
---|
1041 | $within_dir =~ s/\\/\\\\/g; # escape DOS style file separator
|
---|
1042 |
|
---|
1043 | if ($filename =~ m/^$within_dir(.*)$/) {
|
---|
1044 | $filename = $1;
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | return $filename;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | sub filename_within_collection
|
---|
1051 | {
|
---|
1052 | my ($filename) = @_;
|
---|
1053 |
|
---|
1054 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
1055 |
|
---|
1056 | if (defined $collect_dir) {
|
---|
1057 |
|
---|
1058 | # if from within GSDLCOLLECTDIR, then remove directory prefix
|
---|
1059 | # so source_filename is realative to it. This is done to aid
|
---|
1060 | # portability, i.e. the collection can be moved to somewhere
|
---|
1061 | # else on the file system and the archives directory will still
|
---|
1062 | # work. This is needed, for example in the applet version of
|
---|
1063 | # GLI where GSDLHOME/collect on the server will be different to
|
---|
1064 | # the collect directory of the remove user. Of course,
|
---|
1065 | # GSDLCOLLECTDIR subsequently needs to be put back on to turn
|
---|
1066 | # it back into a full pathname.
|
---|
1067 |
|
---|
1068 | $filename = filename_within_directory($filename,$collect_dir);
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | return $filename;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | sub prettyprint_file
|
---|
1075 | {
|
---|
1076 | my ($base_dir,$file,$gli) = @_;
|
---|
1077 |
|
---|
1078 | my $filename_full_path = &util::filename_cat($base_dir,$file);
|
---|
1079 |
|
---|
1080 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
1081 | require Win32;
|
---|
1082 |
|
---|
1083 | # For some reason base_dir in the form c:/a/b/c
|
---|
1084 | # This leads to confusion later on, so turn it back into
|
---|
1085 | # the more usual Windows form
|
---|
1086 | $base_dir =~ s/\//\\/g;
|
---|
1087 | my $long_base_dir = Win32::GetLongPathName($base_dir);
|
---|
1088 | my $long_full_path = Win32::GetLongPathName($filename_full_path);
|
---|
1089 |
|
---|
1090 | $file = filename_within_directory($long_full_path,$long_base_dir);
|
---|
1091 | $file = encode("utf8",$file) if ($gli);
|
---|
1092 | }
|
---|
1093 |
|
---|
1094 | return $file;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 |
|
---|
1098 | sub upgrade_if_dos_filename
|
---|
1099 | {
|
---|
1100 | my ($filename_full_path,$and_encode) = @_;
|
---|
1101 |
|
---|
1102 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
1103 | # Ensure any DOS-like filename, such as test~1.txt, has been upgraded
|
---|
1104 | # to its long (Windows) version
|
---|
1105 | my $long_filename = Win32::GetLongPathName($filename_full_path);
|
---|
1106 | if (defined $long_filename) {
|
---|
1107 | $filename_full_path = $long_filename;
|
---|
1108 | }
|
---|
1109 | # Make sure initial drive letter is lower-case (to fit in with rest of Greenstone)
|
---|
1110 | $filename_full_path =~ s/^(.):/\u$1:/;
|
---|
1111 | if ((defined $and_encode) && ($and_encode)) {
|
---|
1112 | $filename_full_path = encode("utf8",$filename_full_path);
|
---|
1113 | }
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | return $filename_full_path;
|
---|
1117 | }
|
---|
1118 |
|
---|
1119 |
|
---|
1120 | sub downgrade_if_dos_filename
|
---|
1121 | {
|
---|
1122 | my ($filename_full_path) = @_;
|
---|
1123 |
|
---|
1124 | if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
|
---|
1125 | require Win32;
|
---|
1126 |
|
---|
1127 | # Ensure the given long Windows filename is in a form that can
|
---|
1128 | # be opened by Perl => convert it to a short DOS-like filename
|
---|
1129 |
|
---|
1130 | my $short_filename = Win32::GetShortPathName($filename_full_path);
|
---|
1131 | if (defined $short_filename) {
|
---|
1132 | $filename_full_path = $short_filename;
|
---|
1133 | }
|
---|
1134 | # Make sure initial drive letter is lower-case (to fit in
|
---|
1135 | # with rest of Greenstone)
|
---|
1136 | $filename_full_path =~ s/^(.):/\u$1:/;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | return $filename_full_path;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | sub block_filename
|
---|
1143 | {
|
---|
1144 | my ($block_hash,$filename) = @_;
|
---|
1145 |
|
---|
1146 | if ($ENV{'GSDLOS'} =~ m/^windows$/) {
|
---|
1147 |
|
---|
1148 | # lower case the entire thing, eg for cover.jpg when its actually cover.JPG
|
---|
1149 | my $lower_filename = lc($filename);
|
---|
1150 | $block_hash->{'file_blocks'}->{$lower_filename} = 1;
|
---|
1151 | # my $lower_drive = $filename;
|
---|
1152 | # $lower_drive =~ s/^([A-Z]):/\l$1:/i;
|
---|
1153 |
|
---|
1154 | # my $upper_drive = $filename;
|
---|
1155 | # $upper_drive =~ s/^([A-Z]):/\u$1:/i;
|
---|
1156 | #
|
---|
1157 | # $block_hash->{'file_blocks'}->{$lower_drive} = 1;
|
---|
1158 | # $block_hash->{'file_blocks'}->{$upper_drive} = 1;
|
---|
1159 | }
|
---|
1160 | else {
|
---|
1161 | $block_hash->{'file_blocks'}->{$filename} = 1;
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 |
|
---|
1166 | sub filename_is_absolute
|
---|
1167 | {
|
---|
1168 | my ($filename) = @_;
|
---|
1169 |
|
---|
1170 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1171 | return ($filename =~ m/^(\w:)?\\/);
|
---|
1172 | }
|
---|
1173 | else {
|
---|
1174 | return ($filename =~ m/^\//);
|
---|
1175 | }
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 |
|
---|
1179 | ## @method make_absolute()
|
---|
1180 | #
|
---|
1181 | # Ensure the given file path is absolute in respect to the given base path.
|
---|
1182 | #
|
---|
1183 | # @param $base_dir A string denoting the base path the given dir must be
|
---|
1184 | # absolute to.
|
---|
1185 | # @param $dir The directory to be made absolute as a string. Note that the
|
---|
1186 | # dir may already be absolute, in which case it will remain
|
---|
1187 | # unchanged.
|
---|
1188 | # @return The now absolute form of the directory as a string.
|
---|
1189 | #
|
---|
1190 | # @author John Thompson, DL Consulting Ltd.
|
---|
1191 | # @copy 2006 DL Consulting Ltd.
|
---|
1192 | #
|
---|
1193 | #used in buildcol.pl, doesn't work for all cases --kjdon
|
---|
1194 | sub make_absolute {
|
---|
1195 |
|
---|
1196 | my ($base_dir, $dir) = @_;
|
---|
1197 | ### print STDERR "dir = $dir\n";
|
---|
1198 | $dir =~ s/[\\\/]+/\//g;
|
---|
1199 | $dir = $base_dir . "/$dir" unless ($dir =~ m|^(\w:)?/|);
|
---|
1200 | $dir =~ s|^/tmp_mnt||;
|
---|
1201 | 1 while($dir =~ s|/[^/]*/\.\./|/|g);
|
---|
1202 | $dir =~ s|/[.][.]?/|/|g;
|
---|
1203 | $dir =~ tr|/|/|s;
|
---|
1204 | ### print STDERR "dir = $dir\n";
|
---|
1205 |
|
---|
1206 | return $dir;
|
---|
1207 | }
|
---|
1208 | ## make_absolute() ##
|
---|
1209 |
|
---|
1210 | sub get_dirsep {
|
---|
1211 |
|
---|
1212 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1213 | return "\\";
|
---|
1214 | } else {
|
---|
1215 | return "\/";
|
---|
1216 | }
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | sub get_os_dirsep {
|
---|
1220 |
|
---|
1221 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
1222 | return "\\\\";
|
---|
1223 | } else {
|
---|
1224 | return "\\\/";
|
---|
1225 | }
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | sub get_re_dirsep {
|
---|
1229 |
|
---|
1230 | return "\\\\|\\\/";
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 |
|
---|
1234 | sub get_dirsep_tail {
|
---|
1235 | my ($filename) = @_;
|
---|
1236 |
|
---|
1237 | # returns last part of directory or filename
|
---|
1238 | # On unix e.g. a/b.d => b.d
|
---|
1239 | # a/b/c => c
|
---|
1240 |
|
---|
1241 | my $dirsep = get_re_dirsep();
|
---|
1242 | my @dirs = split (/$dirsep/, $filename);
|
---|
1243 | my $tail = pop @dirs;
|
---|
1244 |
|
---|
1245 | # - caused problems under windows
|
---|
1246 | #my ($tail) = ($filename =~ m/^(?:.*?$dirsep)?(.*?)$/);
|
---|
1247 |
|
---|
1248 | return $tail;
|
---|
1249 | }
|
---|
1250 |
|
---|
1251 |
|
---|
1252 | # if this is running on windows we want binaries to end in
|
---|
1253 | # .exe, otherwise they don't have to end in any extension
|
---|
1254 | sub get_os_exe {
|
---|
1255 | return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
1256 | return "";
|
---|
1257 | }
|
---|
1258 |
|
---|
1259 |
|
---|
1260 | # test to see whether this is a big or little endian machine
|
---|
1261 | sub is_little_endian
|
---|
1262 | {
|
---|
1263 | # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
|
---|
1264 | # 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
|
---|
1265 | # Otherwise, it's little endian
|
---|
1266 |
|
---|
1267 | #return 0 if $^O =~ /^darwin$/i;
|
---|
1268 | #return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
|
---|
1269 |
|
---|
1270 | # Going back to stating exactly whether the machine is little endian
|
---|
1271 | # or big endian, without any special case for Macs. Since for rata it comes
|
---|
1272 | # back with little endian and for shuttle with bigendian.
|
---|
1273 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 |
|
---|
1277 | # will return the collection name if successful, "" otherwise
|
---|
1278 | sub use_collection {
|
---|
1279 | my ($collection, $collectdir) = @_;
|
---|
1280 |
|
---|
1281 | if (!defined $collectdir || $collectdir eq "") {
|
---|
1282 | $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | # get and check the collection
|
---|
1286 | if (!defined($collection) || $collection eq "") {
|
---|
1287 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
1288 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
1289 | } else {
|
---|
1290 | print STDOUT "No collection specified\n";
|
---|
1291 | return "";
|
---|
1292 | }
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | if ($collection eq "modelcol") {
|
---|
1296 | print STDOUT "You can't use modelcol.\n";
|
---|
1297 | return "";
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
1301 | # are defined
|
---|
1302 | $ENV{'GSDLCOLLECTION'} = $collection;
|
---|
1303 | $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
|
---|
1304 |
|
---|
1305 | # make sure this collection exists
|
---|
1306 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
1307 | print STDOUT "Invalid collection ($collection).\n";
|
---|
1308 | return "";
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | # everything is ready to go
|
---|
1312 | return $collection;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | sub get_current_collection_name {
|
---|
1316 | return $ENV{'GSDLCOLLECTION'};
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 |
|
---|
1320 | # will return the collection name if successful, "" otherwise.
|
---|
1321 | # Like use_collection (above) but for greenstone 3 (taking account of site level)
|
---|
1322 |
|
---|
1323 | sub use_site_collection {
|
---|
1324 | my ($site, $collection, $collectdir) = @_;
|
---|
1325 |
|
---|
1326 | if (!defined $collectdir || $collectdir eq "") {
|
---|
1327 | die "GSDL3HOME not set.\n" unless defined $ENV{'GSDL3HOME'};
|
---|
1328 | $collectdir = &filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect");
|
---|
1329 | }
|
---|
1330 |
|
---|
1331 | # collectdir explicitly set by this point (using $site variable if required).
|
---|
1332 | # Can call "old" gsdl2 use_collection now.
|
---|
1333 |
|
---|
1334 | return use_collection($collection,$collectdir);
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 |
|
---|
1338 |
|
---|
1339 | sub locate_config_file
|
---|
1340 | {
|
---|
1341 | my ($file) = @_;
|
---|
1342 |
|
---|
1343 | my $locations = locate_config_files($file);
|
---|
1344 |
|
---|
1345 | return shift @$locations; # returns undef if 'locations' is empty
|
---|
1346 | }
|
---|
1347 |
|
---|
1348 |
|
---|
1349 | sub locate_config_files
|
---|
1350 | {
|
---|
1351 | my ($file) = @_;
|
---|
1352 |
|
---|
1353 | my @locations = ();
|
---|
1354 |
|
---|
1355 | if (-e $file) {
|
---|
1356 | # Clearly specified (most likely full filename)
|
---|
1357 | # No need to hunt in 'etc' directories, return value unchanged
|
---|
1358 | push(@locations,$file);
|
---|
1359 | }
|
---|
1360 | else {
|
---|
1361 | # Check for collection specific one before looking in global GSDL 'etc'
|
---|
1362 | if (defined $ENV{'GSDLCOLLECTDIR'} && $ENV{'GSDLCOLLECTDIR'} ne "") {
|
---|
1363 | my $test_collect_etc_filename
|
---|
1364 | = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $file);
|
---|
1365 |
|
---|
1366 | if (-e $test_collect_etc_filename) {
|
---|
1367 | push(@locations,$test_collect_etc_filename);
|
---|
1368 | }
|
---|
1369 | }
|
---|
1370 | my $test_main_etc_filename
|
---|
1371 | = &util::filename_cat($ENV{'GSDLHOME'},"etc", $file);
|
---|
1372 | if (-e $test_main_etc_filename) {
|
---|
1373 | push(@locations,$test_main_etc_filename);
|
---|
1374 | }
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | return \@locations;
|
---|
1378 | }
|
---|
1379 |
|
---|
1380 |
|
---|
1381 | sub hyperlink_text
|
---|
1382 | {
|
---|
1383 | my ($text) = @_;
|
---|
1384 |
|
---|
1385 | $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
|
---|
1386 | $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
|
---|
1387 |
|
---|
1388 | return $text;
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 |
|
---|
1392 | # A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
|
---|
1393 | # Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
|
---|
1394 | sub is_dir_empty
|
---|
1395 | {
|
---|
1396 | my ($path) = @_;
|
---|
1397 | opendir DIR, $path;
|
---|
1398 | while(my $entry = readdir DIR) {
|
---|
1399 | next if($entry =~ /^\.\.?$/);
|
---|
1400 | closedir DIR;
|
---|
1401 | return 0;
|
---|
1402 | }
|
---|
1403 | closedir DIR;
|
---|
1404 | return 1;
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | # Returns the given filename converted using either URL encoding or base64
|
---|
1408 | # encoding, as specified by $rename_method. If the given filename has no suffix
|
---|
1409 | # (if it is just the tailname), then $no_suffix should be some defined value.
|
---|
1410 | # rename_method can be url, none, base64
|
---|
1411 | sub rename_file {
|
---|
1412 | my ($filename, $rename_method, $no_suffix) = @_;
|
---|
1413 |
|
---|
1414 | if(!$filename) { # undefined or empty string
|
---|
1415 | return $filename;
|
---|
1416 | }
|
---|
1417 |
|
---|
1418 | if (!$rename_method) {
|
---|
1419 | print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
|
---|
1420 | # Debugging information
|
---|
1421 | # my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
|
---|
1422 | # print STDERR "Called from method: $cfilename:$cline $cpackage->$csubr\n";
|
---|
1423 | $rename_method = "url";
|
---|
1424 | } elsif($rename_method eq "none") {
|
---|
1425 | return $filename; # would have already been renamed
|
---|
1426 | }
|
---|
1427 |
|
---|
1428 | # No longer replace spaces with underscores, since underscores mess with incremental rebuild
|
---|
1429 | ### Replace spaces with underscore. Do this first else it can go wrong below when getting tailname
|
---|
1430 | ###$filename =~ s/ /_/g;
|
---|
1431 |
|
---|
1432 | my ($tailname,$dirname,$suffix);
|
---|
1433 | if($no_suffix) { # given a tailname, no suffix
|
---|
1434 | ($tailname,$dirname) = File::Basename::fileparse($filename);
|
---|
1435 | }
|
---|
1436 | else {
|
---|
1437 | ($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.(?:[^\\.]+?)\$");
|
---|
1438 | }
|
---|
1439 | if (!$suffix) {
|
---|
1440 | $suffix = "";
|
---|
1441 | }
|
---|
1442 | else {
|
---|
1443 | $suffix = lc($suffix);
|
---|
1444 | }
|
---|
1445 |
|
---|
1446 | if ($rename_method eq "url") {
|
---|
1447 | $tailname = &unicode::url_encode($tailname);
|
---|
1448 | }
|
---|
1449 | elsif ($rename_method eq "base64") {
|
---|
1450 | $tailname = &unicode::base64_encode($tailname);
|
---|
1451 | $tailname =~ s/\s*//sg; # for some reason it adds spaces not just at end but also in middle
|
---|
1452 | }
|
---|
1453 |
|
---|
1454 | $filename = "$tailname$suffix";
|
---|
1455 | $filename = "$dirname$filename" if ($dirname ne "./" && $dirname ne ".\\");
|
---|
1456 |
|
---|
1457 | return $filename;
|
---|
1458 | }
|
---|
1459 |
|
---|
1460 |
|
---|
1461 | # BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files
|
---|
1462 | sub rename_ldb_or_bdb_file {
|
---|
1463 | my ($filename_no_ext) = @_;
|
---|
1464 |
|
---|
1465 | my $new_filename = "$filename_no_ext.gdb";
|
---|
1466 | return if (-f $new_filename); # if the file has the right extension, don't need to do anything
|
---|
1467 | # try ldb
|
---|
1468 | my $old_filename = "$filename_no_ext.ldb";
|
---|
1469 |
|
---|
1470 | if (-f $old_filename) {
|
---|
1471 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
1472 | rename ($old_filename, $new_filename)
|
---|
1473 | || print STDERR "Rename failed: $!\n";
|
---|
1474 | return;
|
---|
1475 | }
|
---|
1476 | # try bdb
|
---|
1477 | $old_filename = "$filename_no_ext.bdb";
|
---|
1478 | if (-f $old_filename) {
|
---|
1479 | print STDERR "Renaming $old_filename to $new_filename\n";
|
---|
1480 | rename ($old_filename, $new_filename)
|
---|
1481 | || print STDERR "Rename failed: $!\n";
|
---|
1482 | return;
|
---|
1483 | }
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 |
|
---|
1487 | # Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file.
|
---|
1488 | # By default, /greenstone3 for GS3 or /greenstone for GS2.
|
---|
1489 | sub get_greenstone_url_prefix() {
|
---|
1490 | # if already set on a previous occasion, just return that
|
---|
1491 | # (Don't want to keep repeating this: cost of re-opening and scanning files.)
|
---|
1492 | return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
|
---|
1493 |
|
---|
1494 | my ($configfile, $urlprefix, $defaultUrlprefix);
|
---|
1495 | my @propertynames = ();
|
---|
1496 |
|
---|
1497 | if($ENV{'GSDL3SRCHOME'}) {
|
---|
1498 | $defaultUrlprefix = "/greenstone3";
|
---|
1499 | $configfile = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
|
---|
1500 | push(@propertynames, qw/path\s*\=/);
|
---|
1501 | } else {
|
---|
1502 | $defaultUrlprefix = "/greenstone";
|
---|
1503 | $configfile = &util::filename_cat($ENV{'GSDLHOME'}, "cgi-bin", "gsdlsite.cfg");
|
---|
1504 | push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other
|
---|
1505 | }
|
---|
1506 |
|
---|
1507 | $urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
|
---|
1508 |
|
---|
1509 | if(!$urlprefix) { # no values found for URL prefix, use default values
|
---|
1510 | $urlprefix = $defaultUrlprefix;
|
---|
1511 | } else {
|
---|
1512 | #gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
|
---|
1513 | $urlprefix =~ s/^\///; # remove the starting slash
|
---|
1514 | my @dirs = split(/(\\|\/)/, $urlprefix);
|
---|
1515 | $urlprefix = shift(@dirs);
|
---|
1516 |
|
---|
1517 | if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
|
---|
1518 | $urlprefix = "/$urlprefix";
|
---|
1519 | }
|
---|
1520 | }
|
---|
1521 |
|
---|
1522 | # set for the future
|
---|
1523 | $ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
|
---|
1524 | # print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
|
---|
1525 | return $urlprefix;
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 |
|
---|
1529 | # Given a config file (xml or java properties file) and a list/array of regular expressions
|
---|
1530 | # that represent property names to match on, this function will return the value for the 1st
|
---|
1531 | # matching property name. If the return value is undefined, no matching property was found.
|
---|
1532 | sub extract_propvalue_from_file() {
|
---|
1533 | my ($configfile, $propertynames) = @_;
|
---|
1534 |
|
---|
1535 | my $value;
|
---|
1536 | unless(open(FIN, "<$configfile")) {
|
---|
1537 | print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
|
---|
1538 | return $value; # not initialised
|
---|
1539 | }
|
---|
1540 |
|
---|
1541 | # Read the entire file at once, as one single line, then close it
|
---|
1542 | my $filecontents;
|
---|
1543 | {
|
---|
1544 | local $/ = undef;
|
---|
1545 | $filecontents = <FIN>;
|
---|
1546 | }
|
---|
1547 | close(FIN);
|
---|
1548 |
|
---|
1549 | foreach my $regex (@$propertynames) {
|
---|
1550 | ($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
|
---|
1551 | if($value) {
|
---|
1552 | $value =~ s/^\"//; # remove any startquotes
|
---|
1553 | $value =~ s/\".*$//; # remove the 1st endquotes (if any) followed by any xml
|
---|
1554 | last; # found value for a matching property, break from loop
|
---|
1555 | }
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | return $value;
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | # Subroutine that sources setup.bash, given GSDLHOME and GSDLOS and
|
---|
1562 | # given that perllib is in @INC in order to invoke this subroutine.
|
---|
1563 | # Call as follows -- after setting up INC to include perllib and
|
---|
1564 | # after setting up GSDLHOME and GSDLOS:
|
---|
1565 | #
|
---|
1566 | # require util;
|
---|
1567 | # &util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
|
---|
1568 | #
|
---|
1569 | sub setup_greenstone_env() {
|
---|
1570 | my ($GSDLHOME, $GSDLOS) = @_;
|
---|
1571 |
|
---|
1572 | #my %env_map = ();
|
---|
1573 | # Get the localised ENV settings of running a localised source setup.bash
|
---|
1574 | # and put it into the ENV here. Need to clear GSDLHOME before running setup
|
---|
1575 | #my $perl_command = "(cd $GSDLHOME; export GSDLHOME=; . ./setup.bash > /dev/null; env)";
|
---|
1576 | my $perl_command = "(cd $GSDLHOME; /bin/bash -c \"export GSDLHOME=; source setup.bash > /dev/null; env\")";
|
---|
1577 | if($GSDLOS =~ m/windows/i) {
|
---|
1578 | #$perl_command = "cmd /C \"cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set\"";
|
---|
1579 | $perl_command = "(cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set)";
|
---|
1580 | }
|
---|
1581 | if (!open(PIN, "$perl_command |")) {
|
---|
1582 | print STDERR ("Unable to execute command: $perl_command. $!\n");
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | while (defined (my $perl_output_line = <PIN>)) {
|
---|
1586 | my($key,$value) = ($perl_output_line =~ m/^([^=]*)[=](.*)$/);
|
---|
1587 | #$env_map{$key}=$value;
|
---|
1588 | $ENV{$key}=$value;
|
---|
1589 | }
|
---|
1590 |
|
---|
1591 | # If any keys in $ENV don't occur in Greenstone's localised env
|
---|
1592 | # (stored in $env_map), delete those entries from $ENV
|
---|
1593 | #foreach $key (keys %ENV) {
|
---|
1594 | # if(!defined $env_map{$key}) {
|
---|
1595 | # print STDOUT "**** DELETING ENV KEY: $key\tVALUE: $ENV{'$key'}\n";
|
---|
1596 | # delete $ENV{$key}; # del $ENV(key, value) pair
|
---|
1597 | # }
|
---|
1598 | #}
|
---|
1599 | #undef %env_map;
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | 1;
|
---|