blob: 71f8f51264fc0ad415b6366f2212edda3adbd2b1 [file] [log] [blame]
Mike Frysingerd89e6292005-04-24 05:07:59 +00001/*
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
14#include <stdio.h>
15#include <sys/types.h>
16#include <string.h>
17#include <grp.h>
18#include <pwd.h>
19#include <time.h>
20
21#include "e2p.h"
22
Mike Frysinger85cffcc2005-06-11 00:08:50 +000023static void print_user(unsigned short uid, FILE *f)
Mike Frysingerd89e6292005-04-24 05:07:59 +000024{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000025 struct passwd *pw = getpwuid(uid);
26 fprintf(f, "%u (user %s)\n", uid,
27 (pw == NULL ? "unknown" : pw->pw_name));
Mike Frysingerd89e6292005-04-24 05:07:59 +000028}
29
Mike Frysinger85cffcc2005-06-11 00:08:50 +000030static void print_group(unsigned short gid, FILE *f)
Mike Frysingerd89e6292005-04-24 05:07:59 +000031{
Mike Frysinger85cffcc2005-06-11 00:08:50 +000032 struct group *gr = getgrgid(gid);
33 fprintf(f, "%u (group %s)\n", gid,
34 (gr == NULL ? "unknown" : gr->gr_name));
Mike Frysingerd89e6292005-04-24 05:07:59 +000035}
36
37#define MONTH_INT (86400 * 30)
38#define WEEK_INT (86400 * 7)
39#define DAY_INT (86400)
40#define HOUR_INT (60 * 60)
41#define MINUTE_INT (60)
42
43static const char *interval_string(unsigned int secs)
44{
45 static char buf[256], tmp[80];
46 int hr, min, num;
47
48 buf[0] = 0;
49
50 if (secs == 0)
51 return "<none>";
52
53 if (secs >= MONTH_INT) {
54 num = secs / MONTH_INT;
55 secs -= num*MONTH_INT;
56 sprintf(buf, "%d month%s", num, (num>1) ? "s" : "");
57 }
58 if (secs >= WEEK_INT) {
59 num = secs / WEEK_INT;
60 secs -= num*WEEK_INT;
61 sprintf(tmp, "%s%d week%s", buf[0] ? ", " : "",
62 num, (num>1) ? "s" : "");
63 strcat(buf, tmp);
64 }
65 if (secs >= DAY_INT) {
66 num = secs / DAY_INT;
67 secs -= num*DAY_INT;
68 sprintf(tmp, "%s%d day%s", buf[0] ? ", " : "",
69 num, (num>1) ? "s" : "");
70 strcat(buf, tmp);
71 }
72 if (secs > 0) {
73 hr = secs / HOUR_INT;
74 secs -= hr*HOUR_INT;
75 min = secs / MINUTE_INT;
76 secs -= min*MINUTE_INT;
77 sprintf(tmp, "%s%d:%02d:%02d", buf[0] ? ", " : "",
78 hr, min, secs);
79 strcat(buf, tmp);
80 }
81 return buf;
82}
83
84static void print_features(struct ext2_super_block * s, FILE *f)
85{
86#ifdef EXT2_DYNAMIC_REV
87 int i, j, printed=0;
88 __u32 *mask = &s->s_feature_compat, m;
89
90 fprintf(f, "Filesystem features: ");
91 for (i=0; i <3; i++,mask++) {
92 for (j=0,m=1; j < 32; j++, m<<=1) {
93 if (*mask & m) {
94 fprintf(f, " %s", e2p_feature2string(i, m));
95 printed++;
96 }
97 }
98 }
99 if (printed == 0)
100 fprintf(f, " (none)");
101 fprintf(f, "\n");
102#endif
103}
104
105static void print_mntopts(struct ext2_super_block * s, FILE *f)
106{
107#ifdef EXT2_DYNAMIC_REV
108 int i, printed=0;
109 __u32 mask = s->s_default_mount_opts, m;
110
111 fprintf(f, "Default mount options: ");
112 if (mask & EXT3_DEFM_JMODE) {
113 fprintf(f, " %s", e2p_mntopt2string(mask & EXT3_DEFM_JMODE));
114 printed++;
115 }
116 for (i=0,m=1; i < 32; i++, m<<=1) {
117 if (m & EXT3_DEFM_JMODE)
118 continue;
119 if (mask & m) {
120 fprintf(f, " %s", e2p_mntopt2string(m));
121 printed++;
122 }
123 }
124 if (printed == 0)
125 fprintf(f, " (none)");
126 fprintf(f, "\n");
127#endif
128}
129
130
131#ifndef EXT2_INODE_SIZE
132#define EXT2_INODE_SIZE(s) sizeof(struct ext2_inode)
133#endif
134
135#ifndef EXT2_GOOD_OLD_REV
136#define EXT2_GOOD_OLD_REV 0
137#endif
138
139void list_super2(struct ext2_super_block * sb, FILE *f)
140{
141 int inode_blocks_per_group;
142 char buf[80], *str;
143 time_t tm;
144
145 inode_blocks_per_group = (((sb->s_inodes_per_group *
146 EXT2_INODE_SIZE(sb)) +
147 EXT2_BLOCK_SIZE(sb) - 1) /
148 EXT2_BLOCK_SIZE(sb));
149 if (sb->s_volume_name[0]) {
150 memset(buf, 0, sizeof(buf));
151 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
152 } else
153 strcpy(buf, "<none>");
154 fprintf(f, "Filesystem volume name: %s\n", buf);
155 if (sb->s_last_mounted[0]) {
156 memset(buf, 0, sizeof(buf));
157 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
158 } else
159 strcpy(buf, "<not available>");
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000160 fprintf(f,
161 "Last mounted on: %s\n"
162 "Filesystem UUID: %s\n"
163 "Filesystem magic number: 0x%04X\n"
164 "Filesystem revision #: %d",
165 buf, e2p_uuid2str(sb->s_uuid), sb->s_magic, sb->s_rev_level);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000166 if (sb->s_rev_level == EXT2_GOOD_OLD_REV) {
167 fprintf(f, " (original)\n");
168#ifdef EXT2_DYNAMIC_REV
169 } else if (sb->s_rev_level == EXT2_DYNAMIC_REV) {
170 fprintf(f, " (dynamic)\n");
171#endif
172 } else
173 fprintf(f, " (unknown)\n");
174 print_features(sb, f);
175 print_mntopts(sb, f);
176 fprintf(f, "Filesystem state: ");
177 print_fs_state (f, sb->s_state);
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000178 fprintf(f, "\nErrors behavior: ");
Mike Frysingerd89e6292005-04-24 05:07:59 +0000179 print_fs_errors(f, sb->s_errors);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000180 str = e2p_os2string(sb->s_creator_os);
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000181 fprintf(f,
182 "\n"
183 "Filesystem OS type: %s\n"
184 "Inode count: %u\n"
185 "Block count: %u\n"
186 "Reserved block count: %u\n"
187 "Free blocks: %u\n"
188 "Free inodes: %u\n"
189 "First block: %u\n"
190 "Block size: %u\n"
191 "Fragment size: %u\n",
192 str, sb->s_inodes_count, sb->s_blocks_count, sb->s_r_blocks_count,
193 sb->s_free_blocks_count, sb->s_free_inodes_count,
194 sb->s_first_data_block, EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
Mike Frysingerd89e6292005-04-24 05:07:59 +0000195 free(str);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000196 if (sb->s_reserved_gdt_blocks)
197 fprintf(f, "Reserved GDT blocks: %u\n",
198 sb->s_reserved_gdt_blocks);
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000199 fprintf(f,
200 "Blocks per group: %u\n"
201 "Fragments per group: %u\n"
202 "Inodes per group: %u\n"
203 "Inode blocks per group: %u\n",
204 sb->s_blocks_per_group, sb->s_frags_per_group,
205 sb->s_inodes_per_group, inode_blocks_per_group);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000206 if (sb->s_first_meta_bg)
207 fprintf(f, "First meta block group: %u\n",
208 sb->s_first_meta_bg);
209 if (sb->s_mkfs_time) {
210 tm = sb->s_mkfs_time;
211 fprintf(f, "Filesystem created: %s", ctime(&tm));
212 }
213 tm = sb->s_mtime;
214 fprintf(f, "Last mount time: %s",
215 sb->s_mtime ? ctime(&tm) : "n/a\n");
216 tm = sb->s_wtime;
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000217 fprintf(f,
218 "Last write time: %s"
219 "Mount count: %u\n"
220 "Maximum mount count: %d\n",
221 ctime(&tm), sb->s_mnt_count, sb->s_max_mnt_count);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000222 tm = sb->s_lastcheck;
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000223 fprintf(f,
224 "Last checked: %s"
225 "Check interval: %u (%s)\n",
226 ctime(&tm),
227 sb->s_checkinterval, interval_string(sb->s_checkinterval));
Mike Frysingerd89e6292005-04-24 05:07:59 +0000228 if (sb->s_checkinterval)
229 {
230 time_t next;
231
232 next = sb->s_lastcheck + sb->s_checkinterval;
233 fprintf(f, "Next check after: %s", ctime(&next));
234 }
235 fprintf(f, "Reserved blocks uid: ");
236 print_user(sb->s_def_resuid, f);
237 fprintf(f, "Reserved blocks gid: ");
238 print_group(sb->s_def_resgid, f);
239 if (sb->s_rev_level >= EXT2_DYNAMIC_REV) {
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000240 fprintf(f,
241 "First inode: %d\n"
242 "Inode size: %d\n",
243 sb->s_first_ino, sb->s_inode_size);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000244 }
245 if (!e2p_is_null_uuid(sb->s_journal_uuid))
246 fprintf(f, "Journal UUID: %s\n",
247 e2p_uuid2str(sb->s_journal_uuid));
248 if (sb->s_journal_inum)
249 fprintf(f, "Journal inode: %u\n",
250 sb->s_journal_inum);
251 if (sb->s_journal_dev)
252 fprintf(f, "Journal device: 0x%04x\n",
253 sb->s_journal_dev);
254 if (sb->s_last_orphan)
255 fprintf(f, "First orphan inode: %u\n",
256 sb->s_last_orphan);
257 if ((sb->s_feature_compat & EXT2_FEATURE_COMPAT_DIR_INDEX) ||
258 sb->s_def_hash_version)
259 fprintf(f, "Default directory hash: %s\n",
260 e2p_hash2string(sb->s_def_hash_version));
261 if (!e2p_is_null_uuid(sb->s_hash_seed))
262 fprintf(f, "Directory Hash Seed: %s\n",
263 e2p_uuid2str(sb->s_hash_seed));
264 if (sb->s_jnl_backup_type) {
265 fprintf(f, "Journal backup: ");
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000266 if (sb->s_jnl_backup_type == 1)
Mike Frysingerd89e6292005-04-24 05:07:59 +0000267 fprintf(f, "inode blocks\n");
Mike Frysinger85cffcc2005-06-11 00:08:50 +0000268 else
Mike Frysingerd89e6292005-04-24 05:07:59 +0000269 fprintf(f, "type %u\n", sb->s_jnl_backup_type);
Mike Frysingerd89e6292005-04-24 05:07:59 +0000270 }
271}