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
12 //config:config CHATTR
13 //config: bool "chattr (3.2 kb)"
16 //config: chattr changes the file attributes on a second extended file system.
18 //applet:IF_CHATTR(APPLET_NOEXEC(chattr, chattr, BB_DIR_BIN, BB_SUID_DROP, chattr))
20 //kbuild:lib-$(CONFIG_CHATTR) += chattr.o e2fs_lib.o
22 //usage:#define chattr_trivial_usage
23 //usage: "[-R] [-v VERSION] [-+=AacDdijsStTu] FILE..."
24 //usage:#define chattr_full_usage "\n\n"
25 //usage: "Change ext2 file attributes\n"
26 //usage: "\n -R Recurse"
27 //usage: "\n -v VER Set version/generation number"
28 //-V, -f accepted but ignored
29 //usage: "\nModifiers:"
30 //usage: "\n -,+,= Remove/add/set attributes"
31 //usage: "\nAttributes:"
32 //usage: "\n A Don't track atime"
33 //usage: "\n a Append mode only"
34 //usage: "\n c Enable compress"
35 //usage: "\n D Write dir contents synchronously"
36 //usage: "\n d Don't backup with dump"
37 //usage: "\n i Cannot be modified (immutable)"
38 //usage: "\n j Write all data to journal first"
39 //usage: "\n s Zero disk storage when deleted"
40 //usage: "\n S Write synchronously"
41 //usage: "\n t Disable tail-merging of partial blocks with other files"
42 //usage: "\n u Allow file to be undeleted"
53 unsigned long version;
60 static unsigned long get_flag(char c)
62 const char *fp = strchr(e2attr_flags_sname_chattr, c);
64 return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
68 static char** decode_arg(char **argv, struct globals *gp)
71 const char *arg = *argv;
78 } else if (opt == '+') {
80 } else { /* if (opt == '=') */
86 //e2fsprogs-1.43.1 accepts:
87 // "-RRR", "-RRRv VER" and even "-ARRRva VER" and "-vvv V1 V2 V3"
89 // IOW: options are parsed as part of "remove attrs" strings,
90 // if "v" is seen, next argv[] is VER, even if more opts/attrs follow in this argv[]!
96 /*"verbose and print program version" (nop for now) */;
100 /*"suppress most error messages" (nop) */;
106 gp->version = xatoul(*argv);
107 gp->flags |= OPT_SET_VER;
110 //TODO: "-p PROJECT_NUM" ?
111 /* not a known option, try as an attribute */
113 *fl |= get_flag(*arg);
119 static void change_attributes(const char *name, struct globals *gp);
121 static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
123 char *path = concat_subpath_file(dir_name, de->d_name);
124 /* path is NULL if de->d_name is "." or "..", else... */
126 change_attributes(path, gp);
132 static void change_attributes(const char *name, struct globals *gp)
134 unsigned long fsflags;
137 if (lstat(name, &st) != 0) {
138 bb_perror_msg("stat %s", name);
141 if (S_ISLNK(st.st_mode) && gp->recursive)
144 /* Don't try to open device files, fifos etc. We probably
145 * ought to display an error if the file was explicitly given
146 * on the command line (whether or not recursive was
148 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
151 if (gp->flags & OPT_SET_VER)
152 if (fsetversion(name, gp->version) != 0)
153 bb_perror_msg("setting version on %s", name);
155 if (gp->flags & OPT_SET) {
158 if (fgetflags(name, &fsflags) != 0) {
159 bb_perror_msg("reading flags on %s", name);
162 /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
164 /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
166 // What is this? And why it's not done for SET case?
167 if (!S_ISDIR(st.st_mode))
168 fsflags &= ~EXT2_DIRSYNC_FL;
170 if (fsetflags(name, fsflags) != 0)
171 bb_perror_msg("setting flags on %s", name);
174 if (gp->recursive && S_ISDIR(st.st_mode))
175 iterate_on_dir(name, chattr_dir_proc, gp);
178 int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
179 int chattr_main(int argc UNUSED_PARAM, char **argv)
183 memset(&g, 0, sizeof(g));
190 if (arg[0] != '-' && arg[0] != '+' && arg[0] != '=')
193 argv = decode_arg(argv, &g);
195 /* note: on loop exit, remaining argv[] is never empty */
197 /* run sanity checks on all the arguments given us */
198 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
199 bb_error_msg_and_die("= is incompatible with - and +");
201 bb_error_msg_and_die("can't set and unset a flag");
203 bb_error_msg_and_die("must use '-v', =, - or +");
205 /* now run chattr on all the files passed to us */
206 do change_attributes(*argv, &g); while (*++argv);