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#define FOR_lsattr
58#include "toys.h"
59#include <linux/fs.h>
60
61static struct ext2_attr {
62 char *name;
63 unsigned long flag;
64 char opt;
65} e2attrs[] = {
66 {"Secure_Deletion", FS_SECRM_FL, 's'},
67 {"Undelete", FS_UNRM_FL, 'u'},
68 {"Compression_Requested", FS_COMPR_FL, 'c'},
69 {"Synchronous_Updates", FS_SYNC_FL, 'S'},
70 {"Immutable", FS_IMMUTABLE_FL, 'i'},
71 {"Append_Only", FS_APPEND_FL, 'a'},
72 {"No_Dump", FS_NODUMP_FL, 'd'},
73 {"No_Atime", FS_NOATIME_FL, 'A'},
74 {"Indexed_directory", FS_INDEX_FL, 'I'},
75 {"Journaled_Data", FS_JOURNAL_DATA_FL, 'j'},
76 {"No_Tailmerging", FS_NOTAIL_FL, 't'},
77 {"Synchronous_Directory_Updates", FS_DIRSYNC_FL, 'D'},
78 {"Top_of_Directory_Hierarchies", FS_TOPDIR_FL, 'T'},
79 {NULL, -1, 0},
80};
81
82
83static int ext2_getflag(int fd, struct stat *sb, unsigned long *flag)
84{
85 if(!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
86 errno = EOPNOTSUPP;
87 return -1;
88 }
89 return (ioctl(fd, FS_IOC_GETFLAGS, (void*)flag));
90}
91
92static void print_file_attr(char *path)
93{
94 unsigned long flag = 0, version = 0;
95 int fd;
96 struct stat sb;
97
98 if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
99 errno = EOPNOTSUPP;
100 goto LABEL1;
101 }
102 if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto LABEL1;
103
104 if (toys.optflags & FLAG_v) {
105 if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto LABEL2;
106 xprintf("%5lu ", version);
107 }
108
109 if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
110 else {
111 struct ext2_attr *ptr = e2attrs;
112
113 if (toys.optflags & FLAG_l) {
114 int name_found = 0;
115
116 xprintf("%-50s ", path);
117 for (; ptr->name; ptr++) {
118 if (flag & ptr->flag) {
119 if (name_found) xprintf(", ");
120 xprintf("%s", ptr->name);
121 name_found = 1;
122 }
123 }
124 if (!name_found) xprintf("---");
125 xputc('\n');
126 } else {
127 int index = 0;
128
129 for (; ptr->name; ptr++)
130 toybuf[index++] = (flag & ptr->flag) ? ptr->opt : '-';
131 toybuf[index] = '\0';
132 xprintf("%s %s\n", toybuf, path);
133 }
134 }
135 xclose(fd);
136 return;
137LABEL2: xclose(fd);
138LABEL1: perror_msg("reading '%s'", path);
139}
140
141
142static int retell_dir(struct dirtree *root)
143{
144 char *fpath = NULL;
145
146 if (root->again) {
147 xputc('\n');
148 return 0;
149 }
150 if (S_ISDIR(root->st.st_mode) && !root->parent)
151 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
152
153 fpath = dirtree_path(root, NULL);
154
155 if ((root->name[0] != '.') || (toys.optflags & FLAG_a)) {
156 print_file_attr(fpath);
157 if (S_ISDIR(root->st.st_mode) && (toys.optflags & FLAG_R)
158 && dirtree_notdotdot(root)) {
159 xprintf("\n%s:\n", fpath);
160 free(fpath);
161 return (DIRTREE_RECURSE | DIRTREE_COMEAGAIN);
162 }
163 }
164 free(fpath);
165 return 0;
166}
167
168void lsattr_main(void)
169{
170 if (!*toys.optargs) dirtree_read(".", retell_dir);
171 else
172 for (; *toys.optargs; toys.optargs++) {
173 struct stat sb;
174
175 if (lstat(*toys.optargs, &sb)) perror_msg("stat '%s'", *toys.optargs);
176 else if (S_ISDIR(sb.st_mode) && !(toys.optflags & FLAG_d))
177 dirtree_read(*toys.optargs, retell_dir);
178 else print_file_attr(*toys.optargs);
179 }
180}
181
182
183#define CLEANUP_lsattr
184#define FOR_chattr
185#include "generated/flags.h"
186
187static struct _chattr {
188 unsigned long add, rm, set, version;
189 unsigned char vflag, recursive;
190} chattr;
191
192
193static inline int ext2_setflag(int fd, struct stat *sb, unsigned long flag)
194{
195 if (!S_ISREG(sb->st_mode) && !S_ISDIR(sb->st_mode)) {
196 errno = EOPNOTSUPP;
197 return -1;
198 }
199 return (ioctl(fd, FS_IOC_SETFLAGS, (void*)&flag));
200}
201
202static unsigned long get_flag_val(char ch)
203{
204 struct ext2_attr *ptr = e2attrs;
205
206 for (; ptr->name; ptr++)
207 if (ptr->opt == ch) return ptr->flag;
208 help_exit("bad '%c'", ch);
209}
210
211
212static void parse_cmdline_arg(char ***argv)
213{
214 char *arg = **argv, *ptr = NULL;
215
216 while (arg) {
217 switch (arg[0]) {
218 case '-':
219 for (ptr = ++arg; *ptr; ptr++) {
220 if (*ptr == 'R') {
221 chattr.recursive = 1;
222 continue;
223 } else if (*ptr == 'v') {
224 char *endptr;
225
226 errno = 0;
227 arg = *(*argv += 1);
228 if (!arg) help_exit("bad -v");
229 if (*arg == '-') perror_exit("Invalid Number '%s'", arg);
230 chattr.version = strtoul(arg, &endptr, 0);
231 if (errno || *endptr) perror_exit("bad version '%s'", arg);
232 chattr.vflag = 1;
233 continue;
234 } else chattr.rm |= get_flag_val(*ptr);
235 }
236 break;
237 case '+':
238 for (ptr = ++arg; *ptr; ptr++)
239 chattr.add |= get_flag_val(*ptr);
240 break;
241 case '=':
242 for (ptr = ++arg; *ptr; ptr++)
243 chattr.set |= get_flag_val(*ptr);
244 break;
245 default: return;
246 }
247 arg = *(*argv += 1);
248 }
249}
250
251
252static int update_attr(struct dirtree *root)
253{
254 unsigned long fval = 0;
255 char *fpath = NULL;
256 int fd;
257
258 if (!dirtree_notdotdot(root)) return 0;
259
260
261
262
263
264 if ((S_ISLNK(root->st.st_mode) && chattr.recursive)
265 || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
266 && !S_ISDIR(root->st.st_mode)))
267 return 0;
268
269 fpath = dirtree_path(root, NULL);
270 if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
271 free(fpath);
272 return DIRTREE_ABORT;
273 }
274
275 if (ext2_getflag(fd, &(root->st), &fval) < 0) {
276 perror_msg("read flags of '%s'", fpath);
277 free(fpath);
278 xclose(fd);
279 return DIRTREE_ABORT;
280 }
281 if (chattr.set) {
282 if (ext2_setflag(fd, &(root->st), chattr.set) < 0)
283 perror_msg("setting flags '%s'", fpath);
284 } else {
285 fval &= ~(chattr.rm);
286 fval |= chattr.add;
287 if (!S_ISDIR(root->st.st_mode)) fval &= ~FS_DIRSYNC_FL;
288 if (ext2_setflag(fd, &(root->st), fval) < 0)
289 perror_msg("setting flags '%s'", fpath);
290 }
291 if (chattr.vflag) {
292 if (ioctl(fd, FS_IOC_SETVERSION, (void*)&chattr.version) < 0)
293 perror_msg("while setting version on '%s'", fpath);
294 }
295 free(fpath);
296 xclose(fd);
297
298 if (S_ISDIR(root->st.st_mode) && chattr.recursive) return DIRTREE_RECURSE;
299 return 0;
300}
301
302void chattr_main(void)
303{
304 char **argv = toys.optargs;
305
306 memset(&chattr, 0, sizeof(struct _chattr));
307 parse_cmdline_arg(&argv);
308 if (!*argv) help_exit("no file");
309 if (chattr.set && (chattr.add || chattr.rm))
310 error_exit("no '=' with '-' or '+'");
311 if (chattr.rm & chattr.add) error_exit("set/unset same flag");
312 if (!(chattr.add || chattr.rm || chattr.set || chattr.vflag))
313 error_exit("need '-v', '=', '-' or '+'");
314 for (; *argv; argv++) dirtree_read(*argv, update_attr);
315 toys.exitval = 0;
316}
317