• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   {"Encrypted",                     FS_ENCRYPT_FL,      'E'},
106   {"Journaled_Data",                FS_JOURNAL_DATA_FL, 'j'},
107   {"Indexed_directory",             FS_INDEX_FL,        'I'},
108   {"No_Tailmerging",                FS_NOTAIL_FL,       't'},
109   {"Top_of_Directory_Hierarchies",  FS_TOPDIR_FL,       'T'},
110   {"Extents",                       FS_EXTENT_FL,       'e'},
111   {"No_COW",                        FS_NOCOW_FL,        'C'},
112   {"Casefold",                      FS_CASEFOLD_FL,     'F'},
113   {"Inline_Data",                   FS_INLINE_DATA_FL,  'N'},
114   {"Project_Hierarchy",             FS_PROJINHERIT_FL,  'P'},
115   {"Verity",                        FS_VERITY_FL,       'V'},
116   {NULL,                            0,                  0},
117 };
118 
119 // Get file flags on a Linux second extended file system.
ext2_getflag(int fd,struct stat * sb,unsigned long * flag)120 static int ext2_getflag(int fd, struct stat *sb, unsigned long *flag)
121 {
122   if(!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
123     errno = EOPNOTSUPP;
124     return -1;
125   }
126   return (ioctl(fd, FS_IOC_GETFLAGS, (void*)flag));
127 }
128 
attrstr(unsigned long attrs,int full)129 static char *attrstr(unsigned long attrs, int full)
130 {
131   struct ext2_attr *a = e2attrs;
132   char *s = toybuf;
133 
134   for (; a->name; a++)
135     if (attrs & a->flag) *s++ = a->opt;
136     else if (full) *s++ = '-';
137   *s = '\0';
138   return toybuf;
139 }
140 
print_file_attr(char * path)141 static void print_file_attr(char *path)
142 {
143   unsigned long flag = 0, version = 0;
144   int fd;
145   struct stat sb;
146 
147   if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
148     errno = EOPNOTSUPP;
149     goto LABEL1;
150   }
151   if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto LABEL1;
152 
153   if (FLAG(p)) {
154     struct fsxattr_4_5 fsx;
155 
156     if (ioctl(fd, FS_IOC_FSGETXATTR_4_5, &fsx)) goto LABEL2;
157     xprintf("%5u ", fsx.fsx_projid);
158   }
159   if (FLAG(v)) {
160     if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto LABEL2;
161     xprintf("%-10lu ", version);
162   }
163 
164   if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
165   else {
166     struct ext2_attr *ptr = e2attrs;
167 
168     if (FLAG(l)) {
169       int name_found = 0;
170 
171       xprintf("%-50s ", path);
172       for (; ptr->name; ptr++) {
173         if (flag & ptr->flag) {
174           if (name_found) xprintf(", "); //for formatting.
175           xprintf("%s", ptr->name);
176           name_found = 1;
177         }
178       }
179       if (!name_found) xprintf("---");
180       xputc('\n');
181     } else xprintf("%s %s\n", attrstr(flag, 1), path);
182   }
183   xclose(fd);
184   return;
185 LABEL2: xclose(fd);
186 LABEL1: perror_msg("reading '%s'", path);
187 }
188 
189 // Get directory information.
retell_dir(struct dirtree * root)190 static int retell_dir(struct dirtree *root)
191 {
192   char *fpath = NULL;
193 
194   if (root->again) {
195     xputc('\n');
196     return 0;
197   }
198   if (S_ISDIR(root->st.st_mode) && !root->parent)
199     return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
200 
201   fpath = dirtree_path(root, NULL);
202   //Special case: with '-a' option and '.'/'..' also included in printing list.
203   if ((root->name[0] != '.') || FLAG(a)) {
204     print_file_attr(fpath);
205     if (S_ISDIR(root->st.st_mode) && FLAG(R) && dirtree_notdotdot(root)) {
206       xprintf("\n%s:\n", fpath);
207       free(fpath);
208       return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
209     }
210   }
211   free(fpath);
212   return 0;
213 }
214 
lsattr_main(void)215 void lsattr_main(void)
216 {
217   if (!*toys.optargs) dirtree_read(".", retell_dir);
218   else
219     for (; *toys.optargs; toys.optargs++) {
220       struct stat sb;
221 
222       if (lstat(*toys.optargs, &sb)) perror_msg("stat '%s'", *toys.optargs);
223       else if (S_ISDIR(sb.st_mode) && !FLAG(d))
224         dirtree_read(*toys.optargs, retell_dir);
225       else print_file_attr(*toys.optargs);// to handle "./Filename" or "./Dir"
226     }
227 }
228 
229 // Switch gears from lsattr to chattr.
230 #define CLEANUP_lsattr
231 #define FOR_chattr
232 #include "generated/flags.h"
233 
234 // Set file flags on a Linux second extended file system.
ext2_setflag(int fd,struct stat * sb,unsigned long flag)235 static inline int ext2_setflag(int fd, struct stat *sb, unsigned long flag)
236 {
237   if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
238     errno = EOPNOTSUPP;
239     return -1;
240   }
241   return (ioctl(fd, FS_IOC_SETFLAGS, (void*)&flag));
242 }
243 
get_flag_val(char ch)244 static unsigned long get_flag_val(char ch)
245 {
246   struct ext2_attr *ptr = e2attrs;
247 
248   for (; ptr->name; ptr++)
249     if (ptr->opt == ch) return ptr->flag;
250   help_exit("bad '%c'", ch);
251 }
252 
253 // Parse command line argument and fill the chattr structure.
parse_cmdline_arg(char *** argv)254 static void parse_cmdline_arg(char ***argv)
255 {
256   char *arg = **argv, *ptr;
257 
258   while (arg) {
259     switch (arg[0]) {
260       case '-':
261         for (ptr = ++arg; *ptr; ptr++)
262           TT.rm |= get_flag_val(*ptr);
263         break;
264       case '+':
265         for (ptr = ++arg; *ptr; ptr++)
266           TT.add |= get_flag_val(*ptr);
267         break;
268       case '=':
269         TT.have_set = 1;
270         for (ptr = ++arg; *ptr; ptr++)
271           TT.set |= get_flag_val(*ptr);
272         break;
273       default: return;
274     }
275     arg = *(*argv += 1);
276   }
277 }
278 
279 // Update attribute of given file.
update_attr(struct dirtree * root)280 static int update_attr(struct dirtree *root)
281 {
282   char *fpath = NULL;
283   int v = TT.v, fd;
284 
285   if (!dirtree_notdotdot(root)) return 0;
286 
287   /*
288    * if file is a link and recursive is set or file is not regular+link+dir
289    * (like fifo or dev file) then escape the file.
290    */
291   if ((S_ISLNK(root->st.st_mode) && FLAG(R))
292     || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
293       && !S_ISDIR(root->st.st_mode)))
294     return 0;
295 
296   fpath = dirtree_path(root, NULL);
297   if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
298     free(fpath);
299     return DIRTREE_ABORT;
300   }
301 
302   // Any potential flag changes?
303   if (TT.have_set | TT.add | TT.rm) {
304     unsigned long orig, new;
305 
306     // Read current flags.
307     if (ext2_getflag(fd, &(root->st), &orig) < 0) {
308       perror_msg("read flags of '%s'", fpath);
309       free(fpath);
310       xclose(fd);
311       return DIRTREE_ABORT;
312     }
313     // Apply the requested changes.
314     if (TT.have_set) new = TT.set; // '='.
315     else { // '-' and/or '+'.
316       new = orig;
317       new &= ~(TT.rm);
318       new |= TT.add;
319       if (!S_ISDIR(root->st.st_mode)) new &= ~FS_DIRSYNC_FL;
320     }
321     // Write them back if there was any change.
322     if (orig != new && ext2_setflag(fd, &(root->st), new)<0)
323       perror_msg("%s: setting flags to =%s failed", fpath, attrstr(new, 0));
324   }
325 
326   // (FS_IOC_SETVERSION works all the way back to 2.6, but FS_IOC_FSSETXATTR
327   // isn't available until 4.5.)
328   if (FLAG(v) && (ioctl(fd, FS_IOC_SETVERSION, &v)<0))
329     perror_msg("%s: setting version to %d failed", fpath, v);
330 
331   if (FLAG(p)) {
332     struct fsxattr_4_5 fsx;
333     int fail = ioctl(fd, FS_IOC_FSGETXATTR_4_5, &fsx);
334 
335     fsx.fsx_projid = TT.p;
336     if (fail || ioctl(fd, FS_IOC_FSSETXATTR_4_5, &fsx))
337       perror_msg("%s: setting projid to %u failed", fpath, fsx.fsx_projid);
338   }
339 
340   free(fpath);
341   xclose(fd);
342   return (FLAG(R) && S_ISDIR(root->st.st_mode)) ? DIRTREE_RECURSE : 0;
343 }
344 
chattr_main(void)345 void chattr_main(void)
346 {
347   char **argv = toys.optargs;
348 
349   parse_cmdline_arg(&argv);
350   if (TT.p < 0 || TT.p > UINT_MAX) error_exit("bad projid %lu", TT.p);
351   if (TT.v < 0 || TT.v > UINT_MAX) error_exit("bad version %ld", TT.v);
352   if (!*argv) help_exit("no file");
353   if (TT.have_set && (TT.add || TT.rm))
354     error_exit("no '=' with '-' or '+'");
355   if (TT.rm & TT.add) error_exit("set/unset same flag");
356   if (!(TT.add || TT.rm || TT.have_set || FLAG(p) || FLAG(v)))
357     error_exit("need '-p', '-v', '=', '-', or '+'");
358   for (; *argv; argv++) dirtree_read(*argv, update_attr);
359 }
360