Mercurial > hg > toybox
annotate toys/pending/blkid.c @ 1084:4f30bf230196 draft
Cleanup blkid
author | Rob Landley <rob@landley.net> |
---|---|
date | Tue, 08 Oct 2013 12:25:02 -0500 |
parents | 8f0a577dcdd3 |
children |
rev | line source |
---|---|
1081 | 1 /* blkid.c - Prints type, label and UUID of filesystem(s). |
2 * | |
3 * Copyright 2013 Brad Conroy <bconroy@uis.edu> | |
4 * | |
5 * See ftp://ftp.kernel.org/pub/linux/utils/util-linux/v2.24/libblkid-docs/api-index-full.html | |
6 | |
1083
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
7 USE_BLKID(NEWTOY(blkid, "<1", TOYFLAG_BIN)) |
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
8 USE_BLKID(OLDTOY(fstype, blkid, "<1", TOYFLAG_BIN)) |
1081 | 9 |
10 config BLKID | |
11 bool "blkid" | |
12 default n | |
13 help | |
14 usage: blkid [block device...] | |
15 Prints type, label and UUID of filesystem. | |
1083
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
16 */ |
1081 | 17 |
18 #define FOR_blkid | |
19 #include "toys.h" | |
20 | |
1084 | 21 struct fstype { |
22 char *name; | |
23 uint64_t magic; | |
24 int magic_len, magic_offset, uuid_off, label_len, label_off; | |
1083
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
25 }; |
1081 | 26 |
1084 | 27 static const struct fstype fstypes[] = { |
28 // ext3 = buf[1116]&4 ext4 = buf[1120]&64 | |
29 {"ext2", 0xEF53, 2, 1080, 1128, 16, 1144}, | |
30 // label actually 8/16 0x4d80 but horrible: 16 bit wide characters via | |
31 // codepage, something called a uuid that's only 8 bytes long... | |
32 {"ntfs", 0x5346544e, 4, 3, 0x48+(8<<24), 0, 0}, | |
1081 | 33 |
1084 | 34 {"adfs", 0xadf5, 2, 0xc00, 0,0,0}, |
35 {"bfs", 0x1badface, 4, 0, 0,0,0}, | |
36 {"btrfs", 0x4D5F53665248425FULL, 8, 65600, 65803, 256, 65819}, | |
37 {"cramfs", 0x28cd3d45, 4, 0, 0, 16, 48}, | |
38 {"f2fs", 0xF2F52010, 4, 1024, 1132, 16, 1110}, | |
39 {"jfs", 0x3153464a, 4, 32768, 32920, 16, 32904}, | |
40 {"nilfs", 0x3434, 2, 1030, 1176, 80, 1192}, | |
41 {"murderfs", 0x724573496552, 6, 8244, 8276, 16, 8292}, | |
42 {"murderfs", 0x724573496552, 6, 65588, 65620, 16, 65536}, | |
43 {"romfs", 0x2d6d6f72, 4, 0, 0,0,0}, | |
44 {"squashfs", 0x73717368, 4, 0, 0,0,0}, | |
45 {"xiafs", 0x012fd16d, 4, 572, 0,0,0}, | |
46 {"xfs", 0x42534658, 4, 0, 32, 12, 108}, | |
47 {"vfat", 0x3233544146, 5, 82, 67+(4<<24), 11, 71}, // fat32 | |
48 {"vfat", 0x31544146, 4, 54, 39+(4<<24), 11, 43} // fat1 | |
49 }; | |
1081 | 50 |
51 /* TODO if no args use proc/partitions */ | |
1083
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
52 void do_blkid(int fd, char *name) |
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
53 { |
1084 | 54 int off, i, j; |
55 char *type; | |
56 | |
57 off = i = 0; | |
58 | |
59 for (;;) { | |
60 int pass = 0, len; | |
61 | |
62 // Read next block of data | |
63 len = readall(fd, toybuf, sizeof(toybuf)); | |
64 if (len != sizeof(toybuf)) return; | |
1081 | 65 |
1084 | 66 // Iterate through types in range |
67 for (i=0; i < sizeof(fstypes)/sizeof(struct fstype); i++) { | |
68 uint64_t test; | |
69 | |
70 // Skip tests not in this 4k block | |
71 if (fstypes[i].magic_offset > off+sizeof(toybuf)) { | |
72 pass++; | |
73 continue; | |
74 } | |
75 if (fstypes[i].magic_offset < off) continue; | |
76 | |
77 // Populate 64 bit little endian magic value | |
78 test = 0; | |
79 for (j = 0; j < fstypes[i].magic_len; j++) | |
80 test += ((uint64_t)toybuf[j+fstypes[i].magic_offset-off])<<(8*j); | |
81 if (test == fstypes[i].magic) break; | |
82 } | |
1081 | 83 |
1084 | 84 if (i == sizeof(fstypes)/sizeof(struct fstype)) { |
85 off += len; | |
86 if (pass) continue; | |
87 return; | |
88 } | |
89 break; | |
90 } | |
91 | |
92 // distinguish ext2/3/4 | |
93 type = fstypes[i].name; | |
94 if (!i) { | |
95 if (toybuf[1116]&4) type = "ext3"; | |
96 if (toybuf[1120]&64) type = "ext4"; | |
97 } | |
98 | |
99 // Could special case NTFS here... | |
1081 | 100 |
1084 | 101 // Output for fstype |
102 if (*toys.which->name == 'f') { | |
103 puts(type); | |
104 return; | |
105 } | |
106 | |
107 // output for blkid | |
108 printf("%s:",name); | |
109 | |
110 if (fstypes[i].label_len) | |
111 printf(" LABEL=\"%.*s\"", fstypes[i].label_len, | |
112 toybuf+fstypes[i].label_off-off); | |
1083
8f0a577dcdd3
blkid cleanup: more whitespace (including fixing a strange unindented if block), convert strange fstype inclusion to OLDTOY(), use "<1" arg so calling with no arguments doesn't hang, convert typedef to normal struct.
Rob Landley <rob@landley.net>
parents:
1082
diff
changeset
|
113 |
1084 | 114 if (fstypes[i].uuid_off) { |
115 int bits = 0x550, size = fstypes[i].uuid_off >> 24, | |
116 uoff = (fstypes[i].uuid_off & ((1<<24)-1))-off; | |
117 | |
118 if (size) bits = 4*(size == 4); | |
119 else size = 16; | |
120 | |
121 printf(" UUID=\""); | |
122 for (j = 0; j < size; j++) printf("-%02x"+!(bits & (1<<j)), toybuf[uoff+j]); | |
123 printf("\""); | |
124 } | |
125 | |
126 printf(" TYPE=\"%s\"\n", fstypes[i].name); | |
1081 | 127 } |
128 | |
129 void blkid_main(void) | |
130 { | |
131 loopfiles(toys.optargs, do_blkid); | |
132 } |