chattr.c revision a6ce1349539f866334ef3d5758bc2ee44a454acd
1/*
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
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)
19 */
20
21#define _LARGEFILE64_SOURCE
22
23#include <sys/types.h>
24#include <dirent.h>
25#include <fcntl.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <string.h>
30#ifdef HAVE_ERRNO_H
31#include <errno.h>
32#endif
33#include <sys/param.h>
34#include <sys/stat.h>
35#include "ext2fs/ext2_fs.h"
36
37#ifdef __GNUC__
38#define EXT2FS_ATTR(x) __attribute__(x)
39#else
40#define EXT2FS_ATTR(x)
41#endif
42
43#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
51#include "et/com_err.h"
52#include "e2p/e2p.h"
53
54#include "../version.h"
55#include "nls-enable.h"
56
57static const char * program_name = "chattr";
58
59static int add;
60static int rem;
61static int set;
62static int set_version;
63
64static unsigned long version;
65
66static int recursive;
67static int verbose;
68static int silent;
69
70static unsigned long af;
71static unsigned long rf;
72static unsigned long sf;
73
74#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
82static void usage(void)
83{
84	fprintf(stderr,
85		_("Usage: %s [-RVf] [-+=AacDdeijsSu] [-v version] files...\n"),
86		program_name);
87	exit(1);
88}
89
90struct 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' },
98	{ EXT2_DIRSYNC_FL, 'D' },
99	{ EXT2_APPEND_FL, 'a' },
100	{ EXT2_COMPR_FL, 'c' },
101	{ EXT2_NODUMP_FL, 'd' },
102	{ EXT4_EXTENTS_FL, 'e'},
103	{ EXT2_IMMUTABLE_FL, 'i' },
104	{ EXT3_JOURNAL_DATA_FL, 'j' },
105	{ EXT2_SECRM_FL, 's' },
106	{ EXT2_UNRM_FL, 'u' },
107	{ EXT2_NOTAIL_FL, 't' },
108	{ EXT2_TOPDIR_FL, 'T' },
109	{ 0, 0 }
110};
111
112static unsigned long get_flag(char c)
113{
114	const struct flags_char *fp;
115
116	for (fp = flags_array; fp->flag != 0; fp++) {
117		if (fp->optchar == c)
118			return fp->flag;
119	}
120	return 0;
121}
122
123
124static int decode_arg (int * i, int argc, char ** argv)
125{
126	char * p;
127	char * tmp;
128	unsigned long fl;
129
130	switch (argv[*i][0])
131	{
132	case '-':
133		for (p = &argv[*i][1]; *p; p++) {
134			if (*p == 'R') {
135				recursive = 1;
136				continue;
137			}
138			if (*p == 'V') {
139				verbose = 1;
140				continue;
141			}
142			if (*p == 'f') {
143				silent = 1;
144				continue;
145			}
146			if (*p == 'v') {
147				(*i)++;
148				if (*i >= argc)
149					usage ();
150				version = strtol (argv[*i], &tmp, 0);
151				if (*tmp) {
152					com_err (program_name, 0,
153						 _("bad version - %s\n"),
154						 argv[*i]);
155					usage ();
156				}
157				set_version = 1;
158				continue;
159			}
160			if ((fl = get_flag(*p)) == 0)
161				usage();
162			rf |= fl;
163			rem = 1;
164		}
165		break;
166	case '+':
167		add = 1;
168		for (p = &argv[*i][1]; *p; p++) {
169			if ((fl = get_flag(*p)) == 0)
170				usage();
171			af |= fl;
172		}
173		break;
174	case '=':
175		set = 1;
176		for (p = &argv[*i][1]; *p; p++) {
177			if ((fl = get_flag(*p)) == 0)
178				usage();
179			sf |= fl;
180		}
181		break;
182	default:
183		return EOF;
184		break;
185	}
186	return 1;
187}
188
189static int chattr_dir_proc(const char *, struct dirent *, void *);
190
191static int change_attributes(const char * name)
192{
193	unsigned long flags;
194	STRUCT_STAT	st;
195	int extent_file = 0;
196
197	if (LSTAT (name, &st) == -1) {
198		if (!silent)
199			com_err (program_name, errno,
200				 _("while trying to stat %s"), name);
201		return -1;
202	}
203
204	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;
212	if (set) {
213		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		}
220		if (verbose) {
221			printf (_("Flags of %s set as "), name);
222			print_flags (stdout, sf, 0);
223			printf ("\n");
224		}
225		if (fsetflags (name, sf) == -1)
226			perror (name);
227	} else {
228		if (rem)
229			flags &= ~rf;
230		if (add)
231			flags |= af;
232		if (extent_file && !(flags & EXT4_EXTENTS_FL)) {
233			if (!silent)
234				com_err(program_name, 0,
235				_("Clearing extent flag not supported on %s"),
236					name);
237			return -1;
238		}
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,
249						_("while setting flags on %s"),
250						name);
251			}
252			return -1;
253		}
254	}
255	if (set_version) {
256		if (verbose)
257			printf (_("Version of %s set as %lu\n"), name, version);
258		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		}
265	}
266	if (S_ISDIR(st.st_mode) && recursive)
267		return iterate_on_dir (name, chattr_dir_proc, NULL);
268	return 0;
269}
270
271static int chattr_dir_proc (const char * dir_name, struct dirent * de,
272			    void * private EXT2FS_ATTR((unused)))
273{
274	int ret = 0;
275
276	if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
277	        char *path;
278
279		path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
280		if (!path) {
281			fprintf(stderr, _("Couldn't allocate path variable "
282					  "in chattr_dir_proc"));
283			return -1;
284		}
285		sprintf(path, "%s/%s", dir_name, de->d_name);
286		ret = change_attributes(path);
287		free(path);
288	}
289	return ret;
290}
291
292int main (int argc, char ** argv)
293{
294	int i, j;
295	int end_arg = 0;
296	int err, retval = 0;
297
298#ifdef ENABLE_NLS
299	setlocale(LC_MESSAGES, "");
300	setlocale(LC_CTYPE, "");
301	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
302	textdomain(NLS_CAT_NAME);
303#endif
304	if (argc && *argv)
305		program_name = *argv;
306	i = 1;
307	while (i < argc && !end_arg) {
308		/* '--' 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)
313			end_arg = 1;
314		else
315			i++;
316	}
317	if (i >= argc)
318		usage ();
319	if (set && (add || rem)) {
320		fputs(_("= is incompatible with - and +\n"), stderr);
321		exit (1);
322	}
323	if ((rf & af) != 0) {
324		fputs("Can't both set and unset same flag.\n", stderr);
325		exit (1);
326	}
327	if (!(add || rem || set || set_version)) {
328		fputs(_("Must use '-v', =, - or +\n"), stderr);
329		exit (1);
330	}
331	if (verbose)
332		fprintf (stderr, "chattr %s (%s)\n",
333			 E2FSPROGS_VERSION, E2FSPROGS_DATE);
334	for (j = i; j < argc; j++) {
335		err = change_attributes (argv[j]);
336		if (err)
337			retval = 1;
338	}
339	exit(retval);
340}
341