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"
16 //config: chattr changes the file attributes on a second extended file system.
18 //applet:IF_CHATTR(APPLET(chattr, BB_DIR_BIN, BB_SUID_DROP))
20 //kbuild:lib-$(CONFIG_CHATTR) += chattr.o e2fs_lib.o
22 //usage:#define chattr_trivial_usage
23 //usage: "[-R] [-+=AacDdijsStTu] [-v VERSION] [FILE]..."
24 //usage:#define chattr_full_usage "\n\n"
25 //usage: "Change ext2 file attributes\n"
26 //usage: "\nModifiers:"
27 //usage: "\n -,+,= Remove/add/set attributes"
28 //usage: "\nAttributes:"
29 //usage: "\n A Don't track atime"
30 //usage: "\n a Append mode only"
31 //usage: "\n c Enable compress"
32 //usage: "\n D Write dir contents synchronously"
33 //usage: "\n d Don't backup with dump"
34 //usage: "\n i Cannot be modified (immutable)"
35 //usage: "\n j Write all data to journal first"
36 //usage: "\n s Zero disk storage when deleted"
37 //usage: "\n S Write synchronously"
38 //usage: "\n t Disable tail-merging of partial blocks with other files"
39 //usage: "\n u Allow file to be undeleted"
40 //usage: "\n -R Recurse"
41 //usage: "\n -v VER Set version/generation number"
52 unsigned long version;
59 static unsigned long get_flag(char c)
61 const char *fp = strchr(e2attr_flags_sname_chattr, c);
63 return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
67 static int decode_arg(const char *arg, struct globals *gp)
76 } else if (opt == '+') {
78 } else if (opt == '=') {
84 *fl |= get_flag(*arg++);
89 static void change_attributes(const char *name, struct globals *gp);
91 static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
93 char *path = concat_subpath_file(dir_name, de->d_name);
94 /* path is NULL if de->d_name is "." or "..", else... */
96 change_attributes(path, gp);
102 static void change_attributes(const char *name, struct globals *gp)
104 unsigned long fsflags;
107 if (lstat(name, &st) != 0) {
108 bb_perror_msg("stat %s", name);
111 if (S_ISLNK(st.st_mode) && gp->recursive)
114 /* Don't try to open device files, fifos etc. We probably
115 * ought to display an error if the file was explicitly given
116 * on the command line (whether or not recursive was
118 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
121 if (gp->flags & OPT_SET_VER)
122 if (fsetversion(name, gp->version) != 0)
123 bb_perror_msg("setting version on %s", name);
125 if (gp->flags & OPT_SET) {
128 if (fgetflags(name, &fsflags) != 0) {
129 bb_perror_msg("reading flags on %s", name);
132 /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
134 /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
136 /* What is this? And why it's not done for SET case? */
137 if (!S_ISDIR(st.st_mode))
138 fsflags &= ~EXT2_DIRSYNC_FL;
140 if (fsetflags(name, fsflags) != 0)
141 bb_perror_msg("setting flags on %s", name);
144 if (gp->recursive && S_ISDIR(st.st_mode))
145 iterate_on_dir(name, chattr_dir_proc, gp);
148 int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
149 int chattr_main(int argc UNUSED_PARAM, char **argv)
154 memset(&g, 0, sizeof(g));
157 while ((arg = *++argv)) {
158 /* take care of -R and -v <version> */
160 && (arg[1] == 'R' || arg[1] == 'v')
170 g.version = xatoul(*argv);
171 g.flags |= OPT_SET_VER;
175 if (!decode_arg(arg, &g))
179 /* run sanity checks on all the arguments given us */
182 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
183 bb_error_msg_and_die("= is incompatible with - and +");
185 bb_error_msg_and_die("can't set and unset a flag");
187 bb_error_msg_and_die("must use '-v', =, - or +");
189 /* now run chattr on all the files passed to us */
190 do change_attributes(*argv, &g); while (*++argv);