1: #!/usr/bin/perl
2: # daniel j shahin
3: # metadb.pl - make a dbm of filenames and it's stats for later comparison
4: # only a little useful, could add checksum too...
5: # you may distribute this under the same terms as perl
6:
7: #use strict;
8: use DB_File;
9: use vars qw($COMMAND $DIRNAME $FILENAME $COLOR @filestats
10: @dbstats @statnames %HASH $i $file $stat );
11: if (@ARGV < 3){die "usage: metadb [makedb | compare] <directory> <dbm>"}
12: $COMMAND = shift;
13: $DIRNAME = shift;
14: $FILENAME = shift;
15:
16: @statnames = ('device', 'inode', 'mode', 'nlink',
17: 'uid', 'gid', 'rdev', 'size', 'atime',
18: 'mtime', 'ctime', 'blksize', 'blocks');
19:
20: if ($COMMAND eq 'makedb'){
21: dbmopen (%HASH, $FILENAME, 0666) or die "Can't open $FILENAME: $!\n";
22: opendir(DIR, $DIRNAME) or die "can't opendir $DIRNAME: $!";
23: while (defined($file = readdir(DIR))) {
24: next if $file =~ /^\.\.?$/;
25: $stat = join(':',stat("$DIRNAME/$file"));
26: $HASH{"$DIRNAME/$file"} = $stat;
27: print "$DIRNAME/$file: $stat \n";
28: }
29: closedir(DIR);
30: dbmclose %HASH;
31: }elsif($COMMAND eq 'compare'){
32: dbmopen %HASH, $FILENAME, 0666 or die "Can't open $FILENAME: $!\n";
33: opendir(DIR, $DIRNAME) or die "can't opendir $DIRNAME: $!";
34: while (defined($file = readdir(DIR))) {
35: next if $file =~ /^\.\.?$/;
36: $stat = join(':',stat("$DIRNAME/$file"));
37: if(exists $HASH{"$DIRNAME/$file"} && $HASH{"$DIRNAME/$file"} ne $stat){
38: if (exists $HASH{"$DIRNAME/$file"}){
39: print "$DIRNAME/$file\n";
40: @filestats = split(':', $stat);
41: @dbstats = split(':', $HASH{"$DIRNAME/$file"});
42: for ($i=0; $i<13; $i++){
43: print "\t$statnames[$i]";
44: print "\tfile:$filestats[$i]";
45: print "\tdb:$dbstats[$i]\n";
46: }
47: }
48: }
49: }
50: }