3
0

ls.c 7.2 KB

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