blob: 3567cfe01aa0a1411baf7f38284a080ec7519163 [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,
JP Abgralle0ed7402014-03-19 19:08:39 -070085 _("Usage: %s [-RVf] [-+=AaCcDdeijsSu] [-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' },
JP Abgralle0ed7402014-03-19 19:08:39 -0700109 { FS_NOCOW_FL, 'C' },
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;
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 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000210 if (set) {
211 if (verbose) {
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000212 printf (_("Flags of %s set as "), name);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000213 print_flags (stdout, sf, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214 printf ("\n");
215 }
216 if (fsetflags (name, sf) == -1)
217 perror (name);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000218 } else {
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530219 if (rem)
220 flags &= ~rf;
221 if (add)
222 flags |= af;
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530223 if (verbose) {
224 printf(_("Flags of %s set as "), name);
225 print_flags(stdout, flags, 0);
226 printf("\n");
227 }
228 if (!S_ISDIR(st.st_mode))
229 flags &= ~EXT2_DIRSYNC_FL;
230 if (fsetflags(name, flags) == -1) {
231 if (!silent) {
232 com_err(program_name, errno,
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400233 _("while setting flags on %s"),
234 name);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400235 }
Aneesh Kumar K.V7c8da6e2009-01-06 12:07:14 +0530236 return -1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 }
238 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000239 if (set_version) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 if (verbose)
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000241 printf (_("Version of %s set as %lu\n"), name, version);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400242 if (fsetversion (name, version) == -1) {
243 if (!silent)
244 com_err (program_name, errno,
245 _("while setting version on %s"),
246 name);
247 return -1;
248 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000249 }
250 if (S_ISDIR(st.st_mode) && recursive)
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400251 return iterate_on_dir (name, chattr_dir_proc, NULL);
252 return 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000253}
254
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000255static int chattr_dir_proc (const char * dir_name, struct dirent * de,
Theodore Ts'o54434922003-12-07 01:28:50 -0500256 void * private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000257{
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400258 int ret = 0;
259
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000260 if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000261 char *path;
262
263 path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
Theodore Ts'o642935c2006-11-14 23:38:17 -0500264 if (!path) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700265 fprintf(stderr, "%s",
266 _("Couldn't allocate path variable "
267 "in chattr_dir_proc"));
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400268 return -1;
Theodore Ts'o642935c2006-11-14 23:38:17 -0500269 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400270 sprintf(path, "%s/%s", dir_name, de->d_name);
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400271 ret = change_attributes(path);
Theodore Ts'oa418d3a1997-04-26 14:00:26 +0000272 free(path);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273 }
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400274 return ret;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000275}
276
Theodore Ts'o00e54331997-09-16 02:13:52 +0000277int main (int argc, char ** argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278{
279 int i, j;
280 int end_arg = 0;
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400281 int err, retval = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000283#ifdef ENABLE_NLS
284 setlocale(LC_MESSAGES, "");
Theodore Ts'o14308a52002-03-05 03:26:52 -0500285 setlocale(LC_CTYPE, "");
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000286 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
287 textdomain(NLS_CAT_NAME);
JP Abgralle0ed7402014-03-19 19:08:39 -0700288 set_com_err_gettext(gettext);
Theodore Ts'od9c56d32000-02-08 00:47:55 +0000289#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +0000290 if (argc && *argv)
291 program_name = *argv;
292 i = 1;
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000293 while (i < argc && !end_arg) {
Theodore Ts'o2293a4d2004-01-20 13:39:01 -0500294 /* '--' arg should end option processing */
295 if (strcmp(argv[i], "--") == 0) {
296 i++;
297 end_arg = 1;
298 } else if (decode_arg (&i, argc, argv) == EOF)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000299 end_arg = 1;
300 else
301 i++;
302 }
303 if (i >= argc)
304 usage ();
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000305 if (set && (add || rem)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500306 fputs(_("= is incompatible with - and +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 exit (1);
308 }
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000309 if ((rf & af) != 0) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500310 fputs("Can't both set and unset same flag.\n", stderr);
Theodore Ts'oe1a0a3e2000-02-11 05:00:19 +0000311 exit (1);
312 }
313 if (!(add || rem || set || set_version)) {
Theodore Ts'o54434922003-12-07 01:28:50 -0500314 fputs(_("Must use '-v', =, - or +\n"), stderr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000315 exit (1);
316 }
Theodore Ts'oa88fa0c1999-01-05 07:02:39 +0000317 if (verbose)
Theodore Ts'o0f8973f2001-08-27 12:44:23 -0400318 fprintf (stderr, "chattr %s (%s)\n",
319 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400320 for (j = i; j < argc; j++) {
Theodore Ts'o2d328bb2008-03-17 23:17:13 -0400321 err = change_attributes (argv[j]);
Theodore Ts'oe68594d2007-10-22 08:51:39 -0400322 if (err)
323 retval = 1;
324 }
325 exit(retval);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000326}