-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchanged.pl
121 lines (94 loc) · 2.71 KB
/
changed.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/perl
=pod
=head1 NAME
changed.pl - Print a list of the files that have changed.
=head1 SYNOPSIS
changed <dir> [<dir> ...]
=head1 DESCRIPTION
The I<changed.pl> looks through the directories specified on the command line
and tells you which files have been created, deleted, or changed since the
last time it was run.
=head1 FILES
=item I<.changed.info>
The file containing the checksums of all the files contained in the
last run. This file will be compared against the current run
in order to compute the change list.
=head1 BUGS
The name of the information file (I<.changed.info>) is hardcoded.
This means that you must run the program from the same directory each
time and must specify the same directory list on the command
line each time. (If you don't you get invalid results.)
=head1 AUTHOR
Steve Oualline, E<lt>[email protected]<gt>.
=head1 COPYRIGHT
Copyright 2005 Steve Oualline.
This program is distributed under the GPL.
=cut
use strict;
use warnings;
use File::Find;
use Digest::MD5;
use Storable qw(nstore retrieve);
# File in which to store the change information
my $info_file_name = ".change.info";
########################################################
# md5(file) -- Give a file, return the MD5 sum
########################################################
sub md5($)
{
my $cur_file = shift;
open(FILE, $cur_file) or return ("");
binmode(FILE);
my $result = Digest::MD5->new->addfile(*FILE)->hexdigest;
close (FILE);
return ($result);
}
# Hash reference containing the existing data
# key -- file name
# value -- MD5 sum
my $file_info;
# Hash of the "real" data
my %real_info;
# The list of directories to search
my @dir_list = @ARGV;
#
# Check for an existing information file and
# read it if there is one.
if (-f $info_file_name) {
$file_info = retrieve($info_file_name);
}
# If nothing there, return nothing
if ($#dir_list < 0) {
print "Nothing to look at\n";
exit (0);
}
# Go through the file tree and store the information on the
# files.
find( sub {
-f && ($real_info{$File::Find::name} = md5($_));
}, @dir_list
);
#
# Check for changed, added files
# (clear any entries from the stored information for
# any files we found.)
foreach my $file (sort keys %real_info) {
if (not defined($file_info->{$file})) {
print "New file: $file\n";
} else {
if ($real_info{$file} ne $file_info->{$file}) {
print "Changed: $file\n";
}
# else the same
delete $file_info->{$file};
}
}
#
# All file information for existing files has been
# removed from the information data. So what's
# left is information on deleted files.
#
foreach my $file (sort keys %$file_info) {
print "Deleted: $file\n";
}
nstore \%real_info, $info_file_name;