blob: 39a601697f329f1eaea3d82d0fc8c19d48eeecd9 [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,
Liu Bofd7042b2012-06-13 15:43:23 -040086 _("Usage: %s [-RVf] [-+=AaCcDdeijsSu] [-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'o0796e662012-06-12 17:09:39 -0400110 { FS_NOCOW_FL, 'C' },
Theodore Ts'o9a718842000-12-31 13:48:12 +0000111 { 0, 0 }
112};
113
114static unsigned long get_flag(char c)
115{
116 const struct flags_char *fp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400117
Theodore Ts'o9a718842000-12-31 13:48:12 +0000118 for (fp = flags_array; fp->flag != 0; fp++) {
119 if (fp->optchar == c)
120 return fp->flag;
121 }
122 return 0;
123}
124
125
Theodore Ts'o3839e651997-04-26 13:21:57 +0000126static int decode_arg (int * i, int argc, char ** argv)
127{
128 char * p;
129 char * tmp;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000130 unsigned long fl;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131
132 switch (argv[*i][0])
133 {
134 case '-':
Theodore Ts'o9a718842000-12-31 13:48:12 +0000135 for (p = &argv[*i][1]; *p; p++) {
136 if (*p == 'R') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 recursive = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000138 continue;
139 }
140 if (*p == 'V') {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 verbose = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000142 continue;
143 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400144 if (*p == 'f') {
145 silent = 1;
146 continue;
147 }
Theodore Ts'o9a718842000-12-31 13:48:12 +0000148 if (*p == 'v') {
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000149 (*i)++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 if (*i >= argc)
151 usage ();
Theodore Ts'o3839e651997-04-26 13:21:57 +0000152 version = strtol (argv[*i], &tmp, 0);
Theodore Ts'o9a718842000-12-31 13:48:12 +0000153 if (*tmp) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 com_err (program_name, 0,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400155 _("bad version - %s\n"),
Theodore Ts'o9a718842000-12-31 13:48:12 +0000156 argv[*i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 usage ();
158 }
159 set_version = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000160 continue;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 }
Theodore Ts'o9a718842000-12-31 13:48:12 +0000162 if ((fl = get_flag(*p)) == 0)
163 usage();
164 rf |= fl;
165 rem = 1;
166 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 break;
168 case '+':
169 add = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000170 for (p = &argv[*i][1]; *p; p++) {
171 if ((fl = get_flag(*p)) == 0)
172 usage();
173 af |= fl;
174 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 break;
176 case '=':
177 set = 1;
Theodore Ts'o9a718842000-12-31 13:48:12 +0000178 for (p = &argv[*i][1]; *p; p++) {
179 if ((fl = get_flag(*p)) == 0)
180 usage();
181 sf |= fl;
182 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 break;
184 default:
185 return EOF;
186 break;
187 }
188 return 1;
189}
190
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400191static int chattr_dir_proc(const char *, struct dirent *, void *);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400193static int change_attributes(const char * name)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194{
195 unsigned long flags;
Theodore Ts'ob7056402001-06-08 02:53:20 +0000196 STRUCT_STAT st;
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 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000211 if (set) {
212 if (verbose) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000213 printf (_("Flags of %s set as "), name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000214 print_flags (stdout, sf, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000215 printf ("\n");
216 }
217 if (fsetflags (name, sf) == -1)
218 perror (name);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000219 } else {
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530220 if (rem)
221 flags &= ~rf;
222 if (add)
223 flags |= af;
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530224 if (verbose) {
225 printf(_("Flags of %s set as "), name);
226 print_flags(stdout, flags, 0);
227 printf("\n");
228 }
229 if (!S_ISDIR(st.st_mode))
230 flags &= ~EXT2_DIRSYNC_FL;
231 if (fsetflags(name, flags) == -1) {
232 if (!silent) {
233 com_err(program_name, errno,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400234 _("while setting flags on %s"),
235 name);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400236 }
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530237 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 }
239 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000240 if (set_version) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000242 printf (_("Version of %s set as %lu\n"), name, version);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400243 if (fsetversion (name, version) == -1) {
244 if (!silent)
245 com_err (program_name, errno,
246 _("while setting version on %s"),
247 name);
248 return -1;
249 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 }
251 if (S_ISDIR(st.st_mode) && recursive)
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400252 return iterate_on_dir (name, chattr_dir_proc, NULL);
253 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000254}
255
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000256static int chattr_dir_proc (const char * dir_name, struct dirent * de,
Theodore Ts'o54434922003-12-07 01:28:50 -0500257 void * private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000258{
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400259 int ret = 0;
260
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000261 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000262 char *path;
263
264 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500265 if (!path) {
Andreas Dilger45ff69f2013-12-15 22:11:40 -0500266 fprintf(stderr, "%s",
267 _("Couldn't allocate path variable "
268 "in chattr_dir_proc"));
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400269 return -1;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500270 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400271 sprintf(path, "%s/%s", dir_name, de->d_name);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400272 ret = change_attributes(path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000273 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400275 return ret;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276}
277
Theodore Ts'o00e54331997-09-16 02:13:52 +0000278int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279{
280 int i, j;
281 int end_arg = 0;
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400282 int err, retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000284#ifdef ENABLE_NLS
285 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500286 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000287 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
288 textdomain(NLS_CAT_NAME);
Theodore Ts'o9d4507c2011-10-05 01:00:30 -0400289 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000290#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 if (argc && *argv)
292 program_name = *argv;
293 i = 1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000294 while (i < argc && !end_arg) {
Theodore Ts'o2293a4d2004-01-20 13:39:01 -0500295 /* '--' arg should end option processing */
296 if (strcmp(argv[i], "--") == 0) {
297 i++;
298 end_arg = 1;
299 } else if (decode_arg (&i, argc, argv) == EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000300 end_arg = 1;
301 else
302 i++;
303 }
304 if (i >= argc)
305 usage ();
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000306 if (set && (add || rem)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500307 fputs(_("= is incompatible with - and +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 exit (1);
309 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000310 if ((rf & af) != 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500311 fputs("Can't both set and unset same flag.\n", stderr);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000312 exit (1);
313 }
314 if (!(add || rem || set || set_version)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500315 fputs(_("Must use '-v', =, - or +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000316 exit (1);
317 }
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000318 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400319 fprintf (stderr, "chattr %s (%s)\n",
320 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400321 for (j = i; j < argc; j++) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400322 err = change_attributes (argv[j]);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400323 if (err)
324 retval = 1;
325 }
326 exit(retval);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327}