Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 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 | */ |
Denys Vlasenko | 000eda4 | 2015-10-18 22:40:23 +0200 | [diff] [blame] | 12 | //config:config CHATTR |
| 13 | //config: bool "chattr" |
| 14 | //config: default y |
| 15 | //config: help |
| 16 | //config: chattr changes the file attributes on a second extended file system. |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 17 | |
Denys Vlasenko | 000eda4 | 2015-10-18 22:40:23 +0200 | [diff] [blame] | 18 | //applet:IF_CHATTR(APPLET(chattr, BB_DIR_BIN, BB_SUID_DROP)) |
| 19 | |
| 20 | //kbuild:lib-$(CONFIG_CHATTR) += chattr.o e2fs_lib.o |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 21 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 22 | //usage:#define chattr_trivial_usage |
| 23 | //usage: "[-R] [-+=AacDdijsStTu] [-v VERSION] [FILE]..." |
| 24 | //usage:#define chattr_full_usage "\n\n" |
Denys Vlasenko | 36647ab | 2015-10-19 01:29:20 +0200 | [diff] [blame] | 25 | //usage: "Change ext2 file attributes\n" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 26 | //usage: "\nModifiers:" |
Denys Vlasenko | 36647ab | 2015-10-19 01:29:20 +0200 | [diff] [blame] | 27 | //usage: "\n -,+,= Remove/add/set attributes" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 28 | //usage: "\nAttributes:" |
| 29 | //usage: "\n A Don't track atime" |
| 30 | //usage: "\n a Append mode only" |
| 31 | //usage: "\n c Enable compress" |
| 32 | //usage: "\n D Write dir contents synchronously" |
| 33 | //usage: "\n d Don't backup with dump" |
| 34 | //usage: "\n i Cannot be modified (immutable)" |
| 35 | //usage: "\n j Write all data to journal first" |
| 36 | //usage: "\n s Zero disk storage when deleted" |
Denys Vlasenko | 36647ab | 2015-10-19 01:29:20 +0200 | [diff] [blame] | 37 | //usage: "\n S Write synchronously" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 38 | //usage: "\n t Disable tail-merging of partial blocks with other files" |
| 39 | //usage: "\n u Allow file to be undeleted" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 40 | //usage: "\n -R Recurse" |
Denys Vlasenko | 36647ab | 2015-10-19 01:29:20 +0200 | [diff] [blame] | 41 | //usage: "\n -v VER Set version/generation number" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 42 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 43 | #include "libbb.h" |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 44 | #include "e2fs_lib.h" |
| 45 | |
| 46 | #define OPT_ADD 1 |
| 47 | #define OPT_REM 2 |
| 48 | #define OPT_SET 4 |
| 49 | #define OPT_SET_VER 8 |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 51 | struct globals { |
| 52 | unsigned long version; |
| 53 | unsigned long af; |
| 54 | unsigned long rf; |
| 55 | smallint flags; |
| 56 | smallint recursive; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static unsigned long get_flag(char c) |
| 60 | { |
Denis Vlasenko | d059ddc | 2007-10-30 19:36:07 +0000 | [diff] [blame] | 61 | const char *fp = strchr(e2attr_flags_sname_chattr, c); |
| 62 | if (fp) |
| 63 | return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr]; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 64 | bb_show_usage(); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 67 | static int decode_arg(const char *arg, struct globals *gp) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 68 | { |
| 69 | unsigned long *fl; |
| 70 | char opt = *arg++; |
| 71 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 72 | fl = &gp->af; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 73 | if (opt == '-') { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 74 | gp->flags |= OPT_REM; |
| 75 | fl = &gp->rf; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 76 | } else if (opt == '+') { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 77 | gp->flags |= OPT_ADD; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 78 | } else if (opt == '=') { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 79 | gp->flags |= OPT_SET; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 80 | } else |
| 81 | return 0; |
| 82 | |
| 83 | while (*arg) |
| 84 | *fl |= get_flag(*arg++); |
| 85 | |
| 86 | return 1; |
| 87 | } |
| 88 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 89 | static void change_attributes(const char *name, struct globals *gp); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 91 | static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 92 | { |
| 93 | char *path = concat_subpath_file(dir_name, de->d_name); |
| 94 | /* path is NULL if de->d_name is "." or "..", else... */ |
| 95 | if (path) { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 96 | change_attributes(path, gp); |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 97 | free(path); |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 102 | static void change_attributes(const char *name, struct globals *gp) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 103 | { |
| 104 | unsigned long fsflags; |
| 105 | struct stat st; |
| 106 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 107 | if (lstat(name, &st) != 0) { |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 108 | bb_perror_msg("stat %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 109 | return; |
| 110 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 111 | if (S_ISLNK(st.st_mode) && gp->recursive) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 112 | return; |
| 113 | |
| 114 | /* Don't try to open device files, fifos etc. We probably |
| 115 | * ought to display an error if the file was explicitly given |
| 116 | * on the command line (whether or not recursive was |
| 117 | * requested). */ |
| 118 | if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode)) |
| 119 | return; |
| 120 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 121 | if (gp->flags & OPT_SET_VER) |
| 122 | if (fsetversion(name, gp->version) != 0) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 123 | bb_perror_msg("setting version on %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 125 | if (gp->flags & OPT_SET) { |
| 126 | fsflags = gp->af; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 127 | } else { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 128 | if (fgetflags(name, &fsflags) != 0) { |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 129 | bb_perror_msg("reading flags on %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 130 | goto skip_setflags; |
| 131 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 132 | /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */ |
| 133 | fsflags &= ~gp->rf; |
| 134 | /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */ |
| 135 | fsflags |= gp->af; |
| 136 | /* What is this? And why it's not done for SET case? */ |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 137 | if (!S_ISDIR(st.st_mode)) |
| 138 | fsflags &= ~EXT2_DIRSYNC_FL; |
| 139 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 140 | if (fsetflags(name, fsflags) != 0) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 141 | bb_perror_msg("setting flags on %s", name); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 142 | |
| 143 | skip_setflags: |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 144 | if (gp->recursive && S_ISDIR(st.st_mode)) |
| 145 | iterate_on_dir(name, chattr_dir_proc, gp); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 148 | int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 149 | int chattr_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 150 | { |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 151 | struct globals g; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 152 | char *arg; |
| 153 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 154 | memset(&g, 0, sizeof(g)); |
| 155 | |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 156 | /* parse the args */ |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 157 | while ((arg = *++argv)) { |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 158 | /* take care of -R and -v <version> */ |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 159 | if (arg[0] == '-' |
| 160 | && (arg[1] == 'R' || arg[1] == 'v') |
| 161 | && !arg[2] |
| 162 | ) { |
| 163 | if (arg[1] == 'R') { |
| 164 | g.recursive = 1; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 165 | continue; |
| 166 | } |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 167 | /* arg[1] == 'v' */ |
| 168 | if (!*++argv) |
| 169 | bb_show_usage(); |
| 170 | g.version = xatoul(*argv); |
| 171 | g.flags |= OPT_SET_VER; |
| 172 | continue; |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 175 | if (!decode_arg(arg, &g)) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 176 | break; |
| 177 | } |
| 178 | |
| 179 | /* run sanity checks on all the arguments given us */ |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 180 | if (!*argv) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 181 | bb_show_usage(); |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 182 | if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM))) |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 183 | bb_error_msg_and_die("= is incompatible with - and +"); |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 184 | if (g.rf & g.af) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 185 | bb_error_msg_and_die("can't set and unset a flag"); |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 186 | if (!g.flags) |
Denis Vlasenko | 5dd7ef0 | 2006-12-26 03:36:28 +0000 | [diff] [blame] | 187 | bb_error_msg_and_die("must use '-v', =, - or +"); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 188 | |
| 189 | /* now run chattr on all the files passed to us */ |
Denis Vlasenko | 8acf521 | 2007-04-15 11:48:27 +0000 | [diff] [blame] | 190 | do change_attributes(*argv, &g); while (*++argv); |
Denis Vlasenko | c4f623e | 2006-12-26 01:30:59 +0000 | [diff] [blame] | 191 | |
| 192 | return EXIT_SUCCESS; |
| 193 | } |