• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "config.h"
24 #include <sys/types.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <string.h>
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include <sys/param.h>
35 #include <sys/stat.h>
36 #include "ext2fs/ext2_fs.h"
37 
38 #ifdef __GNUC__
39 #define EXT2FS_ATTR(x) __attribute__(x)
40 #else
41 #define EXT2FS_ATTR(x)
42 #endif
43 
44 #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 
52 #include "et/com_err.h"
53 #include "e2p/e2p.h"
54 #include "support/nls-enable.h"
55 
56 #include "../version.h"
57 
58 static const char * program_name = "chattr";
59 
60 static int add;
61 static int rem;
62 static int set;
63 static int set_version;
64 
65 static unsigned long version;
66 
67 static int set_project;
68 static unsigned long project;
69 
70 static int recursive;
71 static int verbose;
72 static int silent;
73 
74 static unsigned long af;
75 static unsigned long rf;
76 static unsigned long sf;
77 
78 #ifdef _LFS64_LARGEFILE
79 #define LSTAT		lstat64
80 #define STRUCT_STAT	struct stat64
81 #else
82 #define LSTAT		lstat
83 #define STRUCT_STAT	struct stat
84 #endif
85 
usage(void)86 static void usage(void)
87 {
88 	fprintf(stderr,
89 		_("Usage: %s [-pRVf] [-+=aAcCdDeijPsStTuF] [-v version] files...\n"),
90 		program_name);
91 	exit(1);
92 }
93 
94 struct flags_char {
95 	unsigned long	flag;
96 	char 		optchar;
97 };
98 
99 static const struct flags_char flags_array[] = {
100 	{ EXT2_NOATIME_FL, 'A' },
101 	{ EXT2_SYNC_FL, 'S' },
102 	{ EXT2_DIRSYNC_FL, 'D' },
103 	{ EXT2_APPEND_FL, 'a' },
104 	{ EXT2_COMPR_FL, 'c' },
105 	{ EXT2_NODUMP_FL, 'd' },
106 	{ EXT4_EXTENTS_FL, 'e'},
107 	{ EXT2_IMMUTABLE_FL, 'i' },
108 	{ EXT3_JOURNAL_DATA_FL, 'j' },
109 	{ EXT4_PROJINHERIT_FL, 'P' },
110 	{ EXT2_SECRM_FL, 's' },
111 	{ EXT2_UNRM_FL, 'u' },
112 	{ EXT2_NOTAIL_FL, 't' },
113 	{ EXT2_TOPDIR_FL, 'T' },
114 	{ FS_NOCOW_FL, 'C' },
115 	{ EXT4_CASEFOLD_FL, 'F' },
116 	{ 0, 0 }
117 };
118 
get_flag(char c)119 static unsigned long get_flag(char c)
120 {
121 	const struct flags_char *fp;
122 
123 	for (fp = flags_array; fp->flag != 0; fp++) {
124 		if (fp->optchar == c)
125 			return fp->flag;
126 	}
127 	return 0;
128 }
129 
130 
decode_arg(int * i,int argc,char ** argv)131 static int decode_arg (int * i, int argc, char ** argv)
132 {
133 	char * p;
134 	char * tmp;
135 	unsigned long fl;
136 
137 	switch (argv[*i][0])
138 	{
139 	case '-':
140 		for (p = &argv[*i][1]; *p; p++) {
141 			if (*p == 'R') {
142 				recursive = 1;
143 				continue;
144 			}
145 			if (*p == 'V') {
146 				verbose = 1;
147 				continue;
148 			}
149 			if (*p == 'f') {
150 				silent = 1;
151 				continue;
152 			}
153 			if (*p == 'p') {
154 				(*i)++;
155 				if (*i >= argc)
156 					usage ();
157 				project = strtol (argv[*i], &tmp, 0);
158 				if (*tmp) {
159 					com_err (program_name, 0,
160 						 _("bad project - %s\n"),
161 						 argv[*i]);
162 					usage ();
163 				}
164 				set_project = 1;
165 				continue;
166 			}
167 			if (*p == 'v') {
168 				(*i)++;
169 				if (*i >= argc)
170 					usage ();
171 				version = strtol (argv[*i], &tmp, 0);
172 				if (*tmp) {
173 					com_err (program_name, 0,
174 						 _("bad version - %s\n"),
175 						 argv[*i]);
176 					usage ();
177 				}
178 				set_version = 1;
179 				continue;
180 			}
181 			if ((fl = get_flag(*p)) == 0)
182 				usage();
183 			rf |= fl;
184 			rem = 1;
185 		}
186 		break;
187 	case '+':
188 		add = 1;
189 		for (p = &argv[*i][1]; *p; p++) {
190 			if ((fl = get_flag(*p)) == 0)
191 				usage();
192 			af |= fl;
193 		}
194 		break;
195 	case '=':
196 		set = 1;
197 		for (p = &argv[*i][1]; *p; p++) {
198 			if ((fl = get_flag(*p)) == 0)
199 				usage();
200 			sf |= fl;
201 		}
202 		break;
203 	default:
204 		return EOF;
205 		break;
206 	}
207 	return 1;
208 }
209 
210 static int chattr_dir_proc(const char *, struct dirent *, void *);
211 
change_attributes(const char * name)212 static int change_attributes(const char * name)
213 {
214 	unsigned long flags;
215 	STRUCT_STAT	st;
216 
217 	if (LSTAT (name, &st) == -1) {
218 		if (!silent)
219 			com_err (program_name, errno,
220 				 _("while trying to stat %s"), name);
221 		return -1;
222 	}
223 
224 	if (fgetflags(name, &flags) == -1) {
225 		if (!silent)
226 			com_err(program_name, errno,
227 					_("while reading flags on %s"), name);
228 		return -1;
229 	}
230 	if (set) {
231 		if (verbose) {
232 			printf (_("Flags of %s set as "), name);
233 			print_flags (stdout, sf, 0);
234 			printf ("\n");
235 		}
236 		if (fsetflags (name, sf) == -1)
237 			perror (name);
238 	} else {
239 		if (rem)
240 			flags &= ~rf;
241 		if (add)
242 			flags |= af;
243 		if (verbose) {
244 			printf(_("Flags of %s set as "), name);
245 			print_flags(stdout, flags, 0);
246 			printf("\n");
247 		}
248 		if (!S_ISDIR(st.st_mode))
249 			flags &= ~EXT2_DIRSYNC_FL;
250 		if (fsetflags(name, flags) == -1) {
251 			if (!silent) {
252 				com_err(program_name, errno,
253 						_("while setting flags on %s"),
254 						name);
255 			}
256 			return -1;
257 		}
258 	}
259 	if (set_version) {
260 		if (verbose)
261 			printf (_("Version of %s set as %lu\n"), name, version);
262 		if (fsetversion (name, version) == -1) {
263 			if (!silent)
264 				com_err (program_name, errno,
265 					 _("while setting version on %s"),
266 					 name);
267 			return -1;
268 		}
269 	}
270 	if (set_project) {
271 		if (verbose)
272 			printf (_("Project of %s set as %lu\n"), name, project);
273 		if (fsetproject (name, project) == -1) {
274 			if (!silent)
275 				com_err (program_name, errno,
276 					 _("while setting project on %s"),
277 					 name);
278 			return -1;
279 		}
280 
281 	}
282 	if (S_ISDIR(st.st_mode) && recursive)
283 		return iterate_on_dir (name, chattr_dir_proc, NULL);
284 	return 0;
285 }
286 
chattr_dir_proc(const char * dir_name,struct dirent * de,void * private EXT2FS_ATTR ((unused)))287 static int chattr_dir_proc (const char * dir_name, struct dirent * de,
288 			    void * private EXT2FS_ATTR((unused)))
289 {
290 	int ret = 0;
291 
292 	if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
293 	        char *path;
294 
295 		path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
296 		if (!path) {
297 			fprintf(stderr, "%s",
298 				_("Couldn't allocate path variable "
299 				  "in chattr_dir_proc"));
300 			return -1;
301 		}
302 		sprintf(path, "%s/%s", dir_name, de->d_name);
303 		ret = change_attributes(path);
304 		free(path);
305 	}
306 	return ret;
307 }
308 
main(int argc,char ** argv)309 int main (int argc, char ** argv)
310 {
311 	int i, j;
312 	int end_arg = 0;
313 	int err, retval = 0;
314 
315 #ifdef ENABLE_NLS
316 	setlocale(LC_MESSAGES, "");
317 	setlocale(LC_CTYPE, "");
318 	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
319 	textdomain(NLS_CAT_NAME);
320 	set_com_err_gettext(gettext);
321 #endif
322 	if (argc && *argv)
323 		program_name = *argv;
324 	i = 1;
325 	while (i < argc && !end_arg) {
326 		/* '--' arg should end option processing */
327 		if (strcmp(argv[i], "--") == 0) {
328 			i++;
329 			end_arg = 1;
330 		} else if (decode_arg (&i, argc, argv) == EOF)
331 			end_arg = 1;
332 		else
333 			i++;
334 	}
335 	if (i >= argc)
336 		usage ();
337 	if (set && (add || rem)) {
338 		fputs(_("= is incompatible with - and +\n"), stderr);
339 		exit (1);
340 	}
341 	if ((rf & af) != 0) {
342 		fputs("Can't both set and unset same flag.\n", stderr);
343 		exit (1);
344 	}
345 	if (!(add || rem || set || set_version || set_project )) {
346 		fputs(_("Must use '-v', =, - or +\n"), stderr);
347 		exit (1);
348 	}
349 	if (verbose)
350 		fprintf (stderr, "chattr %s (%s)\n",
351 			 E2FSPROGS_VERSION, E2FSPROGS_DATE);
352 	for (j = i; j < argc; j++) {
353 		err = change_attributes (argv[j]);
354 		if (err)
355 			retval = 1;
356 	}
357 	exit(retval);
358 }
359