chattr.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /*
  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. #include <sys/types.h>
  21. #include <dirent.h>
  22. #include <fcntl.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <sys/param.h>
  29. #include <sys/stat.h>
  30. #include "ext2fs/ext2_fs.h"
  31. #ifdef __GNUC__
  32. # define EXT2FS_ATTR(x) __attribute__(x)
  33. #else
  34. # define EXT2FS_ATTR(x)
  35. #endif
  36. #include "e2fsbb.h"
  37. #include "e2p/e2p.h"
  38. #define OPT_ADD 1
  39. #define OPT_REM 2
  40. #define OPT_SET 4
  41. #define OPT_SET_VER 8
  42. static int flags;
  43. static int recursive;
  44. static unsigned long version;
  45. static unsigned long af;
  46. static unsigned long rf;
  47. static unsigned long sf;
  48. struct flags_char {
  49. unsigned long flag;
  50. char optchar;
  51. };
  52. static const struct flags_char flags_array[] = {
  53. { EXT2_NOATIME_FL, 'A' },
  54. { EXT2_SYNC_FL, 'S' },
  55. { EXT2_DIRSYNC_FL, 'D' },
  56. { EXT2_APPEND_FL, 'a' },
  57. { EXT2_COMPR_FL, 'c' },
  58. { EXT2_NODUMP_FL, 'd' },
  59. { EXT2_IMMUTABLE_FL, 'i' },
  60. { EXT3_JOURNAL_DATA_FL, 'j' },
  61. { EXT2_SECRM_FL, 's' },
  62. { EXT2_UNRM_FL, 'u' },
  63. { EXT2_NOTAIL_FL, 't' },
  64. { EXT2_TOPDIR_FL, 'T' },
  65. { 0, 0 }
  66. };
  67. static unsigned long get_flag(char c)
  68. {
  69. const struct flags_char *fp;
  70. for (fp = flags_array; fp->flag; fp++)
  71. if (fp->optchar == c)
  72. return fp->flag;
  73. bb_show_usage();
  74. return 0;
  75. }
  76. static int decode_arg(char *arg)
  77. {
  78. unsigned long *fl;
  79. char opt = *arg++;
  80. if (opt == '-') {
  81. flags |= OPT_REM;
  82. fl = &rf;
  83. } else if (opt == '+') {
  84. flags |= OPT_ADD;
  85. fl = &af;
  86. } else if (opt == '=') {
  87. flags |= OPT_SET;
  88. fl = &sf;
  89. } else
  90. return EOF;
  91. for (; *arg ; ++arg)
  92. (*fl) |= get_flag(*arg);
  93. return 1;
  94. }
  95. static int chattr_dir_proc(const char *, struct dirent *, void *);
  96. static void change_attributes(const char * name)
  97. {
  98. unsigned long fsflags;
  99. struct stat st;
  100. if (lstat(name, &st) == -1) {
  101. bb_error_msg("stat %s failed", name);
  102. return;
  103. }
  104. if (S_ISLNK(st.st_mode) && recursive)
  105. return;
  106. /* Don't try to open device files, fifos etc. We probably
  107. * ought to display an error if the file was explicitly given
  108. * on the command line (whether or not recursive was
  109. * requested). */
  110. if (!S_ISREG(st.st_mode) && !S_ISLNK(st.st_mode) && !S_ISDIR(st.st_mode))
  111. return;
  112. if (flags & OPT_SET_VER)
  113. if (fsetversion(name, version) == -1)
  114. bb_error_msg("setting version on %s", name);
  115. if (flags & OPT_SET) {
  116. fsflags = sf;
  117. } else {
  118. if (fgetflags(name, &fsflags) == -1) {
  119. bb_error_msg("reading flags on %s", name);
  120. goto skip_setflags;
  121. }
  122. if (flags & OPT_REM)
  123. fsflags &= ~rf;
  124. if (flags & OPT_ADD)
  125. fsflags |= af;
  126. if (!S_ISDIR(st.st_mode))
  127. fsflags &= ~EXT2_DIRSYNC_FL;
  128. }
  129. if (fsetflags(name, fsflags) == -1)
  130. bb_error_msg("setting flags on %s", name);
  131. skip_setflags:
  132. if (S_ISDIR(st.st_mode) && recursive)
  133. iterate_on_dir(name, chattr_dir_proc, NULL);
  134. }
  135. static int chattr_dir_proc(const char *dir_name, struct dirent *de,
  136. void *private EXT2FS_ATTR((unused)))
  137. {
  138. /*if (strcmp(de->d_name, ".") || strcmp(de->d_name, "..")) {*/
  139. if (de->d_name[0] == '.'
  140. && (!de->d_name[1] || (de->d_name[1] == '.' && !de->d_name[2]))
  141. ) {
  142. char *path = concat_subpath_file(dir_name, de->d_name);
  143. if (path) {
  144. change_attributes(path);
  145. free(path);
  146. }
  147. }
  148. return 0;
  149. }
  150. int chattr_main(int argc, char **argv)
  151. {
  152. int i;
  153. char *arg;
  154. /* parse the args */
  155. for (i = 1; i < argc; ++i) {
  156. arg = argv[i];
  157. /* take care of -R and -v <version> */
  158. if (arg[0] == '-') {
  159. if (arg[1] == 'R' && arg[2] == '\0') {
  160. recursive = 1;
  161. continue;
  162. } else if (arg[1] == 'v' && arg[2] == '\0') {
  163. char *tmp;
  164. ++i;
  165. if (i >= argc)
  166. bb_show_usage();
  167. version = strtol(argv[i], &tmp, 0);
  168. if (*tmp)
  169. bb_error_msg_and_die("bad version '%s'", arg);
  170. flags |= OPT_SET_VER;
  171. continue;
  172. }
  173. }
  174. if (decode_arg(arg) == EOF)
  175. break;
  176. }
  177. /* run sanity checks on all the arguments given us */
  178. if (i >= argc)
  179. bb_show_usage();
  180. if ((flags & OPT_SET) && ((flags & OPT_ADD) || (flags & OPT_REM)))
  181. bb_error_msg_and_die("= is incompatible with - and +");
  182. if ((rf & af) != 0)
  183. bb_error_msg_and_die("Can't set and unset a flag");
  184. if (!flags)
  185. bb_error_msg_and_die("Must use '-v', =, - or +");
  186. /* now run chattr on all the files passed to us */
  187. while (i < argc)
  188. change_attributes(argv[i++]);
  189. return EXIT_SUCCESS;
  190. }