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