1 /* lsattr.c - List file attributes on a Linux second extended file system.
3 * Copyright 2013 Ranjan Kumar <ranjankumar.bth@gmail.com>
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
8 USE_LSATTR(NEWTOY(lsattr, "vldaR", TOYFLAG_BIN))
9 USE_CHATTR(NEWTOY(chattr, NULL, TOYFLAG_BIN))
15 usage: lsattr [-Radlv] [Files...]
17 List file attributes on a Linux second extended file system.
19 -R Recursively list attributes of directories and their contents.
20 -a List all files in directories, including files that start with '.'.
21 -d List directories like other files, rather than listing their contents.
22 -l List long flag names.
23 -v List the file's version/generation number.
29 usage: chattr [-R] [-+=AacDdijsStTu] [-v version] [File...]
31 Change file attributes on a Linux second extended file system.
34 '-' Remove attributes.
42 D Write dir contents synchronously.
43 d Don't backup with dump.
44 i Cannot be modified (immutable).
45 j Write all data to journal first.
46 s Zero disk storage when deleted.
47 S Write file contents synchronously.
48 t Disable tail-merging of partial blocks with other files.
49 u Allow file to be undeleted.
51 -v Set the file's version/generation number.
58 static struct ext2_attr {
63 {"Secure_Deletion", FS_SECRM_FL, 's'}, // Secure deletion
64 {"Undelete", FS_UNRM_FL, 'u'}, // Undelete
65 {"Compression_Requested", FS_COMPR_FL, 'c'}, // Compress file
66 {"Synchronous_Updates", FS_SYNC_FL, 'S'}, // Synchronous updates
67 {"Immutable", FS_IMMUTABLE_FL, 'i'}, // Immutable file
68 {"Append_Only", FS_APPEND_FL, 'a'}, // writes to file may only append
69 {"No_Dump", FS_NODUMP_FL, 'd'}, // do not dump file
70 {"No_Atime", FS_NOATIME_FL, 'A'}, // do not update atime
71 {"Indexed_directory", FS_INDEX_FL, 'I'}, // hash-indexed directory
72 {"Journaled_Data", FS_JOURNAL_DATA_FL, 'j'}, // file data should be journaled
73 {"No_Tailmerging", FS_NOTAIL_FL, 't'}, // file tail should not be merged
74 {"Synchronous_Directory_Updates", FS_DIRSYNC_FL, 'D'}, // dirsync behaviour (directories only)
75 {"Top_of_Directory_Hierarchies", FS_TOPDIR_FL, 'T'}, // Top of directory hierarchies
79 // Get file flags on a Linux second extended file system.
80 static int ext2_getflag(int fd, struct stat *sb, unsigned long *flag)
82 if(!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
86 return (ioctl(fd, FS_IOC_GETFLAGS, (void*)flag));
89 static void print_file_attr(char *path)
91 unsigned long flag = 0, version = 0;
95 if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
99 if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto LABEL1;
101 if (toys.optflags & FLAG_v) {
102 if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto LABEL2;
103 xprintf("%5lu ", version);
106 if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
108 struct ext2_attr *ptr = e2attrs;
110 if (toys.optflags & FLAG_l) {
113 xprintf("%-50s ", path);
114 for (; ptr->name; ptr++) {
115 if (flag & ptr->flag) {
116 if (name_found) xprintf(", "); //for formatting.
117 xprintf("%s", ptr->name);
121 if (!name_found) xprintf("---");
126 for (; ptr->name; ptr++)
127 toybuf[index++] = (flag & ptr->flag) ? ptr->opt : '-';
128 toybuf[index] = '\0';
129 xprintf("%s %s\n", toybuf, path);
135 LABEL1: perror_msg("reading '%s'", path);
138 // Get directory information.
139 static int retell_dir(struct dirtree *root)
147 if (S_ISDIR(root->st.st_mode) && !root->parent)
148 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
150 fpath = dirtree_path(root, NULL);
151 //Special case: with '-a' option and '.'/'..' also included in printing list.
152 if ((root->name[0] != '.') || (toys.optflags & FLAG_a)) {
153 print_file_attr(fpath);
154 if (S_ISDIR(root->st.st_mode) && (toys.optflags & FLAG_R)
155 && dirtree_notdotdot(root)) {
156 xprintf("\n%s:\n", fpath);
158 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
165 void lsattr_main(void)
167 if (!*toys.optargs) dirtree_read(".", retell_dir);
169 for (; *toys.optargs; toys.optargs++) {
172 if (lstat(*toys.optargs, &sb)) perror_msg("stat '%s'", *toys.optargs);
173 else if (S_ISDIR(sb.st_mode) && !(toys.optflags & FLAG_d))
174 dirtree_read(*toys.optargs, retell_dir);
175 else print_file_attr(*toys.optargs);// to handle "./Filename" or "./Dir"
179 // Switch gears from lsattr to chattr.
180 #define CLEANUP_lsattr
182 #include "generated/flags.h"
184 static struct _chattr {
185 unsigned long add, rm, set, version;
186 unsigned char vflag, recursive;
189 static inline void chattr_help(void)
192 error_exit("Invalid Argument");
195 // Set file flags on a Linux second extended file system.
196 static inline int ext2_setflag(int fd, struct stat *sb, unsigned long flag)
198 if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
202 return (ioctl(fd, FS_IOC_SETFLAGS, (void*)&flag));
205 static unsigned long get_flag_val(char ch)
207 struct ext2_attr *ptr = e2attrs;
209 for (; ptr->name; ptr++)
210 if (ptr->opt == ch) return ptr->flag;
211 chattr_help(); // if no match found then Show help
212 return 0; // silent warning.
215 // Parse command line argument and fill the chattr structure.
216 static void parse_cmdline_arg(char ***argv)
218 char *arg = **argv, *ptr = NULL;
223 for (ptr = ++arg; *ptr; ptr++) {
225 chattr.recursive = 1;
227 } else if (*ptr == 'v') {// get version from next argv.
232 if (!arg) chattr_help();
233 if (*arg == '-') perror_exit("Invalid Number '%s'", arg);
234 chattr.version = strtoul(arg, &endptr, 0);
235 if (errno || *endptr) perror_exit("bad version '%s'", arg);
238 } else chattr.rm |= get_flag_val(*ptr);
242 for (ptr = ++arg; *ptr; ptr++)
243 chattr.add |= get_flag_val(*ptr);
246 for (ptr = ++arg; *ptr; ptr++)
247 chattr.set |= get_flag_val(*ptr);
255 // Update attribute of given file.
256 static int update_attr(struct dirtree *root)
258 unsigned long fval = 0;
262 if (!dirtree_notdotdot(root)) return 0;
265 * if file is a link and recursive is set or file is not regular+link+dir
266 * (like fifo or dev file) then escape the file.
268 if ((S_ISLNK(root->st.st_mode) && chattr.recursive)
269 || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
270 && !S_ISDIR(root->st.st_mode)))
273 fpath = dirtree_path(root, NULL);
274 if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
276 return DIRTREE_ABORT;
278 // Get current attr of file.
279 if (ext2_getflag(fd, &(root->st), &fval) < 0) {
280 perror_msg("read flags of '%s'", fpath);
283 return DIRTREE_ABORT;
285 if (chattr.set) { // for '=' operator.
286 if (ext2_setflag(fd, &(root->st), chattr.set) < 0)
287 perror_msg("setting flags '%s'", fpath);
288 } else { // for '-' / '+' operator.
289 fval &= ~(chattr.rm);
291 if (!S_ISDIR(root->st.st_mode)) fval &= ~FS_DIRSYNC_FL;
292 if (ext2_setflag(fd, &(root->st), fval) < 0)
293 perror_msg("setting flags '%s'", fpath);
295 if (chattr.vflag) { // set file version
296 if (ioctl(fd, FS_IOC_SETVERSION, (void*)&chattr.version) < 0)
297 perror_msg("while setting version on '%s'", fpath);
302 if (S_ISDIR(root->st.st_mode) && chattr.recursive) return DIRTREE_RECURSE;
306 void chattr_main(void)
308 char **argv = toys.optargs;
310 memset(&chattr, 0, sizeof(struct _chattr));
311 parse_cmdline_arg(&argv);
312 if (!*argv) chattr_help();
313 if (chattr.set && (chattr.add || chattr.rm))
314 error_exit("'=' is incompatible with '-' and '+'");
315 if (chattr.rm & chattr.add) error_exit("Can't set and unset same flag.");
316 if (!(chattr.add || chattr.rm || chattr.set || chattr.vflag))
317 error_exit(("Must use '-v', '=', '-' or '+'"));
318 for (; *argv; argv++) dirtree_read(*argv, update_attr);
319 toys.exitval = 0; //always set success at this point.