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)
22 #include <sys/types.h>
30 #include <sys/param.h>
32 #include "ext2fs/ext2_fs.h"
35 # define EXT2FS_ATTR(x) __attribute__(x)
37 # define EXT2FS_ATTR(x)
50 static unsigned long version;
52 static unsigned long af;
53 static unsigned long rf;
54 static unsigned long sf;
57 # define LSTAT lstat64
58 # define STRUCT_STAT struct stat64
61 # define STRUCT_STAT struct stat
69 static const struct flags_char flags_array[] = {
70 { EXT2_NOATIME_FL, 'A' },
71 { EXT2_SYNC_FL, 'S' },
72 { EXT2_DIRSYNC_FL, 'D' },
73 { EXT2_APPEND_FL, 'a' },
74 { EXT2_COMPR_FL, 'c' },
75 { EXT2_NODUMP_FL, 'd' },
76 { EXT2_IMMUTABLE_FL, 'i' },
77 { EXT3_JOURNAL_DATA_FL, 'j' },
78 { EXT2_SECRM_FL, 's' },
79 { EXT2_UNRM_FL, 'u' },
80 { EXT2_NOTAIL_FL, 't' },
81 { EXT2_TOPDIR_FL, 'T' },
85 static unsigned long get_flag(char c)
87 const struct flags_char *fp;
88 for (fp = flags_array; fp->flag; fp++)
95 static int decode_arg(char *arg)
103 } else if (opt == '+') {
106 } else if (opt == '=') {
113 (*fl) |= get_flag(*arg);
118 static int chattr_dir_proc(const char *, struct dirent *, void *);
120 static void change_attributes(const char * name)
122 unsigned long fsflags;
125 if (LSTAT(name, &st) == -1) {
126 bb_error_msg("stat %s failed", name);
129 if (S_ISLNK(st.st_mode) && recursive)
132 /* Don't try to open device files, fifos etc. We probably
133 * ought to display an error if the file was explicitly given
134 * on the command line (whether or not recursive was
136 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
139 if (flags & OPT_SET_VER)
140 if (fsetversion(name, version) == -1)
141 bb_error_msg("setting version on %s", name);
143 if (flags & OPT_SET) {
146 if (fgetflags(name, &fsflags) == -1) {
147 bb_error_msg("reading flags on %s", name);
154 if (!S_ISDIR(st.st_mode))
155 fsflags &= ~EXT2_DIRSYNC_FL;
157 if (fsetflags(name, fsflags) == -1)
158 bb_error_msg("setting flags on %s", name);
161 if (S_ISDIR(st.st_mode) && recursive)
162 iterate_on_dir(name, chattr_dir_proc, NULL);
165 static int chattr_dir_proc(const char *dir_name, struct dirent *de,
166 void *private EXT2FS_ATTR((unused)))
168 /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
169 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
170 (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
171 char *path = concat_subpath_file(dir_name, de->d_name);
173 change_attributes(path);
180 int chattr_main(int argc, char **argv)
186 for (i = 1; i < argc; ++i) {
189 /* take care of -R and -v <version> */
191 if (arg[1] == 'R' && arg[2] == '\0') {
194 } else if (arg[1] == 'v' && arg[2] == '\0') {
199 version = strtol(argv[i], &tmp, 0);
201 bb_error_msg_and_die("bad version '%s'", arg);
202 flags |= OPT_SET_VER;
207 if (decode_arg(arg) == EOF)
211 /* run sanity checks on all the arguments given us */
214 if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM)))
215 bb_error_msg_and_die("= is incompatible with - and +");
217 bb_error_msg_and_die("Can't set and unset a flag");
219 bb_error_msg_and_die("Must use '-v', =, - or +");
221 /* now run chattr on all the files passed to us */
223 change_attributes(argv[i++]);