3 ###############################################################################
5 # Name : shared_snarfer #
6 # Author : Chris Koeritz #
7 # Rights : Copyright (C) 1996-$now by Author #
11 # A shared library collection for "snarfing up" archives. This uses the #
12 # compressed tar format for files ending in ".snarf" to store collections #
13 # of files, folders, hierarchies and so forth. #
15 ###############################################################################
16 # This program is free software; you can redistribute it and/or modify it #
17 # under the terms of the GNU General Public License as published by the Free #
18 # Software Foundation; either version 2 of the License or (at your option) #
19 # any later version. See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
20 # version of the License. Please send any updates to "fred@gruntose.com". #
21 ###############################################################################
23 require "filename_helper.pl";
29 use Env qw(FEISTY_MEOW_SCRIPTS TMP);
31 $null_log = "/dev/null";
33 $TMP =~ s/\\/\//g; # fix the temp variable for ms-winders.
35 # defines an array of problematic entries we were told to deal with.
38 # these files are considered unimportant and won't be included in the archive.
39 @junk_file_list = ("*~", "*.$$$", "3rdparty", "*.aps", "*.bak", "binaries",
40 "*.bsc", "*.cgl", "*.csm", "CVS", "Debug", "*.dll", "*.err", "*.exe",
41 "generated_*", "*.git", "*.glb", "inprogress", "ipch", "*.llm",
43 "makefile.fw*", "*.mbt", "*.mrt", "*.ncb", "*.o", "obj", "*.obj",
44 "octalforty.Wizardby", "*.obr", "*.opt", "packages",
45 "*.pch", "*.pdb", "*.plg", "*.r$p", "*.rcs", "Release",
46 "*.res", "*.RES", "*.rws", "*.sbr", "*.scc", "*.spx", "*.stackdump",
48 "*.sdf", "*.suo", ".svn", "*.sym", "*.td", "*.tds", "*.tdw", "*.tlb",
49 "*.trw", "*.tmp", "*.tr", "*.user", "*_version.h", "*_version.rc",
50 "*.vspscc", "waste", "zeitgeist");
51 #print "junk list=@junk_file_list\n";
53 for (local($i) = 0; $i < scalar(@junk_file_list); $i++) {
54 push(@excludes, "--exclude=$junk_file_list[$i]");
56 #print "excludes list=@excludes\n";
58 # generic versions work on sane OSes.
59 $find_tool = which('find');
60 $tar_tool = which('tar');
61 #print "find tool: $find_tool\n";
62 #print "tar tool: $tar_tool\n";
64 if ( ! -f "$find_tool" || ! -f "$tar_tool" ) {
65 print "Could not locate either tar or find tools for this platform.\n";
69 # this is somewhat ugly, but it sets up a global variable called
70 # "original_path" so we remember where we started.
71 sub initialize_snarfer {
72 $original_path = cwd();
73 $original_path =~ s/\\/\//g;
76 # returns the current hostname, but without any domain included.
78 local($temphost) = hostname();
80 $temphost =~ s/([^.]*)\..*/\1/;
81 return &lower($temphost);
84 # takes the base name and creates the full snarf prefix name, which includes
85 # a timestamp and hostname.
89 local($date_part) = `$date_tool +%Y-%m-%d-%H%M`;
90 while ($date_part =~ /[\r\n]$/) { chop $date_part; }
91 local($host) = &short_hostname();
92 while ($host =~ /[\r\n]$/) { chop $host; }
93 $base = $base . "_" . $host . "_" . $date_part;
97 # returns the name of the file to create based on the prefix and the
98 # current archive number.
100 local($prefix, $number) = @_;
101 local($path) = &canonicalize($original_path);
102 local($target_file) = $path . '/' . $prefix . "_" . $number . ".tar";
106 # finishes up on the archive file.
108 local($filename) = @_;
109 #print "finalizing now on filename $filename\n";
110 local($outcome) = 0xff & system "gzip", $filename;
111 if ($outcome) { die("failure to finalize"); }
113 if (scalar(@missing_log)) {
114 print "The following files or directories were missing:\n";
115 print "@missing_log\n";
119 # fixes the directory passed in, if required. this is only needed for
120 # dos-like operating systems, where there are drives to worry about and where
121 # cygwin refuses to store the full path for an absolute pathname in the
122 # archive. instead of letting it store partial paths, we change to the top
123 # of the drive and scoop up the files using a relative path.
125 local($directory) = @_;
126 if ( (substr($directory, 0, 2) eq "//")
127 && (substr($directory, 3, 1) eq "/") ) {
128 #print "into special case\n";
129 # it was originally a dos path, so now we must do some directory changing
130 # magic to get the paths to work right.
131 local($drive) = substr($directory, 0, 4); # get just drive letter biz.
132 #print "going to change to $drive\n";
134 #print "cwd now=" . cwd() . "\n";
135 $directory = substr($directory, 4); # rip off absolutist path.
136 #print "using dir now as $directory\n";
137 if (length($directory) == 0) {
138 #print "caught zero length dir, patching to dot now.\n";
145 # snarfer scoops up some files in a directory.
147 local($prefix, $number, $root, $subdir, @extra_flags) = @_;
148 #print "prefix=$prefix, num=$number, root=$root, subdir=$subdir, extra=@extra_flags\n";
150 $root = &chdir_to_top($root);
152 local($target_file) = &snarf_name($prefix, $number);
154 $random_num = (rand() * 1000000) % 1000000;
155 $temp_file = `mktemp "$TMP/zz_snarf_tmp.XXXXXX"`;
158 if (! -d $root . "/" . $subdir) {
159 local($base) = &basename($root . "/" . $subdir);
160 #print "adding to missing in snarfer A: $base\n";
161 push(@missing_log, $base);
164 local($currdir) = cwd();
168 my @lines = qx( $find_tool "$subdir" @extra_flags "-type" "f" );
169 # if ( ($! != 0) || ($? != 0) ) {
170 # die("failure to find files in $subdir");
173 open TEMPY_OUT, ">>$temp_file" or die "cannot open $temp_file";
174 foreach (@lines) { print TEMPY_OUT "$_"; }
177 if (-s $temp_file == 0) {
178 local($base) = &basename($root . "/" . $subdir);
179 #print "adding to missing in snarfer B: $base\n";
180 push(@missing_log, $base);
183 local($outcome) = 0xff & system $tar_tool,
184 "-rf", &canonicalize($target_file), @excludes,
185 "--files-from=" . &canonicalize($temp_file);
188 die("failure to archive");
190 # clean up temporaries.
192 # change back to previous directory.
196 # snarf_file_list is like snarfer but expects a file pattern at the end rather
197 # than a directory name.
198 sub snarf_file_list {
199 local($prefix, $number, $root, $file_pattern, @extra_flags) = @_;
201 #print "prefix=$prefix, num=$number, root=$root, file_pattern=$file_pattern, extra=@extra_flags\n";
203 $root = &chdir_to_top($root);
205 local($target_file) = &snarf_name($prefix, $number);
207 local($currdir) = cwd();
208 #print "got root as: '$root'\n";
211 local(@files) = &glob_list($file_pattern);
212 if (!scalar(@files)) {
213 local($base) = $root . "/" . $file_pattern;
214 $base =~ s/\/\//\//g;
215 #print "adding to missing in snarf_file_list: $base\n";
216 push(@missing_log, $base);
219 foreach $i (@files) {
221 $i = substr $i, 2, length($i) - 2;
223 local($outcome) = 0xff & system $tar_tool,
224 #"--directory=" . "$root",
225 @extra_flags, "-rf", &canonicalize($target_file), @excludes, $i;
226 if ($outcome) { die("failure to archive"); }
231 # backup some specific files.
233 local($prefix, $number, $root, $subdir, @files) = @_;
234 #print "backup_files: ref=$prefix, num=$number, subdir=$subdir, list of files=@files\n";
235 foreach $i (@files) {
236 local($new_path) = $subdir . "/" . $i;
237 if ($subdir eq ".") { $new_path = "$i"; }
238 &snarf_file_list($prefix, $number, $root, $new_path);
242 # backup some specific directories.
243 sub backup_directories {
244 local($prefix, $number, $root, $subdir, @dirs) = @_;
245 #print "backup_directories: ref=$prefix, num=$number, root=$root, subdir=$subdir, list of dirs=@dirs.\n";
247 local($path_to_use) = $subdir . "/" . $i;
249 $path_to_use = $subdir;
251 &snarfer($prefix, $number, $root, $path_to_use, ("-maxdepth", "1"));
255 # removes items from the file that match a pattern.
256 sub remove_from_backup {
257 local($prefix, $number, $pattern) = @_;
258 #print "remove_from_backup: pref=$prefix, num=$number, patt=$pattern,\n";
259 local($target_file) = &snarf_name($prefix, $number);
261 open(TARPROC, "$tar_tool --delete -f " . &canonicalize($target_file)
262 . " \"$pattern\" 2>$null_log |");
266 # recursively scoops up a directory hierarchy.
267 sub backup_hierarchy {
268 local($prefix, $number, $root, $filepart) = @_;
269 #print "backup_hierarchy: pref=$prefix, num=$number, root=$root, filepart=$filepart\n";
270 local(@locus_temp) = &glob_list($root);
271 local($save_root) = $root;
272 local($root) = $locus_temp[0];
273 if (!length($root)) {
274 local($base) = $save_root . "/" . $filepart;
275 #print "adding to missing in backup_hierarchy A: $base\n";
276 push(@missing_log, $base);
279 local($new_pattern) = "$root/$filepart";
280 if ($root =~ /\/$/) { $new_pattern = "$root$filepart"; }
281 local(@mod_locus) = &glob_list($new_pattern);
282 if (!scalar(@mod_locus)) {
283 local($base) = &basename($root . "/" . $filepart);
284 #print "adding to missing in backup_hierarchy B: $base\n";
285 push(@missing_log, $base);
287 foreach $i (@mod_locus) {
288 local($new_locus) = $root;
289 local $offset_len = length($root) + 1;
290 local $char_len = length($i) - length($root) - 1;
291 # make sure we don't double slashes up if one's already there.
292 if ($root =~ /\/$/) { $offset_len--; $char_len++; }
293 local($extra_portion) = substr $i, $offset_len, $char_len;
294 if (!length($extra_portion)) {
295 # well, in this case, there's nothing left of the extra part after
296 # the root. we'll push the last component of the root down into
297 # the extra part so there's actually something to traverse.
298 $new_locus = &dirname($root);
299 $extra_portion = &basename($root);
301 &snarfer($prefix, $number, $new_locus, $extra_portion, ());
306 # recursively scoop up a list of directory hierarchies.
307 sub backup_hierarchies {
308 local($prefix, $number, $root, @dirs) = @_;
309 # print "backup_hierarchy: pref=$prefix, num=$number, root=$root,\n";
310 # print "list of dirs=@dirs.\n";
312 &backup_hierarchy($prefix, $number, $root, $i);
316 # grab up all the files in a directory (second parm) that are named matching
317 # a simple text pattern (third parm). if there is a fourth parameter, it is
318 # used as an extra directory component after the main directory.
319 sub snarf_by_pattern {
320 local($prefix, $dir, $pattern, $extra_component) = @_;
321 local($had_extra) = length($extra_component) != 0;
322 #print "snarf by pattern, dir = $dir, patt = $pattern, extra = $extra_component\n";
324 $dir = "$dir/$extra_component";
326 @dir_contents = &glob_list("$dir/*$pattern*");
327 #print "dir contents: @dir_contents\n";
329 if (!scalar(@dir_contents)) {
330 print "no '$pattern' directores were backed up in $dir.\n";
333 foreach $item (@dir_contents) {
334 if ( ($item =~ /$pattern.*snarf/) || ($item =~ /$pattern.*tar/) ) { next; }
335 if ( ! -d "$item" ) { next; }
336 #print "now really planning to backup hier of $item\n";
337 # normal backup had no extra component.
338 local $upper_dir = &dirname($item);
339 local $dir_plus_base = &basename($item);
340 # if we did have an extra component, we do this a bit differently.
342 $upper_dir = &dirname( &dirname($item) );
343 $dir_plus_base = &basename( &dirname($item) ) . "/" . &basename($item);
345 #print "using upper=$upper_dir and dir+base=$dir_plus_base\n";
346 &backup_hierarchy($prefix, $number, $upper_dir, $dir_plus_base);
350 # gets the number out of the file specified by a basename. the number file
351 # is assumed to be stored in the TMP directory and to have an extension of
353 sub retrieve_number {
354 local($number_prefix) = @_;
355 # get number from the file specified and increment it for the next use.
356 local($NUMBER_FILE) = $TMP."/$number_prefix.num";
357 local($number) = &get_number($NUMBER_FILE);
358 &next_number($NUMBER_FILE);
362 # takes a name to use as the basename for a number file, and stores the
363 # file into the archive specified.
365 local($number_prefix, $snarf_prefix, $number) = @_;
366 #print "backup_number: parms are: numpref=$number_prefix, archpref=$snarf_prefix, num=$number.\n";
367 local($target_file) = $original_path ."/". $snarf_prefix . "_" . $number . ".tar";
368 local($number_file) = $number_prefix . ".num";
370 local($currdir) = cwd();
373 local($outcome) = 0xff & system $tar_tool, "-cf",
374 &canonicalize($target_file), &canonicalize($number_file);
375 if ($outcome) { die("failure to archive"); }
377 local($prefix_file) = "prefix.bac";
378 open(NUM_PREFIX, ">" . $prefix_file);
379 print NUM_PREFIX $number_prefix;
382 $outcome = 0xff & system $tar_tool, "-rf",
383 &canonicalize($target_file), &canonicalize($prefix_file);
384 if ($outcome) { die("failure to archive"); }
385 unlink($prefix_file);
389 # takes a prefix for the number file and a filename where it can be found.
390 # the current number in the temporary directory is compared against the file,
391 # and the new number's used if it's greater.
393 local($number_prefix, $number_file) = @_;
394 #print "restore num has numpref $number_prefix and numfile $number_file\n";
395 local($comparison_file) = "$TMP" . "/" . $number_prefix . ".num";
396 local($number) = &get_number($number_file);
397 local($old_number) = &get_number($comparison_file);
398 if ($number > $old_number) {
399 &store_number($number, $comparison_file);
401 unlink($number_file);
404 # ensures that the special restoration program is used on the archives by
405 # renaming their extension.
407 local($filename) = @_;
408 #print "rename_archive: file=$filename\n";
409 &finalize_snarf($filename);
410 local(@pieces) = split(/\.[^.]*$/, $filename, 3);
411 local($just_dir_and_base) = $pieces[0];
412 local($new_name) = $just_dir_and_base . '.snarf';
413 rename($filename . ".gz", $new_name)
414 || die("could not rename $filename to $new_name.");
417 # undoes a snarfed up archive and pulls out the number.
418 sub restore_archive {
419 local($filename) = &canonicalize(&remove_trailing_slashes(@_));
420 local(@split_name) = &split_filename($filename);
421 if ($#split_name < 1) {
422 print "The name \"$filename\" could not be parsed for restoration.\n";
425 # get the basename of the file.
426 local(@pieces) = split(/\.[^.]*$/, @split_name[1], 2);
427 # we don't want the extension.
428 local($just_dir_and_base) = $split_name[0] . $pieces[0];
429 # now just get the basename without a directory.
430 local(@name_components) = split(/\//, $just_dir_and_base);
431 local($basename) = $name_components[$#name_components];
432 local($new_dir_name) = 'snarf_' . $basename;
434 local($currdir) = cwd();
436 if (!chdir($new_dir_name)) {
437 mkdir($new_dir_name, 0777)
438 || die("could not create directory $new_dir_name.");
439 if (!chdir($new_dir_name)) {
440 die("could not change to directory $new_dir_name.");
444 # patch a relative path name to reflect the fact that we're now underneath
445 # the directory where we were.
446 if (! ($filename =~ /^\//)
447 && ! ($filename =~ /^.:/) ) {
448 $filename = "../" . $filename;
451 local($outcome) = 0xff & system $tar_tool, "-xzf",
452 &canonicalize($filename);
453 if ($outcome) { die("failure to undo archive"); }
456 0xff & system "bash", "$FEISTY_MEOW_SCRIPTS/files/normal_perm.sh", ".";
457 if ($outcome) { die("failure to normalize permissions"); }
459 # remove any links that might have crept in; these can cause mischief.
460 local($outcome) = 0xff & system("$find_tool . -type l -exec rm {} ';'");
462 # read the name of the prefix file.
463 local($prefix_file) = "prefix.bac";
464 open(NUM_PREFIX, "<" . $prefix_file);
465 local($number_prefix) = <NUM_PREFIX>;
468 &restore_number($number_prefix, $number_prefix . ".num");
469 unlink($prefix_file);