3
0

e2fs_lib.c 4.4 KB

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