1 /* lsattr.c - List file attributes on a Linux second extended file system.
2 *
3 * Copyright 2013 Ranjan Kumar <ranjankumar.bth@gmail.com>
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
5 *
6 * No Standard.
7 *
8 * TODO cleanup
9
10 USE_LSATTR(NEWTOY(lsattr, "ldapvR", TOYFLAG_BIN))
11 USE_CHATTR(NEWTOY(chattr, "?p#v#R", TOYFLAG_BIN))
12
13 config LSATTR
14 bool "lsattr"
15 default y
16 help
17 usage: lsattr [-Radlpv] [FILE...]
18
19 List file attributes on a Linux file system.
20 Flag letters are defined in chattr help.
21
22 -R Recursively list attributes of directories and their contents
23 -a List all files in directories, including files that start with '.'
24 -d List directories like other files, rather than listing their contents
25 -l List long flag names
26 -p List the file's project number
27 -v List the file's version/generation number
28
29 config CHATTR
30 bool "chattr"
31 default y
32 help
33 usage: chattr [-R] [-+=AacDdijsStTu] [-p PROJID] [-v VERSION] [FILE...]
34
35 Change file attributes on a Linux file system.
36
37 -R Recurse
38 -p Set the file's project number
39 -v Set the file's version/generation number
40
41 Operators:
42 '-' Remove attributes
43 '+' Add attributes
44 '=' Set attributes
45
46 Attributes:
47 A No atime a Append only
48 C No COW c Compression
49 D Synchronous dir updates d No dump
50 E Encrypted e Extents
51 F Case-insensitive (casefold)
52 I Indexed directory i Immutable
53 j Journal data
54 N Inline data in inode
55 P Project hierarchy
56 S Synchronous file updates s Secure delete
57 T Top of dir hierarchy t No tail-merging
58 u Allow undelete
59 V Verity
60 */
61 #define FOR_lsattr
62 #include "toys.h"
63 #include <linux/fs.h>
64
65 GLOBALS(
66 long v;
67 long p;
68
69 long add, rm, set;
70 // !add and !rm tell us whether they were used, but `chattr =` is meaningful.
71 int have_set;
72 )
73
74 #define FS_PROJINHERT_FL 0x20000000 // Linux 4.5
75 #define FS_CASEFOLD_FL 0x40000000 // Linux 5.4
76 #define FS_VERITY_FL 0x00100000 // Linux 5.4
77
78 // Linux 4.5
79 struct fsxattr_4_5 {
80 unsigned fsx_xflags;
81 unsigned fsx_extsize;
82 unsigned fsx_nextents;
83 unsigned fsx_projid;
84 unsigned fsx_cowextsize;
85 char fsx_pad[8];
86 };
87 #define FS_IOC_FSGETXATTR_4_5 _IOR('X', 31, struct fsxattr_4_5)
88 #define FS_IOC_FSSETXATTR_4_5 _IOW('X', 32, struct fsxattr_4_5)
89
90 static struct ext2_attr {
91 char *name;
92 unsigned long flag;
93 char opt;
94 } e2attrs[] = {
95 // Do not sort! These are in the order that lsattr outputs them.
96 {"Secure_Deletion", FS_SECRM_FL, 's'},
97 {"Undelete", FS_UNRM_FL, 'u'},
98 {"Synchronous_Updates", FS_SYNC_FL, 'S'},
99 {"Synchronous_Directory_Updates", FS_DIRSYNC_FL, 'D'},
100 {"Immutable", FS_IMMUTABLE_FL, 'i'},
101 {"Append_Only", FS_APPEND_FL, 'a'},
102 {"No_Dump", FS_NODUMP_FL, 'd'},
103 {"No_Atime", FS_NOATIME_FL, 'A'},
104 {"Compression_Requested", FS_COMPR_FL, 'c'},
105 // FS_ENCRYPT_FL added to linux 4.5 march 2016, +y7 = 2023
106 {"Encrypted", 0x800, 'E'},
107 {"Journaled_Data", FS_JOURNAL_DATA_FL, 'j'},
108 {"Indexed_directory", FS_INDEX_FL, 'I'},
109 {"No_Tailmerging", FS_NOTAIL_FL, 't'},
110 {"Top_of_Directory_Hierarchies", FS_TOPDIR_FL, 'T'},
111 {"Extents", FS_EXTENT_FL, 'e'},
112 {"No_COW", FS_NOCOW_FL, 'C'},
113 {"Casefold", FS_CASEFOLD_FL, 'F'},
114 {"Inline_Data", FS_INLINE_DATA_FL, 'N'},
115 {"Project_Hierarchy", FS_PROJINHERIT_FL, 'P'},
116 {"Verity", FS_VERITY_FL, 'V'},
117 {NULL, 0, 0},
118 };
119
120 // Get file flags on a Linux second extended file system.
ext2_getflag(int fd,struct stat * sb,unsigned long * flag)121 static int ext2_getflag(int fd, struct stat *sb, unsigned long *flag)
122 {
123 if(!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
124 errno = EOPNOTSUPP;
125 return -1;
126 }
127 return (ioctl(fd, FS_IOC_GETFLAGS, (void*)flag));
128 }
129
attrstr(unsigned long attrs,int full)130 static char *attrstr(unsigned long attrs, int full)
131 {
132 struct ext2_attr *a = e2attrs;
133 char *s = toybuf;
134
135 for (; a->name; a++)
136 if (attrs & a->flag) *s++ = a->opt;
137 else if (full) *s++ = '-';
138 *s = '\0';
139 return toybuf;
140 }
141
print_file_attr(char * path)142 static void print_file_attr(char *path)
143 {
144 unsigned long flag = 0, version = 0;
145 int fd;
146 struct stat sb;
147
148 if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
149 errno = EOPNOTSUPP;
150 goto LABEL1;
151 }
152 if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto LABEL1;
153
154 if (FLAG(p)) {
155 struct fsxattr_4_5 fsx;
156
157 if (ioctl(fd, FS_IOC_FSGETXATTR_4_5, &fsx)) goto LABEL2;
158 xprintf("%5u ", fsx.fsx_projid);
159 }
160 if (FLAG(v)) {
161 if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto LABEL2;
162 xprintf("%-10lu ", version);
163 }
164
165 if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
166 else {
167 struct ext2_attr *ptr = e2attrs;
168
169 if (FLAG(l)) {
170 int name_found = 0;
171
172 xprintf("%-50s ", path);
173 for (; ptr->name; ptr++) {
174 if (flag & ptr->flag) {
175 if (name_found) xprintf(", "); //for formatting.
176 xprintf("%s", ptr->name);
177 name_found = 1;
178 }
179 }
180 if (!name_found) xprintf("---");
181 xputc('\n');
182 } else xprintf("%s %s\n", attrstr(flag, 1), path);
183 }
184 xclose(fd);
185 return;
186 LABEL2: xclose(fd);
187 LABEL1: perror_msg("reading '%s'", path);
188 }
189
190 // Get directory information.
retell_dir(struct dirtree * root)191 static int retell_dir(struct dirtree *root)
192 {
193 char *fpath = NULL;
194
195 if (root->again) {
196 xputc('\n');
197 return 0;
198 }
199 if (S_ISDIR(root->st.st_mode) && !root->parent)
200 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
201
202 fpath = dirtree_path(root, NULL);
203 //Special case: with '-a' option and '.'/'..' also included in printing list.
204 if ((root->name[0] != '.') || FLAG(a)) {
205 print_file_attr(fpath);
206 if (S_ISDIR(root->st.st_mode) && FLAG(R) && dirtree_notdotdot(root)) {
207 xprintf("\n%s:\n", fpath);
208 free(fpath);
209 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
210 }
211 }
212 free(fpath);
213 return 0;
214 }
215
lsattr_main(void)216 void lsattr_main(void)
217 {
218 if (!*toys.optargs) dirtree_read(".", retell_dir);
219 else
220 for (; *toys.optargs; toys.optargs++) {
221 struct stat sb;
222
223 if (lstat(*toys.optargs, &sb)) perror_msg("stat '%s'", *toys.optargs);
224 else if (S_ISDIR(sb.st_mode) && !FLAG(d))
225 dirtree_read(*toys.optargs, retell_dir);
226 else print_file_attr(*toys.optargs);// to handle "./Filename" or "./Dir"
227 }
228 }
229
230 // Switch gears from lsattr to chattr.
231 #define CLEANUP_lsattr
232 #define FOR_chattr
233 #include "generated/flags.h"
234
235 // Set file flags on a Linux second extended file system.
ext2_setflag(int fd,struct stat * sb,unsigned long flag)236 static inline int ext2_setflag(int fd, struct stat *sb, unsigned long flag)
237 {
238 if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
239 errno = EOPNOTSUPP;
240 return -1;
241 }
242 return (ioctl(fd, FS_IOC_SETFLAGS, (void*)&flag));
243 }
244
get_flag_val(char ch)245 static unsigned long get_flag_val(char ch)
246 {
247 struct ext2_attr *ptr = e2attrs;
248
249 for (; ptr->name; ptr++)
250 if (ptr->opt == ch) return ptr->flag;
251 help_exit("bad '%c'", ch);
252 }
253
254 // Parse command line argument and fill the chattr structure.
parse_cmdline_arg(char *** argv)255 static void parse_cmdline_arg(char ***argv)
256 {
257 char *arg = **argv, *ptr;
258
259 while (arg) {
260 switch (arg[0]) {
261 case '-':
262 for (ptr = ++arg; *ptr; ptr++)
263 TT.rm |= get_flag_val(*ptr);
264 break;
265 case '+':
266 for (ptr = ++arg; *ptr; ptr++)
267 TT.add |= get_flag_val(*ptr);
268 break;
269 case '=':
270 TT.have_set = 1;
271 for (ptr = ++arg; *ptr; ptr++)
272 TT.set |= get_flag_val(*ptr);
273 break;
274 default: return;
275 }
276 arg = *(*argv += 1);
277 }
278 }
279
280 // Update attribute of given file.
update_attr(struct dirtree * root)281 static int update_attr(struct dirtree *root)
282 {
283 char *fpath = NULL;
284 int v = TT.v, fd;
285
286 if (!dirtree_notdotdot(root)) return 0;
287
288 /*
289 * if file is a link and recursive is set or file is not regular+link+dir
290 * (like fifo or dev file) then escape the file.
291 */
292 if ((S_ISLNK(root->st.st_mode) && FLAG(R))
293 || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
294 && !S_ISDIR(root->st.st_mode)))
295 return 0;
296
297 fpath = dirtree_path(root, NULL);
298 if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
299 free(fpath);
300 return DIRTREE_ABORT;
301 }
302
303 // Any potential flag changes?
304 if (TT.have_set | TT.add | TT.rm) {
305 unsigned long orig, new;
306
307 // Read current flags.
308 if (ext2_getflag(fd, &(root->st), &orig) < 0) {
309 perror_msg("read flags of '%s'", fpath);
310 free(fpath);
311 xclose(fd);
312 return DIRTREE_ABORT;
313 }
314 // Apply the requested changes.
315 if (TT.have_set) new = TT.set; // '='.
316 else { // '-' and/or '+'.
317 new = orig;
318 new &= ~(TT.rm);
319 new |= TT.add;
320 if (!S_ISDIR(root->st.st_mode)) new &= ~FS_DIRSYNC_FL;
321 }
322 // Write them back if there was any change.
323 if (orig != new && ext2_setflag(fd, &(root->st), new)<0)
324 perror_msg("%s: setting flags to =%s failed", fpath, attrstr(new, 0));
325 }
326
327 // (FS_IOC_SETVERSION works all the way back to 2.6, but FS_IOC_FSSETXATTR
328 // isn't available until 4.5.)
329 if (FLAG(v) && (ioctl(fd, FS_IOC_SETVERSION, &v)<0))
330 perror_msg("%s: setting version to %d failed", fpath, v);
331
332 if (FLAG(p)) {
333 struct fsxattr_4_5 fsx;
334 int fail = ioctl(fd, FS_IOC_FSGETXATTR_4_5, &fsx);
335
336 fsx.fsx_projid = TT.p;
337 if (fail || ioctl(fd, FS_IOC_FSSETXATTR_4_5, &fsx))
338 perror_msg("%s: setting projid to %u failed", fpath, fsx.fsx_projid);
339 }
340
341 free(fpath);
342 xclose(fd);
343 return (FLAG(R) && S_ISDIR(root->st.st_mode)) ? DIRTREE_RECURSE : 0;
344 }
345
chattr_main(void)346 void chattr_main(void)
347 {
348 char **argv = toys.optargs;
349
350 parse_cmdline_arg(&argv);
351 if (TT.p < 0 || TT.p > UINT_MAX) error_exit("bad projid %lu", TT.p);
352 if (TT.v < 0 || TT.v > UINT_MAX) error_exit("bad version %ld", TT.v);
353 if (!*argv) help_exit("no file");
354 if (TT.have_set && (TT.add || TT.rm))
355 error_exit("no '=' with '-' or '+'");
356 if (TT.rm & TT.add) error_exit("set/unset same flag");
357 if (!(TT.add || TT.rm || TT.have_set || FLAG(p) || FLAG(v)))
358 error_exit("need '-p', '-v', '=', '-', or '+'");
359 for (; *argv; argv++) dirtree_read(*argv, update_attr);
360 }
361