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 #define _LARGEFILE64_SOURCE
23 #include <sys/types.h>
33 #include <sys/param.h>
35 #include "ext2fs/ext2_fs.h"
38 #define EXT2FS_ATTR(x) __attribute__(x)
40 #define EXT2FS_ATTR(x)
43 #ifndef S_ISLNK /* So we can compile even with gcc-warn */
45 # define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
47 # define S_ISLNK(mode) 0
51 #include "et/com_err.h"
54 #include "../version.h"
55 #include "nls-enable.h"
57 static const char * program_name = "chattr";
62 static int set_version;
64 static unsigned long version;
70 static unsigned long af;
71 static unsigned long rf;
72 static unsigned long sf;
74 #ifdef _LFS64_LARGEFILE
76 #define STRUCT_STAT struct stat64
79 #define STRUCT_STAT struct stat
82 static void usage(void)
85 _("Usage: %s [-RVf] [-+=AacDdijsSu] [-v version] files...\n"),
95 static const struct flags_char flags_array[] = {
96 { EXT2_NOATIME_FL, 'A' },
97 { EXT2_SYNC_FL, 'S' },
98 { EXT2_DIRSYNC_FL, 'D' },
99 { EXT2_APPEND_FL, 'a' },
100 { EXT2_COMPR_FL, 'c' },
101 { EXT2_NODUMP_FL, 'd' },
102 { EXT2_IMMUTABLE_FL, 'i' },
103 { EXT3_JOURNAL_DATA_FL, 'j' },
104 { EXT2_SECRM_FL, 's' },
105 { EXT2_UNRM_FL, 'u' },
106 { EXT2_NOTAIL_FL, 't' },
107 { EXT2_TOPDIR_FL, 'T' },
111 static unsigned long get_flag(char c)
113 const struct flags_char *fp;
115 for (fp = flags_array; fp->flag != 0; fp++) {
116 if (fp->optchar == c)
123 static int decode_arg (int * i, int argc, char ** argv)
132 for (p = &argv[*i][1]; *p; p++) {
149 version = strtol (argv[*i], &tmp, 0);
151 com_err (program_name, 0,
152 _("bad version - %s\n"),
159 if ((fl = get_flag(*p)) == 0)
167 for (p = &argv[*i][1]; *p; p++) {
168 if ((fl = get_flag(*p)) == 0)
175 for (p = &argv[*i][1]; *p; p++) {
176 if ((fl = get_flag(*p)) == 0)
188 static int chattr_dir_proc (const char *, struct dirent *, void *);
190 static int change_attributes (const char * name, int cmdline)
195 if (LSTAT (name, &st) == -1) {
197 com_err (program_name, errno,
198 _("while trying to stat %s"), name);
204 printf (_("Flags of %s set as "), name);
205 print_flags (stdout, sf, 0);
208 if (fsetflags (name, sf) == -1)
211 if (fgetflags (name, &flags) == -1) {
213 com_err (program_name, errno,
214 _("while reading flags on %s"), name);
222 printf (_("Flags of %s set as "), name);
223 print_flags (stdout, flags, 0);
226 if (!S_ISDIR(st.st_mode))
227 flags &= ~EXT2_DIRSYNC_FL;
228 if (fsetflags (name, flags) == -1) {
230 com_err(program_name, errno,
231 _("while setting flags on %s"),
239 printf (_("Version of %s set as %lu\n"), name, version);
240 if (fsetversion (name, version) == -1) {
242 com_err (program_name, errno,
243 _("while setting version on %s"),
248 if (S_ISDIR(st.st_mode) && recursive)
249 return iterate_on_dir (name, chattr_dir_proc, NULL);
253 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
254 void * private EXT2FS_ATTR((unused)))
258 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
261 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
263 fprintf(stderr, _("Couldn't allocate path variable "
264 "in chattr_dir_proc"));
267 sprintf(path, "%s/%s", dir_name, de->d_name);
268 ret = change_attributes(path, 0);
274 int main (int argc, char ** argv)
281 setlocale(LC_MESSAGES, "");
282 setlocale(LC_CTYPE, "");
283 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
284 textdomain(NLS_CAT_NAME);
287 program_name = *argv;
289 while (i < argc && !end_arg) {
290 /* '--' arg should end option processing */
291 if (strcmp(argv[i], "--") == 0) {
294 } else if (decode_arg (&i, argc, argv) == EOF)
301 if (set && (add || rem)) {
302 fputs(_("= is incompatible with - and +\n"), stderr);
305 if ((rf & af) != 0) {
306 fputs("Can't both set and unset same flag.\n", stderr);
309 if (!(add || rem || set || set_version)) {
310 fputs(_("Must use '-v', =, - or +\n"), stderr);
314 fprintf (stderr, "chattr %s (%s)\n",
315 E2FSPROGS_VERSION, E2FSPROGS_DATE);
316 for (j = i; j < argc; j++) {
317 err = change_attributes (argv[j], 1);