OSDN Git Service

Use rbtree bitmaps for dumpe2fs, debugfs, and tune2fs
[android-x86/external-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
55 #include "../version.h"
56 #include "nls-enable.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 recursive;
68 static int verbose;
69 static int silent;
70
71 static unsigned long af;
72 static unsigned long rf;
73 static unsigned long sf;
74
75 #ifdef _LFS64_LARGEFILE
76 #define LSTAT           lstat64
77 #define STRUCT_STAT     struct stat64
78 #else
79 #define LSTAT           lstat
80 #define STRUCT_STAT     struct stat
81 #endif
82
83 static void usage(void)
84 {
85         fprintf(stderr,
86                 _("Usage: %s [-RVf] [-+=AacDdeijsSu] [-v version] files...\n"),
87                 program_name);
88         exit(1);
89 }
90
91 struct flags_char {
92         unsigned long   flag;
93         char            optchar;
94 };
95
96 static const struct flags_char flags_array[] = {
97         { EXT2_NOATIME_FL, 'A' },
98         { EXT2_SYNC_FL, 'S' },
99         { EXT2_DIRSYNC_FL, 'D' },
100         { EXT2_APPEND_FL, 'a' },
101         { EXT2_COMPR_FL, 'c' },
102         { EXT2_NODUMP_FL, 'd' },
103         { EXT4_EXTENTS_FL, 'e'},
104         { EXT2_IMMUTABLE_FL, 'i' },
105         { EXT3_JOURNAL_DATA_FL, 'j' },
106         { EXT2_SECRM_FL, 's' },
107         { EXT2_UNRM_FL, 'u' },
108         { EXT2_NOTAIL_FL, 't' },
109         { EXT2_TOPDIR_FL, 'T' },
110         { 0, 0 }
111 };
112
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
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
192 static int change_attributes(const char * name)
193 {
194         unsigned long flags;
195         STRUCT_STAT     st;
196         int extent_file = 0;
197
198         if (LSTAT (name, &st) == -1) {
199                 if (!silent)
200                         com_err (program_name, errno,
201                                  _("while trying to stat %s"), name);
202                 return -1;
203         }
204
205         if (fgetflags(name, &flags) == -1) {
206                 if (!silent)
207                         com_err(program_name, errno,
208                                         _("while reading flags on %s"), name);
209                 return -1;
210         }
211         if (flags & EXT4_EXTENTS_FL)
212                 extent_file = 1;
213         if (set) {
214                 if (extent_file && !(sf & EXT4_EXTENTS_FL)) {
215                         if (!silent)
216                                 com_err(program_name, 0,
217                                 _("Clearing extent flag not supported on %s"),
218                                         name);
219                         return -1;
220                 }
221                 if (verbose) {
222                         printf (_("Flags of %s set as "), name);
223                         print_flags (stdout, sf, 0);
224                         printf ("\n");
225                 }
226                 if (fsetflags (name, sf) == -1)
227                         perror (name);
228         } else {
229                 if (rem)
230                         flags &= ~rf;
231                 if (add)
232                         flags |= af;
233                 if (extent_file && !(flags & EXT4_EXTENTS_FL)) {
234                         if (!silent)
235                                 com_err(program_name, 0,
236                                 _("Clearing extent flag not supported on %s"),
237                                         name);
238                         return -1;
239                 }
240                 if (verbose) {
241                         printf(_("Flags of %s set as "), name);
242                         print_flags(stdout, flags, 0);
243                         printf("\n");
244                 }
245                 if (!S_ISDIR(st.st_mode))
246                         flags &= ~EXT2_DIRSYNC_FL;
247                 if (fsetflags(name, flags) == -1) {
248                         if (!silent) {
249                                 com_err(program_name, errno,
250                                                 _("while setting flags on %s"),
251                                                 name);
252                         }
253                         return -1;
254                 }
255         }
256         if (set_version) {
257                 if (verbose)
258                         printf (_("Version of %s set as %lu\n"), name, version);
259                 if (fsetversion (name, version) == -1) {
260                         if (!silent)
261                                 com_err (program_name, errno,
262                                          _("while setting version on %s"),
263                                          name);
264                         return -1;
265                 }
266         }
267         if (S_ISDIR(st.st_mode) && recursive)
268                 return iterate_on_dir (name, chattr_dir_proc, NULL);
269         return 0;
270 }
271
272 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
273                             void * private EXT2FS_ATTR((unused)))
274 {
275         int ret = 0;
276
277         if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
278                 char *path;
279
280                 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
281                 if (!path) {
282                         fprintf(stderr, _("Couldn't allocate path variable "
283                                           "in chattr_dir_proc"));
284                         return -1;
285                 }
286                 sprintf(path, "%s/%s", dir_name, de->d_name);
287                 ret = change_attributes(path);
288                 free(path);
289         }
290         return ret;
291 }
292
293 int main (int argc, char ** argv)
294 {
295         int i, j;
296         int end_arg = 0;
297         int err, retval = 0;
298
299 #ifdef ENABLE_NLS
300         setlocale(LC_MESSAGES, "");
301         setlocale(LC_CTYPE, "");
302         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
303         textdomain(NLS_CAT_NAME);
304         set_com_err_gettext(gettext);
305 #endif
306         if (argc && *argv)
307                 program_name = *argv;
308         i = 1;
309         while (i < argc && !end_arg) {
310                 /* '--' arg should end option processing */
311                 if (strcmp(argv[i], "--") == 0) {
312                         i++;
313                         end_arg = 1;
314                 } else if (decode_arg (&i, argc, argv) == EOF)
315                         end_arg = 1;
316                 else
317                         i++;
318         }
319         if (i >= argc)
320                 usage ();
321         if (set && (add || rem)) {
322                 fputs(_("= is incompatible with - and +\n"), stderr);
323                 exit (1);
324         }
325         if ((rf & af) != 0) {
326                 fputs("Can't both set and unset same flag.\n", stderr);
327                 exit (1);
328         }
329         if (!(add || rem || set || set_version)) {
330                 fputs(_("Must use '-v', =, - or +\n"), stderr);
331                 exit (1);
332         }
333         if (verbose)
334                 fprintf (stderr, "chattr %s (%s)\n",
335                          E2FSPROGS_VERSION, E2FSPROGS_DATE);
336         for (j = i; j < argc; j++) {
337                 err = change_attributes (argv[j]);
338                 if (err)
339                         retval = 1;
340         }
341         exit(retval);
342 }