3
0

ls.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * ls.c - List the contents of an ext2fs superblock
  3. *
  4. * Copyright (C) 1992, 1993, 1994 Remy Card <card@masi.ibp.fr>
  5. * Laboratoire MASI, Institut Blaise Pascal
  6. * Universite Pierre et Marie Curie (Paris VI)
  7. *
  8. * Copyright (C) 1995, 1996, 1997 Theodore Ts'o <tytso@mit.edu>
  9. *
  10. * This file can be redistributed under the terms of the GNU Library General
  11. * Public License
  12. */
  13. #include <stdio.h>
  14. #include <sys/types.h>
  15. #include <string.h>
  16. #include <grp.h>
  17. #include <pwd.h>
  18. #include <time.h>
  19. #include "e2p.h"
  20. static void print_user(unsigned short uid, FILE *f)
  21. {
  22. struct passwd *pw = getpwuid(uid);
  23. fprintf(f, "%u (user %s)\n", uid,
  24. (pw == NULL ? "unknown" : pw->pw_name));
  25. }
  26. static void print_group(unsigned short gid, FILE *f)
  27. {
  28. struct group *gr = getgrgid(gid);
  29. fprintf(f, "%u (group %s)\n", gid,
  30. (gr == NULL ? "unknown" : gr->gr_name));
  31. }
  32. #define MONTH_INT (86400 * 30)
  33. #define WEEK_INT (86400 * 7)
  34. #define DAY_INT (86400)
  35. #define HOUR_INT (60 * 60)
  36. #define MINUTE_INT (60)
  37. static const char *interval_string(unsigned int secs)
  38. {
  39. static char buf[256], tmp[80];
  40. int hr, min, num;
  41. buf[0] = 0;
  42. if (secs == 0)
  43. return "<none>";
  44. if (secs >= MONTH_INT) {
  45. num = secs / MONTH_INT;
  46. secs -= num*MONTH_INT;
  47. sprintf(buf, "%d month%s", num, (num>1) ? "s" : "");
  48. }
  49. if (secs >= WEEK_INT) {
  50. num = secs / WEEK_INT;
  51. secs -= num*WEEK_INT;
  52. sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "",
  53. num, (num>1) ? "s" : "");
  54. strcat(buf, tmp);
  55. }
  56. if (secs >= DAY_INT) {
  57. num = secs / DAY_INT;
  58. secs -= num*DAY_INT;
  59. sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "",
  60. num, (num>1) ? "s" : "");
  61. strcat(buf, tmp);
  62. }
  63. if (secs > 0) {
  64. hr = secs / HOUR_INT;
  65. secs -= hr*HOUR_INT;
  66. min = secs / MINUTE_INT;
  67. secs -= min*MINUTE_INT;
  68. sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "",
  69. hr, min, secs);
  70. strcat(buf, tmp);
  71. }
  72. return buf;
  73. }
  74. static void print_features(struct ext2_super_block * s, FILE *f)
  75. {
  76. #ifdef EXT2_DYNAMIC_REV
  77. int i, j, printed=0;
  78. __u32 *mask = &s->s_feature_compat, m;
  79. fprintf(f, "Filesystem features: ");
  80. for (i=0; i <3; i++,mask++) {
  81. for (j=0,m=1; j < 32; j++, m<<=1) {
  82. if (*mask & m) {
  83. fprintf(f, " %s", e2p_feature2string(i, m));
  84. printed++;
  85. }
  86. }
  87. }
  88. if (printed == 0)
  89. fprintf(f, " (none)");
  90. fprintf(f, "\n");
  91. #endif
  92. }
  93. static void print_mntopts(struct ext2_super_block * s, FILE *f)
  94. {
  95. #ifdef EXT2_DYNAMIC_REV
  96. int i, printed=0;
  97. __u32 mask = s->s_default_mount_opts, m;
  98. fprintf(f, "Default mount options: ");
  99. if (mask & EXT3_DEFM_JMODE) {
  100. fprintf(f, " %s", e2p_mntopt2string(mask & EXT3_DEFM_JMODE));
  101. printed++;
  102. }
  103. for (i=0,m=1; i < 32; i++, m<<=1) {
  104. if (m & EXT3_DEFM_JMODE)
  105. continue;
  106. if (mask & m) {
  107. fprintf(f, " %s", e2p_mntopt2string(m));
  108. printed++;
  109. }
  110. }
  111. if (printed == 0)
  112. fprintf(f, " (none)");
  113. fprintf(f, "\n");
  114. #endif
  115. }
  116. #ifndef EXT2_INODE_SIZE
  117. #define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
  118. #endif
  119. #ifndef EXT2_GOOD_OLD_REV
  120. #define EXT2_GOOD_OLD_REV 0
  121. #endif
  122. void list_super2(struct ext2_super_block * sb, FILE *f)
  123. {
  124. int inode_blocks_per_group;
  125. char buf[80], *str;
  126. time_t tm;
  127. inode_blocks_per_group = (((sb->s_inodes_per_group *
  128. EXT2_INODE_SIZE(sb)) +
  129. EXT2_BLOCK_SIZE(sb) - 1) /
  130. EXT2_BLOCK_SIZE(sb));
  131. if (sb->s_volume_name[0]) {
  132. memset(buf, 0, sizeof(buf));
  133. strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
  134. } else
  135. strcpy(buf, "<none>");
  136. fprintf(f, "Filesystem volume name: %s\n", buf);
  137. if (sb->s_last_mounted[0]) {
  138. memset(buf, 0, sizeof(buf));
  139. strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
  140. } else
  141. strcpy(buf, "<not available>");
  142. fprintf(f,
  143. "Last mounted on: %s\n"
  144. "Filesystem UUID: %s\n"
  145. "Filesystem magic number: 0x%04X\n"
  146. "Filesystem revision #: %d",
  147. buf, e2p_uuid2str(sb->s_uuid), sb->s_magic, sb->s_rev_level);
  148. if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
  149. fprintf(f, " (original)\n");
  150. #ifdef EXT2_DYNAMIC_REV
  151. } else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
  152. fprintf(f, " (dynamic)\n");
  153. #endif
  154. } else
  155. fprintf(f, " (unknown)\n");
  156. print_features(sb, f);
  157. print_mntopts(sb, f);
  158. fprintf(f, "Filesystem state: ");
  159. print_fs_state (f, sb->s_state);
  160. fprintf(f, "\nErrors behavior: ");
  161. print_fs_errors(f, sb->s_errors);
  162. str = e2p_os2string(sb->s_creator_os);
  163. fprintf(f,
  164. "\n"
  165. "Filesystem OS type: %s\n"
  166. "Inode count: %u\n"
  167. "Block count: %u\n"
  168. "Reserved block count: %u\n"
  169. "Free blocks: %u\n"
  170. "Free inodes: %u\n"
  171. "First block: %u\n"
  172. "Block size: %u\n"
  173. "Fragment size: %u\n",
  174. str, sb->s_inodes_count, sb->s_blocks_count, sb->s_r_blocks_count,
  175. sb->s_free_blocks_count, sb->s_free_inodes_count,
  176. sb->s_first_data_block, EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
  177. free(str);
  178. if (sb->s_reserved_gdt_blocks)
  179. fprintf(f, "Reserved GDT blocks: %u\n",
  180. sb->s_reserved_gdt_blocks);
  181. fprintf(f,
  182. "Blocks per group: %u\n"
  183. "Fragments per group: %u\n"
  184. "Inodes per group: %u\n"
  185. "Inode blocks per group: %u\n",
  186. sb->s_blocks_per_group, sb->s_frags_per_group,
  187. sb->s_inodes_per_group, inode_blocks_per_group);
  188. if (sb->s_first_meta_bg)
  189. fprintf(f, "First meta block group: %u\n",
  190. sb->s_first_meta_bg);
  191. if (sb->s_mkfs_time) {
  192. tm = sb->s_mkfs_time;
  193. fprintf(f, "Filesystem created: %s", ctime(&tm));
  194. }
  195. tm = sb->s_mtime;
  196. fprintf(f, "Last mount time: %s",
  197. sb->s_mtime ? ctime(&tm) : "n/a\n");
  198. tm = sb->s_wtime;
  199. fprintf(f,
  200. "Last write time: %s"
  201. "Mount count: %u\n"
  202. "Maximum mount count: %d\n",
  203. ctime(&tm), sb->s_mnt_count, sb->s_max_mnt_count);
  204. tm = sb->s_lastcheck;
  205. fprintf(f,
  206. "Last checked: %s"
  207. "Check interval: %u (%s)\n",
  208. ctime(&tm),
  209. sb->s_checkinterval, interval_string(sb->s_checkinterval));
  210. if (sb->s_checkinterval)
  211. {
  212. time_t next;
  213. next = sb->s_lastcheck + sb->s_checkinterval;
  214. fprintf(f, "Next check after: %s", ctime(&next));
  215. }
  216. fprintf(f, "Reserved blocks uid: ");
  217. print_user(sb->s_def_resuid, f);
  218. fprintf(f, "Reserved blocks gid: ");
  219. print_group(sb->s_def_resgid, f);
  220. if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
  221. fprintf(f,
  222. "First inode: %d\n"
  223. "Inode size: %d\n",
  224. sb->s_first_ino, sb->s_inode_size);
  225. }
  226. if (!e2p_is_null_uuid(sb->s_journal_uuid))
  227. fprintf(f, "Journal UUID: %s\n",
  228. e2p_uuid2str(sb->s_journal_uuid));
  229. if (sb->s_journal_inum)
  230. fprintf(f, "Journal inode: %u\n",
  231. sb->s_journal_inum);
  232. if (sb->s_journal_dev)
  233. fprintf(f, "Journal device: 0x%04x\n",
  234. sb->s_journal_dev);
  235. if (sb->s_last_orphan)
  236. fprintf(f, "First orphan inode: %u\n",
  237. sb->s_last_orphan);
  238. if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
  239. sb->s_def_hash_version)
  240. fprintf(f, "Default directory hash: %s\n",
  241. e2p_hash2string(sb->s_def_hash_version));
  242. if (!e2p_is_null_uuid(sb->s_hash_seed))
  243. fprintf(f, "Directory Hash Seed: %s\n",
  244. e2p_uuid2str(sb->s_hash_seed));
  245. if (sb->s_jnl_backup_type) {
  246. fprintf(f, "Journal backup: ");
  247. if (sb->s_jnl_backup_type == 1)
  248. fprintf(f, "inode blocks\n");
  249. else
  250. fprintf(f, "type %u\n", sb->s_jnl_backup_type);
  251. }
  252. }