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