blob: de33b083c4a15062311cc3e417b23a90bf75cdad [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * chattr.c - Change file attributes on an ext2 file system
3 *
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)
7 *
8 * This file can be redistributed under the terms of the GNU General
9 * Public License
10 */
11
12/*
13 * History:
14 * 93/10/30 - Creation
15 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
16 * 94/02/27 - Integrated in Ted's distribution
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +000017 * 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)
Theodore Ts'o3839e651997-04-26 13:21:57 +000019 */
20
Theodore Ts'offf18b42001-02-08 03:06:43 +000021#define _LARGEFILE64_SOURCE
Theodore Ts'offf18b42001-02-08 03:06:43 +000022
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000023#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000024#include <dirent.h>
25#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000029#include <string.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000030#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000033#include <sys/param.h>
34#include <sys/stat.h>
Theodore Ts'o54c637d2001-05-14 11:45:38 +000035#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000036
Theodore Ts'o54434922003-12-07 01:28:50 -050037#ifdef __GNUC__
38#define EXT2FS_ATTR(x) __attribute__(x)
39#else
40#define EXT2FS_ATTR(x)
41#endif
42
Theodore Ts'o36caf251999-10-26 14:29:22 +000043#ifndef S_ISLNK /* So we can compile even with gcc-warn */
44# ifdef __S_IFLNK
45# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
46# else
47# define S_ISLNK(mode) 0
48# endif
49#endif
50
Theodore Ts'o3839e651997-04-26 13:21:57 +000051#include "et/com_err.h"
52#include "e2p/e2p.h"
53
54#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000055#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000057static const char * program_name = "chattr";
Theodore Ts'o3839e651997-04-26 13:21:57 +000058
Theodore Ts'o9a718842000-12-31 13:48:12 +000059static int add;
60static int rem;
61static int set;
62static int set_version;
Theodore Ts'o3839e651997-04-26 13:21:57 +000063
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000064static unsigned long version;
Theodore Ts'o3839e651997-04-26 13:21:57 +000065
Theodore Ts'o9a718842000-12-31 13:48:12 +000066static int recursive;
67static int verbose;
Theodore Ts'oe68594d2007-10-22 08:51:39 -040068static int silent;
Theodore Ts'o3839e651997-04-26 13:21:57 +000069
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000070static unsigned long af;
71static unsigned long rf;
72static unsigned long sf;
Theodore Ts'o3839e651997-04-26 13:21:57 +000073
Theodore Ts'ob7056402001-06-08 02:53:20 +000074#ifdef _LFS64_LARGEFILE
75#define LSTAT lstat64
76#define STRUCT_STAT struct stat64
77#else
78#define LSTAT lstat
79#define STRUCT_STAT struct stat
80#endif
81
Theodore Ts'o642935c2006-11-14 23:38:17 -050082static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000083{
Theodore Ts'oe68594d2007-10-22 08:51:39 -040084 fprintf(stderr,
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +053085 _("Usage: %s [-RVf] [-+=AacDdeijsSu] [-v version] files...\n"),
Theodore Ts'o642935c2006-11-14 23:38:17 -050086 program_name);
87 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +000088}
89
Theodore Ts'o9a718842000-12-31 13:48:12 +000090struct flags_char {
91 unsigned long flag;
92 char optchar;
93};
94
95static const struct flags_char flags_array[] = {
96 { EXT2_NOATIME_FL, 'A' },
97 { EXT2_SYNC_FL, 'S' },
Theodore Ts'o88372d52002-06-15 18:58:39 -040098 { EXT2_DIRSYNC_FL, 'D' },
Theodore Ts'o9a718842000-12-31 13:48:12 +000099 { EXT2_APPEND_FL, 'a' },
100 { EXT2_COMPR_FL, 'c' },
101 { EXT2_NODUMP_FL, 'd' },
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530102 { EXT4_EXTENTS_FL, 'e'},
Theodore Ts'o9a718842000-12-31 13:48:12 +0000103 { EXT2_IMMUTABLE_FL, 'i' },
104 { EXT3_JOURNAL_DATA_FL, 'j' },
105 { EXT2_SECRM_FL, 's' },
106 { EXT2_UNRM_FL, 'u' },
Theodore Ts'ob3f5b4c2001-11-05 19:22:02 -0500107 { EXT2_NOTAIL_FL, 't' },
Theodore Ts'o15f90112002-11-01 01:53:52 -0500108 { EXT2_TOPDIR_FL, 'T' },
Theodore Ts'o9a718842000-12-31 13:48:12 +0000109 { 0, 0 }
110};
111
112static unsigned long get_flag(char c)
113{
114 const struct flags_char *fp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400115
Theodore Ts'o9a718842000-12-31 13:48:12 +0000116 for (fp = flags_array; fp->flag != 0; fp++) {
117 if (fp->optchar == c)
118 return fp->flag;
119 }
120 return 0;
121}
122
123
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124static int decode_arg (int * i, int argc, char ** argv)
125{
126 char * p;
127 char * tmp;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000128 unsigned long fl;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129
130 switch (argv[*i][0])
131 {
132 case '-':
Theodore Ts'o9a718842000-12-31 13:48:12 +0000133 for (p = &argv[*i][1]; *p; p++) {
134 if (*p == 'R') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135 recursive = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000136 continue;
137 }
138 if (*p == 'V') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000139 verbose = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000140 continue;
141 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400142 if (*p == 'f') {
143 silent = 1;
144 continue;
145 }
Theodore Ts'o9a718842000-12-31 13:48:12 +0000146 if (*p == 'v') {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000147 (*i)++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000148 if (*i >= argc)
149 usage ();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 version = strtol (argv[*i], &tmp, 0);
Theodore Ts'o9a718842000-12-31 13:48:12 +0000151 if (*tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 com_err (program_name, 0,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400153 _("bad version - %s\n"),
Theodore Ts'o9a718842000-12-31 13:48:12 +0000154 argv[*i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155 usage ();
156 }
157 set_version = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000158 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 }
Theodore Ts'o9a718842000-12-31 13:48:12 +0000160 if ((fl = get_flag(*p)) == 0)
161 usage();
162 rf |= fl;
163 rem = 1;
164 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 break;
166 case '+':
167 add = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000168 for (p = &argv[*i][1]; *p; p++) {
169 if ((fl = get_flag(*p)) == 0)
170 usage();
171 af |= fl;
172 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000173 break;
174 case '=':
175 set = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000176 for (p = &argv[*i][1]; *p; p++) {
177 if ((fl = get_flag(*p)) == 0)
178 usage();
179 sf |= fl;
180 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181 break;
182 default:
183 return EOF;
184 break;
185 }
186 return 1;
187}
188
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400189static int chattr_dir_proc(const char *, struct dirent *, void *);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400191static int change_attributes(const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192{
193 unsigned long flags;
Theodore Ts'ob7056402001-06-08 02:53:20 +0000194 STRUCT_STAT st;
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530195 int extent_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000196
Theodore Ts'ob7056402001-06-08 02:53:20 +0000197 if (LSTAT (name, &st) == -1) {
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400198 if (!silent)
199 com_err (program_name, errno,
200 _("while trying to stat %s"), name);
201 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000203
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530204 if (fgetflags(name, &flags) == -1) {
205 if (!silent)
206 com_err(program_name, errno,
207 _("while reading flags on %s"), name);
208 return -1;
209 }
210 if (flags & EXT4_EXTENTS_FL)
211 extent_file = 1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000212 if (set) {
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530213 if (extent_file && !(sf & EXT4_EXTENTS_FL)) {
214 if (!silent)
215 com_err(program_name, 0,
216 _("Clearing extent flag not supported on %s"),
217 name);
218 return -1;
219 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000220 if (verbose) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000221 printf (_("Flags of %s set as "), name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000222 print_flags (stdout, sf, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 printf ("\n");
224 }
225 if (fsetflags (name, sf) == -1)
226 perror (name);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000227 } else {
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530228 if (rem)
229 flags &= ~rf;
230 if (add)
231 flags |= af;
232 if (extent_file && !(flags & EXT4_EXTENTS_FL)) {
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400233 if (!silent)
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530234 com_err(program_name, 0,
235 _("Clearing extent flag not supported on %s"),
236 name);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400237 return -1;
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530238 }
239 if (verbose) {
240 printf(_("Flags of %s set as "), name);
241 print_flags(stdout, flags, 0);
242 printf("\n");
243 }
244 if (!S_ISDIR(st.st_mode))
245 flags &= ~EXT2_DIRSYNC_FL;
246 if (fsetflags(name, flags) == -1) {
247 if (!silent) {
248 com_err(program_name, errno,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400249 _("while setting flags on %s"),
250 name);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400251 }
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530252 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253 }
254 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000255 if (set_version) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000256 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000257 printf (_("Version of %s set as %lu\n"), name, version);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400258 if (fsetversion (name, version) == -1) {
259 if (!silent)
260 com_err (program_name, errno,
261 _("while setting version on %s"),
262 name);
263 return -1;
264 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 }
266 if (S_ISDIR(st.st_mode) && recursive)
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400267 return iterate_on_dir (name, chattr_dir_proc, NULL);
268 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269}
270
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000271static int chattr_dir_proc (const char * dir_name, struct dirent * de,
Theodore Ts'o54434922003-12-07 01:28:50 -0500272 void * private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273{
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400274 int ret = 0;
275
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000276 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000277 char *path;
278
279 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500280 if (!path) {
281 fprintf(stderr, _("Couldn't allocate path variable "
282 "in chattr_dir_proc"));
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400283 return -1;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500284 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400285 sprintf(path, "%s/%s", dir_name, de->d_name);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400286 ret = change_attributes(path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000287 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400289 return ret;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290}
291
Theodore Ts'o00e54331997-09-16 02:13:52 +0000292int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293{
294 int i, j;
295 int end_arg = 0;
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400296 int err, retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000298#ifdef ENABLE_NLS
299 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500300 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000301 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
302 textdomain(NLS_CAT_NAME);
303#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304 if (argc && *argv)
305 program_name = *argv;
306 i = 1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000307 while (i < argc && !end_arg) {
Theodore Ts'o2293a4d2004-01-20 13:39:01 -0500308 /* '--' arg should end option processing */
309 if (strcmp(argv[i], "--") == 0) {
310 i++;
311 end_arg = 1;
312 } else if (decode_arg (&i, argc, argv) == EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000313 end_arg = 1;
314 else
315 i++;
316 }
317 if (i >= argc)
318 usage ();
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000319 if (set && (add || rem)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500320 fputs(_("= is incompatible with - and +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321 exit (1);
322 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000323 if ((rf & af) != 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500324 fputs("Can't both set and unset same flag.\n", stderr);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000325 exit (1);
326 }
327 if (!(add || rem || set || set_version)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500328 fputs(_("Must use '-v', =, - or +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329 exit (1);
330 }
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000331 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400332 fprintf (stderr, "chattr %s (%s)\n",
333 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400334 for (j = i; j < argc; j++) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400335 err = change_attributes (argv[j]);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400336 if (err)
337 retval = 1;
338 }
339 exit(retval);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340}