2 * chattr.c - Change file attributes on an ext2 file system
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)
8 * This file can be redistributed under the terms of the GNU General
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)
21 #include <sys/types.h>
29 #include <sys/param.h>
31 #include <ext2fs/ext2_fs.h>
34 #define EXT2FS_ATTR(x) __attribute__(x)
36 #define EXT2FS_ATTR(x)
49 static unsigned long version;
51 static unsigned long af;
52 static unsigned long rf;
53 static unsigned long sf;
56 # define LSTAT lstat64
57 # define STRUCT_STAT struct stat64
60 # define STRUCT_STAT struct stat
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' },
84 static unsigned long get_flag(char c)
86 const struct flags_char *fp;
87 for (fp = flags_array; fp->flag; fp++)
94 static int decode_arg(char *arg)
102 } else if (opt == '+') {
105 } else if (opt == '=') {
112 (*fl) |= get_flag(*arg);
117 static int chattr_dir_proc(const char *, struct dirent *, void *);
119 static void change_attributes(const char * name)
121 unsigned long fsflags;
124 if (LSTAT(name, &st) == -1) {
125 bb_error_msg("while trying to stat %s", name);
128 if (S_ISLNK(st.st_mode) && recursive)
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
135 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
138 if (flags & OPT_SET_VER)
139 if (fsetversion(name, version) == -1)
140 bb_error_msg("while setting version on %s", name);
142 if (flags & OPT_SET) {
145 if (fgetflags(name, &fsflags) == -1) {
146 bb_error_msg("while reading flags on %s", name);
153 if (!S_ISDIR(st.st_mode))
154 fsflags &= ~EXT2_DIRSYNC_FL;
156 if (fsetflags(name, fsflags) == -1)
157 bb_error_msg("while setting flags on %s", name);
160 if (S_ISDIR(st.st_mode) && recursive)
161 iterate_on_dir(name, chattr_dir_proc, NULL);
164 static int chattr_dir_proc(const char *dir_name, struct dirent *de,
165 void *private EXT2FS_ATTR((unused)))
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'))) {
171 char *path = malloc(strlen(dir_name) + 1 + strlen(de->d_name) + 1);
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);
181 int chattr_main(int argc, char **argv)
187 for (i = 1; i < argc; ++i) {
190 /* take care of -R and -v <version> */
192 if (arg[1] == 'R' && arg[2] == '\0') {
195 } else if (arg[1] == 'v' && arg[2] == '\0') {
200 version = strtol(argv[i], &tmp, 0);
202 bb_error_msg("bad version - %s", arg);
205 flags |= OPT_SET_VER;
210 if (decode_arg(arg) == EOF)
214 /* run sanity checks on all the arguments given us */
217 if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM))) {
218 bb_error_msg("= is incompatible with - and +");
221 if ((rf & af) != 0) {
222 bb_error_msg("Can't both set and unset same flag");
226 bb_error_msg("Must use '-v', =, - or +");
230 /* now run chattr on all the files passed to us */
232 change_attributes(argv[i++]);