bbify to shrink size
[oweals/busybox.git] / e2fsprogs / 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 #include <sys/types.h>
22 #include <dirent.h>
23 #include <fcntl.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <ext2fs/ext2_fs.h>
32
33 #ifdef __GNUC__
34 #define EXT2FS_ATTR(x) __attribute__(x)
35 #else
36 #define EXT2FS_ATTR(x)
37 #endif
38
39 #include "e2fsbb.h"
40 #include "e2p/e2p.h"
41
42 #define OPT_ADD 1
43 #define OPT_REM 2
44 #define OPT_SET 4
45 #define OPT_SET_VER 8
46 static int flags;
47 static int recursive;
48
49 static unsigned long version;
50
51 static unsigned long af;
52 static unsigned long rf;
53 static unsigned long sf;
54
55 #ifdef CONFIG_LFS
56 # define LSTAT lstat64
57 # define STRUCT_STAT struct stat64
58 #else
59 # define LSTAT lstat
60 # define STRUCT_STAT struct stat
61 #endif
62
63 struct flags_char {
64         unsigned long flag;
65         char optchar;
66 };
67
68 static const struct flags_char flags_array[] = {
69         { EXT2_NOATIME_FL,      'A' },
70         { EXT2_SYNC_FL,         'S' },
71         { EXT2_DIRSYNC_FL,      'D' },
72         { EXT2_APPEND_FL,       'a' },
73         { EXT2_COMPR_FL,        'c' },
74         { EXT2_NODUMP_FL,       'd' },
75         { EXT2_IMMUTABLE_FL,    'i' },
76         { EXT3_JOURNAL_DATA_FL, 'j' },
77         { EXT2_SECRM_FL,        's' },
78         { EXT2_UNRM_FL,         'u' },
79         { EXT2_NOTAIL_FL,       't' },
80         { EXT2_TOPDIR_FL,       'T' },
81         { 0, 0 }
82 };
83
84 static unsigned long get_flag(char c)
85 {
86         const struct flags_char *fp;
87         for (fp = flags_array; fp->flag; fp++)
88                 if (fp->optchar == c)
89                         return fp->flag;
90         bb_show_usage();
91         return 0;
92 }
93
94 static int decode_arg(char *arg)
95 {
96         unsigned long *fl;
97         char opt = *arg++;
98
99         if (opt == '-') {
100                 flags |= OPT_REM;
101                 fl = &rf;
102         } else if (opt == '+') {
103                 flags |= OPT_ADD;
104                 fl = &af;
105         } else if (opt == '=') {
106                 flags |= OPT_SET;
107                 fl = &sf;
108         } else
109                 return EOF;
110
111         for (; *arg ; ++arg)
112                 (*fl) |= get_flag(*arg);
113
114         return 1;
115 }
116
117 static int chattr_dir_proc(const char *, struct dirent *, void *);
118
119 static void change_attributes(const char * name)
120 {
121         unsigned long fsflags;
122         STRUCT_STAT     st;
123
124         if (LSTAT(name, &st) == -1) {
125                 bb_error_msg("while trying to stat %s", name);
126                 return;
127         }
128         if (S_ISLNK(st.st_mode) && recursive)
129                 return;
130
131         /* Don't try to open device files, fifos etc.  We probably
132          * ought to display an error if the file was explicitly given
133          * on the command line (whether or not recursive was
134          * requested).  */
135         if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
136                 return;
137
138         if (flags & OPT_SET_VER)
139                 if (fsetversion(name, version) == -1)
140                         bb_error_msg("while setting version on %s", name);
141
142         if (flags & OPT_SET) {
143                 fsflags = sf;
144         } else {
145                 if (fgetflags(name, &fsflags) == -1) {
146                         bb_error_msg("while reading flags on %s", name);
147                         goto skip_setflags;
148                 }
149                 if (flags & OPT_REM)
150                         fsflags &= ~rf;
151                 if (flags & OPT_ADD)
152                         fsflags |= af;
153                 if (!S_ISDIR(st.st_mode))
154                         fsflags &= ~EXT2_DIRSYNC_FL;
155         }
156         if (fsetflags(name, fsflags) == -1)
157                 bb_error_msg("while setting flags on %s", name);
158
159 skip_setflags:
160         if (S_ISDIR(st.st_mode) && recursive)
161                 iterate_on_dir(name, chattr_dir_proc, NULL);
162 }
163
164 static int chattr_dir_proc(const char *dir_name, struct dirent *de, 
165                            void *private EXT2FS_ATTR((unused)))
166 {
167         /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
168         if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
169            (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
170
171                 char *path = malloc(strlen(dir_name) + 1 + strlen(de->d_name) + 1);
172                 if (!path)
173                         bb_error_msg_and_die("Couldn't allocate path variable in chattr_dir_proc");
174                 sprintf(path, "%s/%s", dir_name, de->d_name);
175                 change_attributes(path);
176                 free(path);
177         }
178         return 0;
179 }
180
181 int chattr_main(int argc, char **argv)
182 {
183         int i;
184         char *arg;
185
186         /* parse the args */
187         for (i = 1; i < argc; ++i) {
188                 arg = argv[i];
189
190                 /* take care of -R and -v <version> */
191                 if (arg[0] == '-') {
192                         if (arg[1] == 'R' && arg[2] == '\0') {
193                                 recursive = 1;
194                                 continue;
195                         } else if (arg[1] == 'v' && arg[2] == '\0') {
196                                 char *tmp;
197                                 ++i;
198                                 if (i >= argc)
199                                         bb_show_usage();
200                                 version = strtol(argv[i], &tmp, 0);
201                                 if (*tmp) {
202                                         bb_error_msg("bad version - %s", arg);
203                                         bb_show_usage();
204                                 }
205                                 flags |= OPT_SET_VER;
206                                 continue;
207                         }
208                 }
209
210                 if (decode_arg(arg) == EOF)
211                         break;
212         }
213
214         /* run sanity checks on all the arguments given us */
215         if (i >= argc)
216                 bb_show_usage();
217         if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM))) {
218                 bb_error_msg("= is incompatible with - and +");
219                 return EXIT_FAILURE;
220         }
221         if ((rf & af) != 0) {
222                 bb_error_msg("Can't both set and unset same flag");
223                 return EXIT_FAILURE;
224         }
225         if (!flags) {
226                 bb_error_msg("Must use '-v', =, - or +");
227                 return EXIT_FAILURE;
228         }
229
230         /* now run chattr on all the files passed to us */
231         while (i < argc)
232                 change_attributes(argv[i++]);
233
234         return EXIT_SUCCESS;
235 }