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

Last change on this file since 2018 was 1586, checked in by sjboddie, 24 years ago

fixed a bug in the cp_r perl routine

  • Property svn:keywords set to Author Date Id Revision
File size: 14.6 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 directory)
169# destination must be a directory - to copy one file to
170# another use cp instead
171sub cp_r {
172 my $dest = pop (@_);
173 my (@srcfiles) = @_;
174
175 # a few sanity checks
176 if (scalar (@srcfiles) == 0) {
177 print STDERR "util::cp_r no destination directory given\n";
178 return;
179 } elsif (-f $dest) {
180 print STDERR "util::cp_r destination must be a directory\n";
181 return;
182 }
183
184 # create destination directory if it doesn't exist already
185 if (! -d $dest) {
186 my $store_umask = umask(0002);
187 mkdir ($dest, 0777);
188 umask($store_umask);
189 }
190
191 # copy the files
192 foreach $file (@srcfiles) {
193
194 if (!-e $file) {
195 print STDERR "util::cp_r $file does not exist\n";
196
197 } elsif (-d $file) {
198 # make the new directory
199 my ($filename) = $file =~ /([^\\\/]*)$/;
200 $dest = &util::filename_cat ($dest, $filename);
201 my $store_umask = umask(0002);
202 mkdir ($dest, 0777);
203 umask($store_umask);
204
205 # get the contents of this directory
206 if (!opendir (INDIR, $file)) {
207 print STDERR "util::cp_r could not open directory $file\n";
208 } else {
209 my @filedir = readdir (INDIR);
210 closedir (INDIR);
211 foreach $f (@filedir) {
212 next if $f =~ /^\.\.?$/;
213 # copy all the files in this directory
214 my $ff = &util::filename_cat ($file, $f);
215 &cp_r ($ff, $dest);
216 }
217 }
218
219 } else {
220 &cp($file, $dest);
221 }
222 }
223}
224
225
226sub mk_dir {
227 my ($dir) = @_;
228
229 my $store_umask = umask(0002);
230 my $mkdir_ok = mkdir ($dir, 0777);
231 umask($store_umask);
232
233 if (!$mkdir_ok)
234 {
235 print STDERR "util::mk_dir could not create directory $dir\n";
236 return;
237 }
238}
239
240# in case anyone cares - I did some testing (using perls Benchmark module)
241# on this subroutine against File::Path::mkpath (). mk_all_dir() is apparently
242# slightly faster (surprisingly) - Stefan.
243sub mk_all_dir {
244 my ($dir) = @_;
245
246 # use / for the directory separator, remove duplicate and
247 # trailing slashes
248 $dir=~s/[\\\/]+/\//g;
249 $dir=~s/[\\\/]+$//;
250
251 # make sure the cache directory exists
252 my $dirsofar = "";
253 my $first = 1;
254 foreach $dirname (split ("/", $dir)) {
255 $dirsofar .= "/" unless $first;
256 $first = 0;
257
258 $dirsofar .= $dirname;
259
260 next if $dirname =~ /^(|[a-z]:)$/i;
261 if (!-e $dirsofar)
262 {
263 my $store_umask = umask(0002);
264 my $mkdir_ok = mkdir ($dirsofar, 0777);
265 umask($store_umask);
266 if (!$mkdir_ok)
267 {
268 print STDERR "util::mk_all_dir could not create directory $dirsofar\n";
269 return;
270 }
271 }
272 }
273}
274
275# make hard link to file if supported by OS, otherwise copy the file
276sub hard_link {
277 my ($src, $dest) = @_;
278
279 # remove trailing slashes from source and destination files
280 $src =~ s/[\\\/]+$//;
281 $dest =~ s/[\\\/]+$//;
282
283 # a few sanity checks
284 if (-e $dest) {
285 # destination file already exists
286 return;
287 }
288 elsif (!-e $src) {
289 print STDERR "util::hard_link source file $src does not exist\n";
290 return;
291 }
292 elsif (-d $src) {
293 print STDERR "util::hard_link source $src is a directory\n";
294 return;
295 }
296
297 my $dest_dir = &File::Basename::dirname($dest);
298 mk_all_dir($dest_dir) if (!-e $dest_dir);
299
300 # link not supported on wondows
301 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
302 &File::Copy::copy ($src, $dest);
303
304 } elsif (!link($src, $dest)) {
305 print STDERR "util::hard_link: unable to create hard link. ";
306 print STDERR " Attempting to copy file: $src -> $dest\n";
307 &File::Copy::copy ($src, $dest);
308 }
309}
310
311# make soft link to file if supported by OS, otherwise return error
312sub soft_link {
313 my ($src, $dest) = @_;
314
315 # remove trailing slashes from source and destination files
316 $src =~ s/[\\\/]+$//;
317 $dest =~ s/[\\\/]+$//;
318
319 # a few sanity checks
320 if (!-e $src) {
321 print STDERR "util::soft_link source file $src does not exist\n";
322 return 0;
323 }
324
325 my $dest_dir = &File::Basename::dirname($dest);
326 mk_all_dir($dest_dir) if (!-e $dest_dir);
327
328 if (!symlink($src, $dest))
329 {
330 print STDERR "util::soft_link: unable to create soft link.";
331 return 0;
332 }
333
334 return 1;
335}
336
337
338
339
340# updates a copy of a directory in some other part of the filesystem
341# verbosity settings are: 0=low, 1=normal, 2=high
342# both $fromdir and $todir should be absolute paths
343sub cachedir {
344 my ($fromdir, $todir, $verbosity) = @_;
345 $verbosity = 1 unless defined $verbosity;
346
347 # use / for the directory separator, remove duplicate and
348 # trailing slashes
349 $fromdir=~s/[\\\/]+/\//g;
350 $fromdir=~s/[\\\/]+$//;
351 $todir=~s/[\\\/]+/\//g;
352 $todir=~s/[\\\/]+$//;
353
354 &mk_all_dir ($todir);
355
356 # get the directories in ascending order
357 if (!opendir (FROMDIR, $fromdir)) {
358 print STDERR "util::cachedir could not read directory $fromdir\n";
359 return;
360 }
361 my @fromdir = grep (!/^\.\.?$/, sort(readdir (FROMDIR)));
362 closedir (FROMDIR);
363
364 if (!opendir (TODIR, $todir)) {
365 print STDERR "util::cacedir could not read directory $todir\n";
366 return;
367 }
368 my @todir = grep (!/^\.\.?$/, sort(readdir (TODIR)));
369 closedir (TODIR);
370
371 my $fromi = 0;
372 my $toi = 0;
373
374 while ($fromi < scalar(@fromdir) || $toi < scalar(@todir)) {
375# print "fromi: $fromi toi: $toi\n";
376
377 # see if we should delete a file/directory
378 # this should happen if the file/directory
379 # is not in the from list or if its a different
380 # size, or has an older timestamp
381 if ($toi < scalar(@todir)) {
382 if (($fromi >= scalar(@fromdir)) ||
383 ($todir[$toi] lt $fromdir[$fromi] ||
384 ($todir[$toi] eq $fromdir[$fromi] &&
385 &differentfiles("$fromdir/$fromdir[$fromi]","$todir/$todir[$toi]",
386 $verbosity)))) {
387
388 # the files are different
389 &rm_r("$todir/$todir[$toi]");
390 splice(@todir, $toi, 1); # $toi stays the same
391
392 } elsif ($todir[$toi] eq $fromdir[$fromi]) {
393 # the files are the same
394 # if it is a directory, check its contents
395 if (-d "$todir/$todir[$toi]") {
396 &cachedir ("$fromdir/$fromdir[$fromi]",
397 "$todir/$todir[$toi]", $verbosity);
398 }
399
400 $toi++;
401 $fromi++;
402 next;
403 }
404 }
405
406 # see if we should insert a file/directory
407 # we should insert a file/directory if there
408 # is no tofiles left or if the tofile does not exist
409 if ($fromi < scalar(@fromdir) && ($toi >= scalar(@todir) ||
410 $todir[$toi] gt $fromdir[$fromi])) {
411 &cp_r ("$fromdir/$fromdir[$fromi]", "$todir/$fromdir[$fromi]");
412 splice (@todir, $toi, 0, $fromdir[$fromi]);
413
414 $toi++;
415 $fromi++;
416 }
417 }
418}
419
420# this function returns -1 if either file is not found
421# assumes that $file1 and $file2 are absolute file names or
422# in the current directory
423# $file2 is allowed to be newer than $file1
424sub differentfiles {
425 my ($file1, $file2, $verbosity) = @_;
426 $verbosity = 1 unless defined $verbosity;
427
428 $file1 =~ s/\/+$//;
429 $file2 =~ s/\/+$//;
430
431 my ($file1name) = $file1 =~ /\/([^\/]*)$/;
432 my ($file2name) = $file2 =~ /\/([^\/]*)$/;
433
434 return -1 unless (-e $file1 && -e $file2);
435 if ($file1name ne $file2name) {
436 print STDERR "filenames are not the same\n" if ($verbosity >= 2);
437 return 1;
438 }
439
440 @file1stat = stat ($file1);
441 @file2stat = stat ($file2);
442
443 if (-d $file1) {
444 if (! -d $file2) {
445 print STDERR "one file is a directory\n" if ($verbosity >= 2);
446 return 1;
447 }
448 return 0;
449 }
450
451 # both must be regular files
452 unless (-f $file1 && -f $file2) {
453 print STDERR "one file is not a regular file\n" if ($verbosity >= 2);
454 return 1;
455 }
456
457 # the size of the files must be the same
458 if ($file1stat[7] != $file2stat[7]) {
459 print STDERR "different sized files\n" if ($verbosity >= 2);
460 return 1;
461 }
462
463 # the second file cannot be older than the first
464 if ($file1stat[9] > $file2stat[9]) {
465 print STDERR "file is older\n" if ($verbosity >= 2);
466 return 1;
467 }
468
469 return 0;
470}
471
472
473sub get_tmp_filename {
474 my $tmpdir = "$ENV{'GSDLHOME'}/tmp";
475 &mk_all_dir ($tmpdir) unless -e $tmpdir;
476
477 my $count = 1000;
478 my $rand = int(rand $count);
479 while (-e "$tmpdir/F$rand") {
480 $rand = int(rand $count);
481 $count++;
482 }
483
484 return "$tmpdir/F$rand";
485}
486
487
488sub filename_cat {
489 my (@filenames) = @_;
490 my $filename = join("/", @filenames);
491
492 # remove duplicate slashes and remove the last slash
493 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
494 $filename =~ s/[\\\/]+/\\/g;
495 } else {
496 $filename =~ s/[\/]+/\//g;
497 # DB: want a filename abc\de.html to remain like this
498 }
499 $filename =~ s/[\\\/]$//;
500
501 return $filename;
502}
503
504# returns 1 if filename1 and filename2 point to the same
505# file or directory
506sub filenames_equal {
507 my ($filename1, $filename2) = @_;
508
509 # use filename_cat to clean up trailing slashes and
510 # multiple slashes
511 $filename1 = filename_cat ($filename1);
512 $filename1 = filename_cat ($filename1);
513
514 # filenames not case sensitive on windows
515 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
516 $filename1 =~ tr/[A-Z]/[a-z]/;
517 $filename2 =~ tr/[A-Z]/[a-z]/;
518 }
519 return 1 if $filename1 eq $filename2;
520 return 0;
521}
522
523sub get_os_dirsep {
524
525 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
526 return "\\\\";
527 } else {
528 return "\\\/";
529 }
530}
531
532sub get_re_dirsep {
533
534 return "\\\\|\\\/";
535}
536
537
538# if this is running on windows we want binaries to end in
539# .exe, otherwise they don't have to end in any extension
540sub get_os_exe {
541 return ".exe" if $ENV{'GSDLOS'} =~ /^windows$/i;
542 return "";
543}
544
545
546# test to see whether this is a big or little endian machine
547sub is_little_endian {
548 return (ord(substr(pack("s",1), 0, 1)) == 1);
549}
550
551
552# will return the collection name if successful, "" otherwise
553sub use_collection {
554 my ($collection, $collectdir) = @_;
555
556 if (!defined $collectdir || $collectdir eq "") {
557 $collectdir = &filename_cat ($ENV{'GSDLHOME'}, "collect");
558 }
559
560 # get and check the collection
561 if (!defined($collection) || $collection eq "") {
562 if (defined $ENV{'GSDLCOLLECTION'}) {
563 $collection = $ENV{'GSDLCOLLECTION'};
564 } else {
565 print STDERR "No collection specified\n";
566 return "";
567 }
568 }
569
570 if ($collection eq "modelcol") {
571 print STDERR "You can't use modelcol.\n";
572 return "";
573 }
574
575 # make sure the environment variables GSDLCOLLECTION and GSDLCOLLECTDIR
576 # are defined
577 $ENV{'GSDLCOLLECTION'} = $collection unless defined $ENV{'GSDLCOLLECTION'};
578 $ENV{'GSDLCOLLECTDIR'} = &filename_cat ($collectdir, $collection);
579
580 # make sure this collection exists
581 if (!-e $ENV{'GSDLCOLLECTDIR'}) {
582 print STDERR "Invalid collection ($collection).\n";
583 return "";
584 }
585
586 # everything is ready to go
587 return $collection;
588}
589
5901;
Note: See TracBrowser for help on using the repository browser.