]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/chattr.c
Merge branch 'maint' into next
[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 <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
82 static void usage(void)
83 {
84 fprintf(stderr,
85 _("Usage: %s [-RVf] [-+=AacDdijsSu] [-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 { EXT2_IMMUTABLE_FL, 'i' },
103 { EXT3_JOURNAL_DATA_FL, 'j' },
104 { EXT2_SECRM_FL, 's' },
105 { EXT2_UNRM_FL, 'u' },
106 { EXT2_NOTAIL_FL, 't' },
107 { EXT2_TOPDIR_FL, 'T' },
108 { 0, 0 }
109 };
110
111 static unsigned long get_flag(char c)
112 {
113 const struct flags_char *fp;
114
115 for (fp = flags_array; fp->flag != 0; fp++) {
116 if (fp->optchar == c)
117 return fp->flag;
118 }
119 return 0;
120 }
121
122
123 static int decode_arg (int * i, int argc, char ** argv)
124 {
125 char * p;
126 char * tmp;
127 unsigned long fl;
128
129 switch (argv[*i][0])
130 {
131 case '-':
132 for (p = &argv[*i][1]; *p; p++) {
133 if (*p == 'R') {
134 recursive = 1;
135 continue;
136 }
137 if (*p == 'V') {
138 verbose = 1;
139 continue;
140 }
141 if (*p == 'f') {
142 silent = 1;
143 continue;
144 }
145 if (*p == 'v') {
146 (*i)++;
147 if (*i >= argc)
148 usage ();
149 version = strtol (argv[*i], &tmp, 0);
150 if (*tmp) {
151 com_err (program_name, 0,
152 _("bad version - %s\n"),
153 argv[*i]);
154 usage ();
155 }
156 set_version = 1;
157 continue;
158 }
159 if ((fl = get_flag(*p)) == 0)
160 usage();
161 rf |= fl;
162 rem = 1;
163 }
164 break;
165 case '+':
166 add = 1;
167 for (p = &argv[*i][1]; *p; p++) {
168 if ((fl = get_flag(*p)) == 0)
169 usage();
170 af |= fl;
171 }
172 break;
173 case '=':
174 set = 1;
175 for (p = &argv[*i][1]; *p; p++) {
176 if ((fl = get_flag(*p)) == 0)
177 usage();
178 sf |= fl;
179 }
180 break;
181 default:
182 return EOF;
183 break;
184 }
185 return 1;
186 }
187
188 static int chattr_dir_proc(const char *, struct dirent *, void *);
189
190 static int change_attributes(const char * name)
191 {
192 unsigned long flags;
193 STRUCT_STAT st;
194
195 if (LSTAT (name, &st) == -1) {
196 if (!silent)
197 com_err (program_name, errno,
198 _("while trying to stat %s"), name);
199 return -1;
200 }
201
202 if (set) {
203 if (verbose) {
204 printf (_("Flags of %s set as "), name);
205 print_flags (stdout, sf, 0);
206 printf ("\n");
207 }
208 if (fsetflags (name, sf) == -1)
209 perror (name);
210 } else {
211 if (fgetflags (name, &flags) == -1) {
212 if (!silent)
213 com_err (program_name, errno,
214 _("while reading flags on %s"), name);
215 return -1;
216 } else {
217 if (rem)
218 flags &= ~rf;
219 if (add)
220 flags |= af;
221 if (verbose) {
222 printf (_("Flags of %s set as "), name);
223 print_flags (stdout, flags, 0);
224 printf ("\n");
225 }
226 if (!S_ISDIR(st.st_mode))
227 flags &= ~EXT2_DIRSYNC_FL;
228 if (fsetflags (name, flags) == -1) {
229 if (!silent)
230 com_err(program_name, errno,
231 _("while setting flags on %s"),
232 name);
233 return -1;
234 }
235 }
236 }
237 if (set_version) {
238 if (verbose)
239 printf (_("Version of %s set as %lu\n"), name, version);
240 if (fsetversion (name, version) == -1) {
241 if (!silent)
242 com_err (program_name, errno,
243 _("while setting version on %s"),
244 name);
245 return -1;
246 }
247 }
248 if (S_ISDIR(st.st_mode) && recursive)
249 return iterate_on_dir (name, chattr_dir_proc, NULL);
250 return 0;
251 }
252
253 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
254 void * private EXT2FS_ATTR((unused)))
255 {
256 int ret = 0;
257
258 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
259 char *path;
260
261 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
262 if (!path) {
263 fprintf(stderr, _("Couldn't allocate path variable "
264 "in chattr_dir_proc"));
265 return -1;
266 }
267 sprintf(path, "%s/%s", dir_name, de->d_name);
268 ret = change_attributes(path);
269 free(path);
270 }
271 return ret;
272 }
273
274 int main (int argc, char ** argv)
275 {
276 int i, j;
277 int end_arg = 0;
278 int err, retval = 0;
279
280 #ifdef ENABLE_NLS
281 setlocale(LC_MESSAGES, "");
282 setlocale(LC_CTYPE, "");
283 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
284 textdomain(NLS_CAT_NAME);
285 #endif
286 if (argc && *argv)
287 program_name = *argv;
288 i = 1;
289 while (i < argc && !end_arg) {
290 /* '--' arg should end option processing */
291 if (strcmp(argv[i], "--") == 0) {
292 i++;
293 end_arg = 1;
294 } else if (decode_arg (&i, argc, argv) == EOF)
295 end_arg = 1;
296 else
297 i++;
298 }
299 if (i >= argc)
300 usage ();
301 if (set && (add || rem)) {
302 fputs(_("= is incompatible with - and +\n"), stderr);
303 exit (1);
304 }
305 if ((rf & af) != 0) {
306 fputs("Can't both set and unset same flag.\n", stderr);
307 exit (1);
308 }
309 if (!(add || rem || set || set_version)) {
310 fputs(_("Must use '-v', =, - or +\n"), stderr);
311 exit (1);
312 }
313 if (verbose)
314 fprintf (stderr, "chattr %s (%s)\n",
315 E2FSPROGS_VERSION, E2FSPROGS_DATE);
316 for (j = i; j < argc; j++) {
317 err = change_attributes (argv[j]);
318 if (err)
319 retval = 1;
320 }
321 exit(retval);
322 }