chattr.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. */
  12. //config:config CHATTR
  13. //config: bool "chattr (4.1 kb)"
  14. //config: default y
  15. //config: help
  16. //config: chattr changes the file attributes on a second extended file system.
  17. //applet:IF_CHATTR(APPLET_NOEXEC(chattr, chattr, BB_DIR_BIN, BB_SUID_DROP, chattr))
  18. //kbuild:lib-$(CONFIG_CHATTR) += chattr.o e2fs_lib.o
  19. //usage:#define chattr_trivial_usage
  20. //usage: "[-R] [-v VERSION] [-p PROJID] [-+=AacDdijsStTu] FILE..."
  21. //usage:#define chattr_full_usage "\n\n"
  22. //usage: "Change ext2 file attributes\n"
  23. //usage: "\n -R Recurse"
  24. //usage: "\n -v NUM Set version/generation number"
  25. //usage: "\n -p NUM Set project number"
  26. //-V, -f accepted but ignored
  27. //usage: "\nModifiers:"
  28. //usage: "\n -,+,= Remove/add/set attributes"
  29. //usage: "\nAttributes:"
  30. //usage: "\n A No atime"
  31. //usage: "\n a Append only"
  32. //usage: "\n C No copy-on-write"
  33. //usage: "\n c Compressed"
  34. //usage: "\n D Synchronous dir updates"
  35. //usage: "\n d Don't backup with dump"
  36. //usage: "\n E Encrypted"
  37. //usage: "\n e File uses extents"
  38. //usage: "\n F Case-insensitive dir"
  39. //usage: "\n I Indexed dir"
  40. //usage: "\n i Immutable"
  41. //usage: "\n j Write data to journal first"
  42. //usage: "\n N File is stored in inode"
  43. //usage: "\n P Hierarchical project ID dir"
  44. //usage: "\n S Synchronous file updates"
  45. //usage: "\n s Zero storage when deleted"
  46. //usage: "\n T Top of dir hierarchy"
  47. //usage: "\n t Don't tail-merge with other files"
  48. //usage: "\n u Allow undelete"
  49. //usage: "\n V Verity"
  50. #include "libbb.h"
  51. #include "e2fs_lib.h"
  52. #define OPT_ADD (1 << 0)
  53. #define OPT_REM (1 << 1)
  54. #define OPT_SET (1 << 2)
  55. #define OPT_SET_VER (1 << 3)
  56. #define OPT_SET_PROJ (1 << 4)
  57. struct globals {
  58. unsigned version;
  59. unsigned af;
  60. unsigned rf;
  61. int flags;
  62. uint32_t projid;
  63. smallint recursive;
  64. };
  65. static unsigned long get_flag(char c)
  66. {
  67. const char *fp = strchr(e2attr_flags_sname_chattr, c);
  68. if (fp)
  69. return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
  70. bb_show_usage();
  71. }
  72. static char** decode_arg(char **argv, struct globals *gp)
  73. {
  74. unsigned *fl;
  75. const char *arg = *argv;
  76. char opt = *arg;
  77. fl = &gp->af;
  78. if (opt == '-') {
  79. /* gp->flags |= OPT_REM; - WRONG, it can be an option */
  80. /* testcase: chattr =ae -R FILE should not complain "= is incompatible with - and +" */
  81. /* (and should not read flags, with =FLAGS they can be just set directly) */
  82. fl = &gp->rf;
  83. } else if (opt == '+') {
  84. gp->flags |= OPT_ADD;
  85. } else { /* if (opt == '=') */
  86. gp->flags |= OPT_SET;
  87. }
  88. while (*++arg) {
  89. if (opt == '-') {
  90. //e2fsprogs-1.43.1 accepts:
  91. // "-RRR", "-RRRv VER" and even "-ARRRva VER" and "-vvv V1 V2 V3"
  92. // but not "-vVER".
  93. // IOW: options are parsed as part of "remove attrs" strings,
  94. // if "v" is seen, next argv[] is VER, even if more opts/attrs follow in this argv[]!
  95. if (*arg == 'R') {
  96. gp->recursive = 1;
  97. continue;
  98. }
  99. if (*arg == 'V') {
  100. /*"verbose and print program version" (nop for now) */;
  101. continue;
  102. }
  103. if (*arg == 'f') {
  104. /*"suppress most error messages" (nop) */;
  105. continue;
  106. }
  107. if (*arg == 'v') {
  108. if (!*++argv)
  109. bb_show_usage();
  110. gp->version = xatou(*argv);
  111. gp->flags |= OPT_SET_VER;
  112. continue;
  113. }
  114. if (*arg == 'p') {
  115. if (!*++argv)
  116. bb_show_usage();
  117. gp->projid = xatou32(*argv);
  118. gp->flags |= OPT_SET_PROJ;
  119. continue;
  120. }
  121. /* not a known option, try as an attribute */
  122. gp->flags |= OPT_REM;
  123. }
  124. *fl |= get_flag(*arg); /* aborts on bad flag letter */
  125. }
  126. return argv;
  127. }
  128. static void change_attributes(const char *name, struct globals *gp);
  129. static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
  130. {
  131. //TODO: use de->d_type (if it's not DT_UNKNOWN) to skip !(REG || DIR || LNK) entries without lstat?
  132. char *path = concat_subpath_file(dir_name, de->d_name);
  133. /* path is NULL if de->d_name is "." or "..", else... */
  134. if (path) {
  135. change_attributes(path, gp);
  136. free(path);
  137. }
  138. return 0;
  139. }
  140. static void change_attributes(const char *name, struct globals *gp)
  141. {
  142. unsigned fsflags;
  143. int fd;
  144. struct stat st;
  145. if (lstat(name, &st) != 0) {
  146. bb_perror_msg("can't stat '%s'", name);
  147. return;
  148. }
  149. /* Don't try to open device files, fifos etc. We probably
  150. * ought to display an error if the file was explicitly given
  151. * on the command line (whether or not recursive was
  152. * requested). */
  153. if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
  154. return;
  155. /* There is no way to run needed ioctls on a symlink.
  156. * open(O_PATH | O_NOFOLLOW) _can_ be used to get a fd referring to the symlink,
  157. * but ioctls fail on such a fd (tried on 4.12.0 kernel).
  158. * e2fsprogs-1.46.2 uses open(O_NOFOLLOW), it fails on symlinks.
  159. */
  160. fd = open_or_warn(name, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOFOLLOW);
  161. if (fd >= 0) {
  162. int r;
  163. if (gp->flags & OPT_SET_VER) {
  164. r = ioctl(fd, EXT2_IOC_SETVERSION, &gp->version);
  165. if (r != 0)
  166. bb_perror_msg("setting %s on %s", "version", name);
  167. }
  168. if (gp->flags & OPT_SET_PROJ) {
  169. struct ext2_fsxattr fsxattr;
  170. r = ioctl(fd, EXT2_IOC_FSGETXATTR, &fsxattr);
  171. /* note: ^^^ may fail in 32-bit userspace on 64-bit kernel (seen on 4.12.0) */
  172. if (r != 0) {
  173. bb_perror_msg("getting %s on %s", "project ID", name);
  174. } else {
  175. fsxattr.fsx_projid = gp->projid;
  176. r = ioctl(fd, EXT2_IOC_FSSETXATTR, &fsxattr);
  177. if (r != 0)
  178. bb_perror_msg("setting %s on %s", "project ID", name);
  179. }
  180. }
  181. if (gp->flags & OPT_SET) {
  182. fsflags = gp->af;
  183. } else {
  184. r = ioctl(fd, EXT2_IOC_GETFLAGS, &fsflags);
  185. if (r != 0) {
  186. bb_perror_msg("getting %s on %s", "flags", name);
  187. goto skip_setflags;
  188. }
  189. /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
  190. fsflags &= ~gp->rf;
  191. /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
  192. fsflags |= gp->af;
  193. // What is this? And why it's not done for SET case?
  194. if (!S_ISDIR(st.st_mode))
  195. fsflags &= ~EXT2_DIRSYNC_FL;
  196. }
  197. r = ioctl(fd, EXT2_IOC_SETFLAGS, &fsflags);
  198. if (r != 0)
  199. bb_perror_msg("setting %s on %s", "flags", name);
  200. skip_setflags:
  201. close(fd);
  202. }
  203. if (gp->recursive && S_ISDIR(st.st_mode))
  204. iterate_on_dir(name, chattr_dir_proc, gp);
  205. }
  206. int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  207. int chattr_main(int argc UNUSED_PARAM, char **argv)
  208. {
  209. struct globals g;
  210. memset(&g, 0, sizeof(g));
  211. /* parse the args */
  212. for (;;) {
  213. char *arg = *++argv;
  214. if (!arg)
  215. bb_show_usage();
  216. if (arg[0] != '-' && arg[0] != '+' && arg[0] != '=')
  217. break;
  218. argv = decode_arg(argv, &g);
  219. }
  220. /* note: on loop exit, remaining argv[] is never empty */
  221. /* run sanity checks on all the arguments given us */
  222. if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
  223. bb_simple_error_msg_and_die("= is incompatible with - and +");
  224. if (g.rf & g.af)
  225. bb_simple_error_msg_and_die("can't set and unset a flag");
  226. if (!g.flags)
  227. bb_simple_error_msg_and_die("must use -v, -p, =, - or +");
  228. /* now run chattr on all the files passed to us */
  229. do change_attributes(*argv, &g); while (*++argv);
  230. return EXIT_SUCCESS;
  231. }