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;
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 [-RVf] [-+=aAcCdDeijPsStTu] [-v version] [-p project] 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_NODUMP_FL, 'd' },
106 { EXT4_EXTENTS_FL, 'e'},
107 { EXT2_IMMUTABLE_FL, 'i' },
108 { EXT3_JOURNAL_DATA_FL, 'j' },
109 { EXT4_PROJINHERIT_FL, 'P' },
110 { EXT2_SECRM_FL, 's' },
111 { EXT2_UNRM_FL, 'u' },
112 { EXT2_NOTAIL_FL, 't' },
113 { EXT2_TOPDIR_FL, 'T' },
114 { FS_NOCOW_FL, 'C' },
118 static unsigned long get_flag(char c)
120 const struct flags_char *fp;
122 for (fp = flags_array; fp->flag != 0; fp++) {
123 if (fp->optchar == c)
130 static int decode_arg (int * i, int argc, char ** argv)
139 for (p = &argv[*i][1]; *p; p++) {
156 project = strtol (argv[*i], &tmp, 0);
158 com_err (program_name, 0,
159 _("bad project - %s\n"),
170 version = strtol (argv[*i], &tmp, 0);
172 com_err (program_name, 0,
173 _("bad version - %s\n"),
180 if ((fl = get_flag(*p)) == 0)
188 for (p = &argv[*i][1]; *p; p++) {
189 if ((fl = get_flag(*p)) == 0)
196 for (p = &argv[*i][1]; *p; p++) {
197 if ((fl = get_flag(*p)) == 0)
209 static int chattr_dir_proc(const char *, struct dirent *, void *);
211 static int change_attributes(const char * name)
216 if (LSTAT (name, &st) == -1) {
218 com_err (program_name, errno,
219 _("while trying to stat %s"), name);
223 if (fgetflags(name, &flags) == -1) {
225 com_err(program_name, errno,
226 _("while reading flags on %s"), name);
231 printf (_("Flags of %s set as "), name);
232 print_flags (stdout, sf, 0);
235 if (fsetflags (name, sf) == -1)
243 printf(_("Flags of %s set as "), name);
244 print_flags(stdout, flags, 0);
247 if (!S_ISDIR(st.st_mode))
248 flags &= ~EXT2_DIRSYNC_FL;
249 if (fsetflags(name, flags) == -1) {
251 com_err(program_name, errno,
252 _("while setting flags on %s"),
260 printf (_("Version of %s set as %lu\n"), name, version);
261 if (fsetversion (name, version) == -1) {
263 com_err (program_name, errno,
264 _("while setting version on %s"),
271 printf (_("Project of %s set as %lu\n"), name, version);
272 if (fsetproject (name, project) == -1) {
274 com_err (program_name, errno,
275 _("while setting project on %s"),
281 if (S_ISDIR(st.st_mode) && recursive)
282 return iterate_on_dir (name, chattr_dir_proc, NULL);
286 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
287 void * private EXT2FS_ATTR((unused)))
291 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
294 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
296 fprintf(stderr, "%s",
297 _("Couldn't allocate path variable "
298 "in chattr_dir_proc"));
301 sprintf(path, "%s/%s", dir_name, de->d_name);
302 ret = change_attributes(path);
308 int main (int argc, char ** argv)
315 setlocale(LC_MESSAGES, "");
316 setlocale(LC_CTYPE, "");
317 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
318 textdomain(NLS_CAT_NAME);
319 set_com_err_gettext(gettext);
322 program_name = *argv;
324 while (i < argc && !end_arg) {
325 /* '--' arg should end option processing */
326 if (strcmp(argv[i], "--") == 0) {
329 } else if (decode_arg (&i, argc, argv) == EOF)
336 if (set && (add || rem)) {
337 fputs(_("= is incompatible with - and +\n"), stderr);
340 if ((rf & af) != 0) {
341 fputs("Can't both set and unset same flag.\n", stderr);
344 if (!(add || rem || set || set_version || set_project )) {
345 fputs(_("Must use '-v', =, - or +\n"), stderr);
349 fprintf (stderr, "chattr %s (%s)\n",
350 E2FSPROGS_VERSION, E2FSPROGS_DATE);
351 for (j = i; j < argc; j++) {
352 err = change_attributes (argv[j]);