-
Notifications
You must be signed in to change notification settings - Fork 13
/
fattr.old
executable file
·338 lines (307 loc) · 7.18 KB
/
fattr.old
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#!/usr/bin/env perl
use warnings;
use strict;
use open qw(:std :utf8);
use Getopt::Long qw(:config bundling no_ignore_case);
use MIME::Base64;
use Nullroute::Lib;
# https://freedesktop.org/wiki/CommonExtendedAttributes/
my %ALIAS_ATTRS = (
author => "user.dublincore.creator",
comment => "user.xdg.comment",
date => "user.dublincore.date",
desc => "user.dublincore.description",
description => "user.dublincore.description",
lang => "user.xdg.language",
origin => "user.xdg.origin.url",
mime => "user.mime_type",
mimetype => "user.mime_type",
publisher => "user.dublincore.publisher",
referer => "user.xdg.referrer.url",
relation => "user.dublincore.relation",
subject => "user.dublincore.subject",
title => "user.dublincore.title",
);
my %HIDE_ATTRS = (
"user.com.dropbox.attributes" => 1,
"user.com.dropbox.attrs" => 1,
);
my %Opt = (
all => 0,
);
sub _system {
my (@args) = @_;
_debug("running: @args");
system {$args[0]} @args;
}
sub escape_c {
my ($str) = @_;
$str =~ s/[\\"]/\\$&/g;
$str =~ s/\n/\\n/g;
$str =~ s/\t/\\t/g;
$str =~ s/[\x00-\x1F]/sprintf("\\x%02X", ord($&))/ge;
return $str;
}
sub safe_val {
my ($str) = @_;
if (utf8::is_utf8($str) || utf8::decode($str)) {
return "\"".escape_c($str)."\"";
} else {
return "{".encode_base64($str, "")."}";
}
}
sub format_val {
my ($key, $str) = @_;
if ($key eq "DOSATTRIB") {
my @out;
my ($attrib_hex, $version, $rest) = unpack("Z* S> a*", $str);
if ($attrib_hex =~ /^0x([0-9A-F]+)$/) {
my $bits = hex $1;
my $attrib = "";
if ($bits & 0x01) { $attrib .= "R"; }
if ($bits & 0x02) { $attrib .= "H"; }
if ($bits & 0x04) { $attrib .= "S"; }
if ($bits & 0x20) { $attrib .= "A"; }
push @out, "attrib=".safe_val($attrib);
}
push @out, "version=".safe_val($version);
push @out, "rest=".safe_val($rest);
#return safe_val($str)." "."{@out}";
}
return safe_val($str);
}
sub linux_get_attrs_batch {
my (@files) = @_;
my %results;
if (open(my $fh, "-|",
"getfattr", "--dump",
$Opt{all} ? "--match=.*" : "--match=^user\\.",
"--encoding=base64",
"--absolute-names",
"--", @files))
{
my ($file, $key, $val);
while (<$fh>) {
if (/^# file: (.+)$/) {
$file = $1;
$results{$file} = {};
}
elsif (/^$/) {
$file = undef;
}
elsif (/^(.+?)=0s(.*)$/) {
$key = $1;
$val = decode_base64($2);
$results{$file}{$key} = $val;
}
else {
_die("BUG: getfattr returned unparsable line: $_");
}
}
close($fh);
}
return %results;
}
sub set_attrs {
my ($key, $val, @files) = @_;
my $ret = _system("setfattr", "-n", $key, "-v", $val, "--", @files);
return ($ret > 0);
}
sub unset_attrs {
my ($key, @files) = @_;
my $ret = _system("setfattr", "-x", $key, "--", @files);
return ($ret > 0);
}
sub _show_file_attrs {
my ($file, $attrs, $keys) = @_;
print "\e[1m".$file."\e[m\n";
if (!@$keys) {
print " \e[2m-- no attributes --\e[m\n";
}
for my $key (sort @$keys) {
my $val = $attrs->{$key} // next;
$key =~ s/^user\.// unless ($Opt{all} >= 2);
print " ".$key." = ".format_val($key, $val)."\n";
}
print "\n";
}
my $read_fattrs_batch;
my $read_fattrs;
my $list_fattrs;
my $read_fattr;
my $write_fattr;
if (eval {require File::ExtAttr}) {
_debug("using File::ExtAttr");
$list_fattrs = sub {
my ($path) = @_;
my @keys = ();
$! = undef;
my @namespaces = File::ExtAttr::listfattrns($path);
return if $!;
for my $ns (@namespaces) {
$! = undef;
my @nskeys = File::ExtAttr::listfattr($path, {namespace => $ns});
return if $!;
push @keys, map {"$ns.$_"} @nskeys;
}
return @keys;
};
$read_fattr = sub {
my ($path, $attr) = @_;
my ($ns, $tail) = split(/\./, $attr, 2);
$! = undef;
my $val = File::ExtAttr::getfattr($path, $tail, {namespace => $ns});
return if $!;
return $val;
};
}
elsif (eval {require Linux::UserXAttr}) {
_debug("File::ExtAttr missing; falling back to Linux::UserXAttr");
$list_fattrs = sub {
my ($path) = @_;
my @keys = Linux::UserXAttr::listxattr($path);
return @keys;
};
$read_fattr = sub {
my ($path, $attr) = @_;
my $value = Linux::UserXAttr::getxattr($path, $attr);
return $value;
};
}
else {
_notice("File::ExtAttr missing; falling back to getfattr");
$read_fattrs = sub {
my ($path) = @_;
my @cmd = ("getfattr", "--dump", "--match=.*", "--encoding=base64", "--absolute-names");
my %attrs;
if (open(my $fh, "-|", @cmd, "--", $path)) {
my ($file, $key, $val);
while (<$fh>) {
if (/^# file: (.+)$/) {
$file = $1;
%attrs = ();
} elsif (/^$/) {
$file = undef;
} elsif (/^(.+?)=0s(.*)$/) {
$key = $1;
$val = decode_base64($2);
$attrs{$key} = $val;
} else {
die("BUG: getfattr returned unparsable line: $_");
}
}
close($fh);
} else {
die("fork getfattr failed");
}
return %attrs;
};
$read_fattrs_batch = sub {
my (@paths) = @_;
return linux_get_attrs_batch(@paths);
};
}
sub dump_attrs {
my (@files) = @_;
my $ret = 0;
if ($read_fattrs_batch) {
my %all_attrs = $read_fattrs_batch->(@files);
for my $file (@files) {
my $attrs = $all_attrs{$file};
my @keys = keys %$attrs;
unless ($Opt{all}) {
@keys = grep {/^user\./} @keys;
@keys = grep {!$HIDE_ATTRS{$_}} @keys;
}
unless ($Opt{all} || $Opt{empty}) {
next if !@keys;
}
_show_file_attrs($file, $attrs, \@keys);
}
}
elsif ($list_fattrs && $read_fattr) {
for my $file (@files) {
my @keys = $list_fattrs->($file);
if ($!) {
_err("cannot list attrs of '$file': $!");
++$ret; next;
}
unless ($Opt{all}) {
@keys = grep {/^user\./} @keys;
@keys = grep {!$HIDE_ATTRS{$_}} @keys;
}
unless ($Opt{all} || $Opt{empty}) {
next if !@keys;
}
my %attrs;
for my $key (@keys) {
$attrs{$key} = $read_fattr->($file, $key);
if ($!) {
_err("cannot read attr '$key' of '$file': $!");
++$ret;
}
}
_show_file_attrs($file, \%attrs, \@keys);
}
}
else {
die("no implementation");
}
return ($ret > 0);
}
sub usage {
print "$_\n" for
"Usage: $::arg0 [-a] PATH... [KEY=VALUE...]",
"", #
" -a, --all include non-user namespaces",
" PATH file names to examine/modify",
" KEY=VALUE extended attributes to set",
" (empty value will remove attribute)",
}
utf8::decode($_) for @ARGV;
GetOptions(
"help" => sub { usage(); exit; },
"a|all+" => \$Opt{all},
"e|empty!" => \$Opt{empty},
) or exit(2);
my %attrs;
my @files;
my $attr_re = qr|[^/?=]+|;
for (@ARGV) {
if (-e $_ || -l $_) {
push @files, $_;
}
elsif (/^($attr_re)=(.*)$/) {
my ($key, $val) = ($1, $2);
if ($ALIAS_ATTRS{$key}) {
$key = $ALIAS_ATTRS{$key};
} elsif ($key =~ /^\./) {
$key =~ s///;
} elsif ($key !~ /^(user|system|security|trusted)\./) {
$key = "user.".$key;
}
$attrs{$key} = $val;
}
else {
push @files, $_;
}
}
if (!@files) {
_die("no files specified");
}
my $exit = 0;
if (%attrs) {
for my $key (keys %attrs) {
my $val = $attrs{$key};
if (length $val) {
_debug("setting '$key' to '$val'");
$exit |= set_attrs($key, $val, @files);
} else {
_debug("removing '$key'");
$exit |= unset_attrs($key, @files);
}
}
} else {
$exit |= dump_attrs(@files);
}
exit $exit;