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

Last change on this file since 817 was 812, checked in by sjboddie, 25 years ago

hard_link returns if link destination already exists

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