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
24 #include <sys/types.h>
34 #include <sys/param.h>
36 #include "ext2fs/ext2_fs.h"
39 #define EXT2FS_ATTR(x) __attribute__(x)
41 #define EXT2FS_ATTR(x)
44 #ifndef S_ISLNK /* So we can compile even with gcc-warn */
46 # define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
48 # define S_ISLNK(mode) 0
52 #include "et/com_err.h"
55 #include "../version.h"
56 #include "nls-enable.h"
58 static const char * program_name = "chattr";
63 static int set_version;
65 static unsigned long version;
71 static unsigned long af;
72 static unsigned long rf;
73 static unsigned long sf;
75 #ifdef _LFS64_LARGEFILE
77 #define STRUCT_STAT struct stat64
80 #define STRUCT_STAT struct stat
83 static void usage(void)
86 _("Usage: %s [-RVf] [-+=aAcCdDeijsStTu] [-v version] files...\n"),
96 static const struct flags_char flags_array[] = {
97 { EXT2_NOATIME_FL, 'A' },
98 { EXT2_SYNC_FL, 'S' },
99 { EXT2_DIRSYNC_FL, 'D' },
100 { EXT2_APPEND_FL, 'a' },
101 { EXT2_COMPR_FL, 'c' },
102 { EXT2_NODUMP_FL, 'd' },
103 { EXT4_EXTENTS_FL, 'e'},
104 { EXT2_IMMUTABLE_FL, 'i' },
105 { EXT3_JOURNAL_DATA_FL, 'j' },
106 { EXT2_SECRM_FL, 's' },
107 { EXT2_UNRM_FL, 'u' },
108 { EXT2_NOTAIL_FL, 't' },
109 { EXT2_TOPDIR_FL, 'T' },
110 { FS_NOCOW_FL, 'C' },
114 static unsigned long get_flag(char c)
116 const struct flags_char *fp;
118 for (fp = flags_array; fp->flag != 0; fp++) {
119 if (fp->optchar == c)
126 static int decode_arg (int * i, int argc, char ** argv)
135 for (p = &argv[*i][1]; *p; p++) {
152 version = strtol (argv[*i], &tmp, 0);
154 com_err (program_name, 0,
155 _("bad version - %s\n"),
162 if ((fl = get_flag(*p)) == 0)
170 for (p = &argv[*i][1]; *p; p++) {
171 if ((fl = get_flag(*p)) == 0)
178 for (p = &argv[*i][1]; *p; p++) {
179 if ((fl = get_flag(*p)) == 0)
191 static int chattr_dir_proc(const char *, struct dirent *, void *);
193 static int change_attributes(const char * name)
198 if (LSTAT (name, &st) == -1) {
200 com_err (program_name, errno,
201 _("while trying to stat %s"), name);
205 if (fgetflags(name, &flags) == -1) {
207 com_err(program_name, errno,
208 _("while reading flags on %s"), name);
213 printf (_("Flags of %s set as "), name);
214 print_flags (stdout, sf, 0);
217 if (fsetflags (name, sf) == -1)
225 printf(_("Flags of %s set as "), name);
226 print_flags(stdout, flags, 0);
229 if (!S_ISDIR(st.st_mode))
230 flags &= ~EXT2_DIRSYNC_FL;
231 if (fsetflags(name, flags) == -1) {
233 com_err(program_name, errno,
234 _("while setting flags on %s"),
242 printf (_("Version of %s set as %lu\n"), name, version);
243 if (fsetversion (name, version) == -1) {
245 com_err (program_name, errno,
246 _("while setting version on %s"),
251 if (S_ISDIR(st.st_mode) && recursive)
252 return iterate_on_dir (name, chattr_dir_proc, NULL);
256 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
257 void * private EXT2FS_ATTR((unused)))
261 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
264 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
266 fprintf(stderr, "%s",
267 _("Couldn't allocate path variable "
268 "in chattr_dir_proc"));
271 sprintf(path, "%s/%s", dir_name, de->d_name);
272 ret = change_attributes(path);
278 int main (int argc, char ** argv)
285 setlocale(LC_MESSAGES, "");
286 setlocale(LC_CTYPE, "");
287 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
288 textdomain(NLS_CAT_NAME);
289 set_com_err_gettext(gettext);
292 program_name = *argv;
294 while (i < argc && !end_arg) {
295 /* '--' arg should end option processing */
296 if (strcmp(argv[i], "--") == 0) {
299 } else if (decode_arg (&i, argc, argv) == EOF)
306 if (set && (add || rem)) {
307 fputs(_("= is incompatible with - and +\n"), stderr);
310 if ((rf & af) != 0) {
311 fputs("Can't both set and unset same flag.\n", stderr);
314 if (!(add || rem || set || set_version)) {
315 fputs(_("Must use '-v', =, - or +\n"), stderr);
319 fprintf (stderr, "chattr %s (%s)\n",
320 E2FSPROGS_VERSION, E2FSPROGS_DATE);
321 for (j = i; j < argc; j++) {
322 err = change_attributes (argv[j]);