1 /*
2 * chattr.c - Change file attributes on an ext2 file system
3 *
4 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
7 *
8 * This file can be redistributed under the terms of the GNU General
9 * Public License
10 */
11
12 /*
13 * History:
14 * 93/10/30 - Creation
15 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
16 * 94/02/27 - Integrated in Ted's distribution
17 * 98/12/29 - Ignore symlinks when working recursively (G M Sipe)
18 * 98/12/29 - Display version info only when -V specified (G M Sipe)
19 */
20
21 #define _LARGEFILE64_SOURCE
22
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include "ext2fs/ext2_fs.h"
36
37 #ifdef __GNUC__
38 #define EXT2FS_ATTR(x) __attribute__(x)
39 #else
40 #define EXT2FS_ATTR(x)
41 #endif
42
43 #ifndef S_ISLNK /* So we can compile even with gcc-warn */
44 # ifdef __S_IFLNK
45 # define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
46 # else
47 # define S_ISLNK(mode) 0
48 # endif
49 #endif
50
51 #include "et/com_err.h"
52 #include "e2p/e2p.h"
53
54 #include "../version.h"
55 #include "nls-enable.h"
56
57 static const char * program_name = "chattr";
58
59 static int add;
60 static int rem;
61 static int set;
62 static int set_version;
63
64 static unsigned long version;
65
66 static int recursive;
67 static int verbose;
68 static int silent;
69
70 static unsigned long af;
71 static unsigned long rf;
72 static unsigned long sf;
73
74 #ifdef _LFS64_LARGEFILE
75 #define LSTAT lstat64
76 #define STRUCT_STAT struct stat64
77 #else
78 #define LSTAT lstat
79 #define STRUCT_STAT struct stat
80 #endif
81
usage(void)82 static void usage(void)
83 {
84 fprintf(stderr,
85 _("Usage: %s [-RVf] [-+=AaCcDdeijsSu] [-v version] files...\n"),
86 program_name);
87 exit(1);
88 }
89
90 struct flags_char {
91 unsigned long flag;
92 char optchar;
93 };
94
95 static const struct flags_char flags_array[] = {
96 { EXT2_NOATIME_FL, 'A' },
97 { EXT2_SYNC_FL, 'S' },
98 { EXT2_DIRSYNC_FL, 'D' },
99 { EXT2_APPEND_FL, 'a' },
100 { EXT2_COMPR_FL, 'c' },
101 { EXT2_NODUMP_FL, 'd' },
102 { EXT4_EXTENTS_FL, 'e'},
103 { EXT2_IMMUTABLE_FL, 'i' },
104 { EXT3_JOURNAL_DATA_FL, 'j' },
105 { EXT2_SECRM_FL, 's' },
106 { EXT2_UNRM_FL, 'u' },
107 { EXT2_NOTAIL_FL, 't' },
108 { EXT2_TOPDIR_FL, 'T' },
109 { FS_NOCOW_FL, 'C' },
110 { 0, 0 }
111 };
112
get_flag(char c)113 static unsigned long get_flag(char c)
114 {
115 const struct flags_char *fp;
116
117 for (fp = flags_array; fp->flag != 0; fp++) {
118 if (fp->optchar == c)
119 return fp->flag;
120 }
121 return 0;
122 }
123
124
decode_arg(int * i,int argc,char ** argv)125 static int decode_arg (int * i, int argc, char ** argv)
126 {
127 char * p;
128 char * tmp;
129 unsigned long fl;
130
131 switch (argv[*i][0])
132 {
133 case '-':
134 for (p = &argv[*i][1]; *p; p++) {
135 if (*p == 'R') {
136 recursive = 1;
137 continue;
138 }
139 if (*p == 'V') {
140 verbose = 1;
141 continue;
142 }
143 if (*p == 'f') {
144 silent = 1;
145 continue;
146 }
147 if (*p == 'v') {
148 (*i)++;
149 if (*i >= argc)
150 usage ();
151 version = strtol (argv[*i], &tmp, 0);
152 if (*tmp) {
153 com_err (program_name, 0,
154 _("bad version - %s\n"),
155 argv[*i]);
156 usage ();
157 }
158 set_version = 1;
159 continue;
160 }
161 if ((fl = get_flag(*p)) == 0)
162 usage();
163 rf |= fl;
164 rem = 1;
165 }
166 break;
167 case '+':
168 add = 1;
169 for (p = &argv[*i][1]; *p; p++) {
170 if ((fl = get_flag(*p)) == 0)
171 usage();
172 af |= fl;
173 }
174 break;
175 case '=':
176 set = 1;
177 for (p = &argv[*i][1]; *p; p++) {
178 if ((fl = get_flag(*p)) == 0)
179 usage();
180 sf |= fl;
181 }
182 break;
183 default:
184 return EOF;
185 break;
186 }
187 return 1;
188 }
189
190 static int chattr_dir_proc(const char *, struct dirent *, void *);
191
change_attributes(const char * name)192 static int change_attributes(const char * name)
193 {
194 unsigned long flags;
195 STRUCT_STAT st;
196
197 if (LSTAT (name, &st) == -1) {
198 if (!silent)
199 com_err (program_name, errno,
200 _("while trying to stat %s"), name);
201 return -1;
202 }
203
204 if (fgetflags(name, &flags) == -1) {
205 if (!silent)
206 com_err(program_name, errno,
207 _("while reading flags on %s"), name);
208 return -1;
209 }
210 if (set) {
211 if (verbose) {
212 printf (_("Flags of %s set as "), name);
213 print_flags (stdout, sf, 0);
214 printf ("\n");
215 }
216 if (fsetflags (name, sf) == -1)
217 perror (name);
218 } else {
219 if (rem)
220 flags &= ~rf;
221 if (add)
222 flags |= af;
223 if (verbose) {
224 printf(_("Flags of %s set as "), name);
225 print_flags(stdout, flags, 0);
226 printf("\n");
227 }
228 if (!S_ISDIR(st.st_mode))
229 flags &= ~EXT2_DIRSYNC_FL;
230 if (fsetflags(name, flags) == -1) {
231 if (!silent) {
232 com_err(program_name, errno,
233 _("while setting flags on %s"),
234 name);
235 }
236 return -1;
237 }
238 }
239 if (set_version) {
240 if (verbose)
241 printf (_("Version of %s set as %lu\n"), name, version);
242 if (fsetversion (name, version) == -1) {
243 if (!silent)
244 com_err (program_name, errno,
245 _("while setting version on %s"),
246 name);
247 return -1;
248 }
249 }
250 if (S_ISDIR(st.st_mode) && recursive)
251 return iterate_on_dir (name, chattr_dir_proc, NULL);
252 return 0;
253 }
254
chattr_dir_proc(const char * dir_name,struct dirent * de,void * private EXT2FS_ATTR ((unused)))255 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
256 void * private EXT2FS_ATTR((unused)))
257 {
258 int ret = 0;
259
260 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
261 char *path;
262
263 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
264 if (!path) {
265 fprintf(stderr, "%s",
266 _("Couldn't allocate path variable "
267 "in chattr_dir_proc"));
268 return -1;
269 }
270 sprintf(path, "%s/%s", dir_name, de->d_name);
271 ret = change_attributes(path);
272 free(path);
273 }
274 return ret;
275 }
276
main(int argc,char ** argv)277 int main (int argc, char ** argv)
278 {
279 int i, j;
280 int end_arg = 0;
281 int err, retval = 0;
282
283 #ifdef ENABLE_NLS
284 setlocale(LC_MESSAGES, "");
285 setlocale(LC_CTYPE, "");
286 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
287 textdomain(NLS_CAT_NAME);
288 set_com_err_gettext(gettext);
289 #endif
290 if (argc && *argv)
291 program_name = *argv;
292 i = 1;
293 while (i < argc && !end_arg) {
294 /* '--' arg should end option processing */
295 if (strcmp(argv[i], "--") == 0) {
296 i++;
297 end_arg = 1;
298 } else if (decode_arg (&i, argc, argv) == EOF)
299 end_arg = 1;
300 else
301 i++;
302 }
303 if (i >= argc)
304 usage ();
305 if (set && (add || rem)) {
306 fputs(_("= is incompatible with - and +\n"), stderr);
307 exit (1);
308 }
309 if ((rf & af) != 0) {
310 fputs("Can't both set and unset same flag.\n", stderr);
311 exit (1);
312 }
313 if (!(add || rem || set || set_version)) {
314 fputs(_("Must use '-v', =, - or +\n"), stderr);
315 exit (1);
316 }
317 if (verbose)
318 fprintf (stderr, "chattr %s (%s)\n",
319 E2FSPROGS_VERSION, E2FSPROGS_DATE);
320 for (j = i; j < argc; j++) {
321 err = change_attributes (argv[j]);
322 if (err)
323 retval = 1;
324 }
325 exit(retval);
326 }
327