]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/chattr.c
mke2fs: configure encoding during superblock initialization
[thirdparty/e2fsprogs.git] / misc / chattr.c
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 "config.h"
24 #include <sys/types.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include "ext2fs/ext2_fs.h"
37
38 #ifdef __GNUC__
39 #define EXT2FS_ATTR(x) __attribute__(x)
40 #else
41 #define EXT2FS_ATTR(x)
42 #endif
43
44 #ifndef S_ISLNK /* So we can compile even with gcc-warn */
45 # ifdef __S_IFLNK
46 # define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
47 # else
48 # define S_ISLNK(mode) 0
49 # endif
50 #endif
51
52 #include "et/com_err.h"
53 #include "e2p/e2p.h"
54 #include "support/nls-enable.h"
55
56 #include "../version.h"
57
58 static const char * program_name = "chattr";
59
60 static int add;
61 static int rem;
62 static int set;
63 static int set_version;
64
65 static unsigned long version;
66
67 static int set_project;
68 static unsigned long project;
69
70 static int recursive;
71 static int verbose;
72 static int silent;
73
74 static unsigned long af;
75 static unsigned long rf;
76 static unsigned long sf;
77
78 #ifdef _LFS64_LARGEFILE
79 #define LSTAT lstat64
80 #define STRUCT_STAT struct stat64
81 #else
82 #define LSTAT lstat
83 #define STRUCT_STAT struct stat
84 #endif
85
86 static void usage(void)
87 {
88 fprintf(stderr,
89 _("Usage: %s [-pRVf] [-+=aAcCdDeijPsStTu] [-v version] files...\n"),
90 program_name);
91 exit(1);
92 }
93
94 struct flags_char {
95 unsigned long flag;
96 char optchar;
97 };
98
99 static const struct flags_char flags_array[] = {
100 { EXT2_NOATIME_FL, 'A' },
101 { EXT2_SYNC_FL, 'S' },
102 { EXT2_DIRSYNC_FL, 'D' },
103 { EXT2_APPEND_FL, 'a' },
104 { EXT2_COMPR_FL, 'c' },
105 { EXT2_NODUMP_FL, 'd' },
106 { EXT4_EXTENTS_FL, 'e'},
107 { EXT2_IMMUTABLE_FL, 'i' },
108 { EXT3_JOURNAL_DATA_FL, 'j' },
109 { EXT4_PROJINHERIT_FL, 'P' },
110 { EXT2_SECRM_FL, 's' },
111 { EXT2_UNRM_FL, 'u' },
112 { EXT2_NOTAIL_FL, 't' },
113 { EXT2_TOPDIR_FL, 'T' },
114 { FS_NOCOW_FL, 'C' },
115 { 0, 0 }
116 };
117
118 static unsigned long get_flag(char c)
119 {
120 const struct flags_char *fp;
121
122 for (fp = flags_array; fp->flag != 0; fp++) {
123 if (fp->optchar == c)
124 return fp->flag;
125 }
126 return 0;
127 }
128
129
130 static int decode_arg (int * i, int argc, char ** argv)
131 {
132 char * p;
133 char * tmp;
134 unsigned long fl;
135
136 switch (argv[*i][0])
137 {
138 case '-':
139 for (p = &argv[*i][1]; *p; p++) {
140 if (*p == 'R') {
141 recursive = 1;
142 continue;
143 }
144 if (*p == 'V') {
145 verbose = 1;
146 continue;
147 }
148 if (*p == 'f') {
149 silent = 1;
150 continue;
151 }
152 if (*p == 'p') {
153 (*i)++;
154 if (*i >= argc)
155 usage ();
156 project = strtol (argv[*i], &tmp, 0);
157 if (*tmp) {
158 com_err (program_name, 0,
159 _("bad project - %s\n"),
160 argv[*i]);
161 usage ();
162 }
163 set_project = 1;
164 continue;
165 }
166 if (*p == 'v') {
167 (*i)++;
168 if (*i >= argc)
169 usage ();
170 version = strtol (argv[*i], &tmp, 0);
171 if (*tmp) {
172 com_err (program_name, 0,
173 _("bad version - %s\n"),
174 argv[*i]);
175 usage ();
176 }
177 set_version = 1;
178 continue;
179 }
180 if ((fl = get_flag(*p)) == 0)
181 usage();
182 rf |= fl;
183 rem = 1;
184 }
185 break;
186 case '+':
187 add = 1;
188 for (p = &argv[*i][1]; *p; p++) {
189 if ((fl = get_flag(*p)) == 0)
190 usage();
191 af |= fl;
192 }
193 break;
194 case '=':
195 set = 1;
196 for (p = &argv[*i][1]; *p; p++) {
197 if ((fl = get_flag(*p)) == 0)
198 usage();
199 sf |= fl;
200 }
201 break;
202 default:
203 return EOF;
204 break;
205 }
206 return 1;
207 }
208
209 static int chattr_dir_proc(const char *, struct dirent *, void *);
210
211 static int change_attributes(const char * name)
212 {
213 unsigned long flags;
214 STRUCT_STAT st;
215
216 if (LSTAT (name, &st) == -1) {
217 if (!silent)
218 com_err (program_name, errno,
219 _("while trying to stat %s"), name);
220 return -1;
221 }
222
223 if (fgetflags(name, &flags) == -1) {
224 if (!silent)
225 com_err(program_name, errno,
226 _("while reading flags on %s"), name);
227 return -1;
228 }
229 if (set) {
230 if (verbose) {
231 printf (_("Flags of %s set as "), name);
232 print_flags (stdout, sf, 0);
233 printf ("\n");
234 }
235 if (fsetflags (name, sf) == -1)
236 perror (name);
237 } else {
238 if (rem)
239 flags &= ~rf;
240 if (add)
241 flags |= af;
242 if (verbose) {
243 printf(_("Flags of %s set as "), name);
244 print_flags(stdout, flags, 0);
245 printf("\n");
246 }
247 if (!S_ISDIR(st.st_mode))
248 flags &= ~EXT2_DIRSYNC_FL;
249 if (fsetflags(name, flags) == -1) {
250 if (!silent) {
251 com_err(program_name, errno,
252 _("while setting flags on %s"),
253 name);
254 }
255 return -1;
256 }
257 }
258 if (set_version) {
259 if (verbose)
260 printf (_("Version of %s set as %lu\n"), name, version);
261 if (fsetversion (name, version) == -1) {
262 if (!silent)
263 com_err (program_name, errno,
264 _("while setting version on %s"),
265 name);
266 return -1;
267 }
268 }
269 if (set_project) {
270 if (verbose)
271 printf (_("Project of %s set as %lu\n"), name, project);
272 if (fsetproject (name, project) == -1) {
273 if (!silent)
274 com_err (program_name, errno,
275 _("while setting project on %s"),
276 name);
277 return -1;
278 }
279
280 }
281 if (S_ISDIR(st.st_mode) && recursive)
282 return iterate_on_dir (name, chattr_dir_proc, NULL);
283 return 0;
284 }
285
286 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
287 void * private EXT2FS_ATTR((unused)))
288 {
289 int ret = 0;
290
291 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
292 char *path;
293
294 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
295 if (!path) {
296 fprintf(stderr, "%s",
297 _("Couldn't allocate path variable "
298 "in chattr_dir_proc"));
299 return -1;
300 }
301 sprintf(path, "%s/%s", dir_name, de->d_name);
302 ret = change_attributes(path);
303 free(path);
304 }
305 return ret;
306 }
307
308 int main (int argc, char ** argv)
309 {
310 int i, j;
311 int end_arg = 0;
312 int err, retval = 0;
313
314 #ifdef ENABLE_NLS
315 setlocale(LC_MESSAGES, "");
316 setlocale(LC_CTYPE, "");
317 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
318 textdomain(NLS_CAT_NAME);
319 set_com_err_gettext(gettext);
320 #endif
321 if (argc && *argv)
322 program_name = *argv;
323 i = 1;
324 while (i < argc && !end_arg) {
325 /* '--' arg should end option processing */
326 if (strcmp(argv[i], "--") == 0) {
327 i++;
328 end_arg = 1;
329 } else if (decode_arg (&i, argc, argv) == EOF)
330 end_arg = 1;
331 else
332 i++;
333 }
334 if (i >= argc)
335 usage ();
336 if (set && (add || rem)) {
337 fputs(_("= is incompatible with - and +\n"), stderr);
338 exit (1);
339 }
340 if ((rf & af) != 0) {
341 fputs("Can't both set and unset same flag.\n", stderr);
342 exit (1);
343 }
344 if (!(add || rem || set || set_version || set_project )) {
345 fputs(_("Must use '-v', =, - or +\n"), stderr);
346 exit (1);
347 }
348 if (verbose)
349 fprintf (stderr, "chattr %s (%s)\n",
350 E2FSPROGS_VERSION, E2FSPROGS_DATE);
351 for (j = i; j < argc; j++) {
352 err = change_attributes (argv[j]);
353 if (err)
354 retval = 1;
355 }
356 exit(retval);
357 }