]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - misc/chattr.c
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] [-+=aAcCdDeijPsStTuF] [-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_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' },
115 { EXT4_CASEFOLD_FL
, 'F' },
119 static unsigned long get_flag(char c
)
121 const struct flags_char
*fp
;
123 for (fp
= flags_array
; fp
->flag
!= 0; fp
++) {
124 if (fp
->optchar
== c
)
131 static int decode_arg (int * i
, int argc
, char ** argv
)
140 for (p
= &argv
[*i
][1]; *p
; p
++) {
157 project
= strtol (argv
[*i
], &tmp
, 0);
159 com_err (program_name
, 0,
160 _("bad project - %s\n"),
171 version
= strtol (argv
[*i
], &tmp
, 0);
173 com_err (program_name
, 0,
174 _("bad version - %s\n"),
181 if ((fl
= get_flag(*p
)) == 0)
189 for (p
= &argv
[*i
][1]; *p
; p
++) {
190 if ((fl
= get_flag(*p
)) == 0)
197 for (p
= &argv
[*i
][1]; *p
; p
++) {
198 if ((fl
= get_flag(*p
)) == 0)
210 static int chattr_dir_proc(const char *, struct dirent
*, void *);
212 static int change_attributes(const char * name
)
217 if (LSTAT (name
, &st
) == -1) {
219 com_err (program_name
, errno
,
220 _("while trying to stat %s"), name
);
224 if (fgetflags(name
, &flags
) == -1) {
226 com_err(program_name
, errno
,
227 _("while reading flags on %s"), name
);
232 printf (_("Flags of %s set as "), name
);
233 print_flags (stdout
, sf
, 0);
236 if (fsetflags (name
, sf
) == -1)
244 printf(_("Flags of %s set as "), name
);
245 print_flags(stdout
, flags
, 0);
248 if (!S_ISDIR(st
.st_mode
))
249 flags
&= ~EXT2_DIRSYNC_FL
;
250 if (fsetflags(name
, flags
) == -1) {
252 com_err(program_name
, errno
,
253 _("while setting flags on %s"),
261 printf (_("Version of %s set as %lu\n"), name
, version
);
262 if (fsetversion (name
, version
) == -1) {
264 com_err (program_name
, errno
,
265 _("while setting version on %s"),
272 printf (_("Project of %s set as %lu\n"), name
, project
);
273 if (fsetproject (name
, project
) == -1) {
275 com_err (program_name
, errno
,
276 _("while setting project on %s"),
282 if (S_ISDIR(st
.st_mode
) && recursive
)
283 return iterate_on_dir (name
, chattr_dir_proc
, NULL
);
287 static int chattr_dir_proc (const char * dir_name
, struct dirent
* de
,
288 void * private EXT2FS_ATTR((unused
)))
292 if (strcmp (de
->d_name
, ".") && strcmp (de
->d_name
, "..")) {
295 path
= malloc(strlen (dir_name
) + 1 + strlen (de
->d_name
) + 1);
297 fprintf(stderr
, "%s",
298 _("Couldn't allocate path variable "
299 "in chattr_dir_proc"));
302 sprintf(path
, "%s/%s", dir_name
, de
->d_name
);
303 ret
= change_attributes(path
);
309 int main (int argc
, char ** argv
)
316 setlocale(LC_MESSAGES
, "");
317 setlocale(LC_CTYPE
, "");
318 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
319 textdomain(NLS_CAT_NAME
);
320 set_com_err_gettext(gettext
);
323 program_name
= *argv
;
325 while (i
< argc
&& !end_arg
) {
326 /* '--' arg should end option processing */
327 if (strcmp(argv
[i
], "--") == 0) {
330 } else if (decode_arg (&i
, argc
, argv
) == EOF
)
337 if (set
&& (add
|| rem
)) {
338 fputs(_("= is incompatible with - and +\n"), stderr
);
341 if ((rf
& af
) != 0) {
342 fputs("Can't both set and unset same flag.\n", stderr
);
345 if (!(add
|| rem
|| set
|| set_version
|| set_project
)) {
346 fputs(_("Must use '-v', =, - or +\n"), stderr
);
350 fprintf (stderr
, "chattr %s (%s)\n",
351 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
352 for (j
= i
; j
< argc
; j
++) {
353 err
= change_attributes (argv
[j
]);