3
0

chattr.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 (3.8 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] [-+=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 VER Set version/generation number"
  25. //-V, -f accepted but ignored
  26. //usage: "\nModifiers:"
  27. //usage: "\n -,+,= Remove/add/set attributes"
  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"
  37. //usage: "\n S Write synchronously"
  38. //usage: "\n t Disable tail-merging of partial blocks with other files"
  39. //usage: "\n u Allow file to be undeleted"
  40. #include "libbb.h"
  41. #include "e2fs_lib.h"
  42. #define OPT_ADD 1
  43. #define OPT_REM 2
  44. #define OPT_SET 4
  45. #define OPT_SET_VER 8
  46. struct globals {
  47. unsigned long version;
  48. unsigned long af;
  49. unsigned long rf;
  50. int flags;
  51. smallint recursive;
  52. };
  53. static unsigned long get_flag(char c)
  54. {
  55. const char *fp = strchr(e2attr_flags_sname_chattr, c);
  56. if (fp)
  57. return e2attr_flags_value_chattr[fp - e2attr_flags_sname_chattr];
  58. bb_show_usage();
  59. }
  60. static char** decode_arg(char **argv, struct globals *gp)
  61. {
  62. unsigned long *fl;
  63. const char *arg = *argv;
  64. char opt = *arg;
  65. fl = &gp->af;
  66. if (opt == '-') {
  67. gp->flags |= OPT_REM;
  68. fl = &gp->rf;
  69. } else if (opt == '+') {
  70. gp->flags |= OPT_ADD;
  71. } else { /* if (opt == '=') */
  72. gp->flags |= OPT_SET;
  73. }
  74. while (*++arg) {
  75. if (opt == '-') {
  76. //e2fsprogs-1.43.1 accepts:
  77. // "-RRR", "-RRRv VER" and even "-ARRRva VER" and "-vvv V1 V2 V3"
  78. // but not "-vVER".
  79. // IOW: options are parsed as part of "remove attrs" strings,
  80. // if "v" is seen, next argv[] is VER, even if more opts/attrs follow in this argv[]!
  81. if (*arg == 'R') {
  82. gp->recursive = 1;
  83. continue;
  84. }
  85. if (*arg == 'V') {
  86. /*"verbose and print program version" (nop for now) */;
  87. continue;
  88. }
  89. if (*arg == 'f') {
  90. /*"suppress most error messages" (nop) */;
  91. continue;
  92. }
  93. if (*arg == 'v') {
  94. if (!*++argv)
  95. bb_show_usage();
  96. gp->version = xatoul(*argv);
  97. gp->flags |= OPT_SET_VER;
  98. continue;
  99. }
  100. //TODO: "-p PROJECT_NUM" ?
  101. /* not a known option, try as an attribute */
  102. }
  103. *fl |= get_flag(*arg);
  104. }
  105. return argv;
  106. }
  107. static void change_attributes(const char *name, struct globals *gp);
  108. static int FAST_FUNC chattr_dir_proc(const char *dir_name, struct dirent *de, void *gp)
  109. {
  110. char *path = concat_subpath_file(dir_name, de->d_name);
  111. /* path is NULL if de->d_name is "." or "..", else... */
  112. if (path) {
  113. change_attributes(path, gp);
  114. free(path);
  115. }
  116. return 0;
  117. }
  118. static void change_attributes(const char *name, struct globals *gp)
  119. {
  120. unsigned long fsflags;
  121. struct stat st;
  122. if (lstat(name, &st) != 0) {
  123. bb_perror_msg("stat %s", name);
  124. return;
  125. }
  126. if (S_ISLNK(st.st_mode) && gp->recursive)
  127. return;
  128. /* Don't try to open device files, fifos etc. We probably
  129. * ought to display an error if the file was explicitly given
  130. * on the command line (whether or not recursive was
  131. * requested). */
  132. if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
  133. return;
  134. if (gp->flags & OPT_SET_VER)
  135. if (fsetversion(name, gp->version) != 0)
  136. bb_perror_msg("setting version on %s", name);
  137. if (gp->flags & OPT_SET) {
  138. fsflags = gp->af;
  139. } else {
  140. if (fgetflags(name, &fsflags) != 0) {
  141. bb_perror_msg("reading flags on %s", name);
  142. goto skip_setflags;
  143. }
  144. /*if (gp->flags & OPT_REM) - not needed, rf is zero otherwise */
  145. fsflags &= ~gp->rf;
  146. /*if (gp->flags & OPT_ADD) - not needed, af is zero otherwise */
  147. fsflags |= gp->af;
  148. // What is this? And why it's not done for SET case?
  149. if (!S_ISDIR(st.st_mode))
  150. fsflags &= ~EXT2_DIRSYNC_FL;
  151. }
  152. if (fsetflags(name, fsflags) != 0)
  153. bb_perror_msg("setting flags on %s", name);
  154. skip_setflags:
  155. if (gp->recursive && S_ISDIR(st.st_mode))
  156. iterate_on_dir(name, chattr_dir_proc, gp);
  157. }
  158. int chattr_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  159. int chattr_main(int argc UNUSED_PARAM, char **argv)
  160. {
  161. struct globals g;
  162. memset(&g, 0, sizeof(g));
  163. /* parse the args */
  164. for (;;) {
  165. char *arg = *++argv;
  166. if (!arg)
  167. bb_show_usage();
  168. if (arg[0] != '-' && arg[0] != '+' && arg[0] != '=')
  169. break;
  170. argv = decode_arg(argv, &g);
  171. }
  172. /* note: on loop exit, remaining argv[] is never empty */
  173. /* run sanity checks on all the arguments given us */
  174. if ((g.flags & OPT_SET) && (g.flags & (OPT_ADD|OPT_REM)))
  175. bb_error_msg_and_die("= is incompatible with - and +");
  176. if (g.rf & g.af)
  177. bb_error_msg_and_die("can't set and unset a flag");
  178. if (!g.flags)
  179. bb_error_msg_and_die("must use '-v', =, - or +");
  180. /* now run chattr on all the files passed to us */
  181. do change_attributes(*argv, &g); while (*++argv);
  182. return EXIT_SUCCESS;
  183. }