blob: 69aaed1cc4aa33e6af45e4faaba3bd44e4e9c12e [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'od1154eb2011-09-18 17:34:37 -040023#include "config.h"
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000024#include <sys/types.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000025#include <dirent.h>
26#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <stdio.h>
28#include <stdlib.h>
29#include <unistd.h>
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000030#include <string.h>
Theodore Ts'oa418d3a1997-04-26 14:00:26 +000031#ifdef HAVE_ERRNO_H
32#include <errno.h>
33#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000034#include <sys/param.h>
35#include <sys/stat.h>
Theodore Ts'o54c637d2001-05-14 11:45:38 +000036#include "ext2fs/ext2_fs.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000037
Theodore Ts'o54434922003-12-07 01:28:50 -050038#ifdef __GNUC__
39#define EXT2FS_ATTR(x) __attribute__(x)
40#else
41#define EXT2FS_ATTR(x)
42#endif
43
Theodore Ts'o36caf251999-10-26 14:29:22 +000044#ifndef S_ISLNK /* So we can compile even with gcc-warn */
45# ifdef __S_IFLNK
46# define S_ISLNK(mode) __S_ISTYPE((mode), __S_IFLNK)
47# else
48# define S_ISLNK(mode) 0
49# endif
50#endif
51
Theodore Ts'o3839e651997-04-26 13:21:57 +000052#include "et/com_err.h"
53#include "e2p/e2p.h"
54
55#include "../version.h"
Theodore Ts'od9c56d32000-02-08 00:47:55 +000056#include "nls-enable.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000057
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000058static const char * program_name = "chattr";
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
Theodore Ts'o9a718842000-12-31 13:48:12 +000060static int add;
61static int rem;
62static int set;
63static int set_version;
Theodore Ts'o3839e651997-04-26 13:21:57 +000064
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000065static unsigned long version;
Theodore Ts'o3839e651997-04-26 13:21:57 +000066
Theodore Ts'o9a718842000-12-31 13:48:12 +000067static int recursive;
68static int verbose;
Theodore Ts'oe68594d2007-10-22 08:51:39 -040069static int silent;
Theodore Ts'o3839e651997-04-26 13:21:57 +000070
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +000071static unsigned long af;
72static unsigned long rf;
73static unsigned long sf;
Theodore Ts'o3839e651997-04-26 13:21:57 +000074
Theodore Ts'ob7056402001-06-08 02:53:20 +000075#ifdef _LFS64_LARGEFILE
76#define LSTAT lstat64
77#define STRUCT_STAT struct stat64
78#else
79#define LSTAT lstat
80#define STRUCT_STAT struct stat
81#endif
82
Theodore Ts'o642935c2006-11-14 23:38:17 -050083static void usage(void)
Theodore Ts'o3839e651997-04-26 13:21:57 +000084{
Theodore Ts'oe68594d2007-10-22 08:51:39 -040085 fprintf(stderr,
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +053086 _("Usage: %s [-RVf] [-+=AacDdeijsSu] [-v version] files...\n"),
Theodore Ts'o642935c2006-11-14 23:38:17 -050087 program_name);
88 exit(1);
Theodore Ts'o3839e651997-04-26 13:21:57 +000089}
90
Theodore Ts'o9a718842000-12-31 13:48:12 +000091struct flags_char {
92 unsigned long flag;
93 char optchar;
94};
95
96static const struct flags_char flags_array[] = {
97 { EXT2_NOATIME_FL, 'A' },
98 { EXT2_SYNC_FL, 'S' },
Theodore Ts'o88372d52002-06-15 18:58:39 -040099 { EXT2_DIRSYNC_FL, 'D' },
Theodore Ts'o9a718842000-12-31 13:48:12 +0000100 { EXT2_APPEND_FL, 'a' },
101 { EXT2_COMPR_FL, 'c' },
102 { EXT2_NODUMP_FL, 'd' },
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530103 { EXT4_EXTENTS_FL, 'e'},
Theodore Ts'o9a718842000-12-31 13:48:12 +0000104 { EXT2_IMMUTABLE_FL, 'i' },
105 { EXT3_JOURNAL_DATA_FL, 'j' },
106 { EXT2_SECRM_FL, 's' },
107 { EXT2_UNRM_FL, 'u' },
Theodore Ts'ob3f5b4c2001-11-05 19:22:02 -0500108 { EXT2_NOTAIL_FL, 't' },
Theodore Ts'o15f90112002-11-01 01:53:52 -0500109 { EXT2_TOPDIR_FL, 'T' },
Theodore Ts'o9a718842000-12-31 13:48:12 +0000110 { 0, 0 }
111};
112
113static unsigned long get_flag(char c)
114{
115 const struct flags_char *fp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400116
Theodore Ts'o9a718842000-12-31 13:48:12 +0000117 for (fp = flags_array; fp->flag != 0; fp++) {
118 if (fp->optchar == c)
119 return fp->flag;
120 }
121 return 0;
122}
123
124
Theodore Ts'o3839e651997-04-26 13:21:57 +0000125static int decode_arg (int * i, int argc, char ** argv)
126{
127 char * p;
128 char * tmp;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000129 unsigned long fl;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130
131 switch (argv[*i][0])
132 {
133 case '-':
Theodore Ts'o9a718842000-12-31 13:48:12 +0000134 for (p = &argv[*i][1]; *p; p++) {
135 if (*p == 'R') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 recursive = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000137 continue;
138 }
139 if (*p == 'V') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 verbose = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000141 continue;
142 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400143 if (*p == 'f') {
144 silent = 1;
145 continue;
146 }
Theodore Ts'o9a718842000-12-31 13:48:12 +0000147 if (*p == 'v') {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000148 (*i)++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000149 if (*i >= argc)
150 usage ();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000151 version = strtol (argv[*i], &tmp, 0);
Theodore Ts'o9a718842000-12-31 13:48:12 +0000152 if (*tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000153 com_err (program_name, 0,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400154 _("bad version - %s\n"),
Theodore Ts'o9a718842000-12-31 13:48:12 +0000155 argv[*i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000156 usage ();
157 }
158 set_version = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000159 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 }
Theodore Ts'o9a718842000-12-31 13:48:12 +0000161 if ((fl = get_flag(*p)) == 0)
162 usage();
163 rf |= fl;
164 rem = 1;
165 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 break;
167 case '+':
168 add = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000169 for (p = &argv[*i][1]; *p; p++) {
170 if ((fl = get_flag(*p)) == 0)
171 usage();
172 af |= fl;
173 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 break;
175 case '=':
176 set = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000177 for (p = &argv[*i][1]; *p; p++) {
178 if ((fl = get_flag(*p)) == 0)
179 usage();
180 sf |= fl;
181 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000182 break;
183 default:
184 return EOF;
185 break;
186 }
187 return 1;
188}
189
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400190static int chattr_dir_proc(const char *, struct dirent *, void *);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400192static int change_attributes(const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193{
194 unsigned long flags;
Theodore Ts'ob7056402001-06-08 02:53:20 +0000195 STRUCT_STAT st;
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530196 int extent_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197
Theodore Ts'ob7056402001-06-08 02:53:20 +0000198 if (LSTAT (name, &st) == -1) {
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400199 if (!silent)
200 com_err (program_name, errno,
201 _("while trying to stat %s"), name);
202 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000204
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530205 if (fgetflags(name, &flags) == -1) {
206 if (!silent)
207 com_err(program_name, errno,
208 _("while reading flags on %s"), name);
209 return -1;
210 }
211 if (flags & EXT4_EXTENTS_FL)
212 extent_file = 1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000213 if (set) {
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530214 if (extent_file && !(sf & EXT4_EXTENTS_FL)) {
215 if (!silent)
216 com_err(program_name, 0,
217 _("Clearing extent flag not supported on %s"),
218 name);
219 return -1;
220 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000221 if (verbose) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000222 printf (_("Flags of %s set as "), name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000223 print_flags (stdout, sf, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224 printf ("\n");
225 }
226 if (fsetflags (name, sf) == -1)
227 perror (name);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000228 } else {
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530229 if (rem)
230 flags &= ~rf;
231 if (add)
232 flags |= af;
233 if (extent_file && !(flags & EXT4_EXTENTS_FL)) {
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400234 if (!silent)
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530235 com_err(program_name, 0,
236 _("Clearing extent flag not supported on %s"),
237 name);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400238 return -1;
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530239 }
240 if (verbose) {
241 printf(_("Flags of %s set as "), name);
242 print_flags(stdout, flags, 0);
243 printf("\n");
244 }
245 if (!S_ISDIR(st.st_mode))
246 flags &= ~EXT2_DIRSYNC_FL;
247 if (fsetflags(name, flags) == -1) {
248 if (!silent) {
249 com_err(program_name, errno,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400250 _("while setting flags on %s"),
251 name);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400252 }
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530253 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254 }
255 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000256 if (set_version) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000257 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000258 printf (_("Version of %s set as %lu\n"), name, version);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400259 if (fsetversion (name, version) == -1) {
260 if (!silent)
261 com_err (program_name, errno,
262 _("while setting version on %s"),
263 name);
264 return -1;
265 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266 }
267 if (S_ISDIR(st.st_mode) && recursive)
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400268 return iterate_on_dir (name, chattr_dir_proc, NULL);
269 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270}
271
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000272static int chattr_dir_proc (const char * dir_name, struct dirent * de,
Theodore Ts'o54434922003-12-07 01:28:50 -0500273 void * private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274{
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400275 int ret = 0;
276
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000277 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000278 char *path;
279
280 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500281 if (!path) {
282 fprintf(stderr, _("Couldn't allocate path variable "
283 "in chattr_dir_proc"));
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400284 return -1;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500285 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400286 sprintf(path, "%s/%s", dir_name, de->d_name);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400287 ret = change_attributes(path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000288 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400290 return ret;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291}
292
Theodore Ts'o00e54331997-09-16 02:13:52 +0000293int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294{
295 int i, j;
296 int end_arg = 0;
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400297 int err, retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000299#ifdef ENABLE_NLS
300 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500301 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000302 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
303 textdomain(NLS_CAT_NAME);
304#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 if (argc && *argv)
306 program_name = *argv;
307 i = 1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000308 while (i < argc && !end_arg) {
Theodore Ts'o2293a4d2004-01-20 13:39:01 -0500309 /* '--' arg should end option processing */
310 if (strcmp(argv[i], "--") == 0) {
311 i++;
312 end_arg = 1;
313 } else if (decode_arg (&i, argc, argv) == EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314 end_arg = 1;
315 else
316 i++;
317 }
318 if (i >= argc)
319 usage ();
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000320 if (set && (add || rem)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500321 fputs(_("= is incompatible with - and +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322 exit (1);
323 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000324 if ((rf & af) != 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500325 fputs("Can't both set and unset same flag.\n", stderr);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000326 exit (1);
327 }
328 if (!(add || rem || set || set_version)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500329 fputs(_("Must use '-v', =, - or +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330 exit (1);
331 }
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000332 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400333 fprintf (stderr, "chattr %s (%s)\n",
334 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400335 for (j = i; j < argc; j++) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400336 err = change_attributes (argv[j]);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400337 if (err)
338 retval = 1;
339 }
340 exit(retval);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341}