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
17a88fa0c0135307288562d4922438ae52fdbad855Theodore Ts'o * 98/12/29	- Ignore symlinks when working recursively (G M Sipe)
18a88fa0c0135307288562d4922438ae52fdbad855Theodore Ts'o * 98/12/29	- Display version info only when -V specified (G M Sipe)
193839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o */
203839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
21fff18b4e76d3757a78e5583010bbce3772461f4dTheodore Ts'o#define _LARGEFILE64_SOURCE
22fff18b4e76d3757a78e5583010bbce3772461f4dTheodore Ts'o
23a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <sys/types.h>
243839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <dirent.h>
253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <fcntl.h>
263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdio.h>
273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <stdlib.h>
283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <unistd.h>
2919c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o#include <string.h>
30a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#ifdef HAVE_ERRNO_H
31a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#include <errno.h>
32a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o#endif
333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/param.h>
343839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include <sys/stat.h>
3554c637d4d29af3e6365779f8b12976abe95a4753Theodore Ts'o#include "ext2fs/ext2_fs.h"
363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
37544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o#ifdef __GNUC__
38544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o#define EXT2FS_ATTR(x) __attribute__(x)
39544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o#else
40544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o#define EXT2FS_ATTR(x)
41544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o#endif
42544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o
4336caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o#ifndef S_ISLNK			/* So we can compile even with gcc-warn */
4436caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o# ifdef __S_IFLNK
4536caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o#  define S_ISLNK(mode)	 __S_ISTYPE((mode), __S_IFLNK)
4636caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o# else
4736caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o#  define S_ISLNK(mode)  0
4836caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o# endif
4936caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o#endif
5036caf25f8d61eb8ffddc9895463bce5807e96808Theodore Ts'o
513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "et/com_err.h"
523839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "e2p/e2p.h"
533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o#include "../version.h"
55d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#include "nls-enable.h"
563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
57e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'ostatic const char * program_name = "chattr";
583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
599a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic int add;
609a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic int rem;
619a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic int set;
629a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic int set_version;
633839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
64e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'ostatic unsigned long version;
653839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
669a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic int recursive;
679a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic int verbose;
68e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'ostatic int silent;
693839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
70e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'ostatic unsigned long af;
71e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'ostatic unsigned long rf;
72e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'ostatic unsigned long sf;
733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
74b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#ifdef _LFS64_LARGEFILE
75b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#define LSTAT		lstat64
76b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#define STRUCT_STAT	struct stat64
77b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#else
78b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#define LSTAT		lstat
79b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#define STRUCT_STAT	struct stat
80b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o#endif
81b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o
82642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'ostatic void usage(void)
833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
84e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	fprintf(stderr,
85e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall		_("Usage: %s [-RVf] [-+=AaCcDdeijsSu] [-v version] files...\n"),
86642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o		program_name);
87642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o	exit(1);
883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
909a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostruct flags_char {
919a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	unsigned long	flag;
929a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	char 		optchar;
939a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o};
949a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o
959a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic const struct flags_char flags_array[] = {
969a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_NOATIME_FL, 'A' },
979a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_SYNC_FL, 'S' },
9888372d5c4b2ebd0405446b42de65bf2b0ebb2408Theodore Ts'o	{ EXT2_DIRSYNC_FL, 'D' },
999a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_APPEND_FL, 'a' },
1009a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_COMPR_FL, 'c' },
1019a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_NODUMP_FL, 'd' },
1027c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V	{ EXT4_EXTENTS_FL, 'e'},
1039a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_IMMUTABLE_FL, 'i' },
1049a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT3_JOURNAL_DATA_FL, 'j' },
1059a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_SECRM_FL, 's' },
1069a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ EXT2_UNRM_FL, 'u' },
107b3f5b4c22939daba86465d05842bce76053bc3cfTheodore Ts'o	{ EXT2_NOTAIL_FL, 't' },
10815f9011add4013d32ea2f7ca7dbf860f95b8ec53Theodore Ts'o	{ EXT2_TOPDIR_FL, 'T' },
109e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	{ FS_NOCOW_FL, 'C' },
1109a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	{ 0, 0 }
1119a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o};
1129a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o
1139a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'ostatic unsigned long get_flag(char c)
1149a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o{
1159a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	const struct flags_char *fp;
116efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o
1179a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	for (fp = flags_array; fp->flag != 0; fp++) {
1189a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		if (fp->optchar == c)
1199a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			return fp->flag;
1209a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	}
1219a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	return 0;
1229a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o}
1239a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o
1249a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o
1253839e65723771b85975f4263102dd3ceec4523cTheodore Ts'ostatic int decode_arg (int * i, int argc, char ** argv)
1263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1273839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	char * p;
1283839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	char * tmp;
1299a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o	unsigned long fl;
1303839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1313839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	switch (argv[*i][0])
1323839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	{
1333839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	case '-':
1349a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		for (p = &argv[*i][1]; *p; p++) {
1359a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			if (*p == 'R') {
1363839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				recursive = 1;
1379a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				continue;
1389a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			}
1399a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			if (*p == 'V') {
1403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				verbose = 1;
1419a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				continue;
1429a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			}
143e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			if (*p == 'f') {
144e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o				silent = 1;
145e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o				continue;
146e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			}
1479a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			if (*p == 'v') {
1481e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o				(*i)++;
1493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				if (*i >= argc)
1503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					usage ();
1513839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				version = strtol (argv[*i], &tmp, 0);
1529a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				if (*tmp) {
1533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					com_err (program_name, 0,
154e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o						 _("bad version - %s\n"),
1559a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o						 argv[*i]);
1563839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o					usage ();
1573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				}
1583839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o				set_version = 1;
1599a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				continue;
1603839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			}
1619a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			if ((fl = get_flag(*p)) == 0)
1629a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				usage();
1639a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			rf |= fl;
1649a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			rem = 1;
1659a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		}
1663839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
1673839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	case '+':
1683839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		add = 1;
1699a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		for (p = &argv[*i][1]; *p; p++) {
1709a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			if ((fl = get_flag(*p)) == 0)
1719a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				usage();
1729a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			af |= fl;
1739a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		}
1743839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
1753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	case '=':
1763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		set = 1;
1779a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		for (p = &argv[*i][1]; *p; p++) {
1789a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			if ((fl = get_flag(*p)) == 0)
1799a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o				usage();
1809a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o			sf |= fl;
1819a7188496851014f62592077b2ec603cc2c300a6Theodore Ts'o		}
1823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
1833839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	default:
1843839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		return EOF;
1853839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		break;
1863839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
1873839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	return 1;
1883839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
1893839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1902d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'ostatic int chattr_dir_proc(const char *, struct dirent *, void *);
1913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
1922d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'ostatic int change_attributes(const char * name)
1933839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
1943839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	unsigned long flags;
195b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o	STRUCT_STAT	st;
1963839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
197b705640ae162093d40fc9f77e716bec891561ffeTheodore Ts'o	if (LSTAT (name, &st) == -1) {
198e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		if (!silent)
199e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			com_err (program_name, errno,
200e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o				 _("while trying to stat %s"), name);
201e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		return -1;
2023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
203e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o
2047c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V	if (fgetflags(name, &flags) == -1) {
2057c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		if (!silent)
2067c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			com_err(program_name, errno,
2077c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V					_("while reading flags on %s"), name);
2087c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		return -1;
2097c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V	}
210e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	if (set) {
211e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o		if (verbose) {
212d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			printf (_("Flags of %s set as "), name);
213f3db3566b5e1342e49dffc5ec3f418a838584194Theodore Ts'o			print_flags (stdout, sf, 0);
2143839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			printf ("\n");
2153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
2163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (fsetflags (name, sf) == -1)
2173839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			perror (name);
218e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	} else {
2197c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		if (rem)
2207c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			flags &= ~rf;
2217c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		if (add)
2227c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			flags |= af;
2237c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		if (verbose) {
2247c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			printf(_("Flags of %s set as "), name);
2257c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			print_flags(stdout, flags, 0);
2267c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			printf("\n");
2277c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		}
2287c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		if (!S_ISDIR(st.st_mode))
2297c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			flags &= ~EXT2_DIRSYNC_FL;
2307c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V		if (fsetflags(name, flags) == -1) {
2317c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			if (!silent) {
2327c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V				com_err(program_name, errno,
233e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o						_("while setting flags on %s"),
234e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o						name);
235e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			}
2367c8da6e3768bc7cc7ffde72b4f87fd2ec3c7c7e7Aneesh Kumar K.V			return -1;
2373839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		}
2383839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
239e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	if (set_version) {
2403839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		if (verbose)
241d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o			printf (_("Version of %s set as %lu\n"), name, version);
242e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		if (fsetversion (name, version) == -1) {
243e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			if (!silent)
244e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o				com_err (program_name, errno,
245e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o					 _("while setting version on %s"),
246e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o					 name);
247e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			return -1;
248e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		}
2493839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
2503839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (S_ISDIR(st.st_mode) && recursive)
251e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		return iterate_on_dir (name, chattr_dir_proc, NULL);
252e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	return 0;
2533839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2543839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
255e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'ostatic int chattr_dir_proc (const char * dir_name, struct dirent * de,
256544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o			    void * private EXT2FS_ATTR((unused)))
2573839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
258e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	int ret = 0;
259e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o
260e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	if (strcmp (de->d_name, ".") && strcmp (de->d_name, "..")) {
261a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o	        char *path;
262a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o
263a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		path = malloc(strlen (dir_name) + 1 + strlen (de->d_name) + 1);
264642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o		if (!path) {
265e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall			fprintf(stderr, "%s",
266e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall				_("Couldn't allocate path variable "
267e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall				  "in chattr_dir_proc"));
268e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			return -1;
269642935c082ca22e1186fc9926fe06e4207d5ab56Theodore Ts'o		}
270e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		sprintf(path, "%s/%s", dir_name, de->d_name);
2712d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'o		ret = change_attributes(path);
272a418d3ad819323f871005d253f7f9ac378e78ba5Theodore Ts'o		free(path);
2733839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
274e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	return ret;
2753839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
2763839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
27700e5433eb5e9f70f485968b809fdcf297d7fe7b9Theodore Ts'oint main (int argc, char ** argv)
2783839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o{
2793839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int i, j;
2803839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	int end_arg = 0;
281e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	int err, retval = 0;
2823839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o
283d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#ifdef ENABLE_NLS
284d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	setlocale(LC_MESSAGES, "");
28514308a5398984842e808faa3ff2dd6a1c52d90bdTheodore Ts'o	setlocale(LC_CTYPE, "");
286d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
287d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o	textdomain(NLS_CAT_NAME);
288e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall	set_com_err_gettext(gettext);
289d9c56d3ca0bee11e3446ff7e12e3124d28e298a7Theodore Ts'o#endif
2903839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (argc && *argv)
2913839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		program_name = *argv;
2923839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	i = 1;
293e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	while (i < argc && !end_arg) {
2942293a4d5deb30b02d90c4d0aee516e8280092a58Theodore Ts'o		/* '--' arg should end option processing */
2952293a4d5deb30b02d90c4d0aee516e8280092a58Theodore Ts'o		if (strcmp(argv[i], "--") == 0) {
2962293a4d5deb30b02d90c4d0aee516e8280092a58Theodore Ts'o			i++;
2972293a4d5deb30b02d90c4d0aee516e8280092a58Theodore Ts'o			end_arg = 1;
2982293a4d5deb30b02d90c4d0aee516e8280092a58Theodore Ts'o		} else if (decode_arg (&i, argc, argv) == EOF)
2993839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			end_arg = 1;
3003839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		else
3013839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o			i++;
3023839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
3033839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	if (i >= argc)
3043839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		usage ();
305e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	if (set && (add || rem)) {
306544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o		fputs(_("= is incompatible with - and +\n"), stderr);
3073839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit (1);
3083839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
309e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	if ((rf & af) != 0) {
310544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o		fputs("Can't both set and unset same flag.\n", stderr);
311e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o		exit (1);
312e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	}
313e1a0a3e304229a625c37f0e845c0c9fff117c8c1Theodore Ts'o	if (!(add || rem || set || set_version)) {
314544349270e4c74a6feb971123884a8cf5052a7eeTheodore Ts'o		fputs(_("Must use '-v', =, - or +\n"), stderr);
3153839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o		exit (1);
3163839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o	}
317a88fa0c0135307288562d4922438ae52fdbad855Theodore Ts'o	if (verbose)
3180f8973fb092a40fd0a11b7ec95c09128c9fb8f0cTheodore Ts'o		fprintf (stderr, "chattr %s (%s)\n",
3190f8973fb092a40fd0a11b7ec95c09128c9fb8f0cTheodore Ts'o			 E2FSPROGS_VERSION, E2FSPROGS_DATE);
320e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	for (j = i; j < argc; j++) {
3212d328bb76d2d63bdfdba923b54c28bd686bd8fecTheodore Ts'o		err = change_attributes (argv[j]);
322e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o		if (err)
323e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o			retval = 1;
324e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	}
325e68594d2376afd10e03b440b4bc288508ad13aa2Theodore Ts'o	exit(retval);
3263839e65723771b85975f4263102dd3ceec4523cTheodore Ts'o}
327