chattr.c revision 19c78dc07fce2d6f39b5e541562afc3ca1ea38ff
13839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
23839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * chattr.c		- Change file attributes on an ext2 file system
33839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o *
43839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Copyright (C) 1993, 1994  Remy Card <card@masi.ibp.fr>
53839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o *                           Laboratoire MASI, Institut Blaise Pascal
63839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o *                           Universite Pierre et Marie Curie (Paris VI)
73839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o *
83839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * This file can be redistributed under the terms of the GNU General
93839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * Public License
103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o/*
133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * History:
143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * 93/10/30	- Creation
153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * 93/11/13	- Replace stat() calls by lstat() to avoid loops
163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o * 94/02/27	- Integrated in Ted's distribution
173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
19a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <sys/types.h>
203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <dirent.h>
213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <fcntl.h>
22a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_GETOPT_H
233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <getopt.h>
24a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdio.h>
263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdlib.h>
273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <unistd.h>
2819c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
29a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_ERRNO_H
30a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <errno.h>
31a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/param.h>
333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/stat.h>
343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <linux/ext2_fs.h>
353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "et/com_err.h"
373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "e2p/e2p.h"
383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "../version.h"
403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oconst char * program_name = "chattr";
423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint add = 0;
443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint rem = 0;
453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint set = 0;
463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint set_version = 0;
473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ounsigned long version;
493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint recursive = 0;
513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'oint verbose = 0;
523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ounsigned long af;
543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ounsigned long rf;
553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ounsigned long sf;
563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void volatile fatal_error (const char * fmt_string, int errcode)
583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	fprintf (stderr, fmt_string, program_name);
603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	exit (errcode);
613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
63f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#define usage() fatal_error ("usage: %s [-RV] [-+=acdisSu] [-v version] files...\n", \
643839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			     1)
653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic int decode_arg (int * i, int argc, char ** argv)
673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	char * p;
693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	char * tmp;
703839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	switch (argv[*i][0])
723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	case '-':
743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		for (p = &argv[*i][1]; *p; p++)
753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			switch (*p)
763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			{
773839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'R':
783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				recursive = 1;
793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'S':
813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rf |= EXT2_SYNC_FL;
823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rem = 1;
833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'V':
853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				verbose = 1;
863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
87f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_APPEND_FL
88f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'a':
89f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				rf |= EXT2_APPEND_FL;
90f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				rem = 1;
91f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
92f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
935c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o#ifdef EXT2_NOATIME_FL
945c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o			case 'A':
955c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				rf |= EXT2_NOATIME_FL;
965c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				rem = 1;
975c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				break;
985c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o#endif
993839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'c':
1003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rf |= EXT2_COMPR_FL;
1013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rem = 1;
1023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
103f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_NODUMP_FL
104f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'd':
105f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				rf |= EXT2_NODUMP_FL;
106f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				rem = 1;
107f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
108f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
109f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_IMMUTABLE_FL
110f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'i':
111f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				rf |= EXT2_IMMUTABLE_FL;
112f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				rem = 1;
113f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
114f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
1153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 's':
1163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rf |= EXT2_SECRM_FL;
1173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rem = 1;
1183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
1193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'u':
1203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rf |= EXT2_UNRM_FL;
1213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				rem = 1;
1223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
1233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'v':
1241e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o				(*i)++;
1253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				if (*i >= argc)
1263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					usage ();
1273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				version = strtol (argv[*i], &tmp, 0);
1283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				if (*tmp)
1293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				{
1303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					com_err (program_name, 0,
1313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o						 "bad version - %s\n", argv[*i]);
1323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					usage ();
1333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				}
1343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				set_version = 1;
1353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
1363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			default:
1373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				fprintf (stderr, "%s: Unrecognized argument: %c\n",
1383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					 program_name, *p);
1393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				usage ();
1403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
1413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
1423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	case '+':
1433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		add = 1;
1443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		for (p = &argv[*i][1]; *p; p++)
1453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			switch (*p)
1463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			{
1473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'S':
1483839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				af |= EXT2_SYNC_FL;
1493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
150f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_APPEND_FL
151f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'a':
152f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				af |= EXT2_APPEND_FL;
153f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
154f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
1555c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o#ifdef EXT2_NOATIME_FL
1565c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o			case 'A':
1575c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				af |= EXT2_NOATIME_FL;
1585c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				break;
1595c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o#endif
1603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'c':
1613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				af |= EXT2_COMPR_FL;
1623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
163f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_NODUMP_FL
164f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'd':
165f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				af |= EXT2_NODUMP_FL;
166f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
167f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
168f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_IMMUTABLE_FL
169f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'i':
170f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				af |= EXT2_IMMUTABLE_FL;
171f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
172f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
1733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 's':
1743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				af |= EXT2_SECRM_FL;
1753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
1763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'u':
1773839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				af |= EXT2_UNRM_FL;
1783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
1793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			default:
1803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				usage ();
1813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
1823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
1833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	case '=':
1843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		set = 1;
1853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		for (p = &argv[*i][1]; *p; p++)
1863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			switch (*p)
1873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			{
1883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'S':
1893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				sf |= EXT2_SYNC_FL;
1903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
191f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_APPEND_FL
192f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'a':
193f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				sf |= EXT2_APPEND_FL;
194f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
195f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
1965c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o#ifdef EXT2_NOATIME_FL
1975c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o			case 'A':
1985c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				sf |= EXT2_NOATIME_FL;
1995c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o				break;
2005c576477ccb2f0ca8c5d5af2e2354fd8eeff1589Theodore Ts'o#endif
2013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'c':
2023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				sf |= EXT2_COMPR_FL;
2033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
204f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_NODUMP_FL
205f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'd':
206f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				sf |= EXT2_NODUMP_FL;
207f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
208f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
209f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#ifdef	EXT2_IMMUTABLE_FL
210f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			case 'i':
211f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				sf |= EXT2_IMMUTABLE_FL;
212f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				break;
213f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o#endif
2143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 's':
2153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				sf |= EXT2_SECRM_FL;
2163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
2173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			case 'u':
2183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				sf |= EXT2_UNRM_FL;
2193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				break;
2203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			default:
2213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				usage ();
2223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
2233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
2243839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	default:
2253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return EOF;
2263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
2273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return 1;
2293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic int chattr_dir_proc (const char *, struct dirent *, void *);
2323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic void change_attributes (const char * name)
2343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
2353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	unsigned long flags;
2363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	struct stat st;
2373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (lstat (name, &st) == -1)
2393839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
2403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		com_err (program_name, errno, "while stating %s", name);
2413839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return;
2423839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2433839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (set)
2443839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
2453839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (verbose)
2463839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		{
2473839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			printf ("Flags of %s set as ", name);
248f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			print_flags (stdout, sf, 0);
2493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			printf ("\n");
2503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
2513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (fsetflags (name, sf) == -1)
2523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			perror (name);
2533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	else
2553839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
2563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (fgetflags (name, &flags) == -1)
2573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			com_err (program_name, errno,
2583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			         "while reading flags on %s", name);
2593839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		else
2603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		{
2613839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			if (rem)
2623839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				flags &= ~rf;
2633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			if (add)
2643839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				flags |= af;
2653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			if (verbose)
2663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			{
2673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				printf ("Flags of %s set as ", name);
268f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o				print_flags (stdout, flags, 0);
2693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				printf ("\n");
2703839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
2713839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			if (fsetflags (name, flags) == -1)
2723839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				com_err (program_name, errno,
2733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				         "while setting flags on %s", name);
2743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
2753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (set_version)
2773839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
2783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (verbose)
2793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			printf ("Version of %s set as %lu\n", name, version);
2803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (fsetversion (name, version) == -1)
2813839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			com_err (program_name, errno,
2823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			         "while setting version on %s", name);
2833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (S_ISDIR(st.st_mode) && recursive)
2853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		iterate_on_dir (name, chattr_dir_proc, (void *) NULL);
2863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
2883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic int chattr_dir_proc (const char * dir_name, struct dirent * de, void * private)
2893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
2903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (strcmp (de->d_name, ".") && strcmp (de->d_name, ".."))
2913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
292a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	        char *path;
293a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
294a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
295a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		if (!path)
296a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o			fatal_error("Couldn't allocate path variable "
297a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o				    "in chattr_dir_proc", 1);
2983839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		sprintf (path, "%s/%s", dir_name, de->d_name);
2993839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		change_attributes (path);
300a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		free(path);
3013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
3023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return 0;
3033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
3043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
3053839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ovoid main (int argc, char ** argv)
3063839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
3073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int i, j;
3083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int end_arg = 0;
3093839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
3103839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	fprintf (stderr, "chattr %s, %s for EXT2 FS %s, %s\n",
3113839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		 E2FSPROGS_VERSION, E2FSPROGS_DATE,
3123839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		 EXT2FS_VERSION, EXT2FS_DATE);
3133839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (argc && *argv)
3143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		program_name = *argv;
3153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	i = 1;
3163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	while (i < argc && !end_arg)
3173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
3183839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (decode_arg (&i, argc, argv) == EOF)
3193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			end_arg = 1;
3203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		else
3213839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			i++;
3223839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
3233839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (i >= argc)
3243839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		usage ();
3253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (set && (add || rem))
3263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
3273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		fprintf (stderr, "= is incompatible with - and +\n");
3283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit (1);
3293839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
3301e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o	if (!(add || rem || set || set_version))
3313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
3321e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o		fprintf (stderr, "Must use '-v', =, - or +\n");
3333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit (1);
3343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
3353839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	for (j = i; j < argc; j++)
3363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		change_attributes (argv[j]);
3373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
338