2 * chattr.c - Change file attributes on an ext2 file system
4 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
5 * Laboratoire MASI, Institut Blaise Pascal
6 * Universite Pierre et Marie Curie (Paris VI)
8 * This file can be redistributed under the terms of the GNU General
15 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
16 * 94/02/27 - Integrated in Ted's distribution
17 * 98/12/29 - Ignore symlinks when working recursively (G M Sipe)
18 * 98/12/29 - Display version info only when -V specified (G M Sipe)
21 #include <sys/types.h>
31 #include <sys/param.h>
33 #include <linux/ext2_fs.h>
35 #include "et/com_err.h"
38 #include "../version.h"
40 const char * program_name = "chattr";
47 unsigned long version;
56 static void fatal_error(const char * fmt_string, int errcode)
58 fprintf (stderr, fmt_string, program_name);
62 #define usage() fatal_error("usage: %s [-RV] [-+=AacdisSu] [-v version] files...\n", \
65 static int decode_arg (int * i, int argc, char ** argv)
73 for (p = &argv[*i][1]; *p; p++)
92 #ifdef EXT2_NOATIME_FL
94 rf |= EXT2_NOATIME_FL;
102 #ifdef EXT2_NODUMP_FL
104 rf |= EXT2_NODUMP_FL;
108 #ifdef EXT2_IMMUTABLE_FL
110 rf |= EXT2_IMMUTABLE_FL;
126 version = strtol (argv[*i], &tmp, 0);
129 com_err (program_name, 0,
130 "bad version - %s\n", argv[*i]);
136 fprintf (stderr, "%s: Unrecognized argument: %c\n",
143 for (p = &argv[*i][1]; *p; p++)
149 #ifdef EXT2_APPEND_FL
151 af |= EXT2_APPEND_FL;
154 #ifdef EXT2_NOATIME_FL
156 af |= EXT2_NOATIME_FL;
162 #ifdef EXT2_NODUMP_FL
164 af |= EXT2_NODUMP_FL;
167 #ifdef EXT2_IMMUTABLE_FL
169 af |= EXT2_IMMUTABLE_FL;
184 for (p = &argv[*i][1]; *p; p++)
190 #ifdef EXT2_APPEND_FL
192 sf |= EXT2_APPEND_FL;
195 #ifdef EXT2_NOATIME_FL
197 sf |= EXT2_NOATIME_FL;
203 #ifdef EXT2_NODUMP_FL
205 sf |= EXT2_NODUMP_FL;
208 #ifdef EXT2_IMMUTABLE_FL
210 sf |= EXT2_IMMUTABLE_FL;
230 static int chattr_dir_proc (const char *, struct dirent *, void *);
232 static void change_attributes (const char * name)
237 if (lstat (name, &st) == -1)
239 com_err (program_name, errno, "while stating %s", name);
242 if (S_ISLNK(st.st_mode) && recursive)
248 printf ("Flags of %s set as ", name);
249 print_flags (stdout, sf, 0);
252 if (fsetflags (name, sf) == -1)
257 if (fgetflags (name, &flags) == -1)
258 com_err (program_name, errno,
259 "while reading flags on %s", name);
268 printf ("Flags of %s set as ", name);
269 print_flags (stdout, flags, 0);
272 if (fsetflags (name, flags) == -1)
273 com_err (program_name, errno,
274 "while setting flags on %s", name);
280 printf ("Version of %s set as %lu\n", name, version);
281 if (fsetversion (name, version) == -1)
282 com_err (program_name, errno,
283 "while setting version on %s", name);
285 if (S_ISDIR(st.st_mode) && recursive)
286 iterate_on_dir (name, chattr_dir_proc, (void *) NULL);
289 static int chattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
291 if (strcmp (de->d_name, ".") && strcmp (de->d_name, ".."))
295 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
297 fatal_error("Couldn't allocate path variable "
298 "in chattr_dir_proc", 1);
299 sprintf (path, "%s/%s", dir_name, de->d_name);
300 change_attributes (path);
306 int main (int argc, char ** argv)
312 program_name = *argv;
314 while (i < argc && !end_arg)
316 if (decode_arg (&i, argc, argv) == EOF)
323 if (set && (add || rem))
325 fprintf (stderr, "= is incompatible with - and +\n");
328 if (!(add || rem || set || set_version))
330 fprintf (stderr, "Must use '-v', =, - or +\n");
334 fprintf (stderr, "chattr %s, %s for EXT2 FS %s, %s\n",
335 E2FSPROGS_VERSION, E2FSPROGS_DATE,
336 EXT2FS_VERSION, EXT2FS_DATE);
337 for (j = i; j < argc; j++)
338 change_attributes (argv[j]);