source: trunk/gsdl/perllib/util.pm@ 900

Last change on this file since 900 was 836, checked in by davidb, 24 years ago

improvements to utils

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