2 # This script will parse the output of "find ARG [ARG...] -ls" and
3 # apply (at your discretion) the permissions, owner, and group info
4 # it reads onto any existing files and dirs (it doesn't try to affect
5 # symlinks). Run this with --help (-h) for a usage summary.
10 our($p_opt, $o_opt, $g_opt, $map_file, $dry_run, $verbosity, $help_opt);
12 &Getopt
::Long
::Configure
('bundling');
13 &usage
if !&GetOptions
(
14 'all|a' => sub { $p_opt = $o_opt = $g_opt = 1 },
17 'groups|g' => \
$g_opt,
18 'map|m=s' => \
$map_file,
19 'dry-run|n' => \
$dry_run,
20 'help|h' => \
$help_opt,
21 'verbose|v+' => \
$verbosity,
24 our(%uid_hash, %gid_hash);
26 $" = ', '; # How to join arrays referenced in double-quotes.
28 &parse_map_file($map_file) if defined $map_file;
31 ^ \s* \d+ \s+ # ignore inode
33 ([-bcdlps]) # 1. File type
34 ( [-r][-w][-xsS] # 2. user-permissions
35 [-r][-w][-xsS] # group-permissions
36 [-r][-w][-xtT] ) \s+ # other-permissions
37 \d+ \s+ # ignore number of links
40 (?: \d+ \s+ )? # ignore size (when present)
41 \w+ \s+ \d+ \s+ # ignore month and date
42 \d+ (?: : \d+ )? \s+ # ignore time or year
43 ([^\r\n]+) $ # 5. name
47 my($type, $perms, $owner, $group, $name) = /$detail_line/;
48 die "Invalid input line
$.:\n$_" unless defined $name;
49 die "A filename is
not properly escaped
:\n$_" unless $name =~ /^[^"\\]*(\\(\d\d\d
|\D
)[^"\\]*)*$/;
51 $fn =~ s/\\(\d+|.)/ eval "\"\\$1\"" /eg;
53 undef $type unless -f $fn;
54 } elsif ($type eq 'd') {
55 undef $type unless -d $fn;
56 } elsif ($type eq 'b') {
57 undef $type unless -b $fn;
58 } elsif ($type eq 'c') {
59 undef $type unless -c $fn;
60 } elsif ($type eq 'p') {
61 undef $type unless -p $fn;
62 } elsif ($type eq 's') {
63 undef $type unless -S $fn;
70 $type = "type
'$type'";
72 print "Skipping
$name ($type ignored
)\n";
77 my $reason = -e _ ? "types don
't match" : 'missing
';
78 print "Skipping $name ($reason)\n";
81 my($cur_mode, $cur_uid, $cur_gid) = (stat(_))[2,4,5];
83 my $highs = join('', $perms =~ /..(.)..(.)..(.)/);
84 $highs =~ tr/-rwxSTst/00001111/;
85 $perms =~ tr/-STrwxst/00011111/;
86 my $mode = $p_opt ? oct('0b
' . $highs . $perms) : $cur_mode;
87 my $uid = $o_opt ? $uid_hash{$owner} : $cur_uid;
89 if ($owner =~ /^\d+$/) {
92 $uid = getpwnam($owner);
94 $uid_hash{$owner} = $uid;
96 my $gid = $g_opt ? $gid_hash{$group} : $cur_gid;
98 if ($group =~ /^\d+$/) {
101 $gid = getgrnam($group);
103 $gid_hash{$group} = $gid;
107 if ($mode != $cur_mode) {
108 push(@changes, 'permissions
');
109 if (!$dry_run && !chmod($mode, $fn)) {
110 warn "chmod($mode, \"$name\") failed: $!\n";
113 if ($uid != $cur_uid || $gid != $cur_gid) {
114 push(@changes, 'owner
') if $uid != $cur_uid;
115 push(@changes, 'group
') if $gid != $cur_gid;
117 if (!chown($uid, $gid, $fn)) {
118 warn "chown($uid, $gid, \"$name\") failed: $!\n";
120 if (($mode & 06000) && !chmod($mode, $fn)) {
121 warn "post-chown chmod($mode, \"$name\") failed: $!\n";
126 print "$name: changed @changes\n";
127 } elsif ($verbosity) {
136 open(IN, $fn) or die "Unable to open $fn: $!\n";
138 if (/^user\s+(\S+)\s+(\S+)/) {
140 } elsif (/^group\s+(\S+)\s+(\S+)/) {
143 die "Invalid line #$. in mapfile `$fn':\n$_";
152 Usage: file-attr-restore [OPTIONS] FILE [FILE...]
153 -a, --all Restore all the attributes (-pog)
154 -p, --perms Restore the permissions
155 -o, --owner Restore the ownership
156 -g, --groups Restore the group
157 -m, --map=FILE Read user/group mappings from FILE
158 -n, --dry-run Don't actually make the changes
159 -v, --verbose Increase verbosity
160 -h, --help Show this help text
162 The FILE arg(s) should have been created by running the "find
"
163 program with "-ls
" as the output specifier.
165 The input file for the --map option must be in this format:
170 The "FROM
" should be an user/group mentioned in the input, and the TO
171 should be either a uid/gid number, or a local user/group name.