-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfattr
executable file
·159 lines (146 loc) · 4.55 KB
/
fattr
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
#!/usr/bin/env python3
import argparse
import itertools
import os
import re
import sys
attr_aliases = {
"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",
}
# Hidden from default view
hidden_attrs = {
"user.com.dropbox.attributes",
"user.com.dropbox.attrs",
"user.wine.sd",
}
def fmt_dosattrib_samba3(buf):
# ~/src/samba/librpc/idl/xattr.idl
import enum
import io
import struct
class DosAttrib(enum.IntFlag):
ReadOnly = 0x1
Hidden = 0x2
System = 0x4
Volume = 0x8
Directory = 0x10
Archive = 0x20
Device = 0x40
Normal = 0x80
Temporary = 0x100
Sparse = 0x200
ReparsePoint = 0x400
Compressed = 0x800
Offline = 0x1000
NonIndexed = 0x2000
Encrypted = 0x4000
# Read legacy attrib_hex
attrib, _, rest = buf.partition(b"\x00")
if attrib:
attrib = int(attrib, 16)
attrib = DosAttrib(attrib)
return f"({attrib._name_}) {buf}"
else:
print(f"fattr: XXX: attrib_hex not present in {buf!r}",
file=sys.stderr)
# Someday later
#class ValidFlags(enum.IntFlag):
# Attrib = 0x1
# EaSize = 0x2
# Size = 0x4
# AllocSize = 0x8
# CreateTime = 0x10
# ChangeTime = 0x20
# ITime = 0x40
#if buf:
# rd = io.BytesIO(buf)
# version = struct.unpack(">H", rd.read(2))
# print(f"{version = }")
# if version == 3:
return buf
def show_attrs(file, *,
follow=False,
all_ns=False,
_count=itertools.count()):
print(f"\033[1m{file}\033[m")
keys = os.listxattr(file, follow_symlinks=follow)
if not args.all:
keys = {k for k in keys if k.startswith("user.")}
keys -= hidden_attrs
#if not keys and not (args.all or args.empty):
# continue
attrs = {key: os.getxattr(file, key, follow_symlinks=follow)
for key in keys}
if not attrs:
print(f" (No attributes.)")
for key, value in sorted(attrs.items()):
if key == "user.DOSATTRIB" and not args.all:
value = fmt_dosattrib_samba3(value)
if not args.all:
key = key.removeprefix("user.")
print(f" {key} = {value}")
if next(_count) > 1:
print()
parser = argparse.ArgumentParser(add_help=False)
parser.add_argument("--help", action="help",
help="show this help message and exit")
parser.add_argument("-a", "--all", action="store_true",
help="do not hide any attributes")
parser.add_argument("-h", "--no-dereference", action="store_true",
help="do not dereference symbolic links")
parser.add_argument("-r", "--recurse", action="store_true",
help="descend into directories")
parser.add_argument("-s", "--set", metavar="KEY=VALUE",
default=[], action="append",
help="set an attribute")
parser.add_argument("path", nargs="+")
args = parser.parse_args()
if args.set:
exit("Not yet implemented")
sargs = dict(follow=not args.no_dereference,
all_ns=args.all)
for path in args.path:
show_attrs(path, **sargs)
if os.path.isdir(path) and args.recurse:
for dirpath, dirnames, filenames in os.walk(path):
for name in dirnames + filenames:
show_attrs(os.path.join(dirpath, name), **sargs)
#files = []
#attrs = {}
#
#for arg in sys.argv[1:]:
# if m := re.match(r"^([^/?=]+)=(.*)$", arg):
# key = m.group(1)
# val = m.group(2)
# if key in attr_aliases:
# key = attr_aliases[key]
# elif key[0] == ".":
# key = key[1:]
# elif not re.match(r"^(user|system|security|trusted)\.", key):
# key = "user." + key
# if key in attrs:
# Core.warn("attribute %r seen multiple times", key)
# attrs[key] = val
# else:
# files.append(arg)
#
#print(files)
#print(attrs)
#
#if attrs:
# ...
#else:
# ret = dump_attrs(files, args)