e2fs_lib.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * See README for additional information
  4. *
  5. * Licensed under GPLv2, see file LICENSE in this source tree.
  6. */
  7. #include "libbb.h"
  8. #include "e2fs_lib.h"
  9. #define HAVE_EXT2_IOCTLS 1
  10. #if INT_MAX == LONG_MAX
  11. #define IF_LONG_IS_SAME(...) __VA_ARGS__
  12. #define IF_LONG_IS_WIDER(...)
  13. #else
  14. #define IF_LONG_IS_SAME(...)
  15. #define IF_LONG_IS_WIDER(...) __VA_ARGS__
  16. #endif
  17. static void close_silently(int fd)
  18. {
  19. int e = errno;
  20. close(fd);
  21. errno = e;
  22. }
  23. /* Iterate a function on each entry of a directory */
  24. int iterate_on_dir(const char *dir_name,
  25. int FAST_FUNC (*func)(const char *, struct dirent *, void *),
  26. void *private)
  27. {
  28. DIR *dir;
  29. struct dirent *de;
  30. dir = opendir(dir_name);
  31. if (dir == NULL) {
  32. return -1;
  33. }
  34. while ((de = readdir(dir)) != NULL) {
  35. func(dir_name, de, private);
  36. }
  37. closedir(dir);
  38. return 0;
  39. }
  40. /* Get/set a file version on an ext2 file system */
  41. int fgetsetversion(const char *name, unsigned long *get_version, unsigned long set_version)
  42. {
  43. #if HAVE_EXT2_IOCTLS
  44. int fd, r;
  45. IF_LONG_IS_WIDER(int ver;)
  46. fd = open(name, O_RDONLY | O_NONBLOCK);
  47. if (fd == -1)
  48. return -1;
  49. if (!get_version) {
  50. IF_LONG_IS_WIDER(
  51. ver = (int) set_version;
  52. r = ioctl(fd, EXT2_IOC_SETVERSION, &ver);
  53. )
  54. IF_LONG_IS_SAME(
  55. r = ioctl(fd, EXT2_IOC_SETVERSION, (void*)&set_version);
  56. )
  57. } else {
  58. IF_LONG_IS_WIDER(
  59. r = ioctl(fd, EXT2_IOC_GETVERSION, &ver);
  60. *get_version = ver;
  61. )
  62. IF_LONG_IS_SAME(
  63. r = ioctl(fd, EXT2_IOC_GETVERSION, (void*)get_version);
  64. )
  65. }
  66. close_silently(fd);
  67. return r;
  68. #else /* ! HAVE_EXT2_IOCTLS */
  69. errno = EOPNOTSUPP;
  70. return -1;
  71. #endif /* ! HAVE_EXT2_IOCTLS */
  72. }
  73. /* Get/set a file flags on an ext2 file system */
  74. int fgetsetflags(const char *name, unsigned long *get_flags, unsigned long set_flags)
  75. {
  76. #if HAVE_EXT2_IOCTLS
  77. struct stat buf;
  78. int fd, r;
  79. IF_LONG_IS_WIDER(int f;)
  80. if (stat(name, &buf) == 0 /* stat is ok */
  81. && !S_ISREG(buf.st_mode) && !S_ISDIR(buf.st_mode)
  82. ) {
  83. goto notsupp;
  84. }
  85. fd = open(name, O_RDONLY | O_NONBLOCK); /* neither read nor write asked for */
  86. if (fd == -1)
  87. return -1;
  88. if (!get_flags) {
  89. IF_LONG_IS_WIDER(
  90. f = (int) set_flags;
  91. r = ioctl(fd, EXT2_IOC_SETFLAGS, &f);
  92. )
  93. IF_LONG_IS_SAME(
  94. r = ioctl(fd, EXT2_IOC_SETFLAGS, (void*)&set_flags);
  95. )
  96. } else {
  97. IF_LONG_IS_WIDER(
  98. r = ioctl(fd, EXT2_IOC_GETFLAGS, &f);
  99. *get_flags = f;
  100. )
  101. IF_LONG_IS_SAME(
  102. r = ioctl(fd, EXT2_IOC_GETFLAGS, (void*)get_flags);
  103. )
  104. }
  105. close_silently(fd);
  106. return r;
  107. notsupp:
  108. #endif /* HAVE_EXT2_IOCTLS */
  109. errno = EOPNOTSUPP;
  110. return -1;
  111. }
  112. /* Print file attributes on an ext2 file system */
  113. const uint32_t e2attr_flags_value[] = {
  114. #ifdef ENABLE_COMPRESSION
  115. EXT2_COMPRBLK_FL,
  116. EXT2_DIRTY_FL,
  117. EXT2_NOCOMPR_FL,
  118. EXT2_ECOMPR_FL,
  119. #endif
  120. EXT2_INDEX_FL,
  121. EXT2_SECRM_FL,
  122. EXT2_UNRM_FL,
  123. EXT2_SYNC_FL,
  124. EXT2_DIRSYNC_FL,
  125. EXT2_IMMUTABLE_FL,
  126. EXT2_APPEND_FL,
  127. EXT2_NODUMP_FL,
  128. EXT2_NOATIME_FL,
  129. EXT2_COMPR_FL,
  130. EXT3_JOURNAL_DATA_FL,
  131. EXT2_NOTAIL_FL,
  132. EXT2_TOPDIR_FL
  133. };
  134. const char e2attr_flags_sname[] =
  135. #ifdef ENABLE_COMPRESSION
  136. "BZXE"
  137. #endif
  138. "I"
  139. "suSDiadAcjtT";
  140. static const char e2attr_flags_lname[] =
  141. #ifdef ENABLE_COMPRESSION
  142. "Compressed_File" "\0"
  143. "Compressed_Dirty_File" "\0"
  144. "Compression_Raw_Access" "\0"
  145. "Compression_Error" "\0"
  146. #endif
  147. "Indexed_directory" "\0"
  148. "Secure_Deletion" "\0"
  149. "Undelete" "\0"
  150. "Synchronous_Updates" "\0"
  151. "Synchronous_Directory_Updates" "\0"
  152. "Immutable" "\0"
  153. "Append_Only" "\0"
  154. "No_Dump" "\0"
  155. "No_Atime" "\0"
  156. "Compression_Requested" "\0"
  157. "Journaled_Data" "\0"
  158. "No_Tailmerging" "\0"
  159. "Top_of_Directory_Hierarchies" "\0"
  160. /* Another trailing NUL is added by compiler */;
  161. void print_e2flags(FILE *f, unsigned long flags, unsigned options)
  162. {
  163. const uint32_t *fv;
  164. const char *fn;
  165. fv = e2attr_flags_value;
  166. if (options & PFOPT_LONG) {
  167. int first = 1;
  168. fn = e2attr_flags_lname;
  169. do {
  170. if (flags & *fv) {
  171. if (!first)
  172. fputs(", ", f);
  173. fputs(fn, f);
  174. first = 0;
  175. }
  176. fv++;
  177. fn += strlen(fn) + 1;
  178. } while (*fn);
  179. if (first)
  180. fputs("---", f);
  181. } else {
  182. fn = e2attr_flags_sname;
  183. do {
  184. char c = '-';
  185. if (flags & *fv)
  186. c = *fn;
  187. fputc(c, f);
  188. fv++;
  189. fn++;
  190. } while (*fn);
  191. }
  192. }