diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-26 03:36:28 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-26 03:36:28 +0000 |
commit | 5dd7ef0f37373e397a7160cb431a32ae57f9f7d9 (patch) | |
tree | d798306c4cc23052caf0d75f82d2f5618610e633 | |
parent | 407b8c0b7001b60f79181d62eb57e810f75e0e27 (diff) | |
download | busybox-w32-5dd7ef0f37373e397a7160cb431a32ae57f9f7d9.tar.gz busybox-w32-5dd7ef0f37373e397a7160cb431a32ae57f9f7d9.tar.bz2 busybox-w32-5dd7ef0f37373e397a7160cb431a32ae57f9f7d9.zip |
chattr: bugfixes and size reduction
-rw-r--r-- | e2fsprogs/chattr.c | 66 | ||||
-rw-r--r-- | include/usage.h | 2 | ||||
-rw-r--r-- | libbb/concat_subpath_file.c | 4 |
3 files changed, 31 insertions, 41 deletions
diff --git a/e2fsprogs/chattr.c b/e2fsprogs/chattr.c index 28a391771..f46646d71 100644 --- a/e2fsprogs/chattr.c +++ b/e2fsprogs/chattr.c | |||
@@ -59,14 +59,13 @@ static const struct flags_char flags_array[] = { | |||
59 | static unsigned long get_flag(char c) | 59 | static unsigned long get_flag(char c) |
60 | { | 60 | { |
61 | const struct flags_char *fp; | 61 | const struct flags_char *fp; |
62 | for (fp = flags_array; fp->flag; fp++) | 62 | for (fp = flags_array; fp->optchar; fp++) |
63 | if (fp->optchar == c) | 63 | if (fp->optchar == c) |
64 | return fp->flag; | 64 | return fp->flag; |
65 | bb_show_usage(); | 65 | bb_show_usage(); |
66 | /*return 0;*/ | ||
67 | } | 66 | } |
68 | 67 | ||
69 | static int decode_arg(char *arg) | 68 | static int decode_arg(const char *arg) |
70 | { | 69 | { |
71 | unsigned long *fl; | 70 | unsigned long *fl; |
72 | char opt = *arg++; | 71 | char opt = *arg++; |
@@ -89,15 +88,27 @@ static int decode_arg(char *arg) | |||
89 | return 1; | 88 | return 1; |
90 | } | 89 | } |
91 | 90 | ||
92 | static int chattr_dir_proc(const char *, struct dirent *, void *); | 91 | static void change_attributes(const char *name); |
93 | 92 | ||
94 | static void change_attributes(const char * name) | 93 | static 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 | |||
105 | static void change_attributes(const char *name) | ||
95 | { | 106 | { |
96 | unsigned long fsflags; | 107 | unsigned long fsflags; |
97 | struct stat st; | 108 | struct stat st; |
98 | 109 | ||
99 | if (lstat(name, &st) == -1) { | 110 | if (lstat(name, &st) == -1) { |
100 | bb_error_msg("stat %s failed", name); | 111 | bb_perror_msg("stat %s", name); |
101 | return; | 112 | return; |
102 | } | 113 | } |
103 | if (S_ISLNK(st.st_mode) && recursive) | 114 | if (S_ISLNK(st.st_mode) && recursive) |
@@ -112,13 +123,13 @@ static void change_attributes(const char * name) | |||
112 | 123 | ||
113 | if (flags & OPT_SET_VER) | 124 | if (flags & OPT_SET_VER) |
114 | if (fsetversion(name, version) == -1) | 125 | if (fsetversion(name, version) == -1) |
115 | bb_error_msg("setting version on %s", name); | 126 | bb_perror_msg("setting version on %s", name); |
116 | 127 | ||
117 | if (flags & OPT_SET) { | 128 | if (flags & OPT_SET) { |
118 | fsflags = sf; | 129 | fsflags = sf; |
119 | } else { | 130 | } else { |
120 | if (fgetflags(name, &fsflags) == -1) { | 131 | if (fgetflags(name, &fsflags) == -1) { |
121 | bb_error_msg("reading flags on %s", name); | 132 | bb_perror_msg("reading flags on %s", name); |
122 | goto skip_setflags; | 133 | goto skip_setflags; |
123 | } | 134 | } |
124 | if (flags & OPT_REM) | 135 | if (flags & OPT_REM) |
@@ -129,38 +140,19 @@ static void change_attributes(const char * name) | |||
129 | fsflags &= ~EXT2_DIRSYNC_FL; | 140 | fsflags &= ~EXT2_DIRSYNC_FL; |
130 | } | 141 | } |
131 | if (fsetflags(name, fsflags) == -1) | 142 | if (fsetflags(name, fsflags) == -1) |
132 | bb_error_msg("setting flags on %s", name); | 143 | bb_perror_msg("setting flags on %s", name); |
133 | 144 | ||
134 | skip_setflags: | 145 | skip_setflags: |
135 | if (S_ISDIR(st.st_mode) && recursive) | 146 | if (recursive && S_ISDIR(st.st_mode)) |
136 | iterate_on_dir(name, chattr_dir_proc, NULL); | 147 | iterate_on_dir(name, chattr_dir_proc, NULL); |
137 | } | 148 | } |
138 | 149 | ||
139 | static int chattr_dir_proc(const char *dir_name, struct dirent *de, | ||
140 | void *private ATTRIBUTE_UNUSED) | ||
141 | { | ||
142 | /*if (strcmp(de->d_name, ".") || strcmp(de->d_name, "..")) {*/ | ||
143 | if (de->d_name[0] == '.' | ||
144 | && (!de->d_name[1] || LONE_CHAR(de->d_name+1, '.')) | ||
145 | ) { | ||
146 | char *path = concat_subpath_file(dir_name, de->d_name); | ||
147 | if (path) { | ||
148 | change_attributes(path); | ||
149 | free(path); | ||
150 | } | ||
151 | } | ||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | int chattr_main(int argc, char **argv) | 150 | int chattr_main(int argc, char **argv) |
156 | { | 151 | { |
157 | int i; | ||
158 | char *arg; | 152 | char *arg; |
159 | 153 | ||
160 | /* parse the args */ | 154 | /* parse the args */ |
161 | for (i = 1; i < argc; ++i) { | 155 | while ((arg = *++argv)) { |
162 | arg = argv[i]; | ||
163 | |||
164 | /* take care of -R and -v <version> */ | 156 | /* take care of -R and -v <version> */ |
165 | if (arg[0] == '-') { | 157 | if (arg[0] == '-') { |
166 | if (arg[1] == 'R' && arg[2] == '\0') { | 158 | if (arg[1] == 'R' && arg[2] == '\0') { |
@@ -168,10 +160,9 @@ int chattr_main(int argc, char **argv) | |||
168 | continue; | 160 | continue; |
169 | } | 161 | } |
170 | if (arg[1] == 'v' && arg[2] == '\0') { | 162 | if (arg[1] == 'v' && arg[2] == '\0') { |
171 | ++i; | 163 | if (!*++argv) |
172 | if (i >= argc) | ||
173 | bb_show_usage(); | 164 | bb_show_usage(); |
174 | version = xatoul(argv[i]); | 165 | version = xatoul(*argv); |
175 | flags |= OPT_SET_VER; | 166 | flags |= OPT_SET_VER; |
176 | continue; | 167 | continue; |
177 | } | 168 | } |
@@ -182,18 +173,17 @@ int chattr_main(int argc, char **argv) | |||
182 | } | 173 | } |
183 | 174 | ||
184 | /* run sanity checks on all the arguments given us */ | 175 | /* run sanity checks on all the arguments given us */ |
185 | if (i >= argc) | 176 | if (!*argv) |
186 | bb_show_usage(); | 177 | bb_show_usage(); |
187 | if ((flags & OPT_SET) && (flags & (OPT_ADD|OPT_REM))) | 178 | if ((flags & OPT_SET) && (flags & (OPT_ADD|OPT_REM))) |
188 | bb_error_msg_and_die("= is incompatible with - and +"); | 179 | bb_error_msg_and_die("= is incompatible with - and +"); |
189 | if (rf & af) | 180 | if (rf & af) |
190 | bb_error_msg_and_die("Can't set and unset a flag"); | 181 | bb_error_msg_and_die("can't set and unset a flag"); |
191 | if (!flags) | 182 | if (!flags) |
192 | bb_error_msg_and_die("Must use '-v', =, - or +"); | 183 | bb_error_msg_and_die("must use '-v', =, - or +"); |
193 | 184 | ||
194 | /* now run chattr on all the files passed to us */ | 185 | /* now run chattr on all the files passed to us */ |
195 | while (i < argc) | 186 | do change_attributes(*argv); while (*++argv); |
196 | change_attributes(argv[i++]); | ||
197 | 187 | ||
198 | return EXIT_SUCCESS; | 188 | return EXIT_SUCCESS; |
199 | } | 189 | } |
diff --git a/include/usage.h b/include/usage.h index 42b51fa81..af518de0b 100644 --- a/include/usage.h +++ b/include/usage.h | |||
@@ -174,7 +174,7 @@ | |||
174 | " S Write file contents synchronously\n" \ | 174 | " S Write file contents synchronously\n" \ |
175 | " t Disable tail-merging of partial blocks with other files\n" \ | 175 | " t Disable tail-merging of partial blocks with other files\n" \ |
176 | " u Allow file to be undeleted\n" \ | 176 | " u Allow file to be undeleted\n" \ |
177 | " Options:\n" \ | 177 | "Options:\n" \ |
178 | " -R Recursively list subdirectories\n" \ | 178 | " -R Recursively list subdirectories\n" \ |
179 | " -v Set the file's version/generation number" | 179 | " -v Set the file's version/generation number" |
180 | 180 | ||
diff --git a/libbb/concat_subpath_file.c b/libbb/concat_subpath_file.c index 6ebc01bf5..6bbe49ab5 100644 --- a/libbb/concat_subpath_file.c +++ b/libbb/concat_subpath_file.c | |||
@@ -10,14 +10,14 @@ | |||
10 | /* | 10 | /* |
11 | This function make special for recursive actions with usage | 11 | This function make special for recursive actions with usage |
12 | concat_path_file(path, filename) | 12 | concat_path_file(path, filename) |
13 | and skiping "." and ".." directory entries | 13 | and skipping "." and ".." directory entries |
14 | */ | 14 | */ |
15 | 15 | ||
16 | #include "libbb.h" | 16 | #include "libbb.h" |
17 | 17 | ||
18 | char *concat_subpath_file(const char *path, const char *f) | 18 | char *concat_subpath_file(const char *path, const char *f) |
19 | { | 19 | { |
20 | if(f && *f == '.' && (!f[1] || (f[1] == '.' && !f[2]))) | 20 | if (f && *f == '.' && (!f[1] || (f[1] == '.' && !f[2]))) |
21 | return NULL; | 21 | return NULL; |
22 | return concat_path_file(path, f); | 22 | return concat_path_file(path, f); |
23 | } | 23 | } |