blob: 1b7942e1f675be1b5fb38e5246dfa8292aa71c38 [file] [log] [blame]
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +00001/* vi: set sw=4 ts=4: */
2/*
3 * chattr.c - Change file attributes on an ext2 file system
4 *
5 * Copyright (C) 1993, 1994 Remy Card <card@masi.ibp.fr>
6 * Laboratoire MASI, Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
8 *
9 * This file can be redistributed under the terms of the GNU General
10 * Public License
11 */
12
13/*
14 * History:
15 * 93/10/30 - Creation
16 * 93/11/13 - Replace stat() calls by lstat() to avoid loops
17 * 94/02/27 - Integrated in Ted's distribution
18 * 98/12/29 - Ignore symlinks when working recursively (G M Sipe)
19 * 98/12/29 - Display version info only when -V specified (G M Sipe)
20 */
21
22#include "busybox.h"
23#include "e2fs_lib.h"
24
25#define OPT_ADD 1
26#define OPT_REM 2
27#define OPT_SET 4
28#define OPT_SET_VER 8
29static int flags;
30static int recursive;
31
32static unsigned long version;
33
34static unsigned long af;
35static unsigned long rf;
36static unsigned long sf;
37
38struct flags_char {
39 unsigned long flag;
40 char optchar;
41};
42
43static const struct flags_char flags_array[] = {
44 { EXT2_NOATIME_FL, 'A' },
45 { EXT2_SYNC_FL, 'S' },
46 { EXT2_DIRSYNC_FL, 'D' },
47 { EXT2_APPEND_FL, 'a' },
48 { EXT2_COMPR_FL, 'c' },
49 { EXT2_NODUMP_FL, 'd' },
50 { EXT2_IMMUTABLE_FL, 'i' },
51 { EXT3_JOURNAL_DATA_FL, 'j' },
52 { EXT2_SECRM_FL, 's' },
53 { EXT2_UNRM_FL, 'u' },
54 { EXT2_NOTAIL_FL, 't' },
55 { EXT2_TOPDIR_FL, 'T' },
56 { 0, 0 }
57};
58
59static unsigned long get_flag(char c)
60{
61 const struct flags_char *fp;
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000062 for (fp = flags_array; fp->optchar; fp++)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000063 if (fp->optchar == c)
64 return fp->flag;
65 bb_show_usage();
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000066}
67
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000068static int decode_arg(const char *arg)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000069{
70 unsigned long *fl;
71 char opt = *arg++;
72
73 if (opt == '-') {
74 flags |= OPT_REM;
75 fl = &rf;
76 } else if (opt == '+') {
77 flags |= OPT_ADD;
78 fl = &af;
79 } else if (opt == '=') {
80 flags |= OPT_SET;
81 fl = &sf;
82 } else
83 return 0;
84
85 while (*arg)
86 *fl |= get_flag(*arg++);
87
88 return 1;
89}
90
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000091static void change_attributes(const char *name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +000092
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +000093static int chattr_dir_proc(const char *dir_name, struct dirent *de,
94 void *private ATTRIBUTE_UNUSED)
95{
96 char *path = concat_subpath_file(dir_name, de->d_name);
97 /* path is NULL if de->d_name is "." or "..", else... */
98 if (path) {
99 change_attributes(path);
100 free(path);
101 }
102 return 0;
103}
104
105static void change_attributes(const char *name)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000106{
107 unsigned long fsflags;
108 struct stat st;
109
110 if (lstat(name, &st) == -1) {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000111 bb_perror_msg("stat %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000112 return;
113 }
114 if (S_ISLNK(st.st_mode) && recursive)
115 return;
116
117 /* Don't try to open device files, fifos etc. We probably
118 * ought to display an error if the file was explicitly given
119 * on the command line (whether or not recursive was
120 * requested). */
121 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
122 return;
123
124 if (flags & OPT_SET_VER)
125 if (fsetversion(name, version) == -1)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000126 bb_perror_msg("setting version on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000127
128 if (flags & OPT_SET) {
129 fsflags = sf;
130 } else {
131 if (fgetflags(name, &fsflags) == -1) {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000132 bb_perror_msg("reading flags on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000133 goto skip_setflags;
134 }
135 if (flags & OPT_REM)
136 fsflags &= ~rf;
137 if (flags & OPT_ADD)
138 fsflags |= af;
139 if (!S_ISDIR(st.st_mode))
140 fsflags &= ~EXT2_DIRSYNC_FL;
141 }
142 if (fsetflags(name, fsflags) == -1)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000143 bb_perror_msg("setting flags on %s", name);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000144
145 skip_setflags:
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000146 if (recursive && S_ISDIR(st.st_mode))
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000147 iterate_on_dir(name, chattr_dir_proc, NULL);
148}
149
Denis Vlasenko06af2162007-02-03 17:28:39 +0000150int chattr_main(int argc, char **argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000151int chattr_main(int argc, char **argv)
152{
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000153 char *arg;
154
155 /* parse the args */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000156 while ((arg = *++argv)) {
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000157 /* take care of -R and -v <version> */
158 if (arg[0] == '-') {
159 if (arg[1] == 'R' && arg[2] == '\0') {
160 recursive = 1;
161 continue;
162 }
163 if (arg[1] == 'v' && arg[2] == '\0') {
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000164 if (!*++argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000165 bb_show_usage();
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000166 version = xatoul(*argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000167 flags |= OPT_SET_VER;
168 continue;
169 }
170 }
171
172 if (!decode_arg(arg))
173 break;
174 }
175
176 /* run sanity checks on all the arguments given us */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000177 if (!*argv)
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000178 bb_show_usage();
179 if ((flags & OPT_SET) && (flags & (OPT_ADD|OPT_REM)))
180 bb_error_msg_and_die("= is incompatible with - and +");
181 if (rf & af)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000182 bb_error_msg_and_die("can't set and unset a flag");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000183 if (!flags)
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000184 bb_error_msg_and_die("must use '-v', =, - or +");
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000185
186 /* now run chattr on all the files passed to us */
Denis Vlasenko5dd7ef02006-12-26 03:36:28 +0000187 do change_attributes(*argv); while (*++argv);
Denis Vlasenkoc4f623e2006-12-26 01:30:59 +0000188
189 return EXIT_SUCCESS;
190}