blob: c3900382185512167d39c3a04d723a33240c3c5e [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
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#include <sys/types.h>
22#include <dirent.h>
23#include <fcntl.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <string.h>
28#include <errno.h>
29#include <sys/param.h>
30#include <sys/stat.h>
"Vladimir N. Oleynik"083d3f42005-10-10 11:35:17 +000031#include "ext2fs/ext2_fs.h"
Mike Frysingerd89e6292005-04-24 05:07:59 +000032
33#ifdef __GNUC__
Mike Frysingerea338ff2005-05-07 07:17:43 +000034# define EXT2FS_ATTR(x) __attribute__(x)
Mike Frysingerd89e6292005-04-24 05:07:59 +000035#else
Mike Frysingerea338ff2005-05-07 07:17:43 +000036# define EXT2FS_ATTR(x)
Mike Frysingerd89e6292005-04-24 05:07:59 +000037#endif
38
Mike Frysingerd89e6292005-04-24 05:07:59 +000039#include "e2fsbb.h"
40#include "e2p/e2p.h"
41
Mike Frysingerd2a64d22005-04-25 04:10:35 +000042#define OPT_ADD 1
43#define OPT_REM 2
44#define OPT_SET 4
45#define OPT_SET_VER 8
46static int flags;
47static int recursive;
Mike Frysingerd89e6292005-04-24 05:07:59 +000048
49static unsigned long version;
50
Mike Frysingerd89e6292005-04-24 05:07:59 +000051static unsigned long af;
52static unsigned long rf;
53static unsigned long sf;
54
Mike Frysingerd2a64d22005-04-25 04:10:35 +000055#ifdef CONFIG_LFS
56# define LSTAT lstat64
57# define STRUCT_STAT struct stat64
Mike Frysingerd89e6292005-04-24 05:07:59 +000058#else
Mike Frysingerd2a64d22005-04-25 04:10:35 +000059# define LSTAT lstat
60# define STRUCT_STAT struct stat
Mike Frysingerd89e6292005-04-24 05:07:59 +000061#endif
62
63struct flags_char {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000064 unsigned long flag;
65 char optchar;
Mike Frysingerd89e6292005-04-24 05:07:59 +000066};
67
68static const struct flags_char flags_array[] = {
Mike Frysingerd2a64d22005-04-25 04:10:35 +000069 { EXT2_NOATIME_FL, 'A' },
70 { EXT2_SYNC_FL, 'S' },
71 { EXT2_DIRSYNC_FL, 'D' },
72 { EXT2_APPEND_FL, 'a' },
73 { EXT2_COMPR_FL, 'c' },
74 { EXT2_NODUMP_FL, 'd' },
75 { EXT2_IMMUTABLE_FL, 'i' },
Mike Frysingerd89e6292005-04-24 05:07:59 +000076 { EXT3_JOURNAL_DATA_FL, 'j' },
Mike Frysingerd2a64d22005-04-25 04:10:35 +000077 { EXT2_SECRM_FL, 's' },
78 { EXT2_UNRM_FL, 'u' },
79 { EXT2_NOTAIL_FL, 't' },
80 { EXT2_TOPDIR_FL, 'T' },
Mike Frysingerd89e6292005-04-24 05:07:59 +000081 { 0, 0 }
82};
83
84static unsigned long get_flag(char c)
85{
86 const struct flags_char *fp;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000087 for (fp = flags_array; fp->flag; fp++)
Mike Frysingerd89e6292005-04-24 05:07:59 +000088 if (fp->optchar == c)
89 return fp->flag;
Mike Frysingerd2a64d22005-04-25 04:10:35 +000090 bb_show_usage();
Mike Frysingerd89e6292005-04-24 05:07:59 +000091 return 0;
92}
93
Mike Frysingerd2a64d22005-04-25 04:10:35 +000094static int decode_arg(char *arg)
Mike Frysingerd89e6292005-04-24 05:07:59 +000095{
Mike Frysingerd2a64d22005-04-25 04:10:35 +000096 unsigned long *fl;
97 char opt = *arg++;
Mike Frysingerd89e6292005-04-24 05:07:59 +000098
Mike Frysingerd2a64d22005-04-25 04:10:35 +000099 if (opt == '-') {
100 flags |= OPT_REM;
101 fl = &rf;
102 } else if (opt == '+') {
103 flags |= OPT_ADD;
104 fl = &af;
105 } else if (opt == '=') {
106 flags |= OPT_SET;
107 fl = &sf;
108 } else
Mike Frysingerd89e6292005-04-24 05:07:59 +0000109 return EOF;
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000110
111 for (; *arg ; ++arg)
112 (*fl) |= get_flag(*arg);
113
Mike Frysingerd89e6292005-04-24 05:07:59 +0000114 return 1;
115}
116
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000117static int chattr_dir_proc(const char *, struct dirent *, void *);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000118
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000119static void change_attributes(const char * name)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000120{
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000121 unsigned long fsflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000122 STRUCT_STAT st;
123
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000124 if (LSTAT(name, &st) == -1) {
Mike Frysingerea338ff2005-05-07 07:17:43 +0000125 bb_error_msg("stat %s failed", name);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000126 return;
127 }
128 if (S_ISLNK(st.st_mode) && recursive)
129 return;
130
131 /* Don't try to open device files, fifos etc. We probably
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000132 * ought to display an error if the file was explicitly given
133 * on the command line (whether or not recursive was
134 * requested). */
135 if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
Mike Frysingerd89e6292005-04-24 05:07:59 +0000136 return;
137
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000138 if (flags & OPT_SET_VER)
139 if (fsetversion(name, version) == -1)
Mike Frysingerea338ff2005-05-07 07:17:43 +0000140 bb_error_msg("setting version on %s", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000141
142 if (flags & OPT_SET) {
143 fsflags = sf;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000144 } else {
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000145 if (fgetflags(name, &fsflags) == -1) {
Mike Frysingerea338ff2005-05-07 07:17:43 +0000146 bb_error_msg("reading flags on %s", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000147 goto skip_setflags;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000148 }
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000149 if (flags & OPT_REM)
150 fsflags &= ~rf;
151 if (flags & OPT_ADD)
152 fsflags |= af;
153 if (!S_ISDIR(st.st_mode))
154 fsflags &= ~EXT2_DIRSYNC_FL;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000155 }
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000156 if (fsetflags(name, fsflags) == -1)
Mike Frysingerea338ff2005-05-07 07:17:43 +0000157 bb_error_msg("setting flags on %s", name);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000158
159skip_setflags:
Mike Frysingerd89e6292005-04-24 05:07:59 +0000160 if (S_ISDIR(st.st_mode) && recursive)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000161 iterate_on_dir(name, chattr_dir_proc, NULL);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000162}
163
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000164static int chattr_dir_proc(const char *dir_name, struct dirent *de,
165 void *private EXT2FS_ATTR((unused)))
Mike Frysingerd89e6292005-04-24 05:07:59 +0000166{
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000167 /*if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {*/
168 if (de->d_name[0] == '.' && (de->d_name[1] == '\0' || \
169 (de->d_name[1] == '.' && de->d_name[2] == '\0'))) {
Mike Frysingerea338ff2005-05-07 07:17:43 +0000170 char *path = concat_subpath_file(dir_name, de->d_name);
171 if (path) {
172 change_attributes(path);
173 free(path);
174 }
Mike Frysingerd89e6292005-04-24 05:07:59 +0000175 }
176 return 0;
177}
178
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000179int chattr_main(int argc, char **argv)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000180{
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000181 int i;
182 char *arg;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000183
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000184 /* parse the args */
185 for (i = 1; i < argc; ++i) {
186 arg = argv[i];
187
188 /* take care of -R and -v <version> */
189 if (arg[0] == '-') {
190 if (arg[1] == 'R' && arg[2] == '\0') {
191 recursive = 1;
192 continue;
193 } else if (arg[1] == 'v' && arg[2] == '\0') {
194 char *tmp;
195 ++i;
196 if (i >= argc)
197 bb_show_usage();
198 version = strtol(argv[i], &tmp, 0);
Mike Frysingerea338ff2005-05-07 07:17:43 +0000199 if (*tmp)
200 bb_error_msg_and_die("bad version '%s'", arg);
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000201 flags |= OPT_SET_VER;
202 continue;
203 }
204 }
205
206 if (decode_arg(arg) == EOF)
207 break;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000208 }
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000209
210 /* run sanity checks on all the arguments given us */
Mike Frysingerd89e6292005-04-24 05:07:59 +0000211 if (i >= argc)
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000212 bb_show_usage();
Mike Frysingerea338ff2005-05-07 07:17:43 +0000213 if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM)))
214 bb_error_msg_and_die("= is incompatible with - and +");
215 if ((rf & af) != 0)
216 bb_error_msg_and_die("Can't set and unset a flag");
217 if (!flags)
218 bb_error_msg_and_die("Must use '-v', =, - or +");
Mike Frysingerd2a64d22005-04-25 04:10:35 +0000219
220 /* now run chattr on all the files passed to us */
221 while (i < argc)
222 change_attributes(argv[i++]);
223
224 return EXIT_SUCCESS;
Mike Frysingerd89e6292005-04-24 05:07:59 +0000225}