blob: 618d8c440a78ddb4a69ef7ed45a32a1629ce5834 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mike Frysingerd89e6292005-04-24 05:07:59 +00002/*
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 <sys/types.h>
23#include <dirent.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <unistd.h>
28#include <string.h>
29#include <errno.h>
30#include <sys/param.h>
31#include <sys/stat.h>
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000032#include "ext2fs/ext2_fs.h"
Mike Frysingerd89e6292005-04-24 05:07:59 +000033
34#ifdef __GNUC__
Mike Frysingerea338ff2005-05-07 07:17:43 +000035# define EXT2FS_ATTR(x) __attribute__(x)
Mike Frysingerd89e6292005-04-24 05:07:59 +000036#else
Mike Frysingerea338ff2005-05-07 07:17:43 +000037# define EXT2FS_ATTR(x)
Mike Frysingerd89e6292005-04-24 05:07:59 +000038#endif
39
Mike Frysingerd89e6292005-04-24 05:07:59 +000040#include "e2fsbb.h"
41#include "e2p/e2p.h"
42
Mike Frysingerd2a64d22005-04-25 04:10:35 +000043#define OPT_ADD 1
44#define OPT_REM 2
45#define OPT_SET 4
46#define OPT_SET_VER 8
47static int flags;
48static int recursive;
Mike Frysingerd89e6292005-04-24 05:07:59 +000049
50static unsigned long version;
51
Mike Frysingerd89e6292005-04-24 05:07:59 +000052static unsigned long af;
53static unsigned long rf;
54static unsigned long sf;
55
Mike Frysingerd89e6292005-04-24 05:07:59 +000056struct flags_char {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000057 unsigned long flag;
58 char optchar;
Mike Frysingerd89e6292005-04-24 05:07:59 +000059};
60
61static const struct flags_char flags_array[] = {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000062 { EXT2_NOATIME_FL, 'A' },
63 { EXT2_SYNC_FL, 'S' },
64 { EXT2_DIRSYNC_FL, 'D' },
65 { EXT2_APPEND_FL, 'a' },
66 { EXT2_COMPR_FL, 'c' },
67 { EXT2_NODUMP_FL, 'd' },
68 { EXT2_IMMUTABLE_FL, 'i' },
Mike Frysingerd89e6292005-04-24 05:07:59 +000069 { EXT3_JOURNAL_DATA_FL, 'j' },
Mike Frysingerd2a64d22005-04-25 04:10:35 +000070 { EXT2_SECRM_FL, 's' },
71 { EXT2_UNRM_FL, 'u' },
72 { EXT2_NOTAIL_FL, 't' },
73 { EXT2_TOPDIR_FL, 'T' },
Mike Frysingerd89e6292005-04-24 05:07:59 +000074 { 0, 0 }
75};
76
77static unsigned long get_flag(char c)
78{
79 const struct flags_char *fp;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000080 for (fp = flags_array; fp->flag; fp++)
Mike Frysingerd89e6292005-04-24 05:07:59 +000081 if (fp->optchar == c)
82 return fp->flag;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000083 bb_show_usage();
Mike Frysingerd89e6292005-04-24 05:07:59 +000084 return 0;
85}
86
Mike Frysingerd2a64d22005-04-25 04:10:35 +000087static int decode_arg(char *arg)
Mike Frysingerd89e6292005-04-24 05:07:59 +000088{
Mike Frysingerd2a64d22005-04-25 04:10:35 +000089 unsigned long *fl;
90 char opt = *arg++;
Mike Frysingerd89e6292005-04-24 05:07:59 +000091
Mike Frysingerd2a64d22005-04-25 04:10:35 +000092 if (opt == '-') {
93 flags |= OPT_REM;
94 fl = &rf;
95 } else if (opt == '+') {
96 flags |= OPT_ADD;
97 fl = &af;
98 } else if (opt == '=') {
99 flags |= OPT_SET;
100 fl = &sf;
101 } else
Mike Frysingerd89e6292005-04-24 05:07:59 +0000102 return EOF;
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000103
104 for (; *arg ; ++arg)
105 (*fl) |= get_flag(*arg);
106
Mike Frysingerd89e6292005-04-24 05:07:59 +0000107 return 1;
108}
109
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000110static int chattr_dir_proc(const char *, struct dirent *, void *);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000111
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000112static void change_attributes(const char * name)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000113{
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000114 unsigned long fsflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000115 STRUCT_STAT st;
116
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000117 if (LSTAT(name, &st) == -1) {
Mike Frysingerea338ff2005-05-07 07:17:43 +0000118 bb_error_msg("stat %s failed", name);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000119 return;
120 }
121 if (S_ISLNK(st.st_mode) && recursive)
122 return;
123
124 /* Don't try to open device files, fifos etc. We probably
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000125 * ought to display an error if the file was explicitly given
126 * on the command line (whether or not recursive was
127 * requested). */
128 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
Mike Frysingerd89e6292005-04-24 05:07:59 +0000129 return;
130
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000131 if (flags & OPT_SET_VER)
132 if (fsetversion(name, version) == -1)
Mike Frysingerea338ff2005-05-07 07:17:43 +0000133 bb_error_msg("setting version on %s", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000134
135 if (flags & OPT_SET) {
136 fsflags = sf;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000137 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000138 if (fgetflags(name, &fsflags) == -1) {
Mike Frysingerea338ff2005-05-07 07:17:43 +0000139 bb_error_msg("reading flags on %s", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000140 goto skip_setflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000141 }
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000142 if (flags & OPT_REM)
143 fsflags &= ~rf;
144 if (flags & OPT_ADD)
145 fsflags |= af;
146 if (!S_ISDIR(st.st_mode))
147 fsflags &= ~EXT2_DIRSYNC_FL;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000148 }
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000149 if (fsetflags(name, fsflags) == -1)
Mike Frysingerea338ff2005-05-07 07:17:43 +0000150 bb_error_msg("setting flags on %s", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000151
152skip_setflags:
Mike Frysingerd89e6292005-04-24 05:07:59 +0000153 if (S_ISDIR(st.st_mode) && recursive)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000154 iterate_on_dir(name, chattr_dir_proc, NULL);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000155}
156
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000157static int chattr_dir_proc(const char *dir_name, struct dirent *de,
158 void *private EXT2FS_ATTR((unused)))
Mike Frysingerd89e6292005-04-24 05:07:59 +0000159{
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000160 /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
161 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
162 (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
Mike Frysingerea338ff2005-05-07 07:17:43 +0000163 char *path = concat_subpath_file(dir_name, de->d_name);
164 if (path) {
165 change_attributes(path);
166 free(path);
167 }
Mike Frysingerd89e6292005-04-24 05:07:59 +0000168 }
169 return 0;
170}
171
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000172int chattr_main(int argc, char **argv)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000173{
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000174 int i;
175 char *arg;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000176
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000177 /* parse the args */
178 for (i = 1; i < argc; ++i) {
179 arg = argv[i];
180
181 /* take care of -R and -v <version> */
182 if (arg[0] == '-') {
183 if (arg[1] == 'R' && arg[2] == '\0') {
184 recursive = 1;
185 continue;
186 } else if (arg[1] == 'v' && arg[2] == '\0') {
187 char *tmp;
188 ++i;
189 if (i >= argc)
190 bb_show_usage();
191 version = strtol(argv[i], &tmp, 0);
Mike Frysingerea338ff2005-05-07 07:17:43 +0000192 if (*tmp)
193 bb_error_msg_and_die("bad version '%s'", arg);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000194 flags |= OPT_SET_VER;
195 continue;
196 }
197 }
198
199 if (decode_arg(arg) == EOF)
200 break;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000201 }
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000202
203 /* run sanity checks on all the arguments given us */
Mike Frysingerd89e6292005-04-24 05:07:59 +0000204 if (i >= argc)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000205 bb_show_usage();
Mike Frysingerea338ff2005-05-07 07:17:43 +0000206 if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM)))
207 bb_error_msg_and_die("= is incompatible with - and +");
208 if ((rf & af) != 0)
209 bb_error_msg_and_die("Can't set and unset a flag");
210 if (!flags)
211 bb_error_msg_and_die("Must use '-v', =, - or +");
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000212
213 /* now run chattr on all the files passed to us */
214 while (i < argc)
215 change_attributes(argv[i++]);
216
217 return EXIT_SUCCESS;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000218}