]>
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"
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] [-+=AacDdeijsSu] [-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
)
199 if (LSTAT (name
, &st
) == -1) {
201 com_err (program_name
, errno
,
202 _("while trying to stat %s"), name
);
206 if (fgetflags(name
, &flags
) == -1) {
208 com_err(program_name
, errno
,
209 _("while reading flags on %s"), name
);
212 if (flags
& EXT4_EXTENTS_FL
)
215 if (extent_file
&& !(sf
& EXT4_EXTENTS_FL
)) {
217 com_err(program_name
, 0,
218 _("Clearing extent flag not supported on %s"),
223 printf (_("Flags of %s set as "), name
);
224 print_flags (stdout
, sf
, 0);
227 if (fsetflags (name
, sf
) == -1)
234 if (extent_file
&& !(flags
& EXT4_EXTENTS_FL
)) {
236 com_err(program_name
, 0,
237 _("Clearing extent flag not supported on %s"),
242 printf(_("Flags of %s set as "), name
);
243 print_flags(stdout
, flags
, 0);
246 if (!S_ISDIR(st
.st_mode
))
247 flags
&= ~EXT2_DIRSYNC_FL
;
248 if (fsetflags(name
, flags
) == -1) {
250 com_err(program_name
, errno
,
251 _("while setting flags on %s"),
259 printf (_("Version of %s set as %lu\n"), name
, version
);
260 if (fsetversion (name
, version
) == -1) {
262 com_err (program_name
, errno
,
263 _("while setting version on %s"),
268 if (S_ISDIR(st
.st_mode
) && recursive
)
269 return iterate_on_dir (name
, chattr_dir_proc
, NULL
);
273 static int chattr_dir_proc (const char * dir_name
, struct dirent
* de
,
274 void * private EXT2FS_ATTR((unused
)))
278 if (strcmp (de
->d_name
, ".") && strcmp (de
->d_name
, "..")) {
281 path
= malloc(strlen (dir_name
) + 1 + strlen (de
->d_name
) + 1);
283 fprintf(stderr
, _("Couldn't allocate path variable "
284 "in chattr_dir_proc"));
287 sprintf(path
, "%s/%s", dir_name
, de
->d_name
);
288 ret
= change_attributes(path
);
294 int main (int argc
, char ** argv
)
301 setlocale(LC_MESSAGES
, "");
302 setlocale(LC_CTYPE
, "");
303 bindtextdomain(NLS_CAT_NAME
, LOCALEDIR
);
304 textdomain(NLS_CAT_NAME
);
305 set_com_err_gettext(gettext
);
308 program_name
= *argv
;
310 while (i
< argc
&& !end_arg
) {
311 /* '--' arg should end option processing */
312 if (strcmp(argv
[i
], "--") == 0) {
315 } else if (decode_arg (&i
, argc
, argv
) == EOF
)
322 if (set
&& (add
|| rem
)) {
323 fputs(_("= is incompatible with - and +\n"), stderr
);
326 if ((rf
& af
) != 0) {
327 fputs("Can't both set and unset same flag.\n", stderr
);
330 if (!(add
|| rem
|| set
|| set_version
)) {
331 fputs(_("Must use '-v', =, - or +\n"), stderr
);
335 fprintf (stderr
, "chattr %s (%s)\n",
336 E2FSPROGS_VERSION
, E2FSPROGS_DATE
);
337 for (j
= i
; j
< argc
; j
++) {
338 err
= change_attributes (argv
[j
]);