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 /* Two separate vectors take less space than vector of structs */
41 static const char flags_letter[] = "ASDacdijsutT";
42 static const unsigned long flags_val[] = {
43 /* A */ EXT2_NOATIME_FL,
45 /* D */ EXT2_DIRSYNC_FL,
46 /* a */ EXT2_APPEND_FL,
47 /* c */ EXT2_COMPR_FL,
48 /* d */ EXT2_NODUMP_FL,
49 /* i */ EXT2_IMMUTABLE_FL,
50 /* j */ EXT3_JOURNAL_DATA_FL,
51 /* s */ EXT2_SECRM_FL,
53 /* t */ EXT2_NOTAIL_FL,
54 /* T */ EXT2_TOPDIR_FL,
58 for (fp = flags_letter; *fp; fp++)
60 return flags_val[fp - flags_letter];
64 static int decode_arg(const char *arg, struct globals *gp)
73 } else if (opt == '+') {
75 } else if (opt == '=') {
81 *fl |= get_flag(*arg++);
86 static void change_attributes(const char *name, struct globals *gp);
88 static int chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
90 char *path = concat_subpath_file(dir_name, de->d_name);
91 /* path is NULL if de->d_name is "." or "..", else... */
93 change_attributes(path, gp);
99 static void change_attributes(const char *name, struct globals *gp)
101 unsigned long fsflags;
104 if (lstat(name, &st) != 0) {
105 bb_perror_msg("stat %s", name);
108 if (S_ISLNK(st.st_mode) && gp->recursive)
111 /* Don't try to open device files, fifos etc. We probably
112 * ought to display an error if the file was explicitly given
113 * on the command line (whether or not recursive was
115 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
118 if (gp->flags & OPT_SET_VER)
119 if (fsetversion(name, gp->version) != 0)
120 bb_perror_msg("setting version on %s", name);
122 if (gp->flags & OPT_SET) {
125 if (fgetflags(name, &fsflags) != 0) {
126 bb_perror_msg("reading flags on %s", name);
129 /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
131 /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
133 /* What is this? And why it's not done for SET case? */
134 if (!S_ISDIR(st.st_mode))
135 fsflags &= ~EXT2_DIRSYNC_FL;
137 if (fsetflags(name, fsflags) != 0)
138 bb_perror_msg("setting flags on %s", name);
141 if (gp->recursive && S_ISDIR(st.st_mode))
142 iterate_on_dir(name, chattr_dir_proc, gp);
145 int chattr_main(int argc, char **argv);
146 int chattr_main(int argc, char **argv)
151 memset(&g, 0, sizeof(g));
154 while ((arg = *++argv)) {
155 /* take care of -R and -v <version> */
157 && (arg[1] == 'R' || arg[1] == 'v')
167 g.version = xatoul(*argv);
168 g.flags |= OPT_SET_VER;
172 if (!decode_arg(arg, &g))
176 /* run sanity checks on all the arguments given us */
179 if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
180 bb_error_msg_and_die("= is incompatible with - and +");
182 bb_error_msg_and_die("can't set and unset a flag");
184 bb_error_msg_and_die("must use '-v', =, - or +");
186 /* now run chattr on all the files passed to us */
187 do change_attributes(*argv, &g); while (*++argv);