source: MondoRescue/branches/stable/mindi-busybox/e2fsprogs/chattr.c@ 821

Last change on this file since 821 was 821, checked in by Bruno Cornec, 18 years ago

Addition of busybox 1.2.1 as a mindi-busybox new package
This should avoid delivering binary files in mindi not built there (Fedora and Debian are quite serious about that)

File size: 5.0 KB
Line 
1/*
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>
31#include "ext2fs/ext2_fs.h"
32
33#ifdef __GNUC__
34# define EXT2FS_ATTR(x) __attribute__(x)
35#else
36# define EXT2FS_ATTR(x)
37#endif
38
39#include "e2fsbb.h"
40#include "e2p/e2p.h"
41
42#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;
48
49static unsigned long version;
50
51static unsigned long af;
52static unsigned long rf;
53static unsigned long sf;
54
55#ifdef CONFIG_LFS
56# define LSTAT lstat64
57# define STRUCT_STAT struct stat64
58#else
59# define LSTAT lstat
60# define STRUCT_STAT struct stat
61#endif
62
63struct flags_char {
64 unsigned long flag;
65 char optchar;
66};
67
68static const struct flags_char flags_array[] = {
69 { 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' },
76 { EXT3_JOURNAL_DATA_FL, 'j' },
77 { EXT2_SECRM_FL, 's' },
78 { EXT2_UNRM_FL, 'u' },
79 { EXT2_NOTAIL_FL, 't' },
80 { EXT2_TOPDIR_FL, 'T' },
81 { 0, 0 }
82};
83
84static unsigned long get_flag(char c)
85{
86 const struct flags_char *fp;
87 for (fp = flags_array; fp->flag; fp++)
88 if (fp->optchar == c)
89 return fp->flag;
90 bb_show_usage();
91 return 0;
92}
93
94static int decode_arg(char *arg)
95{
96 unsigned long *fl;
97 char opt = *arg++;
98
99 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
109 return EOF;
110
111 for (; *arg ; ++arg)
112 (*fl) |= get_flag(*arg);
113
114 return 1;
115}
116
117static int chattr_dir_proc(const char *, struct dirent *, void *);
118
119static void change_attributes(const char * name)
120{
121 unsigned long fsflags;
122 STRUCT_STAT st;
123
124 if (LSTAT(name, &st) == -1) {
125 bb_error_msg("stat %s failed", name);
126 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
132 * 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))
136 return;
137
138 if (flags & OPT_SET_VER)
139 if (fsetversion(name, version) == -1)
140 bb_error_msg("setting version on %s", name);
141
142 if (flags & OPT_SET) {
143 fsflags = sf;
144 } else {
145 if (fgetflags(name, &fsflags) == -1) {
146 bb_error_msg("reading flags on %s", name);
147 goto skip_setflags;
148 }
149 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;
155 }
156 if (fsetflags(name, fsflags) == -1)
157 bb_error_msg("setting flags on %s", name);
158
159skip_setflags:
160 if (S_ISDIR(st.st_mode) && recursive)
161 iterate_on_dir(name, chattr_dir_proc, NULL);
162}
163
164static int chattr_dir_proc(const char *dir_name, struct dirent *de,
165 void *private EXT2FS_ATTR((unused)))
166{
167 /*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'))) {
170 char *path = concat_subpath_file(dir_name, de->d_name);
171 if (path) {
172 change_attributes(path);
173 free(path);
174 }
175 }
176 return 0;
177}
178
179int chattr_main(int argc, char **argv)
180{
181 int i;
182 char *arg;
183
184 /* 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);
199 if (*tmp)
200 bb_error_msg_and_die("bad version '%s'", arg);
201 flags |= OPT_SET_VER;
202 continue;
203 }
204 }
205
206 if (decode_arg(arg) == EOF)
207 break;
208 }
209
210 /* run sanity checks on all the arguments given us */
211 if (i >= argc)
212 bb_show_usage();
213 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 +");
219
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;
225}
Note: See TracBrowser for help on using the repository browser.