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