3
0

chattr.c 5.0 KB

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