1 /* vi: set sw=4 ts=4: */
3 * chattr.c - Change file attributes on an ext2 file system
5 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
9 * This file can be redistributed under the terms of the GNU General
16 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
17 * 94/02/27 - Integrated in Ted's distribution
18 * 98/12/29 - Ignore symlinks when working recursively (G M Sipe)
19 * 98/12/29 - Display version info only when -V specified (G M Sipe)
32 static unsigned long version;
34 static unsigned long af;
35 static unsigned long rf;
36 static unsigned long sf;
43 static const struct flags_char flags_array[] = {
44 { EXT2_NOATIME_FL, 'A' },
45 { EXT2_SYNC_FL, 'S' },
46 { EXT2_DIRSYNC_FL, 'D' },
47 { EXT2_APPEND_FL, 'a' },
48 { EXT2_COMPR_FL, 'c' },
49 { EXT2_NODUMP_FL, 'd' },
50 { EXT2_IMMUTABLE_FL, 'i' },
51 { EXT3_JOURNAL_DATA_FL, 'j' },
52 { EXT2_SECRM_FL, 's' },
53 { EXT2_UNRM_FL, 'u' },
54 { EXT2_NOTAIL_FL, 't' },
55 { EXT2_TOPDIR_FL, 'T' },
59 static unsigned long get_flag(char c)
61 const struct flags_char *fp;
62 for (fp = flags_array; fp->optchar; fp++)
68 static int decode_arg(const char *arg)
76 } else if (opt == '+') {
79 } else if (opt == '=') {
86 *fl |= get_flag(*arg++);
91 static void change_attributes(const char *name);
93 static int chattr_dir_proc(const char *dir_name, struct dirent *de,
94 void *private ATTRIBUTE_UNUSED)
96 char *path = concat_subpath_file(dir_name, de->d_name);
97 /* path is NULL if de->d_name is "." or "..", else... */
99 change_attributes(path);
105 static void change_attributes(const char *name)
107 unsigned long fsflags;
110 if (lstat(name, &st) == -1) {
111 bb_perror_msg("stat %s", name);
114 if (S_ISLNK(st.st_mode) && recursive)
117 /* Don't try to open device files, fifos etc. We probably
118 * ought to display an error if the file was explicitly given
119 * on the command line (whether or not recursive was
121 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
124 if (flags & OPT_SET_VER)
125 if (fsetversion(name, version) == -1)
126 bb_perror_msg("setting version on %s", name);
128 if (flags & OPT_SET) {
131 if (fgetflags(name, &fsflags) == -1) {
132 bb_perror_msg("reading flags on %s", name);
139 if (!S_ISDIR(st.st_mode))
140 fsflags &= ~EXT2_DIRSYNC_FL;
142 if (fsetflags(name, fsflags) == -1)
143 bb_perror_msg("setting flags on %s", name);
146 if (recursive && S_ISDIR(st.st_mode))
147 iterate_on_dir(name, chattr_dir_proc, NULL);
150 int chattr_main(int argc, char **argv)
155 while ((arg = *++argv)) {
156 /* take care of -R and -v <version> */
158 if (arg[1] == 'R' && arg[2] == '\0') {
162 if (arg[1] == 'v' && arg[2] == '\0') {
165 version = xatoul(*argv);
166 flags |= OPT_SET_VER;
171 if (!decode_arg(arg))
175 /* run sanity checks on all the arguments given us */
178 if ((flags & OPT_SET) && (flags & (OPT_ADD|OPT_REM)))
179 bb_error_msg_and_die("= is incompatible with - and +");
181 bb_error_msg_and_die("can't set and unset a flag");
183 bb_error_msg_and_die("must use '-v', =, - or +");
185 /* now run chattr on all the files passed to us */
186 do change_attributes(*argv); while (*++argv);