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#define FOR_lsattr
62#include "toys.h"
63#include <linux/fs.h>
64
65GLOBALS(
66 long v, p;
67
68 unsigned add, rm, set;
69
70 int have_set;
71)
72
73
74#ifndef FS_INLINE_DATA_FL
75#define FS_INLINE_DATA_FL 0x10000000
76#endif
77#ifndef FS_PROJINHERIT_FL
78#define FS_PROJINHERIT_FL 0x20000000
79#endif
80#ifndef FS_CASEFOLD_FL
81#define FS_CASEFOLD_FL 0x40000000
82#endif
83#ifndef FS_VERITY_FL
84#define FS_VERITY_FL 0x00100000
85#endif
86
87#ifndef FS_IOC_FSGETXATTR
88
89struct fsxattr {
90 unsigned fsx_xflags, fsx_extsize, fsx_nextents, fsx_projid, fsx_cowextsize;
91 char fsx_pad[8];
92};
93#define FS_IOC_FSGETXATTR _IOR('X', 31, struct fsxattr)
94#define FS_IOC_FSSETXATTR _IOW('X', 32, struct fsxattr)
95#endif
96
97static struct ext2_attr {
98 char *name;
99 unsigned flag;
100 char opt;
101} e2attrs[] = {
102
103 {"Secure_Deletion", FS_SECRM_FL, 's'},
104 {"Undelete", FS_UNRM_FL, 'u'},
105 {"Synchronous_Updates", FS_SYNC_FL, 'S'},
106 {"Synchronous_Directory_Updates", FS_DIRSYNC_FL, 'D'},
107 {"Immutable", FS_IMMUTABLE_FL, 'i'},
108 {"Append_Only", FS_APPEND_FL, 'a'},
109 {"No_Dump", FS_NODUMP_FL, 'd'},
110 {"No_Atime", FS_NOATIME_FL, 'A'},
111 {"Compression_Requested", FS_COMPR_FL, 'c'},
112
113 {"Encrypted", 0x800, 'E'},
114 {"Journaled_Data", FS_JOURNAL_DATA_FL, 'j'},
115 {"Indexed_directory", FS_INDEX_FL, 'I'},
116 {"No_Tailmerging", FS_NOTAIL_FL, 't'},
117 {"Top_of_Directory_Hierarchies", FS_TOPDIR_FL, 'T'},
118 {"Extents", FS_EXTENT_FL, 'e'},
119 {"No_COW", FS_NOCOW_FL, 'C'},
120 {"Casefold", FS_CASEFOLD_FL, 'F'},
121 {"Inline_Data", FS_INLINE_DATA_FL, 'N'},
122 {"Project_Hierarchy", FS_PROJINHERIT_FL, 'P'},
123 {"Verity", FS_VERITY_FL, 'V'},
124 {NULL, 0, 0},
125};
126
127
128static int ext2_getflag(int fd, struct stat *sb, unsigned *flag)
129{
130 if(!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
131 errno = EOPNOTSUPP;
132 return -1;
133 }
134 return (ioctl(fd, FS_IOC_GETFLAGS, (void*)flag));
135}
136
137static char *attrstr(unsigned attrs, int full)
138{
139 struct ext2_attr *a = e2attrs;
140 char *s = toybuf;
141
142 for (; a->name; a++)
143 if (attrs & a->flag) *s++ = a->opt;
144 else if (full) *s++ = '-';
145 *s = 0;
146
147 return toybuf;
148}
149
150static void print_file_attr(char *path)
151{
152 unsigned flag = 0, version = 0;
153 int fd = -1;
154 struct stat sb;
155
156 if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
157 errno = EOPNOTSUPP;
158 goto error;
159 }
160 if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto error;
161
162 if (FLAG(p)) {
163 struct fsxattr fsx;
164
165 if (ioctl(fd, FS_IOC_FSGETXATTR, &fsx)) goto error;
166 xprintf("%5u ", fsx.fsx_projid);
167 }
168 if (FLAG(v)) {
169 if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto error;
170 xprintf("%-10u ", version);
171 }
172
173 if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
174 else {
175 struct ext2_attr *ptr = e2attrs;
176
177 if (FLAG(l)) {
178 int name_found = 0;
179
180 xprintf("%-50s ", path);
181 for (; ptr->name; ptr++) {
182 if (flag & ptr->flag) {
183 if (name_found) xprintf(", ");
184 xprintf("%s", ptr->name);
185 name_found = 1;
186 }
187 }
188 if (!name_found) xprintf("---");
189 xputc('\n');
190 } else xprintf("%s %s\n", attrstr(flag, 1), path);
191 }
192 path = 0;
193error:
194 xclose(fd);
195 if (path) perror_msg("reading '%s'", path);
196}
197
198
199static int retell_dir(struct dirtree *root)
200{
201 char *fpath = NULL;
202
203 if (root->again) {
204 xputc('\n');
205 return 0;
206 }
207 if (S_ISDIR(root->st.st_mode) && !root->parent)
208 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
209
210 fpath = dirtree_path(root, NULL);
211
212 if ((root->name[0] != '.') || FLAG(a)) {
213 print_file_attr(fpath);
214 if (S_ISDIR(root->st.st_mode) && FLAG(R) && dirtree_notdotdot(root)) {
215 xprintf("\n%s:\n", fpath);
216 free(fpath);
217 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
218 }
219 }
220 free(fpath);
221 return 0;
222}
223
224void lsattr_main(void)
225{
226 if (!*toys.optargs) dirtree_read(".", retell_dir);
227 else
228 for (; *toys.optargs; toys.optargs++) {
229 struct stat sb;
230
231 if (lstat(*toys.optargs, &sb)) perror_msg("stat '%s'", *toys.optargs);
232 else if (S_ISDIR(sb.st_mode) && !FLAG(d))
233 dirtree_read(*toys.optargs, retell_dir);
234 else print_file_attr(*toys.optargs);
235 }
236}
237
238
239#define FOR_chattr
240#include "generated/flags.h"
241
242
243static inline int ext2_setflag(int fd, struct stat *sb, unsigned flag)
244{
245 if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
246 errno = EOPNOTSUPP;
247 return -1;
248 }
249 return (ioctl(fd, FS_IOC_SETFLAGS, (void*)&flag));
250}
251
252static unsigned get_flag_val(char ch)
253{
254 struct ext2_attr *ptr = e2attrs;
255
256 for (; ptr->name; ptr++) if (ptr->opt == ch) return ptr->flag;
257 help_exit("bad '%c'", ch);
258}
259
260
261static void parse_cmdline_arg(char ***argv)
262{
263 char *arg = **argv, *ptr;
264
265 while (arg) {
266 switch (arg[0]) {
267 case '-':
268 for (ptr = ++arg; *ptr; ptr++)
269 TT.rm |= get_flag_val(*ptr);
270 break;
271 case '+':
272 for (ptr = ++arg; *ptr; ptr++)
273 TT.add |= get_flag_val(*ptr);
274 break;
275 case '=':
276 TT.have_set = 1;
277 for (ptr = ++arg; *ptr; ptr++)
278 TT.set |= get_flag_val(*ptr);
279 break;
280 default: return;
281 }
282 arg = *(*argv += 1);
283 }
284}
285
286
287static int update_attr(struct dirtree *root)
288{
289 char *fpath = NULL;
290 int vv = TT.v, fd;
291
292 if (!dirtree_notdotdot(root)) return 0;
293
294
295
296
297
298 if ((S_ISLNK(root->st.st_mode) && FLAG(R))
299 || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
300 && !S_ISDIR(root->st.st_mode)))
301 return 0;
302
303 fpath = dirtree_path(root, NULL);
304 if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
305 free(fpath);
306 return DIRTREE_ABORT;
307 }
308
309
310 if (TT.have_set | TT.add | TT.rm) {
311 unsigned orig, new;
312
313
314 if (ext2_getflag(fd, &(root->st), &orig) < 0) {
315 perror_msg("read flags of '%s'", fpath);
316 free(fpath);
317 xclose(fd);
318 return DIRTREE_ABORT;
319 }
320
321 if (TT.have_set) new = TT.set;
322 else {
323 new = orig;
324 new &= ~(TT.rm);
325 new |= TT.add;
326 if (!S_ISDIR(root->st.st_mode)) new &= ~FS_DIRSYNC_FL;
327 }
328
329 if (orig != new && ext2_setflag(fd, &(root->st), new)<0)
330 perror_msg("%s: setting flags to =%s failed", fpath, attrstr(new, 0));
331 }
332
333
334
335 if (FLAG(v) && (ioctl(fd, FS_IOC_SETVERSION, &vv)<0))
336 perror_msg("%s: setting version to %d failed", fpath, vv);
337
338 if (FLAG(p)) {
339 struct fsxattr fsx;
340 int fail = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
341
342 fsx.fsx_projid = TT.p;
343 if (fail || ioctl(fd, FS_IOC_FSSETXATTR, &fsx))
344 perror_msg("%s: setting projid to %u failed", fpath, fsx.fsx_projid);
345 }
346
347 free(fpath);
348 xclose(fd);
349 return (FLAG(R) && S_ISDIR(root->st.st_mode)) ? DIRTREE_RECURSE : 0;
350}
351
352void chattr_main(void)
353{
354 char **argv = toys.optargs;
355
356 parse_cmdline_arg(&argv);
357 if (TT.p < 0 || TT.p > UINT_MAX) error_exit("bad projid %lu", TT.p);
358 if (TT.v < 0 || TT.v > UINT_MAX) error_exit("bad version %ld", TT.v);
359 if (!*argv) help_exit("no file");
360 if (TT.have_set && (TT.add || TT.rm))
361 error_exit("no '=' with '-' or '+'");
362 if (TT.rm & TT.add) error_exit("set/unset same flag");
363 if (!(TT.add || TT.rm || TT.have_set || FLAG(p) || FLAG(v)))
364 error_exit("need '-p', '-v', '=', '-', or '+'");
365 for (; *argv; argv++) dirtree_read(*argv, update_attr);
366}
367