xref: /aosp_15_r20/external/toybox/toys/other/lsattr.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* lsattr.c - List file attributes on a Linux second extended file system.
2  *
3  * Copyright 2013 Ranjan Kumar <[email protected]>
4  * Copyright 2013 Kyungwan Han <[email protected]>
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, p;
67 
68   unsigned add, rm, set;
69   // !add and !rm tell us whether they were used, but `chattr =` is meaningful.
70   int have_set;
71 )
72 
73 // Added more recently than the 7 year support horizon. TODO: remove
74 #ifndef FS_CASEFOLD_FL
75 #define FS_CASEFOLD_FL    0x40000000 // commit 71e90b4654a92 2019-07-23
76 #endif
77 #ifndef FS_VERITY_FL
78 #define FS_VERITY_FL      0x00100000 // commit fe9918d3b228b 2019-07-22
79 #endif
80 
81 static struct ext2_attr {
82   char *name;
83   unsigned flag;
84   char opt;
85 } e2attrs[] = {
86   // Do not sort! These are in the order that lsattr outputs them.
87   {"Secure_Deletion",               FS_SECRM_FL,        's'},
88   {"Undelete",                      FS_UNRM_FL,         'u'},
89   {"Synchronous_Updates",           FS_SYNC_FL,         'S'},
90   {"Synchronous_Directory_Updates", FS_DIRSYNC_FL,      'D'},
91   {"Immutable",                     FS_IMMUTABLE_FL,    'i'},
92   {"Append_Only",                   FS_APPEND_FL,       'a'},
93   {"No_Dump",                       FS_NODUMP_FL,       'd'},
94   {"No_Atime",                      FS_NOATIME_FL,      'A'},
95   {"Compression_Requested",         FS_COMPR_FL,        'c'},
96   {"Encrypted",                     FS_ENCRYPT_FL,      'E'},
97   {"Journaled_Data",                FS_JOURNAL_DATA_FL, 'j'},
98   {"Indexed_directory",             FS_INDEX_FL,        'I'},
99   {"No_Tailmerging",                FS_NOTAIL_FL,       't'},
100   {"Top_of_Directory_Hierarchies",  FS_TOPDIR_FL,       'T'},
101   {"Extents",                       FS_EXTENT_FL,       'e'},
102   {"No_COW",                        FS_NOCOW_FL,        'C'},
103   {"Casefold",                      FS_CASEFOLD_FL,     'F'},
104   {"Inline_Data",                   FS_INLINE_DATA_FL,  'N'},
105   {"Project_Hierarchy",             FS_PROJINHERIT_FL,  'P'},
106   {"Verity",                        FS_VERITY_FL,       'V'},
107   {NULL,                            0,                  0},
108 };
109 
110 // Get file flags on a Linux second extended file system.
ext2_getflag(int fd,struct stat * sb,unsigned * flag)111 static int ext2_getflag(int fd, struct stat *sb, unsigned *flag)
112 {
113   if(!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
114     errno = EOPNOTSUPP;
115     return -1;
116   }
117   return ioctl(fd, FS_IOC_GETFLAGS, flag);
118 }
119 
attrstr(unsigned attrs,int full)120 static char *attrstr(unsigned attrs, int full)
121 {
122   struct ext2_attr *a = e2attrs;
123   char *s = toybuf;
124 
125   for (; a->name; a++)
126     if (attrs & a->flag) *s++ = a->opt;
127     else if (full) *s++ = '-';
128   *s = 0;
129 
130   return toybuf;
131 }
132 
print_file_attr(char * path)133 static void print_file_attr(char *path)
134 {
135   unsigned flag = 0, version = 0;
136   int fd = -1;
137   struct stat sb;
138 
139   if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
140     errno = EOPNOTSUPP;
141     goto error;
142   }
143   if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto error;
144 
145   if (FLAG(p)) {
146     struct fsxattr fsx;
147 
148     if (ioctl(fd, FS_IOC_FSGETXATTR, &fsx)) goto error;
149     xprintf("%5u ", fsx.fsx_projid);
150   }
151   if (FLAG(v)) {
152     if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto error;
153     xprintf("%-10u ", version);
154   }
155 
156   if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
157   else {
158     struct ext2_attr *ptr = e2attrs;
159     int name_found = 0;
160 
161     if (FLAG(l)) {
162       xprintf("%-50s ", path);
163       for (; ptr->name; ptr++) {
164         if (flag & ptr->flag) {
165           if (name_found) xprintf(", "); //for formatting.
166           xprintf("%s", ptr->name);
167           name_found = 1;
168         }
169       }
170       if (!name_found) xprintf("---");
171       xputc('\n');
172     } else xprintf("%s %s\n", attrstr(flag, 1), path);
173   }
174   path = 0;
175 error:
176   xclose(fd);
177   if (path) perror_msg("reading '%s'", path);
178 }
179 
180 // Get directory information.
retell_dir(struct dirtree * root)181 static int retell_dir(struct dirtree *root)
182 {
183   char *fpath = 0;
184 
185   if (root->again) {
186     xputc('\n');
187 
188     return 0;
189   }
190   if (S_ISDIR(root->st.st_mode) && !root->parent)
191     return DIRTREE_RECURSE|DIRTREE_COMEAGAIN;
192 
193   fpath = dirtree_path(root, NULL);
194   if (*root->name != '.' || FLAG(a)) {
195     print_file_attr(fpath);
196     if (S_ISDIR(root->st.st_mode) && FLAG(R) && dirtree_notdotdot(root)) {
197       xprintf("\n%s:\n", fpath);
198       free(fpath);
199       return DIRTREE_RECURSE|DIRTREE_COMEAGAIN;
200     }
201   }
202   free(fpath);
203 
204   return 0;
205 }
206 
lsattr_main(void)207 void lsattr_main(void)
208 {
209   if (!*toys.optargs) dirtree_read(".", retell_dir);
210   else for (; *toys.optargs; toys.optargs++) {
211     struct stat sb;
212 
213     if (lstat(*toys.optargs, &sb)) perror_msg("stat '%s'", *toys.optargs);
214     else if (S_ISDIR(sb.st_mode) && !FLAG(d))
215       dirtree_read(*toys.optargs, retell_dir);
216     else print_file_attr(*toys.optargs);// to handle "./Filename" or "./Dir"
217   }
218 }
219 
220 // Switch gears from lsattr to chattr.
221 #define FOR_chattr
222 #include "generated/flags.h"
223 
224 // Set file flags on a Linux second extended file system.
ext2_setflag(int fd,struct stat * sb,unsigned flag)225 static inline int ext2_setflag(int fd, struct stat *sb, unsigned flag)
226 {
227   if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
228     errno = EOPNOTSUPP;
229     return -1;
230   }
231   return ioctl(fd, FS_IOC_SETFLAGS, &flag);
232 }
233 
get_flag_val(char ch)234 static unsigned get_flag_val(char ch)
235 {
236   struct ext2_attr *ptr = e2attrs;
237 
238   for (; ptr->name; ptr++) if (ptr->opt == ch) return ptr->flag;
239   help_exit("bad '%c'", ch);
240 }
241 
242 // Parse command line argument and fill the chattr structure.
parse_cmdline_arg(char *** argv)243 static void parse_cmdline_arg(char ***argv)
244 {
245   char *arg = **argv, *ptr;
246 
247   while (arg) {
248     if (*arg=='-') for (ptr = ++arg; *ptr; ptr++) TT.rm |= get_flag_val(*ptr);
249     else if (*arg=='+')
250       for (ptr = ++arg; *ptr; ptr++) TT.add |= get_flag_val(*ptr);
251     else if (*arg=='=') {
252       TT.have_set = 1;
253       for (ptr = ++arg; *ptr; ptr++) TT.set |= get_flag_val(*ptr);
254     } else return;
255     arg = *(*argv += 1);
256   }
257 }
258 
259 // Update attribute of given file.
update_attr(struct dirtree * root)260 static int update_attr(struct dirtree *root)
261 {
262   char *fpath = 0;
263   int vv = TT.v, fd;
264 
265   if (!dirtree_notdotdot(root)) return 0;
266 
267   // if file is a link and recursive is set or file is not regular+link+dir
268   // (like fifo or dev file) then escape the file.
269   if ((S_ISLNK(root->st.st_mode) && FLAG(R))
270     || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
271       && !S_ISDIR(root->st.st_mode)))
272     return 0;
273 
274   fpath = dirtree_path(root, NULL);
275   if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
276     free(fpath);
277     return DIRTREE_ABORT;
278   }
279 
280   // Any potential flag changes?
281   if (TT.have_set | TT.add | TT.rm) {
282     unsigned orig, new;
283 
284     // Read current flags.
285     if (ext2_getflag(fd, &(root->st), &orig) < 0) {
286       perror_msg("read flags of '%s'", fpath);
287       free(fpath);
288       xclose(fd);
289       return DIRTREE_ABORT;
290     }
291     // Apply the requested changes.
292     if (TT.have_set) new = TT.set; // '='.
293     else { // '-' and/or '+'.
294       new = orig;
295       new &= ~(TT.rm);
296       new |= TT.add;
297       if (!S_ISDIR(root->st.st_mode)) new &= ~FS_DIRSYNC_FL;
298     }
299     // Write them back if there was any change.
300     if (orig != new && ext2_setflag(fd, &(root->st), new)<0)
301       perror_msg("%s: setting flags to =%s failed", fpath, attrstr(new, 0));
302   }
303 
304   // (FS_IOC_SETVERSION works all the way back to 2.6, but FS_IOC_FSSETXATTR
305   // isn't available until 4.5.)
306   if (FLAG(v) && (ioctl(fd, FS_IOC_SETVERSION, &vv)<0))
307     perror_msg("%s: setting version to %d failed", fpath, vv);
308 
309   if (FLAG(p)) {
310     struct fsxattr fsx;
311     int fail = ioctl(fd, FS_IOC_FSGETXATTR, &fsx);
312 
313     fsx.fsx_projid = TT.p;
314     if (fail || ioctl(fd, FS_IOC_FSSETXATTR, &fsx))
315       perror_msg("%s: setting projid to %u failed", fpath, fsx.fsx_projid);
316   }
317 
318   free(fpath);
319   xclose(fd);
320   return (FLAG(R) && S_ISDIR(root->st.st_mode)) ? DIRTREE_RECURSE : 0;
321 }
322 
chattr_main(void)323 void chattr_main(void)
324 {
325   char **argv = toys.optargs;
326 
327   parse_cmdline_arg(&argv);
328   if (TT.p < 0 || TT.p > UINT_MAX) error_exit("bad projid %lu", TT.p);
329   if (TT.v < 0 || TT.v > UINT_MAX) error_exit("bad version %ld", TT.v);
330   if (!*argv) help_exit("no file");
331   if (TT.have_set && (TT.add || TT.rm))
332     error_exit("no '=' with '-' or '+'");
333   if (TT.rm & TT.add) error_exit("set/unset same flag");
334   if (!(TT.add || TT.rm || TT.have_set || FLAG(p) || FLAG(v)))
335     error_exit("need '-p', '-v', '=', '-', or '+'");
336   for (; *argv; argv++) dirtree_read(*argv, update_attr);
337 }
338