-
Notifications
You must be signed in to change notification settings - Fork 13
/
diff-inodes
executable file
·193 lines (166 loc) · 4.68 KB
/
diff-inodes
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/usr/bin/perl
#
# Compares output of dump-inodes with inode details in another tree.
#
# TODO:
# - Options for controlling which inode fields get checked
# - Option for applying the differences rather than just reporting them
# - Option to increase verbosity to see progress (defaults to no output
# if no changes required).
use strict;
use warnings;
use File::stat;
use File::Basename;
use Getopt::Long;
sub usage {
my $me = basename($0);
die <<EOF;
Usage: dump-inodes SRCDIR | $me [options] DSTDIR
General options:
--patch Apply the changes rather than just reporting differences
-e, --existing Ignore files which don't already exist in DSTDIR
-v, --verbose Increase verbosity (can be repeated)
-h, --help
Selecting which inode fields to check/apply:
-u, --uids Check/apply uids
-g, --gids Check/apply gids
-m, --modes Check/apply modes
-A, --atimes Check/apply access times
-M, --mtimes Check/apply modify times
Defaults to uids, gids, and modes.
EOF
}
my %opts = (verbose => 0);
process_opts();
my $dir = $ARGV[0];
chdir($dir) or die "chdir($dir) failed: $!\n";
my $FORMAT = 'line';
$/ = "" if $FORMAT eq 'para'; # one paragraph corresponds to one file
while (<STDIN>) {
my $data = parse_line($_);
next unless $data;
my $st = lstat($data->{file});
if (! $st) {
warn "# No such file $data->{file} in $dir\n"
unless $opts{existing};
next;
}
if ($opts{patch} ? $opts{verbose} >= 2 : $opts{verbose}) {
print "# ", $data->{file}, "\n";
}
if ($opts{uids} and $st->uid != $data->{uid}) {
if (! $opts{patch} || $opts{verbose}) {
printf "chown %s %s # %s (%s) -> %s\n",
$data->{uid}, $data->{file},
$st->uid, uid2user($st->uid),
$data->{user};
}
if ($opts{patch}) {
chown $data->{uid}, -1, $data->{file}
or warn "chown($data->{uid}, $data->{file}) failed: $!\n";
}
}
if ($opts{gids} and $st->gid != $data->{gid}) {
if (! $opts{patch} || $opts{verbose}) {
printf "chgrp %s %s # %s (%s) -> %s\n",
$data->{gid}, $data->{file},
$st->gid, gid2group($st->gid),
$data->{group};
}
if ($opts{patch}) {
chown -1, $data->{gid}, $data->{file}
or warn "chown($data->{gid}, $data->{file}) failed: $!\n";
}
}
if ($opts{modes}) {
# mode from File::stat is a genuine number:
my $mode = sprintf("%lo", $st->mode & 07777);
# mode from text file in $data is a standard string e.g. '664'
if ($mode ne $data->{mode}) {
if (! $opts{patch} || $opts{verbose}) {
printf "chmod %s %s # was %s\n", $data->{mode}, $data->{file}, $mode;
}
if ($opts{patch}) {
chmod oct($data->{mode}), $data->{file}
or warn "chmod($data->{mode}, $data->{file}) failed: $!\n";
}
}
}
if ($opts{atimes}) {
die "not yet";
if ($data->{atime} != $st->atime) {
if (! $opts{patch} || $opts{verbose}) {
print "touch -ac -d FIXME $data->{file}";
}
if ($opts{patch}) {
# leave mtime as it was
utime $data->{atime}, $st->mtime, $data->{file};
}
}
}
if ($opts{mtimes}) {
die "not yet";
if ($data->{mtime} != $st->mtime) {
if (! $opts{patch} || $opts{verbose}) {
print "touch -mc -d FIXME $data->{file}";
}
if ($opts{patch}) {
# leave atime as it was
utime $st->atime, $data->{mtime}, $data->{file};
}
}
}
}
sub parse_line {
$_ = shift;
my %data;
if ($FORMAT eq 'line') {
# line-based output format
return undef if /^\s*#/;
@data{qw/file user uid group gid mode atime ctime mtime/}
= /^(.+):(\w+):(\d+):(\w+):(\d+):(\d+):(\d+):(\d+):(\d+)\s*$/
or die "Failed to parse line $.:\n$_";
}
elsif ($FORMAT eq 'para') {
# paragraph-based output format
%data = /^(\w+)\s+(.+?)\s*$/gm;
die "file not found in block:\n$_" unless $data{file};
}
else {
die "huh? unknown \$FORMAT $FORMAT";
}
return \%data;
}
sub process_opts {
Getopt::Long::Configure('bundling');
GetOptions(
\%opts,
'patch',
'existing|e',
'verbose|v+',
'help|h',
'uids|u', 'gids|g', 'modes|m',
'atimes|A', 'mtimes|M',
) or usage();
usage() if $opts{help};
die "--atimes/--mtimes not supported yet\n"
if $opts{atimes} || $opts{mtimes};
usage() unless @ARGV == 1 and -d $ARGV[0];
my $any_opt = 0;
foreach my $opt (qw{uids gids modes atimes mtimes}) {
$any_opt++ if $opts{$opt};
}
if (! $any_opt) {
$opts{$_}++ foreach qw{uids gids modes};
}
}
{
my %uid_cache;
sub uid2user {
return $uid_cache{$_[0]} ||= getpwuid($_[0]);
}
my %gid_cache;
sub gid2group {
return $gid_cache{$_[0]} ||= getgrgid($_[0]);
}
}