###########################################################################
#
# util.pm -- various useful utilities
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################
package util;
use strict;
use Encode;
use File::Copy;
use File::Basename;
# Config for getting the perlpath in the recommended way, though it uses paths that are
# hard-coded into the Config file that's generated upon configuring and compiling perl.
# $^X works better in some cases to return the path to perl used to launch the script,
# but if launched with plain "perl" (no full-path), that will be just what it returns.
use Config;
# removes files (but not directories)
sub rm {
my (@files) = @_;
my @filefiles = ();
# make sure the files we want to delete exist
# and are regular files
foreach my $file (@files) {
if (!-e $file) {
print STDERR "util::rm $file does not exist\n";
} elsif ((!-f $file) && (!-l $file)) {
print STDERR "util::rm $file is not a regular (or symbolic) file\n";
} else {
push (@filefiles, $file);
}
}
# remove the files
my $numremoved = unlink @filefiles;
# check to make sure all of them were removed
if ($numremoved != scalar(@filefiles)) {
print STDERR "util::rm Not all files were removed\n";
}
}
# removes files (but not directories) - can rename this to the default
# "rm" subroutine when debugging the deletion of individual files.
sub rm_debug {
my (@files) = @_;
my @filefiles = ();
# make sure the files we want to delete exist
# and are regular files
foreach my $file (@files) {
if (!-e $file) {
print STDERR "util::rm $file does not exist\n";
} elsif ((!-f $file) && (!-l $file)) {
print STDERR "util::rm $file is not a regular (or symbolic) file\n";
} else { # debug message
unlink($file) or warn "Could not delete file $file: $!\n";
}
}
}
# recursive removal
sub filtered_rm_r {
my ($files,$file_accept_re,$file_reject_re) = @_;
# my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(2);
# my ($lcfilename) = ($cfilename =~ m/([^\\\/]*)$/);
# print STDERR "** Calling method (2): $lcfilename:$cline $cpackage->$csubr\n";
my @files_array = (ref $files eq "ARRAY") ? @$files : ($files);
# recursively remove the files
foreach my $file (@files_array) {
$file =~ s/[\/\\]+$//; # remove trailing slashes
if (!-e $file) {
print STDERR "util::filtered_rm_r $file does not exist\n";
} elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
# get the contents of this directory
if (!opendir (INDIR, $file)) {
print STDERR "util::filtered_rm_r could not open directory $file\n";
} else {
my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
closedir (INDIR);
# remove all the files in this directory
map {$_="$file/$_";} @filedir;
&filtered_rm_r (\@filedir,$file_accept_re,$file_reject_re);
if (!defined $file_accept_re && !defined $file_reject_re) {
# remove this directory
if (!rmdir $file) {
print STDERR "util::filtered_rm_r couldn't remove directory $file\n";
}
}
}
} else {
next if (defined $file_reject_re && ($file =~ m/$file_reject_re/));
if ((!defined $file_accept_re) || ($file =~ m/$file_accept_re/)) {
# remove this file
&rm ($file);
}
}
}
}
# recursive removal
sub rm_r {
my (@files) = @_;
# use the more general (but reterospectively written function
# filtered_rm_r function()
filtered_rm_r(\@files,undef,undef); # no accept or reject expressions
}
# moves a file or a group of files
sub mv {
my $dest = pop (@_);
my (@srcfiles) = @_;
# remove trailing slashes from source and destination files
$dest =~ s/[\\\/]+$//;
map {$_ =~ s/[\\\/]+$//;} @srcfiles;
# a few sanity checks
if (scalar (@srcfiles) == 0) {
print STDERR "util::mv no destination directory given\n";
return;
} elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
print STDERR "util::mv if multiple source files are given the ".
"destination must be a directory\n";
return;
}
# move the files
foreach my $file (@srcfiles) {
my $tempdest = $dest;
if (-d $tempdest) {
my ($filename) = $file =~ /([^\\\/]+)$/;
$tempdest .= "/$filename";
}
if (!-e $file) {
print STDERR "util::mv $file does not exist\n";
} else {
if(!rename ($file, $tempdest)) {
print STDERR "**** Failed to rename $file to $tempdest\n";
&File::Copy::copy($file, $tempdest);
&rm($file);
}
elsif(-e $file) { # rename (partially) succeeded) but srcfile still exists after rename
#print STDERR "*** srcfile $file still exists after rename to $tempdest\n";
if(!-e $tempdest) {
print STDERR "@@@@ ERROR: $tempdest does not exist\n";
}
# Sometimes the rename operation fails (as does File::Copy::move).
# This turns out to be because the files are hardlinked.
# Need to do a copy-delete in this case, however, the copy step is not necessary:
# the srcfile got renamed into tempdest, but srcfile itself still exists, delete it.
#&File::Copy::copy($file, $tempdest);
&rm($file);
}
}
}
}
# Move the contents of source directory into target directory
# (as opposed to merely replacing target dir with the src dir)
# This can overwrite any files with duplicate names in the target
# but other files and folders in the target will continue to exist
sub mv_dir_contents {
my ($src_dir, $dest_dir) = @_;
# Obtain listing of all files within src_dir
# Note that readdir lists relative paths, as well as . and ..
opendir(DIR, "$src_dir");
my @files= readdir(DIR);
close(DIR);
my @full_path_files = ();
foreach my $file (@files) {
# process all except . and ..
unless($file eq "." || $file eq "..") {
my $dest_subdir = &filename_cat($dest_dir, $file); # $file is still a relative path
# construct absolute paths
$file = &filename_cat($src_dir, $file); # $file is now an absolute path
# Recurse on directories which have an equivalent in target dest_dir
# If $file is a directory that already exists in target $dest_dir,
# then a simple move operation will fail (definitely on Windows).
if(-d $file && -d $dest_subdir) {
#print STDERR "**** $file is a directory also existing in target, its contents to be copied to $dest_subdir\n";
&mv_dir_contents($file, $dest_subdir);
# now all content is moved across, delete empty dir in source folder
if(&is_dir_empty($file)) {
if (!rmdir $file) {
print STDERR "ERROR. util::mv_dir_contents couldn't remove directory $file\n";
}
} else { # error
print STDERR "ERROR. util::mv_dir_contents: subfolder $file still non-empty after moving contents to $dest_subdir\n";
}
} else { # process files and any directories that don't already exist with a simple move
push(@full_path_files, $file);
}
}
}
if(!&dir_exists($dest_dir)) { # create target toplevel folder or subfolders if they don't exist
&mk_dir($dest_dir);
}
#print STDERR "@@@@@ Copying files |".join(",", @full_path_files)."| to: $dest_dir\n";
if(@full_path_files) { # if non-empty, there's something to copy across
&mv(@full_path_files, $dest_dir);
}
}
# copies a file or a group of files
sub cp {
my $dest = pop (@_);
my (@srcfiles) = @_;
# remove trailing slashes from source and destination files
$dest =~ s/[\\\/]+$//;
map {$_ =~ s/[\\\/]+$//;} @srcfiles;
# a few sanity checks
if (scalar (@srcfiles) == 0) {
print STDERR "util::cp no destination directory given\n";
return;
} elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
print STDERR "util::cp if multiple source files are given the ".
"destination must be a directory\n";
return;
}
# copy the files
foreach my $file (@srcfiles) {
my $tempdest = $dest;
if (-d $tempdest) {
my ($filename) = $file =~ /([^\\\/]+)$/;
$tempdest .= "/$filename";
}
if (!-e $file) {
print STDERR "util::cp $file does not exist\n";
} elsif (!-f $file) {
print STDERR "util::cp $file is not a plain file\n";
} else {
&File::Copy::copy ($file, $tempdest);
}
}
}
# recursively copies a file or group of files
# syntax: cp_r (sourcefiles, destination directory)
# destination must be a directory - to copy one file to
# another use cp instead
sub cp_r {
my $dest = pop (@_);
my (@srcfiles) = @_;
# a few sanity checks
if (scalar (@srcfiles) == 0) {
print STDERR "util::cp_r no destination directory given\n";
return;
} elsif (-f $dest) {
print STDERR "util::cp_r destination must be a directory\n";
return;
}
# create destination directory if it doesn't exist already
if (! -d $dest) {
my $store_umask = umask(0002);
mkdir ($dest, 0777);
umask($store_umask);
}
# copy the files
foreach my $file (@srcfiles) {
if (!-e $file) {
print STDERR "util::cp_r $file does not exist\n";
} elsif (-d $file) {
# make the new directory
my ($filename) = $file =~ /([^\\\/]*)$/;
$dest = &util::filename_cat ($dest, $filename);
my $store_umask = umask(0002);
mkdir ($dest, 0777);
umask($store_umask);
# get the contents of this directory
if (!opendir (INDIR, $file)) {
print STDERR "util::cp_r could not open directory $file\n";
} else {
my @filedir = readdir (INDIR);
closedir (INDIR);
foreach my $f (@filedir) {
next if $f =~ /^\.\.?$/;
# copy all the files in this directory
my $ff = &util::filename_cat ($file, $f);
&cp_r ($ff, $dest);
}
}
} else {
&cp($file, $dest);
}
}
}
# recursively copies a file or group of files
# syntax: cp_r (sourcefiles, destination directory)
# destination must be a directory - to copy one file to
# another use cp instead
sub cp_r_nosvn {
my $dest = pop (@_);
my (@srcfiles) = @_;
# a few sanity checks
if (scalar (@srcfiles) == 0) {
print STDERR "util::cp_r no destination directory given\n";
return;
} elsif (-f $dest) {
print STDERR "util::cp_r destination must be a directory\n";
return;
}
# create destination directory if it doesn't exist already
if (! -d $dest) {
my $store_umask = umask(0002);
mkdir ($dest, 0777);
umask($store_umask);
}
# copy the files
foreach my $file (@srcfiles) {
if (!-e $file) {
print STDERR "util::cp_r $file does not exist\n";
} elsif (-d $file) {
# make the new directory
my ($filename) = $file =~ /([^\\\/]*)$/;
$dest = &util::filename_cat ($dest, $filename);
my $store_umask = umask(0002);
mkdir ($dest, 0777);
umask($store_umask);
# get the contents of this directory
if (!opendir (INDIR, $file)) {
print STDERR "util::cp_r could not open directory $file\n";
} else {
my @filedir = readdir (INDIR);
closedir (INDIR);
foreach my $f (@filedir) {
next if $f =~ /^\.\.?$/;
next if $f =~ /^\.svn$/;
# copy all the files in this directory
my $ff = &util::filename_cat ($file, $f);
&cp_r ($ff, $dest);
}
}
} else {
&cp($file, $dest);
}
}
}
# copies a directory and its contents, excluding subdirectories, into a new directory
sub cp_r_toplevel {
my $dest = pop (@_);
my (@srcfiles) = @_;
# a few sanity checks
if (scalar (@srcfiles) == 0) {
print STDERR "util::cp_r no destination directory given\n";
return;
} elsif (-f $dest) {
print STDERR "util::cp_r destination must be a directory\n";
return;
}
# create destination directory if it doesn't exist already
if (! -d $dest) {
my $store_umask = umask(0002);
mkdir ($dest, 0777);
umask($store_umask);
}
# copy the files
foreach my $file (@srcfiles) {
if (!-e $file) {
print STDERR "util::cp_r $file does not exist\n";
} elsif (-d $file) {
# make the new directory
my ($filename) = $file =~ /([^\\\/]*)$/;
$dest = &util::filename_cat ($dest, $filename);
my $store_umask = umask(0002);
mkdir ($dest, 0777);
umask($store_umask);
# get the contents of this directory
if (!opendir (INDIR, $file)) {
print STDERR "util::cp_r could not open directory $file\n";
} else {
my @filedir = readdir (INDIR);
closedir (INDIR);
foreach my $f (@filedir) {
next if $f =~ /^\.\.?$/;
# copy all the files in this directory, but not directories
my $ff = &util::filename_cat ($file, $f);
if (-f $ff) {
&cp($ff, $dest);
#&cp_r ($ff, $dest);
}
}
}
} else {
&cp($file, $dest);
}
}
}
sub mk_dir {
my ($dir) = @_;
my $store_umask = umask(0002);
my $mkdir_ok = mkdir ($dir, 0777);
umask($store_umask);
if (!$mkdir_ok)
{
print STDERR "util::mk_dir could not create directory $dir\n";
return;
}
}
# in case anyone cares - I did some testing (using perls Benchmark module)
# on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
# slightly faster (surprisingly) - Stefan.
sub mk_all_dir {
my ($dir) = @_;
# use / for the directory separator, remove duplicate and
# trailing slashes
$dir=~s/[\\\/]+/\//g;
$dir=~s/[\\\/]+$//;
# make sure the cache directory exists
my $dirsofar = "";
my $first = 1;
foreach my $dirname (split ("/", $dir)) {
$dirsofar .= "/" unless $first;
$first = 0;
$dirsofar .= $dirname;
next if $dirname =~ /^(|[a-z]:)$/i;
if (!-e $dirsofar)
{
my $store_umask = umask(0002);
my $mkdir_ok = mkdir ($dirsofar, 0777);
umask($store_umask);
if (!$mkdir_ok)
{
print STDERR "util::mk_all_dir could not create directory $dirsofar\n";
return;
}
}
}
}
# make hard link to file if supported by OS, otherwise copy the file
sub hard_link {
my ($src, $dest, $verbosity) = @_;
# remove trailing slashes from source and destination files
$src =~ s/[\\\/]+$//;
$dest =~ s/[\\\/]+$//;
## print STDERR "**** src = ", unicode::debug_unicode_string($src),"\n";
# a few sanity checks
if (-e $dest) {
# destination file already exists
return;
}
elsif (!-e $src) {
print STDERR "util::hard_link source file \"$src\" does not exist\n";
return 1;
}
elsif (-d $src) {
print STDERR "util::hard_link source \"$src\" is a directory\n";
return 1;
}
my $dest_dir = &File::Basename::dirname($dest);
mk_all_dir($dest_dir) if (!-e $dest_dir);
if (!link($src, $dest)) {
if ((!defined $verbosity) || ($verbosity>2)) {
print STDERR "util::hard_link: unable to create hard link. ";
print STDERR " Copying file: $src -> $dest\n";
}
&File::Copy::copy ($src, $dest);
}
return 0;
}
# make soft link to file if supported by OS, otherwise copy file
sub soft_link {
my ($src, $dest, $ensure_paths_absolute) = @_;
# remove trailing slashes from source and destination files
$src =~ s/[\\\/]+$//;
$dest =~ s/[\\\/]+$//;
# Ensure file paths are absolute IF requested to do so
# Soft_linking didn't work for relative paths
if(defined $ensure_paths_absolute && $ensure_paths_absolute) {
# We need to ensure that the src file is the absolute path
# See http://perldoc.perl.org/File/Spec.html
if(!File::Spec->file_name_is_absolute( $src )) { # it's relative
$src = File::Spec->rel2abs($src); # make absolute
}
# Might as well ensure that the destination file's absolute path is used
if(!File::Spec->file_name_is_absolute( $dest )) {
$dest = File::Spec->rel2abs($dest); # make absolute
}
}
# a few sanity checks
if (!-e $src) {
print STDERR "util::soft_link source file $src does not exist\n";
return 0;
}
my $dest_dir = &File::Basename::dirname($dest);
mk_all_dir($dest_dir) if (!-e $dest_dir);
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
# symlink not supported on windows
&File::Copy::copy ($src, $dest);
} elsif (!eval {symlink($src, $dest)}) {
print STDERR "util::soft_link: unable to create soft link.\n";
return 0;
}
return 1;
}
# Primarily for filenames generated by processing
# content of HTML files (which are mapped to UTF-8 internally)
#
# To turn this into an octet string that really exists on the file
# system:
# 1. don't need to do anything special for Unix-based systems
# (as underlying file system is byte-code)
# 2. need to map to short DOS filenames for Windows
sub utf8_to_real_filename
{
my ($utf8_filename) = @_;
my $real_filename;
if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
require Win32;
print STDERR "***** utf8 filename = $utf8_filename\n\n\n";
my $unicode_filename = decode("utf8",$utf8_filename);
$real_filename = Win32::GetShortPathName($unicode_filename);
}
else {
$real_filename = $utf8_filename;
}
return $real_filename;
}
sub fd_exists
{
my $filename_full_path = shift @_;
my $test_op = shift @_ || "-e";
# By default tests for existance of file or directory (-e)
# Can be made more specific by providing second parameter (e.g. -f or -d)
my $exists = 0;
if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
require Win32;
my $filename_short_path = Win32::GetShortPathName($filename_full_path);
if (!defined $filename_short_path) {
# Was probably still in UTF8 form (not what is needed on Windows)
my $unicode_filename_full_path = eval "decode(\"utf8\",\$filename_full_path)";
if (defined $unicode_filename_full_path) {
$filename_short_path = Win32::GetShortPathName($unicode_filename_full_path);
}
}
$filename_full_path = $filename_short_path;
}
if (defined $filename_full_path) {
$exists = eval "($test_op \$filename_full_path)";
}
return $exists;
}
sub file_exists
{
my ($filename_full_path) = @_;
return fd_exists($filename_full_path,"-f");
}
sub dir_exists
{
my ($filename_full_path) = @_;
return fd_exists($filename_full_path,"-d");
}
# updates a copy of a directory in some other part of the filesystem
# verbosity settings are: 0=low, 1=normal, 2=high
# both $fromdir and $todir should be absolute paths
sub cachedir {
my ($fromdir, $todir, $verbosity) = @_;
$verbosity = 1 unless defined $verbosity;
# use / for the directory separator, remove duplicate and
# trailing slashes
$fromdir=~s/[\\\/]+/\//g;
$fromdir=~s/[\\\/]+$//;
$todir=~s/[\\\/]+/\//g;
$todir=~s/[\\\/]+$//;
&mk_all_dir ($todir);
# get the directories in ascending order
if (!opendir (FROMDIR, $fromdir)) {
print STDERR "util::cachedir could not read directory $fromdir\n";
return;
}
my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
closedir (FROMDIR);
if (!opendir (TODIR, $todir)) {
print STDERR "util::cacedir could not read directory $todir\n";
return;
}
my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
closedir (TODIR);
my $fromi = 0;
my $toi = 0;
while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
# print "fromi: $fromi toi: $toi\n";
# see if we should delete a file/directory
# this should happen if the file/directory
# is not in the from list or if its a different
# size, or has an older timestamp
if ($toi < scalar(@todir)) {
if (($fromi >= scalar(@fromdir)) ||
($todir[$toi] lt $fromdir[$fromi] ||
($todir[$toi] eq $fromdir[$fromi] &&
&differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
$verbosity)))) {
# the files are different
&rm_r("$todir/$todir[$toi]");
splice(@todir, $toi, 1); # $toi stays the same
} elsif ($todir[$toi] eq $fromdir[$fromi]) {
# the files are the same
# if it is a directory, check its contents
if (-d "$todir/$todir[$toi]") {
&cachedir ("$fromdir/$fromdir[$fromi]",
"$todir/$todir[$toi]", $verbosity);
}
$toi++;
$fromi++;
next;
}
}
# see if we should insert a file/directory
# we should insert a file/directory if there
# is no tofiles left or if the tofile does not exist
if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
$todir[$toi] gt $fromdir[$fromi])) {
&cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
splice (@todir, $toi, 0, $fromdir[$fromi]);
$toi++;
$fromi++;
}
}
}
# this function returns -1 if either file is not found
# assumes that $file1 and $file2 are absolute file names or
# in the current directory
# $file2 is allowed to be newer than $file1
sub differentfiles {
my ($file1, $file2, $verbosity) = @_;
$verbosity = 1 unless defined $verbosity;
$file1 =~ s/\/+$//;
$file2 =~ s/\/+$//;
my ($file1name) = $file1 =~ /\/([^\/]*)$/;
my ($file2name) = $file2 =~ /\/([^\/]*)$/;
return -1 unless (-e $file1 && -e $file2);
if ($file1name ne $file2name) {
print STDERR "filenames are not the same\n" if ($verbosity >= 2);
return 1;
}
my @file1stat = stat ($file1);
my @file2stat = stat ($file2);
if (-d $file1) {
if (! -d $file2) {
print STDERR "one file is a directory\n" if ($verbosity >= 2);
return 1;
}
return 0;
}
# both must be regular files
unless (-f $file1 && -f $file2) {
print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
return 1;
}
# the size of the files must be the same
if ($file1stat[7] != $file2stat[7]) {
print STDERR "different sized files\n" if ($verbosity >= 2);
return 1;
}
# the second file cannot be older than the first
if ($file1stat[9] > $file2stat[9]) {
print STDERR "file is older\n" if ($verbosity >= 2);
return 1;
}
return 0;
}
sub get_tmp_filename
{
my $file_ext = shift(@_) || undef;
my $opt_dot_file_ext = "";
if (defined $file_ext) {
if ($file_ext !~ m/\./) {
# no dot, so needs one added in at start
$opt_dot_file_ext = ".$file_ext"
}
else {
# allow for "extensions" such as _metadata.txt to be handled
# gracefully
$opt_dot_file_ext = $file_ext;
}
}
my $tmpdir = filename_cat($ENV{'GSDLHOME'}, "tmp");
&mk_all_dir ($tmpdir) unless -e $tmpdir;
my $count = 1000;
my $rand = int(rand $count);
my $full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
while (-e $full_tmp_filename) {
$rand = int(rand $count);
$full_tmp_filename = &filename_cat($tmpdir, "F$rand$opt_dot_file_ext");
$count++;
}
return $full_tmp_filename;
}
sub get_timestamped_tmp_folder
{
my $tmp_dirname;
if(defined $ENV{'GSDLCOLLECTDIR'}) {
$tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
} elsif(defined $ENV{'GSDLHOME'}) {
$tmp_dirname = $ENV{'GSDLHOME'};
} else {
return undef;
}
$tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
&util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
# add the timestamp into the path otherwise we can run into problems
# if documents have the same name
my $timestamp = time;
my $time_tmp_dirname = &util::filename_cat($tmp_dirname, $timestamp);
$tmp_dirname = $time_tmp_dirname;
my $i = 1;
while (-e $tmp_dirname) {
$tmp_dirname = "$time_tmp_dirname$i";
$i++;
}
&util::mk_dir($tmp_dirname);
return $tmp_dirname;
}
sub get_timestamped_tmp_filename_in_collection
{
my ($input_filename, $output_ext) = @_;
# derive tmp filename from input filename
my ($tailname, $dirname, $suffix)
= &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
# softlink to collection tmp dir
my $tmp_dirname = &util::get_timestamped_tmp_folder();
$tmp_dirname = $dirname unless defined $tmp_dirname;
# following two steps copied from ConvertBinaryFile
# do we need them?? can't use them as is, as they use plugin methods.
#$tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
# URLEncode this since htmls with images where the html filename is utf8 don't seem
# to work on Windows (IE or Firefox), as browsers are looking for filesystem-encoded
# files on the filesystem.
#$tailname = &util::rename_file($tailname, $self->{'file_rename_method'}, "without_suffix");
if (defined $output_ext) {
$output_ext = ".$output_ext"; # add the dot
} else {
$output_ext = $suffix;
}
$output_ext= lc($output_ext);
my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$output_ext");
return $tmp_filename;
}
sub get_toplevel_tmp_dir
{
return filename_cat($ENV{'GSDLHOME'}, "tmp");
}
sub filename_to_regex {
my $filename = shift (@_);
# need to make single backslashes double so that regex works
$filename =~ s/\\/\\\\/g; # if ($ENV{'GSDLOS'} =~ /^windows$/i);
# note that the first part of a substitution is a regex, so RE chars need to be escaped,
# the second part of a substitution is not a regex, so for e.g. full-stop can be specified literally
$filename =~ s/\./\\./g; # in case there are extensions/other full stops, escape them
$filename =~ s@\(@\\(@g; # escape brackets
$filename =~ s@\)@\\)@g; # escape brackets
$filename =~ s@\[@\\[@g; # escape brackets
$filename =~ s@\]@\\]@g; # escape brackets
return $filename;
}
sub unregex_filename {
my $filename = shift (@_);
# need to put doubled backslashes for regex back to single
$filename =~ s/\\\./\./g; # remove RE syntax for .
$filename =~ s@\\\(@(@g; # remove RE syntax for ( => "\(" turns into "("
$filename =~ s@\\\)@)@g; # remove RE syntax for ) => "\)" turns into ")"
$filename =~ s@\\\[@[@g; # remove RE syntax for [ => "\[" turns into "["
$filename =~ s@\\\]@]@g; # remove RE syntax for ] => "\]" turns into "]"
# \\ goes to \
# This is the last step in reverse mirroring the order of steps in filename_to_regex()
$filename =~ s/\\\\/\\/g; # remove RE syntax for \
return $filename;
}
sub filename_cat {
my $first_file = shift(@_);
my (@filenames) = @_;
# Useful for debugging
# -- might make sense to call caller(0) rather than (1)??
# my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
# print STDERR "Calling method: $cfilename:$cline $cpackage->$csubr\n";
# If first_file is not null or empty, then add it back into the list
if (defined $first_file && $first_file =~ /\S/) {
unshift(@filenames, $first_file);
}
my $filename = join("/", @filenames);
# remove duplicate slashes and remove the last slash
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
$filename =~ s/[\\\/]+/\\/g;
} else {
$filename =~ s/[\/]+/\//g;
# DB: want a filename abc\de.html to remain like this
}
$filename =~ s/[\\\/]$//;
return $filename;
}
sub pathname_cat {
my $first_path = shift(@_);
my (@pathnames) = @_;
# If first_path is not null or empty, then add it back into the list
if (defined $first_path && $first_path =~ /\S/) {
unshift(@pathnames, $first_path);
}
my $join_char;
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
$join_char = ";";
} else {
$join_char = ":";
}
my $pathname = join($join_char, @pathnames);
# remove duplicate slashes
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
$pathname =~ s/[\\\/]+/\\/g;
} else {
$pathname =~ s/[\/]+/\//g;
# DB: want a pathname abc\de.html to remain like this
}
return $pathname;
}
sub tidy_up_oid {
my ($OID) = @_;
if ($OID =~ /\./) {
print STDERR "Warning, identifier $OID contains periods (.), removing them\n";
$OID =~ s/\.//g; #remove any periods
}
if ($OID =~ /^\s.*\s$/) {
print STDERR "Warning, identifier $OID starts or ends with whitespace. Removing it\n";
# remove starting and trailing whitespace
$OID =~ s/^\s+//;
$OID =~ s/\s+$//;
}
if ($OID =~ /^[\d]*$/) {
print STDERR "Warning, identifier $OID contains only digits. Prepending 'D'.\n";
$OID = "D" . $OID;
}
return $OID;
}
sub envvar_prepend {
my ($var,$val) = @_;
# do not prepend any value/path that's already in the environment variable
my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
if (!defined($ENV{$var})) {
$ENV{$var} = "$val";
}
elsif($ENV{$var} !~ m/$escaped_val/) {
$ENV{$var} = "$val;".$ENV{$var};
}
}
sub envvar_append {
my ($var,$val) = @_;
# do not append any value/path that's already in the environment variable
my $escaped_val = &filename_to_regex($val); # escape any backslashes and brackets for upcoming regex
if (!defined($ENV{$var})) {
$ENV{$var} = "$val";
}
elsif($ENV{$var} !~ m/$escaped_val/) {
$ENV{$var} .= ";$val";
}
}
# splits a filename into a prefix and a tail extension using the tail_re, or
# if that fails, splits on the file_extension . (dot)
sub get_prefix_and_tail_by_regex {
my ($filename,$tail_re) = @_;
my ($file_prefix,$file_ext) = ($filename =~ m/^(.*?)($tail_re)$/);
if ((!defined $file_prefix) || (!defined $file_ext)) {
($file_prefix,$file_ext) = ($filename =~ m/^(.*)(\..*?)$/);
}
return ($file_prefix,$file_ext);
}
# get full path and file only path from a base_dir (which may be empty) and
# file (which may contain directories)
sub get_full_filenames {
my ($base_dir, $file) = @_;
my $filename_full_path = $file;
# add on directory if present
$filename_full_path = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\S/;
my $filename_no_path = $file;
# remove directory if present
$filename_no_path =~ s/^.*[\/\\]//;
return ($filename_full_path, $filename_no_path);
}
# returns the path of a file without the filename -- ie. the directory the file is in
sub filename_head {
my $filename = shift(@_);
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
$filename =~ s/[^\\\\]*$//;
}
else {
$filename =~ s/[^\\\/]*$//;
}
return $filename;
}
# returns 1 if filename1 and filename2 point to the same
# file or directory
sub filenames_equal {
my ($filename1, $filename2) = @_;
# use filename_cat to clean up trailing slashes and
# multiple slashes
$filename1 = filename_cat ($filename1);
$filename2 = filename_cat ($filename2);
# filenames not case sensitive on windows
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
$filename1 =~ tr/[A-Z]/[a-z]/;
$filename2 =~ tr/[A-Z]/[a-z]/;
}
return 1 if $filename1 eq $filename2;
return 0;
}
# If filename is relative to within_dir, returns the relative path of filename to that directory
# with slashes in the filename returned as they were in the original (absolute) filename.
sub filename_within_directory
{
my ($filename,$within_dir) = @_;
if ($within_dir !~ m/[\/\\]$/) {
my $dirsep = &util::get_dirsep();
$within_dir .= $dirsep;
}
$within_dir = &filename_to_regex($within_dir); # escape DOS style file separator and brackets
if ($filename =~ m/^$within_dir(.*)$/) {
$filename = $1;
}
return $filename;
}
# If filename is relative to within_dir, returns the relative path of filename to that directory in URL format.
# Filename and within_dir can be any type of slashes, but will be compared as URLs (i.e. unix-style slashes).
# The subpath returned will also be a URL type filename.
sub filename_within_directory_url_format
{
my ($filename,$within_dir) = @_;
# convert parameters only to / slashes if Windows
my $filename_urlformat = &filepath_to_url_format($filename);
my $within_dir_urlformat = &filepath_to_url_format($within_dir);
#if ($within_dir_urlformat !~ m/\/$/) {
# make sure directory ends with a slash
#$within_dir_urlformat .= "/";
#}
my $within_dir_urlformat_re = &filename_to_regex($within_dir_urlformat); # escape any special RE characters, such as brackets
#print STDERR "@@@@@ $filename_urlformat =~ $within_dir_urlformat_re\n";
# dir prefix may or may not end with a slash (this is discarded when extracting the sub-filepath)
if ($filename_urlformat =~ m/^$within_dir_urlformat_re(?:\/)*(.*)$/) {
$filename_urlformat = $1;
}
return $filename_urlformat;
}
# Convert parameter to use / slashes if Windows (if on Linux leave any \ as is,
# since on Linux it doesn't represent a file separator but an escape char).
sub filepath_to_url_format
{
my ($filepath) = @_;
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
# Only need to worry about Windows, as Unix style directories already in url-format
# Convert Windows style \ => /
$filepath =~ s@\\@/@g;
}
return $filepath;
}
# regex filepaths on windows may include \\ as path separator. Convert \\ to /
sub filepath_regex_to_url_format
{
my ($filepath) = @_;
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
# Only need to worry about Windows, as Unix style directories already in url-format
# Convert Windows style \\ => /
$filepath =~ s@\\\\@/@g;
}
return $filepath;
}
# Like File::Basename::fileparse, but expects filepath in url format (ie only / slash for dirsep)
# and ignores trailing /
# returns (file, dirs) dirs will be empty if no subdirs
sub url_fileparse
{
my ($filepath) = @_;
# remove trailing /
$filepath =~ s@/$@@;
if ($filepath !~ m@/@) {
return ($filepath, "");
}
my ($dirs, $file) = $filepath =~ m@(.+/)([^/]+)@;
return ($file, $dirs);
}
sub filename_within_collection
{
my ($filename) = @_;
my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
if (defined $collect_dir) {
# if from within GSDLCOLLECTDIR, then remove directory prefix
# so source_filename is realative to it. This is done to aid
# portability, i.e. the collection can be moved to somewhere
# else on the file system and the archives directory will still
# work. This is needed, for example in the applet version of
# GLI where GSDLHOME/collect on the server will be different to
# the collect directory of the remove user. Of course,
# GSDLCOLLECTDIR subsequently needs to be put back on to turn
# it back into a full pathname.
$filename = filename_within_directory($filename,$collect_dir);
}
return $filename;
}
sub prettyprint_file
{
my ($base_dir,$file,$gli) = @_;
my $filename_full_path = &util::filename_cat($base_dir,$file);
if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
require Win32;
# For some reason base_dir in the form c:/a/b/c
# This leads to confusion later on, so turn it back into
# the more usual Windows form
$base_dir =~ s/\//\\/g;
my $long_base_dir = Win32::GetLongPathName($base_dir);
my $long_full_path = Win32::GetLongPathName($filename_full_path);
$file = filename_within_directory($long_full_path,$long_base_dir);
$file = encode("utf8",$file) if ($gli);
}
return $file;
}
sub upgrade_if_dos_filename
{
my ($filename_full_path,$and_encode) = @_;
if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
# Ensure any DOS-like filename, such as test~1.txt, has been upgraded
# to its long (Windows) version
my $long_filename = Win32::GetLongPathName($filename_full_path);
if (defined $long_filename) {
$filename_full_path = $long_filename;
}
# Make sure initial drive letter is lower-case (to fit in with rest of Greenstone)
$filename_full_path =~ s/^(.):/\u$1:/;
if ((defined $and_encode) && ($and_encode)) {
$filename_full_path = encode("utf8",$filename_full_path);
}
}
return $filename_full_path;
}
sub downgrade_if_dos_filename
{
my ($filename_full_path) = @_;
if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
require Win32;
# Ensure the given long Windows filename is in a form that can
# be opened by Perl => convert it to a short DOS-like filename
my $short_filename = Win32::GetShortPathName($filename_full_path);
if (defined $short_filename) {
$filename_full_path = $short_filename;
}
# Make sure initial drive letter is lower-case (to fit in
# with rest of Greenstone)
$filename_full_path =~ s/^(.):/\u$1:/;
}
return $filename_full_path;
}
sub block_filename
{
my ($block_hash,$filename) = @_;
if ($ENV{'GSDLOS'} =~ m/^windows$/) {
# lower case the entire thing, eg for cover.jpg when its actually cover.JPG
my $lower_filename = lc($filename);
$block_hash->{'file_blocks'}->{$lower_filename} = 1;
# my $lower_drive = $filename;
# $lower_drive =~ s/^([A-Z]):/\l$1:/i;
# my $upper_drive = $filename;
# $upper_drive =~ s/^([A-Z]):/\u$1:/i;
#
# $block_hash->{'file_blocks'}->{$lower_drive} = 1;
# $block_hash->{'file_blocks'}->{$upper_drive} = 1;
}
else {
$block_hash->{'file_blocks'}->{$filename} = 1;
}
}
sub filename_is_absolute
{
my ($filename) = @_;
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
return ($filename =~ m/^(\w:)?\\/);
}
else {
return ($filename =~ m/^\//);
}
}
## @method make_absolute()
#
# Ensure the given file path is absolute in respect to the given base path.
#
# @param $base_dir A string denoting the base path the given dir must be
# absolute to.
# @param $dir The directory to be made absolute as a string. Note that the
# dir may already be absolute, in which case it will remain
# unchanged.
# @return The now absolute form of the directory as a string.
#
# @author John Thompson, DL Consulting Ltd.
# @copy 2006 DL Consulting Ltd.
#
#used in buildcol.pl, doesn't work for all cases --kjdon
sub make_absolute {
my ($base_dir, $dir) = @_;
### print STDERR "dir = $dir\n";
$dir =~ s/[\\\/]+/\//g;
$dir = $base_dir . "/$dir" unless ($dir =~ m|^(\w:)?/|);
$dir =~ s|^/tmp_mnt||;
1 while($dir =~ s|/[^/]*/\.\./|/|g);
$dir =~ s|/[.][.]?/|/|g;
$dir =~ tr|/|/|s;
### print STDERR "dir = $dir\n";
return $dir;
}
## make_absolute() ##
sub get_dirsep {
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
return "\\";
} else {
return "\/";
}
}
sub get_os_dirsep {
if ($ENV{'GSDLOS'} =~ /^windows$/i) {
return "\\\\";
} else {
return "\\\/";
}
}
sub get_re_dirsep {
return "\\\\|\\\/";
}
sub get_dirsep_tail {
my ($filename) = @_;
# returns last part of directory or filename
# On unix e.g. a/b.d => b.d
# a/b/c => c
my $dirsep = get_re_dirsep();
my @dirs = split (/$dirsep/, $filename);
my $tail = pop @dirs;
# - caused problems under windows
#my ($tail) = ($filename =~ m/^(?:.*?$dirsep)?(.*?)$/);
return $tail;
}
# if this is running on windows we want binaries to end in
# .exe, otherwise they don't have to end in any extension
sub get_os_exe {
return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
return "";
}
# test to see whether this is a big or little endian machine
sub is_little_endian
{
# To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
# 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
# Otherwise, it's little endian
#return 0 if $^O =~ /^darwin$/i;
#return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
# Going back to stating exactly whether the machine is little endian
# or big endian, without any special case for Macs. Since for rata it comes
# back with little endian and for shuttle with bigendian.
return (ord(substr(pack("s",1), 0, 1)) == 1);
}
# will return the collection name if successful, "" otherwise
sub use_collection {
my ($collection, $collectdir) = @_;
if (!defined $collectdir || $collectdir eq "") {
$collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
}
# get and check the collection
if (!defined($collection) || $collection eq "") {
if (defined $ENV{'GSDLCOLLECTION'}) {
$collection = $ENV{'GSDLCOLLECTION'};
} else {
print STDOUT "No collection specified\n";
return "";
}
}
if ($collection eq "modelcol") {
print STDOUT "You can't use modelcol.\n";
return "";
}
# make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
# are defined
$ENV{'GSDLCOLLECTION'} = $collection;
$ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
# make sure this collection exists
if (!-e $ENV{'GSDLCOLLECTDIR'}) {
print STDOUT "Invalid collection ($collection).\n";
return "";
}
# everything is ready to go
return $collection;
}
sub get_current_collection_name {
return $ENV{'GSDLCOLLECTION'};
}
# will return the collection name if successful, "" otherwise.
# Like use_collection (above) but for greenstone 3 (taking account of site level)
sub use_site_collection {
my ($site, $collection, $collectdir) = @_;
if (!defined $collectdir || $collectdir eq "") {
die "GSDL3HOME not set.\n" unless defined $ENV{'GSDL3HOME'};
$collectdir = &filename_cat ($ENV{'GSDL3HOME'}, "sites", $site, "collect");
}
# collectdir explicitly set by this point (using $site variable if required).
# Can call "old" gsdl2 use_collection now.
return use_collection($collection,$collectdir);
}
sub locate_config_file
{
my ($file) = @_;
my $locations = locate_config_files($file);
return shift @$locations; # returns undef if 'locations' is empty
}
sub locate_config_files
{
my ($file) = @_;
my @locations = ();
if (-e $file) {
# Clearly specified (most likely full filename)
# No need to hunt in 'etc' directories, return value unchanged
push(@locations,$file);
}
else {
# Check for collection specific one before looking in global GSDL 'etc'
if (defined $ENV{'GSDLCOLLECTDIR'} && $ENV{'GSDLCOLLECTDIR'} ne "") {
my $test_collect_etc_filename
= &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"etc", $file);
if (-e $test_collect_etc_filename) {
push(@locations,$test_collect_etc_filename);
}
}
my $test_main_etc_filename
= &util::filename_cat($ENV{'GSDLHOME'},"etc", $file);
if (-e $test_main_etc_filename) {
push(@locations,$test_main_etc_filename);
}
}
return \@locations;
}
sub hyperlink_text
{
my ($text) = @_;
$text =~ s/(http:\/\/[^\s]+)/$1<\/a>/mg;
$text =~ s/(^|\s+)(www\.(\w|\.)+)/$2<\/a>/mg;
return $text;
}
# A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
# Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
sub is_dir_empty
{
my ($path) = @_;
opendir DIR, $path;
while(my $entry = readdir DIR) {
next if($entry =~ /^\.\.?$/);
closedir DIR;
return 0;
}
closedir DIR;
return 1;
}
# Returns the given filename converted using either URL encoding or base64
# encoding, as specified by $rename_method. If the given filename has no suffix
# (if it is just the tailname), then $no_suffix should be some defined value.
# rename_method can be url, none, base64
sub rename_file {
my ($filename, $rename_method, $no_suffix) = @_;
if(!$filename) { # undefined or empty string
return $filename;
}
if (!$rename_method) {
print STDERR "WARNING: no file renaming method specified. Defaulting to using URL encoding...\n";
# Debugging information
# my ($cpackage,$cfilename,$cline,$csubr,$chas_args,$cwantarray) = caller(1);
# print STDERR "Called from method: $cfilename:$cline $cpackage->$csubr\n";
$rename_method = "url";
} elsif($rename_method eq "none") {
return $filename; # would have already been renamed
}
# No longer replace spaces with underscores, since underscores mess with incremental rebuild
### Replace spaces with underscore. Do this first else it can go wrong below when getting tailname
###$filename =~ s/ /_/g;
my ($tailname,$dirname,$suffix);
if($no_suffix) { # given a tailname, no suffix
($tailname,$dirname) = File::Basename::fileparse($filename);
}
else {
($tailname,$dirname,$suffix) = File::Basename::fileparse($filename, "\\.(?:[^\\.]+?)\$");
}
if (!$suffix) {
$suffix = "";
}
else {
$suffix = lc($suffix);
}
if ($rename_method eq "url") {
$tailname = &unicode::url_encode($tailname);
}
elsif ($rename_method eq "base64") {
$tailname = &unicode::base64_encode($tailname);
$tailname =~ s/\s*//sg; # for some reason it adds spaces not just at end but also in middle
}
$filename = "$tailname$suffix";
$filename = "$dirname$filename" if ($dirname ne "./" && $dirname ne ".\\");
return $filename;
}
# BACKWARDS COMPATIBILITY: Just in case there are old .ldb/.bdb files
sub rename_ldb_or_bdb_file {
my ($filename_no_ext) = @_;
my $new_filename = "$filename_no_ext.gdb";
return if (-f $new_filename); # if the file has the right extension, don't need to do anything
# try ldb
my $old_filename = "$filename_no_ext.ldb";
if (-f $old_filename) {
print STDERR "Renaming $old_filename to $new_filename\n";
rename ($old_filename, $new_filename)
|| print STDERR "Rename failed: $!\n";
return;
}
# try bdb
$old_filename = "$filename_no_ext.bdb";
if (-f $old_filename) {
print STDERR "Renaming $old_filename to $new_filename\n";
rename ($old_filename, $new_filename)
|| print STDERR "Rename failed: $!\n";
return;
}
}
sub os_dir() {
my $gsdlarch = "";
if(defined $ENV{'GSDLARCH'}) {
$gsdlarch = $ENV{'GSDLARCH'};
}
return $ENV{'GSDLOS'}.$gsdlarch;
}
# Returns the greenstone URL prefix extracted from the appropriate GS2/GS3 config file.
# By default, /greenstone3 for GS3 or /greenstone for GS2.
sub get_greenstone_url_prefix() {
# if already set on a previous occasion, just return that
# (Don't want to keep repeating this: cost of re-opening and scanning files.)
return $ENV{'GREENSTONE_URL_PREFIX'} if($ENV{'GREENSTONE_URL_PREFIX'});
my ($configfile, $urlprefix, $defaultUrlprefix);
my @propertynames = ();
if($ENV{'GSDL3SRCHOME'}) {
$defaultUrlprefix = "/greenstone3";
$configfile = &util::filename_cat($ENV{'GSDL3SRCHOME'}, "packages", "tomcat", "conf", "Catalina", "localhost", "greenstone3.xml");
push(@propertynames, qw/path\s*\=/);
} else {
$defaultUrlprefix = "/greenstone";
$configfile = &util::filename_cat($ENV{'GSDLHOME'}, "cgi-bin", &os_dir(), "gsdlsite.cfg");
push(@propertynames, (qw/\nhttpprefix/, qw/\ngwcgi/)); # inspect one property then the other
}
$urlprefix = &extract_propvalue_from_file($configfile, \@propertynames);
if(!$urlprefix) { # no values found for URL prefix, use default values
$urlprefix = $defaultUrlprefix;
} else {
#gwcgi can contain more than the wanted prefix, we split on / to get the first "directory" level
$urlprefix =~ s/^\///; # remove the starting slash
my @dirs = split(/(\\|\/)/, $urlprefix);
$urlprefix = shift(@dirs);
if($urlprefix !~ m/^\//) { # in all cases: ensure the required forward slash is at the front
$urlprefix = "/$urlprefix";
}
}
# set for the future
$ENV{'GREENSTONE_URL_PREFIX'} = $urlprefix;
# print STDERR "*** in get_greenstone_url_prefix(): $urlprefix\n\n";
return $urlprefix;
}
# Given a config file (xml or java properties file) and a list/array of regular expressions
# that represent property names to match on, this function will return the value for the 1st
# matching property name. If the return value is undefined, no matching property was found.
sub extract_propvalue_from_file() {
my ($configfile, $propertynames) = @_;
my $value;
unless(open(FIN, "<$configfile")) {
print STDERR "extract_propvalue_from_file(): Unable to open $configfile. $!\n";
return $value; # not initialised
}
# Read the entire file at once, as one single line, then close it
my $filecontents;
{
local $/ = undef;
$filecontents = ;
}
close(FIN);
foreach my $regex (@$propertynames) {
($value) = $filecontents=~ m/$regex\s*(\S*)/s; # read value of the property given by regex up to the 1st space
if($value) {
$value =~ s/^\"//; # remove any startquotes
$value =~ s/\".*$//; # remove the 1st endquotes (if any) followed by any xml
last; # found value for a matching property, break from loop
}
}
return $value;
}
# Subroutine that sources setup.bash, given GSDLHOME and GSDLOS and
# given that perllib is in @INC in order to invoke this subroutine.
# Call as follows -- after setting up INC to include perllib and
# after setting up GSDLHOME and GSDLOS:
#
# require util;
# &util::setup_greenstone_env($ENV{'GSDLHOME'}, $ENV{'GSDLOS'});
#
sub setup_greenstone_env() {
my ($GSDLHOME, $GSDLOS) = @_;
#my %env_map = ();
# Get the localised ENV settings of running a localised source setup.bash
# and put it into the ENV here. Need to clear GSDLHOME before running setup
#my $perl_command = "(cd $GSDLHOME; export GSDLHOME=; . ./setup.bash > /dev/null; env)";
my $perl_command = "(cd $GSDLHOME; /bin/bash -c \"export GSDLHOME=; source setup.bash > /dev/null; env\")";
if($GSDLOS =~ m/windows/i) {
#$perl_command = "cmd /C \"cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set\"";
$perl_command = "(cd $GSDLHOME&& set GSDLHOME=&& setup.bat > nul&& set)";
}
if (!open(PIN, "$perl_command |")) {
print STDERR ("Unable to execute command: $perl_command. $!\n");
}
while (defined (my $perl_output_line = )) {
my($key,$value) = ($perl_output_line =~ m/^([^=]*)[=](.*)$/);
#$env_map{$key}=$value;
$ENV{$key}=$value;
}
close (PIN);
# If any keys in $ENV don't occur in Greenstone's localised env
# (stored in $env_map), delete those entries from $ENV
#foreach $key (keys %ENV) {
# if(!defined $env_map{$key}) {
# print STDOUT "**** DELETING ENV KEY: $key\tVALUE: $ENV{'$key'}\n";
# delete $ENV{$key}; # del $ENV(key, value) pair
# }
#}
#undef %env_map;
}
sub get_perl_exec() {
my $perl_exec = $^X; # may return just "perl"
if($ENV{'PERLPATH'}) {
# OR: # $perl_exec = &util::filename_cat($ENV{'PERLPATH'},"perl");
if($ENV{'GSDLOS'} =~ m/windows/) {
$perl_exec = "$ENV{'PERLPATH'}\\Perl.exe";
} else {
$perl_exec = "$ENV{'PERLPATH'}/perl";
}
} else { # no PERLPATH, use Config{perlpath} else $^X: special variables
# containing the full path to the current perl executable we're using
$perl_exec = $Config{perlpath}; # configured path for perl
if (!-e $perl_exec) { # may not point to location on this machine
$perl_exec = $^X; # may return just "perl"
if($perl_exec =~ m/^perl/i) { # warn if just perl or Perl.exe
print STDERR "**** WARNING: Perl exec found contains no path: $perl_exec";
}
}
}
return $perl_exec;
}
# returns the path to the java command in the JRE included with GS (if any),
# quoted to safeguard any spaces in this path, otherwise a simple java
# command is returned which assumes and will try for a system java.
sub get_java_command {
my $java = "java";
if(defined $ENV{'GSDLHOME'}) { # should be, as this script would be launched from the cmd line
# after running setup.bat or from GLI which also runs setup.bat
my $java_bin = &util::filename_cat($ENV{'GSDLHOME'},"packages","jre","bin");
if(-d $java_bin) {
$java = &util::filename_cat($java_bin,"java");
$java = "\"".$java."\""; # quoted to preserve spaces in path
}
}
return $java;
}
# Given the qualified collection name (colgroup/collection),
# returns the collection and colgroup parts
sub get_collection_parts {
# http://perldoc.perl.org/File/Basename.html
# my($filename, $directories, $suffix) = fileparse($path);
# "$directories contains everything up to and including the last directory separator in the $path
# including the volume (if applicable). The remainder of the $path is the $filename."
#my ($collection, $colgroup) = &File::Basename::fileparse($qualified_collection);
my $qualified_collection = shift(@_);
# Since activate.pl can be launched from the command-line, including by a user,
# best not to assume colgroup uses URL-style slashes as would be the case with GLI
# Also allow for the accidental inclusion of multiple slashes
my ($colgroup, $collection) = split(/[\/\\]+/, $qualified_collection); #split('/', $qualified_collection);
if(!defined $collection) {
$collection = $colgroup;
$colgroup = "";
}
return ($collection, $colgroup);
}
# work out the "collectdir/collection" location
sub resolve_collection_dir {
my ($collect_dir, $qualified_collection, $site) = @_; #, $gs_mode
my ($colgroup, $collection) = &util::get_collection_parts($qualified_collection);
if (defined $collect_dir) {
return &util::filename_cat($collect_dir,$colgroup, $collection);
}
else {
if (defined $site) {
return &util::filename_cat($ENV{'GSDL3HOME'},"sites",$site,"collect",$colgroup, $collection);
}
else {
return &util::filename_cat($ENV{'GSDLHOME'},"collect",$colgroup, $collection);
}
}
}
1;