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