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)
31 unsigned long version;
38 static unsigned long get_flag(char c)
40 const char *fp = strchr(e2attr_flags_sname_chattr, c);
42 return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
46 static int decode_arg(const char *arg, struct globals *gp)
55 } else if (opt == '+') {
57 } else if (opt == '=') {
63 *fl |= get_flag(*arg++);
68 static void change_attributes(const char *name, struct globals *gp);
70 static int chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
72 char *path = concat_subpath_file(dir_name, de->d_name);
73 /* path is NULL if de->d_name is "." or "..", else... */
75 change_attributes(path, gp);
81 static void change_attributes(const char *name, struct globals *gp)
83 unsigned long fsflags;
86 if (lstat(name, &st) != 0) {
87 bb_perror_msg("stat %s", name);
90 if (S_ISLNK(st.st_mode) && gp->recursive)
93 /* Don't try to open device files, fifos etc. We probably
94 * ought to display an error if the file was explicitly given
95 * on the command line (whether or not recursive was
97 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
100 if (gp->flags & OPT_SET_VER)
101 if (fsetversion(name, gp->version) != 0)
102 bb_perror_msg("setting version on %s", name);
104 if (gp->flags & OPT_SET) {
107 if (fgetflags(name, &fsflags) != 0) {
108 bb_perror_msg("reading flags on %s", name);
111 /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
113 /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
115 /* What is this? And why it's not done for SET case? */
116 if (!S_ISDIR(st.st_mode))
117 fsflags &= ~EXT2_DIRSYNC_FL;
119 if (fsetflags(name, fsflags) != 0)
120 bb_perror_msg("setting flags on %s", name);
123 if (gp->recursive && S_ISDIR(st.st_mode))
124 iterate_on_dir(name, chattr_dir_proc, gp);
127 int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
128 int chattr_main(int argc UNUSED_PARAM, char **argv)
133 memset(&g, 0, sizeof(g));
136 while ((arg = *++argv)) {
137 /* take care of -R and -v <version> */
139 && (arg[1] == 'R' || arg[1] == 'v')
149 g.version = xatoul(*argv);
150 g.flags |= OPT_SET_VER;
154 if (!decode_arg(arg, &g))
158 /* run sanity checks on all the arguments given us */
161 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
162 bb_error_msg_and_die("= is incompatible with - and +");
164 bb_error_msg_and_die("can't set and unset a flag");
166 bb_error_msg_and_die("must use '-v', =, - or +");
168 /* now run chattr on all the files passed to us */
169 do change_attributes(*argv, &g); while (*++argv);