1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # util.pm -- various useful utilities
|
---|
4 | # A component of the Greenstone digital library software
|
---|
5 | # from the New Zealand Digital Library Project at the
|
---|
6 | # University of Waikato, New Zealand.
|
---|
7 | #
|
---|
8 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
9 | #
|
---|
10 | # This program is free software; you can redistribute it and/or modify
|
---|
11 | # it under the terms of the GNU General Public License as published by
|
---|
12 | # the Free Software Foundation; either version 2 of the License, or
|
---|
13 | # (at your option) any later version.
|
---|
14 | #
|
---|
15 | # This program is distributed in the hope that it will be useful,
|
---|
16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | # GNU General Public License for more details.
|
---|
19 | #
|
---|
20 | # You should have received a copy of the GNU General Public License
|
---|
21 | # along with this program; if not, write to the Free Software
|
---|
22 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
23 | #
|
---|
24 | ###########################################################################
|
---|
25 |
|
---|
26 | package util;
|
---|
27 |
|
---|
28 | use File::Copy;
|
---|
29 | use File::Basename;
|
---|
30 |
|
---|
31 | # removes files (but not directories)
|
---|
32 | sub rm {
|
---|
33 | my (@files) = @_;
|
---|
34 | my @filefiles = ();
|
---|
35 |
|
---|
36 | # make sure the files we want to delete exist
|
---|
37 | # and are regular files
|
---|
38 | foreach my $file (@files) {
|
---|
39 | if (!-e $file) {
|
---|
40 | print STDERR "util::rm $file does not exist\n";
|
---|
41 | } elsif ((!-f $file) && (!-l $file)) {
|
---|
42 | print STDERR "util::rm $file is not a regular (or symbolic) file\n";
|
---|
43 | } else {
|
---|
44 | push (@filefiles, $file);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | # remove the files
|
---|
49 | my $numremoved = unlink @filefiles;
|
---|
50 |
|
---|
51 | # check to make sure all of them were removed
|
---|
52 | if ($numremoved != scalar(@filefiles)) {
|
---|
53 | print STDERR "util::rm Not all files were removed\n";
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 |
|
---|
59 | # recursive removal
|
---|
60 | sub filtered_rm_r {
|
---|
61 | my ($files,$file_accept_re,$file_reject_re) = @_;
|
---|
62 |
|
---|
63 | my @files_array = (ref $files eq "ARRAY") ? @$files : ($files);
|
---|
64 |
|
---|
65 | # recursively remove the files
|
---|
66 | foreach my $file (@files_array) {
|
---|
67 | $file =~ s/[\/\\]+$//; # remove trailing slashes
|
---|
68 |
|
---|
69 | if (!-e $file) {
|
---|
70 | print STDERR "util::filtered_rm_r $file does not exist\n";
|
---|
71 |
|
---|
72 | } elsif ((-d $file) && (!-l $file)) { # don't recurse down symbolic link
|
---|
73 | # get the contents of this directory
|
---|
74 | if (!opendir (INDIR, $file)) {
|
---|
75 | print STDERR "util::filtered_rm_r could not open directory $file\n";
|
---|
76 | } else {
|
---|
77 | my @filedir = grep (!/^\.\.?$/, readdir (INDIR));
|
---|
78 | closedir (INDIR);
|
---|
79 |
|
---|
80 | # remove all the files in this directory
|
---|
81 | map {$_="$file/$_";} @filedir;
|
---|
82 | &filtered_rm_r (\@filedir,$file_accept_re,$file_reject_re);
|
---|
83 |
|
---|
84 | if (!defined $file_accept_re && !defined $file_reject_re) {
|
---|
85 | # remove this directory
|
---|
86 | if (!rmdir $file) {
|
---|
87 | print STDERR "util::filtered_rm_r couldn't remove directory $file\n";
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 | } else {
|
---|
92 | next if (defined $file_reject_re && ($file =~ m/$file_reject_re/));
|
---|
93 |
|
---|
94 | if ((!defined $file_accept_re) || ($file =~ m/$file_accept_re/)) {
|
---|
95 | # remove this file
|
---|
96 | &rm ($file);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | # recursive removal
|
---|
104 | sub rm_r {
|
---|
105 | my (@files) = @_;
|
---|
106 |
|
---|
107 | # use the more general (but reterospectively written function
|
---|
108 | # filtered_rm_r function()
|
---|
109 |
|
---|
110 | filtered_rm_r(\@files,undef,undef); # no accept or reject expressions
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 | # moves a file or a group of files
|
---|
117 | sub mv {
|
---|
118 | my $dest = pop (@_);
|
---|
119 | my (@srcfiles) = @_;
|
---|
120 |
|
---|
121 | # remove trailing slashes from source and destination files
|
---|
122 | $dest =~ s/[\\\/]+$//;
|
---|
123 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
124 |
|
---|
125 | # a few sanity checks
|
---|
126 | if (scalar (@srcfiles) == 0) {
|
---|
127 | print STDERR "util::mv no destination directory given\n";
|
---|
128 | return;
|
---|
129 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
130 | print STDERR "util::mv if multiple source files are given the ".
|
---|
131 | "destination must be a directory\n";
|
---|
132 | return;
|
---|
133 | }
|
---|
134 |
|
---|
135 | # move the files
|
---|
136 | foreach my $file (@srcfiles) {
|
---|
137 | my $tempdest = $dest;
|
---|
138 | if (-d $tempdest) {
|
---|
139 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
140 | $tempdest .= "/$filename";
|
---|
141 | }
|
---|
142 | if (!-e $file) {
|
---|
143 | print STDERR "util::mv $file does not exist\n";
|
---|
144 | } else {
|
---|
145 | rename ($file, $tempdest);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | # copies a file or a group of files
|
---|
152 | sub cp {
|
---|
153 | my $dest = pop (@_);
|
---|
154 | my (@srcfiles) = @_;
|
---|
155 |
|
---|
156 | # remove trailing slashes from source and destination files
|
---|
157 | $dest =~ s/[\\\/]+$//;
|
---|
158 | map {$_ =~ s/[\\\/]+$//;} @srcfiles;
|
---|
159 |
|
---|
160 | # a few sanity checks
|
---|
161 | if (scalar (@srcfiles) == 0) {
|
---|
162 | print STDERR "util::cp no destination directory given\n";
|
---|
163 | return;
|
---|
164 | } elsif ((scalar (@srcfiles) > 1) && (!-d $dest)) {
|
---|
165 | print STDERR "util::cp if multiple source files are given the ".
|
---|
166 | "destination must be a directory\n";
|
---|
167 | return;
|
---|
168 | }
|
---|
169 |
|
---|
170 | # copy the files
|
---|
171 | foreach my $file (@srcfiles) {
|
---|
172 | my $tempdest = $dest;
|
---|
173 | if (-d $tempdest) {
|
---|
174 | my ($filename) = $file =~ /([^\\\/]+)$/;
|
---|
175 | $tempdest .= "/$filename";
|
---|
176 | }
|
---|
177 | if (!-e $file) {
|
---|
178 | print STDERR "util::cp $file does not exist\n";
|
---|
179 | } elsif (!-f $file) {
|
---|
180 | print STDERR "util::cp $file is not a plain file\n";
|
---|
181 | } else {
|
---|
182 | &File::Copy::copy ($file, $tempdest);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 |
|
---|
189 | # recursively copies a file or group of files
|
---|
190 | # syntax: cp_r (sourcefiles, destination directory)
|
---|
191 | # destination must be a directory - to copy one file to
|
---|
192 | # another use cp instead
|
---|
193 | sub cp_r {
|
---|
194 | my $dest = pop (@_);
|
---|
195 | my (@srcfiles) = @_;
|
---|
196 |
|
---|
197 | # a few sanity checks
|
---|
198 | if (scalar (@srcfiles) == 0) {
|
---|
199 | print STDERR "util::cp_r no destination directory given\n";
|
---|
200 | return;
|
---|
201 | } elsif (-f $dest) {
|
---|
202 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
203 | return;
|
---|
204 | }
|
---|
205 |
|
---|
206 | # create destination directory if it doesn't exist already
|
---|
207 | if (! -d $dest) {
|
---|
208 | my $store_umask = umask(0002);
|
---|
209 | mkdir ($dest, 0777);
|
---|
210 | umask($store_umask);
|
---|
211 | }
|
---|
212 |
|
---|
213 | # copy the files
|
---|
214 | foreach my $file (@srcfiles) {
|
---|
215 |
|
---|
216 | if (!-e $file) {
|
---|
217 | print STDERR "util::cp_r $file does not exist\n";
|
---|
218 |
|
---|
219 | } elsif (-d $file) {
|
---|
220 | # make the new directory
|
---|
221 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
222 | $dest = &util::filename_cat ($dest, $filename);
|
---|
223 | my $store_umask = umask(0002);
|
---|
224 | mkdir ($dest, 0777);
|
---|
225 | umask($store_umask);
|
---|
226 |
|
---|
227 | # get the contents of this directory
|
---|
228 | if (!opendir (INDIR, $file)) {
|
---|
229 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
230 | } else {
|
---|
231 | my @filedir = readdir (INDIR);
|
---|
232 | closedir (INDIR);
|
---|
233 | foreach my $f (@filedir) {
|
---|
234 | next if $f =~ /^\.\.?$/;
|
---|
235 | # copy all the files in this directory
|
---|
236 | my $ff = &util::filename_cat ($file, $f);
|
---|
237 | &cp_r ($ff, $dest);
|
---|
238 | }
|
---|
239 | }
|
---|
240 |
|
---|
241 | } else {
|
---|
242 | &cp($file, $dest);
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | # copies a directory and its contents, excluding subdirectories, into a new directory
|
---|
248 | sub cp_r_toplevel {
|
---|
249 | my $dest = pop (@_);
|
---|
250 | my (@srcfiles) = @_;
|
---|
251 |
|
---|
252 | # a few sanity checks
|
---|
253 | if (scalar (@srcfiles) == 0) {
|
---|
254 | print STDERR "util::cp_r no destination directory given\n";
|
---|
255 | return;
|
---|
256 | } elsif (-f $dest) {
|
---|
257 | print STDERR "util::cp_r destination must be a directory\n";
|
---|
258 | return;
|
---|
259 | }
|
---|
260 |
|
---|
261 | # create destination directory if it doesn't exist already
|
---|
262 | if (! -d $dest) {
|
---|
263 | my $store_umask = umask(0002);
|
---|
264 | mkdir ($dest, 0777);
|
---|
265 | umask($store_umask);
|
---|
266 | }
|
---|
267 |
|
---|
268 | # copy the files
|
---|
269 | foreach my $file (@srcfiles) {
|
---|
270 |
|
---|
271 | if (!-e $file) {
|
---|
272 | print STDERR "util::cp_r $file does not exist\n";
|
---|
273 |
|
---|
274 | } elsif (-d $file) {
|
---|
275 | # make the new directory
|
---|
276 | my ($filename) = $file =~ /([^\\\/]*)$/;
|
---|
277 | $dest = &util::filename_cat ($dest, $filename);
|
---|
278 | my $store_umask = umask(0002);
|
---|
279 | mkdir ($dest, 0777);
|
---|
280 | umask($store_umask);
|
---|
281 |
|
---|
282 | # get the contents of this directory
|
---|
283 | if (!opendir (INDIR, $file)) {
|
---|
284 | print STDERR "util::cp_r could not open directory $file\n";
|
---|
285 | } else {
|
---|
286 | my @filedir = readdir (INDIR);
|
---|
287 | closedir (INDIR);
|
---|
288 | foreach my $f (@filedir) {
|
---|
289 | next if $f =~ /^\.\.?$/;
|
---|
290 |
|
---|
291 | # copy all the files in this directory, but not directories
|
---|
292 | my $ff = &util::filename_cat ($file, $f);
|
---|
293 | if (-f $ff) {
|
---|
294 | &cp($ff, $dest);
|
---|
295 | #&cp_r ($ff, $dest);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 |
|
---|
300 | } else {
|
---|
301 | &cp($file, $dest);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|
305 |
|
---|
306 | sub mk_dir {
|
---|
307 | my ($dir) = @_;
|
---|
308 |
|
---|
309 | my $store_umask = umask(0002);
|
---|
310 | my $mkdir_ok = mkdir ($dir, 0777);
|
---|
311 | umask($store_umask);
|
---|
312 |
|
---|
313 | if (!$mkdir_ok)
|
---|
314 | {
|
---|
315 | print STDERR "util::mk_dir could not create directory $dir\n";
|
---|
316 | return;
|
---|
317 | }
|
---|
318 | }
|
---|
319 |
|
---|
320 | # in case anyone cares - I did some testing (using perls Benchmark module)
|
---|
321 | # on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
|
---|
322 | # slightly faster (surprisingly) - Stefan.
|
---|
323 | sub mk_all_dir {
|
---|
324 | my ($dir) = @_;
|
---|
325 |
|
---|
326 | # use / for the directory separator, remove duplicate and
|
---|
327 | # trailing slashes
|
---|
328 | $dir=~s/[\\\/]+/\//g;
|
---|
329 | $dir=~s/[\\\/]+$//;
|
---|
330 |
|
---|
331 | # make sure the cache directory exists
|
---|
332 | my $dirsofar = "";
|
---|
333 | my $first = 1;
|
---|
334 | foreach my $dirname (split ("/", $dir)) {
|
---|
335 | $dirsofar .= "/" unless $first;
|
---|
336 | $first = 0;
|
---|
337 |
|
---|
338 | $dirsofar .= $dirname;
|
---|
339 |
|
---|
340 | next if $dirname =~ /^(|[a-z]:)$/i;
|
---|
341 | if (!-e $dirsofar)
|
---|
342 | {
|
---|
343 | my $store_umask = umask(0002);
|
---|
344 | my $mkdir_ok = mkdir ($dirsofar, 0777);
|
---|
345 | umask($store_umask);
|
---|
346 | if (!$mkdir_ok)
|
---|
347 | {
|
---|
348 | print STDERR "util::mk_all_dir could not create directory $dirsofar\n";
|
---|
349 | return;
|
---|
350 | }
|
---|
351 | }
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | # make hard link to file if supported by OS, otherwise copy the file
|
---|
356 | sub hard_link {
|
---|
357 | my ($src, $dest) = @_;
|
---|
358 |
|
---|
359 | # remove trailing slashes from source and destination files
|
---|
360 | $src =~ s/[\\\/]+$//;
|
---|
361 | $dest =~ s/[\\\/]+$//;
|
---|
362 |
|
---|
363 | # a few sanity checks
|
---|
364 | if (-e $dest) {
|
---|
365 | # destination file already exists
|
---|
366 | return;
|
---|
367 | }
|
---|
368 | elsif (!-e $src) {
|
---|
369 | print STDERR "util::hard_link source file $src does not exist\n";
|
---|
370 | return 1;
|
---|
371 | }
|
---|
372 | elsif (-d $src) {
|
---|
373 | print STDERR "util::hard_link source $src is a directory\n";
|
---|
374 | return 1;
|
---|
375 | }
|
---|
376 |
|
---|
377 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
378 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
379 |
|
---|
380 | # link not supported on windows 9x
|
---|
381 | if (($ENV{'GSDLOS'} =~ /^windows$/i) && (Win32::FsType() !~ /^ntfs$/i)) {
|
---|
382 | print STDERR "util::hard_link: win32: using copy for hard link.\n";
|
---|
383 | &File::Copy::copy ($src, $dest);
|
---|
384 |
|
---|
385 | } elsif (!link($src, $dest)) {
|
---|
386 | print STDERR "util::hard_link: unable to create hard link. ";
|
---|
387 | print STDERR " Attempting to copy file: $src -> $dest\n";
|
---|
388 | &File::Copy::copy ($src, $dest);
|
---|
389 | }
|
---|
390 | return 0;
|
---|
391 | }
|
---|
392 |
|
---|
393 | # make soft link to file if supported by OS, otherwise copy file
|
---|
394 | sub soft_link {
|
---|
395 | my ($src, $dest) = @_;
|
---|
396 |
|
---|
397 | # remove trailing slashes from source and destination files
|
---|
398 | $src =~ s/[\\\/]+$//;
|
---|
399 | $dest =~ s/[\\\/]+$//;
|
---|
400 |
|
---|
401 | # a few sanity checks
|
---|
402 | if (!-e $src) {
|
---|
403 | print STDERR "util::soft_link source file $src does not exist\n";
|
---|
404 | return 0;
|
---|
405 | }
|
---|
406 |
|
---|
407 | my $dest_dir = &File::Basename::dirname($dest);
|
---|
408 | mk_all_dir($dest_dir) if (!-e $dest_dir);
|
---|
409 |
|
---|
410 | # symlink not supported on windows
|
---|
411 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
412 |
|
---|
413 | if ( (Win32::FsType() =~ /^ntfs$/i) &&
|
---|
414 | ($src =~ /^.:\\.*/) && ($dest =~ /^.:\\.*/) &&
|
---|
415 | (substr($src,0,3) eq substr($dest,0,3)) ) {
|
---|
416 |
|
---|
417 | # if filesystem is NTFS and both source and destination is on the same local drive,
|
---|
418 | # use hardlink instead of symlink
|
---|
419 | #print STDERR "util::soft_link: win32: using hard link instead of soft ink.\n";
|
---|
420 | require Win32::Hardlink;
|
---|
421 | hard_link($src,$dest);
|
---|
422 | } else {
|
---|
423 | #print STDERR "util::soft_link: win32: using copy for soft link.\n ";
|
---|
424 | &File::Copy::copy ($src, $dest);
|
---|
425 | }
|
---|
426 |
|
---|
427 | } elsif (!eval {symlink($src, $dest)}) {
|
---|
428 | print STDERR "util::soft_link: unable to create soft link.\n";
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 | return 1;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 |
|
---|
437 |
|
---|
438 | # updates a copy of a directory in some other part of the filesystem
|
---|
439 | # verbosity settings are: 0=low, 1=normal, 2=high
|
---|
440 | # both $fromdir and $todir should be absolute paths
|
---|
441 | sub cachedir {
|
---|
442 | my ($fromdir, $todir, $verbosity) = @_;
|
---|
443 | $verbosity = 1 unless defined $verbosity;
|
---|
444 |
|
---|
445 | # use / for the directory separator, remove duplicate and
|
---|
446 | # trailing slashes
|
---|
447 | $fromdir=~s/[\\\/]+/\//g;
|
---|
448 | $fromdir=~s/[\\\/]+$//;
|
---|
449 | $todir=~s/[\\\/]+/\//g;
|
---|
450 | $todir=~s/[\\\/]+$//;
|
---|
451 |
|
---|
452 | &mk_all_dir ($todir);
|
---|
453 |
|
---|
454 | # get the directories in ascending order
|
---|
455 | if (!opendir (FROMDIR, $fromdir)) {
|
---|
456 | print STDERR "util::cachedir could not read directory $fromdir\n";
|
---|
457 | return;
|
---|
458 | }
|
---|
459 | my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
|
---|
460 | closedir (FROMDIR);
|
---|
461 |
|
---|
462 | if (!opendir (TODIR, $todir)) {
|
---|
463 | print STDERR "util::cacedir could not read directory $todir\n";
|
---|
464 | return;
|
---|
465 | }
|
---|
466 | my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
|
---|
467 | closedir (TODIR);
|
---|
468 |
|
---|
469 | my $fromi = 0;
|
---|
470 | my $toi = 0;
|
---|
471 |
|
---|
472 | while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
|
---|
473 | # print "fromi: $fromi toi: $toi\n";
|
---|
474 |
|
---|
475 | # see if we should delete a file/directory
|
---|
476 | # this should happen if the file/directory
|
---|
477 | # is not in the from list or if its a different
|
---|
478 | # size, or has an older timestamp
|
---|
479 | if ($toi < scalar(@todir)) {
|
---|
480 | if (($fromi >= scalar(@fromdir)) ||
|
---|
481 | ($todir[$toi] lt $fromdir[$fromi] ||
|
---|
482 | ($todir[$toi] eq $fromdir[$fromi] &&
|
---|
483 | &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
|
---|
484 | $verbosity)))) {
|
---|
485 |
|
---|
486 | # the files are different
|
---|
487 | &rm_r("$todir/$todir[$toi]");
|
---|
488 | splice(@todir, $toi, 1); # $toi stays the same
|
---|
489 |
|
---|
490 | } elsif ($todir[$toi] eq $fromdir[$fromi]) {
|
---|
491 | # the files are the same
|
---|
492 | # if it is a directory, check its contents
|
---|
493 | if (-d "$todir/$todir[$toi]") {
|
---|
494 | &cachedir ("$fromdir/$fromdir[$fromi]",
|
---|
495 | "$todir/$todir[$toi]", $verbosity);
|
---|
496 | }
|
---|
497 |
|
---|
498 | $toi++;
|
---|
499 | $fromi++;
|
---|
500 | next;
|
---|
501 | }
|
---|
502 | }
|
---|
503 |
|
---|
504 | # see if we should insert a file/directory
|
---|
505 | # we should insert a file/directory if there
|
---|
506 | # is no tofiles left or if the tofile does not exist
|
---|
507 | if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
|
---|
508 | $todir[$toi] gt $fromdir[$fromi])) {
|
---|
509 | &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
|
---|
510 | splice (@todir, $toi, 0, $fromdir[$fromi]);
|
---|
511 |
|
---|
512 | $toi++;
|
---|
513 | $fromi++;
|
---|
514 | }
|
---|
515 | }
|
---|
516 | }
|
---|
517 |
|
---|
518 | # this function returns -1 if either file is not found
|
---|
519 | # assumes that $file1 and $file2 are absolute file names or
|
---|
520 | # in the current directory
|
---|
521 | # $file2 is allowed to be newer than $file1
|
---|
522 | sub differentfiles {
|
---|
523 | my ($file1, $file2, $verbosity) = @_;
|
---|
524 | $verbosity = 1 unless defined $verbosity;
|
---|
525 |
|
---|
526 | $file1 =~ s/\/+$//;
|
---|
527 | $file2 =~ s/\/+$//;
|
---|
528 |
|
---|
529 | my ($file1name) = $file1 =~ /\/([^\/]*)$/;
|
---|
530 | my ($file2name) = $file2 =~ /\/([^\/]*)$/;
|
---|
531 |
|
---|
532 | return -1 unless (-e $file1 && -e $file2);
|
---|
533 | if ($file1name ne $file2name) {
|
---|
534 | print STDERR "filenames are not the same\n" if ($verbosity >= 2);
|
---|
535 | return 1;
|
---|
536 | }
|
---|
537 |
|
---|
538 | my @file1stat = stat ($file1);
|
---|
539 | my @file2stat = stat ($file2);
|
---|
540 |
|
---|
541 | if (-d $file1) {
|
---|
542 | if (! -d $file2) {
|
---|
543 | print STDERR "one file is a directory\n" if ($verbosity >= 2);
|
---|
544 | return 1;
|
---|
545 | }
|
---|
546 | return 0;
|
---|
547 | }
|
---|
548 |
|
---|
549 | # both must be regular files
|
---|
550 | unless (-f $file1 && -f $file2) {
|
---|
551 | print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
|
---|
552 | return 1;
|
---|
553 | }
|
---|
554 |
|
---|
555 | # the size of the files must be the same
|
---|
556 | if ($file1stat[7] != $file2stat[7]) {
|
---|
557 | print STDERR "different sized files\n" if ($verbosity >= 2);
|
---|
558 | return 1;
|
---|
559 | }
|
---|
560 |
|
---|
561 | # the second file cannot be older than the first
|
---|
562 | if ($file1stat[9] > $file2stat[9]) {
|
---|
563 | print STDERR "file is older\n" if ($verbosity >= 2);
|
---|
564 | return 1;
|
---|
565 | }
|
---|
566 |
|
---|
567 | return 0;
|
---|
568 | }
|
---|
569 |
|
---|
570 |
|
---|
571 | sub get_tmp_filename {
|
---|
572 | my $tmpdir = filename_cat($ENV{'GSDLHOME'}, "tmp");
|
---|
573 | &mk_all_dir ($tmpdir) unless -e $tmpdir;
|
---|
574 |
|
---|
575 | my $count = 1000;
|
---|
576 | my $rand = int(rand $count);
|
---|
577 | while (-e &filename_cat($tmpdir, "F$rand")) {
|
---|
578 | $rand = int(rand $count);
|
---|
579 | $count++;
|
---|
580 | }
|
---|
581 |
|
---|
582 | return filename_cat($tmpdir, "F$rand");
|
---|
583 | }
|
---|
584 |
|
---|
585 |
|
---|
586 | sub filename_cat {
|
---|
587 | my $first_file = shift(@_);
|
---|
588 | my (@filenames) = @_;
|
---|
589 |
|
---|
590 | # Check for empty first filename
|
---|
591 | if ($first_file =~ /\S/) {
|
---|
592 | unshift(@filenames, $first_file);
|
---|
593 | }
|
---|
594 |
|
---|
595 | my $filename = join("/", @filenames);
|
---|
596 |
|
---|
597 | # remove duplicate slashes and remove the last slash
|
---|
598 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
599 | $filename =~ s/[\\\/]+/\\/g;
|
---|
600 | } else {
|
---|
601 | $filename =~ s/[\/]+/\//g;
|
---|
602 | # DB: want a filename abc\de.html to remain like this
|
---|
603 | }
|
---|
604 | $filename =~ s/[\\\/]$//;
|
---|
605 |
|
---|
606 | return $filename;
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | sub envvar_prepend {
|
---|
611 | my ($var,$val) = @_;
|
---|
612 |
|
---|
613 | my $current_val = $ENV{$var};
|
---|
614 |
|
---|
615 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
616 | $ENV{$var} .= "$val;$current_val";
|
---|
617 | }
|
---|
618 | else {
|
---|
619 | $ENV{$var} .= "$val:$current_val";
|
---|
620 | }
|
---|
621 | }
|
---|
622 |
|
---|
623 | sub envvar_append {
|
---|
624 | my ($var,$val) = @_;
|
---|
625 |
|
---|
626 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
627 | $ENV{$var} .= ";$val";
|
---|
628 | }
|
---|
629 | else {
|
---|
630 | $ENV{$var} .= ":$val";
|
---|
631 | }
|
---|
632 | }
|
---|
633 |
|
---|
634 |
|
---|
635 | # returns the path of a file without the filename -- ie. the directory the file is in
|
---|
636 | sub filename_head {
|
---|
637 | my $filename = shift(@_);
|
---|
638 |
|
---|
639 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
640 | $filename =~ s/[^\\\\]*$//;
|
---|
641 | }
|
---|
642 | else {
|
---|
643 | $filename =~ s/[^\\\/]*$//;
|
---|
644 | }
|
---|
645 |
|
---|
646 | return $filename;
|
---|
647 | }
|
---|
648 |
|
---|
649 |
|
---|
650 | # returns 1 if filename1 and filename2 point to the same
|
---|
651 | # file or directory
|
---|
652 | sub filenames_equal {
|
---|
653 | my ($filename1, $filename2) = @_;
|
---|
654 |
|
---|
655 | # use filename_cat to clean up trailing slashes and
|
---|
656 | # multiple slashes
|
---|
657 | $filename1 = filename_cat ($filename1);
|
---|
658 | $filename2 = filename_cat ($filename2);
|
---|
659 |
|
---|
660 | # filenames not case sensitive on windows
|
---|
661 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
662 | $filename1 =~ tr/[A-Z]/[a-z]/;
|
---|
663 | $filename2 =~ tr/[A-Z]/[a-z]/;
|
---|
664 | }
|
---|
665 | return 1 if $filename1 eq $filename2;
|
---|
666 | return 0;
|
---|
667 | }
|
---|
668 |
|
---|
669 | sub filename_within_collection
|
---|
670 | {
|
---|
671 | my ($filename) = @_;
|
---|
672 |
|
---|
673 | my $collect_dir = $ENV{'GSDLCOLLECTDIR'};
|
---|
674 |
|
---|
675 | if (defined $collect_dir) {
|
---|
676 | my $dirsep = &util::get_dirsep();
|
---|
677 | if ($collect_dir !~ m/$dirsep$/) {
|
---|
678 | $collect_dir .= $dirsep;
|
---|
679 | }
|
---|
680 |
|
---|
681 | $collect_dir =~ s/\\/\\\\/g; # escape DOS style file separator
|
---|
682 |
|
---|
683 | if ($filename =~ /^$collect_dir(.*)$/) {
|
---|
684 | $filename = $1;
|
---|
685 | }
|
---|
686 | }
|
---|
687 |
|
---|
688 | return $filename;
|
---|
689 | }
|
---|
690 |
|
---|
691 |
|
---|
692 | sub get_dirsep {
|
---|
693 |
|
---|
694 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
695 | return "\\";
|
---|
696 | } else {
|
---|
697 | return "\/";
|
---|
698 | }
|
---|
699 | }
|
---|
700 |
|
---|
701 | sub get_os_dirsep {
|
---|
702 |
|
---|
703 | if ($ENV{'GSDLOS'} =~ /^windows$/i) {
|
---|
704 | return "\\\\";
|
---|
705 | } else {
|
---|
706 | return "\\\/";
|
---|
707 | }
|
---|
708 | }
|
---|
709 |
|
---|
710 | sub get_re_dirsep {
|
---|
711 |
|
---|
712 | return "\\\\|\\\/";
|
---|
713 | }
|
---|
714 |
|
---|
715 |
|
---|
716 | # if this is running on windows we want binaries to end in
|
---|
717 | # .exe, otherwise they don't have to end in any extension
|
---|
718 | sub get_os_exe {
|
---|
719 | return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
|
---|
720 | return "";
|
---|
721 | }
|
---|
722 |
|
---|
723 |
|
---|
724 | # test to see whether this is a big or little endian machine
|
---|
725 | sub is_little_endian {
|
---|
726 | # To determine the name of the operating system, the variable $^O is a cheap alternative to pulling it out of the Config module;
|
---|
727 | # What we do here is, if it is a Macintosh machine (i.e. the Darwin operating system), regardless it is running on the IBM power-pc cpu or it is the x86 Intel-based chip with a power-pc emulator running on top of it, it requires the big-endian data format in the gdbm database file, we make the file extension .bdb; otherwise it's .ldb extension.
|
---|
728 |
|
---|
729 | #return 0 if $^O =~ /^darwin$/i;
|
---|
730 | return 0 if $ENV{'GSDLOS'} =~ /^darwin$/i;
|
---|
731 | return (ord(substr(pack("s",1), 0, 1)) == 1);
|
---|
732 | }
|
---|
733 |
|
---|
734 |
|
---|
735 | # will return the collection name if successful, "" otherwise
|
---|
736 | sub use_collection {
|
---|
737 | my ($collection, $collectdir) = @_;
|
---|
738 |
|
---|
739 | if (!defined $collectdir || $collectdir eq "") {
|
---|
740 | $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
|
---|
741 | }
|
---|
742 |
|
---|
743 | # get and check the collection
|
---|
744 | if (!defined($collection) || $collection eq "") {
|
---|
745 | if (defined $ENV{'GSDLCOLLECTION'}) {
|
---|
746 | $collection = $ENV{'GSDLCOLLECTION'};
|
---|
747 | } else {
|
---|
748 | print STDOUT "No collection specified\n";
|
---|
749 | return "";
|
---|
750 | }
|
---|
751 | }
|
---|
752 |
|
---|
753 | if ($collection eq "modelcol") {
|
---|
754 | print STDOUT "You can't use modelcol.\n";
|
---|
755 | return "";
|
---|
756 | }
|
---|
757 |
|
---|
758 | # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
|
---|
759 | # are defined
|
---|
760 | $ENV{'GSDLCOLLECTION'} = $collection unless defined $ENV{'GSDLCOLLECTION'};
|
---|
761 | $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
|
---|
762 |
|
---|
763 | # make sure this collection exists
|
---|
764 | if (!-e $ENV{'GSDLCOLLECTDIR'}) {
|
---|
765 | print STDOUT "Invalid collection ($collection).\n";
|
---|
766 | return "";
|
---|
767 | }
|
---|
768 |
|
---|
769 | # everything is ready to go
|
---|
770 | return $collection;
|
---|
771 | }
|
---|
772 |
|
---|
773 | sub hyperlink_text
|
---|
774 | {
|
---|
775 | my ($text) = @_;
|
---|
776 |
|
---|
777 | $text =~ s/(http:\/\/[^\s]+)/<a href=\"$1\">$1<\/a>/mg;
|
---|
778 | $text =~ s/(^|\s+)(www\.(\w|\.)+)/<a href=\"http:\/\/$2\">$2<\/a>/mg;
|
---|
779 |
|
---|
780 | return $text;
|
---|
781 | }
|
---|
782 |
|
---|
783 |
|
---|
784 | 1;
|
---|