e2fs_lib.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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(x) x
  13. #define IF_LONG_IS_WIDER(x)
  14. #else
  15. #define IF_LONG_IS_SAME(x)
  16. #define IF_LONG_IS_WIDER(x) x
  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. struct flags_name {
  127. unsigned long flag;
  128. char short_name;
  129. const char *long_name;
  130. };
  131. static const struct flags_name flags_array[] = {
  132. { EXT2_SECRM_FL, 's', "Secure_Deletion" },
  133. { EXT2_UNRM_FL, 'u' , "Undelete" },
  134. { EXT2_SYNC_FL, 'S', "Synchronous_Updates" },
  135. { EXT2_DIRSYNC_FL, 'D', "Synchronous_Directory_Updates" },
  136. { EXT2_IMMUTABLE_FL, 'i', "Immutable" },
  137. { EXT2_APPEND_FL, 'a', "Append_Only" },
  138. { EXT2_NODUMP_FL, 'd', "No_Dump" },
  139. { EXT2_NOATIME_FL, 'A', "No_Atime" },
  140. { EXT2_COMPR_FL, 'c', "Compression_Requested" },
  141. #ifdef ENABLE_COMPRESSION
  142. { EXT2_COMPRBLK_FL, 'B', "Compressed_File" },
  143. { EXT2_DIRTY_FL, 'Z', "Compressed_Dirty_File" },
  144. { EXT2_NOCOMPR_FL, 'X', "Compression_Raw_Access" },
  145. { EXT2_ECOMPR_FL, 'E', "Compression_Error" },
  146. #endif
  147. { EXT3_JOURNAL_DATA_FL, 'j', "Journaled_Data" },
  148. { EXT2_INDEX_FL, 'I', "Indexed_directory" },
  149. { EXT2_NOTAIL_FL, 't', "No_Tailmerging" },
  150. { EXT2_TOPDIR_FL, 'T', "Top_of_Directory_Hierarchies" },
  151. { 0, '\0', NULL }
  152. };
  153. void print_flags(FILE *f, unsigned long flags, unsigned options)
  154. {
  155. const struct flags_name *fp;
  156. if (options & PFOPT_LONG) {
  157. int first = 1;
  158. for (fp = flags_array; fp->short_name; fp++) {
  159. if (flags & fp->flag) {
  160. if (!first)
  161. fputs(", ", f);
  162. fputs(fp->long_name, f);
  163. first = 0;
  164. }
  165. }
  166. if (first)
  167. fputs("---", f);
  168. } else {
  169. for (fp = flags_array; fp->short_name; fp++) {
  170. char c = '-';
  171. if (flags & fp->flag)
  172. c = fp->short_name;
  173. fputc(c, f);
  174. }
  175. }
  176. }