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"
54 #include "support/nls-enable.h"
56 #include "../version.h"
58 static const char * program_name = "chattr";
63 static int set_version;
65 static unsigned long version;
67 static int set_project;
68 static unsigned long project;
74 static unsigned long af;
75 static unsigned long rf;
76 static unsigned long sf;
78 #ifdef _LFS64_LARGEFILE
80 #define STRUCT_STAT struct stat64
83 #define STRUCT_STAT struct stat
86 static void usage(void)
89 _("Usage: %s [-pRVf] [-+=aAcCdDeijPsStTuFx] [-v version] files...\n"),
99 static const struct flags_char flags_array[] = {
100 { EXT2_NOATIME_FL, 'A' },
101 { EXT2_SYNC_FL, 'S' },
102 { EXT2_DIRSYNC_FL, 'D' },
103 { EXT2_APPEND_FL, 'a' },
104 { EXT2_COMPR_FL, 'c' },
105 { EXT2_NOCOMPR_FL, 'm' },
106 { EXT2_NODUMP_FL, 'd' },
107 { EXT4_EXTENTS_FL, 'e'},
108 { EXT2_IMMUTABLE_FL, 'i' },
109 { EXT3_JOURNAL_DATA_FL, 'j' },
110 { EXT4_PROJINHERIT_FL, 'P' },
111 { EXT2_SECRM_FL, 's' },
112 { EXT2_UNRM_FL, 'u' },
113 { EXT2_NOTAIL_FL, 't' },
114 { EXT2_TOPDIR_FL, 'T' },
115 { FS_NOCOW_FL, 'C' },
117 { EXT4_CASEFOLD_FL, 'F' },
121 static unsigned long get_flag(char c)
123 const struct flags_char *fp;
125 for (fp = flags_array; fp->flag != 0; fp++) {
126 if (fp->optchar == c)
133 static int decode_arg (int * i, int argc, char ** argv)
142 for (p = &argv[*i][1]; *p; p++) {
159 project = strtol (argv[*i], &tmp, 0);
161 com_err (program_name, 0,
162 _("bad project - %s\n"),
173 version = strtol (argv[*i], &tmp, 0);
175 com_err (program_name, 0,
176 _("bad version - %s\n"),
183 if ((fl = get_flag(*p)) == 0)
191 for (p = &argv[*i][1]; *p; p++) {
192 if ((fl = get_flag(*p)) == 0)
199 for (p = &argv[*i][1]; *p; p++) {
200 if ((fl = get_flag(*p)) == 0)
211 static int chattr_dir_proc(const char *, struct dirent *, void *);
213 static int change_attributes(const char * name)
218 if (LSTAT (name, &st) == -1) {
220 com_err (program_name, errno,
221 _("while trying to stat %s"), name);
225 if (fgetflags(name, &flags) == -1) {
227 com_err(program_name, errno,
228 _("while reading flags on %s"), name);
233 printf (_("Flags of %s set as "), name);
234 print_flags (stdout, sf, 0);
237 if (fsetflags (name, sf) == -1)
245 printf(_("Flags of %s set as "), name);
246 print_flags(stdout, flags, 0);
249 if (!S_ISDIR(st.st_mode))
250 flags &= ~EXT2_DIRSYNC_FL;
251 if (fsetflags(name, flags) == -1) {
253 com_err(program_name, errno,
254 _("while setting flags on %s"),
262 printf (_("Version of %s set as %lu\n"), name, version);
263 if (fsetversion (name, version) == -1) {
265 com_err (program_name, errno,
266 _("while setting version on %s"),
273 printf (_("Project of %s set as %lu\n"), name, project);
274 if (fsetproject (name, project) == -1) {
276 com_err (program_name, errno,
277 _("while setting project on %s"),
283 if (S_ISDIR(st.st_mode) && recursive)
284 return iterate_on_dir (name, chattr_dir_proc, NULL);
288 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
289 void * private EXT2FS_ATTR((unused)))
293 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
296 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
298 fprintf(stderr, "%s",
299 _("Couldn't allocate path variable "
300 "in chattr_dir_proc"));
303 sprintf(path, "%s/%s", dir_name, de->d_name);
304 ret = change_attributes(path);
310 int main (int argc, char ** argv)
317 setlocale(LC_MESSAGES, "");
318 setlocale(LC_CTYPE, "");
319 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
320 textdomain(NLS_CAT_NAME);
321 set_com_err_gettext(gettext);
324 program_name = *argv;
326 while (i < argc && !end_arg) {
327 /* '--' arg should end option processing */
328 if (strcmp(argv[i], "--") == 0) {
331 } else if (decode_arg (&i, argc, argv) == EOF)
338 if (set && (add || rem)) {
339 fputs(_("= is incompatible with - and +\n"), stderr);
342 if ((rf & af) != 0) {
343 fputs("Can't both set and unset same flag.\n", stderr);
346 if (!(add || rem || set || set_version || set_project )) {
347 fputs(_("Must use '-v', =, - or +\n"), stderr);
351 fprintf (stderr, "chattr %s (%s)\n",
352 E2FSPROGS_VERSION, E2FSPROGS_DATE);
353 for (j = i; j < argc; j++) {
354 err = change_attributes (argv[j]);