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