Changes for kernel and Busybox
[tomato.git] / release / src / router / busybox / util-linux / volume_id / util.c
blob5bb897cfca588d9a1880107a241fc075ee4652aa
1 /*
2 * volume_id - reads filesystem label and uuid
4 * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "volume_id_internal.h"
23 #ifdef UTIL2
24 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
26 unsigned i, j;
27 unsigned c;
29 j = 0;
30 for (i = 0; i + 2 <= count; i += 2) {
31 if (endianess == LE)
32 c = (buf[i+1] << 8) | buf[i];
33 else
34 c = (buf[i] << 8) | buf[i+1];
35 if (c == 0) {
36 str[j] = '\0';
37 break;
38 } else if (c < 0x80) {
39 if (j+1 >= len)
40 break;
41 str[j++] = (uint8_t) c;
42 } else if (c < 0x800) {
43 if (j+2 >= len)
44 break;
45 str[j++] = (uint8_t) (0xc0 | (c >> 6));
46 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
47 } else {
48 if (j+3 >= len)
49 break;
50 str[j++] = (uint8_t) (0xe0 | (c >> 12));
51 str[j++] = (uint8_t) (0x80 | ((c >> 6) & 0x3f));
52 str[j++] = (uint8_t) (0x80 | (c & 0x3f));
55 str[j] = '\0';
57 #endif
59 #ifndef UTIL2
60 #ifdef UNUSED
61 static const char *usage_to_string(enum volume_id_usage usage_id)
63 switch (usage_id) {
64 case VOLUME_ID_FILESYSTEM:
65 return "filesystem";
66 case VOLUME_ID_PARTITIONTABLE:
67 return "partitiontable";
68 case VOLUME_ID_OTHER:
69 return "other";
70 case VOLUME_ID_RAID:
71 return "raid";
72 case VOLUME_ID_DISKLABEL:
73 return "disklabel";
74 case VOLUME_ID_CRYPTO:
75 return "crypto";
76 case VOLUME_ID_UNPROBED:
77 return "unprobed";
78 case VOLUME_ID_UNUSED:
79 return "unused";
81 return NULL;
84 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
86 part->usage_id = usage_id;
87 part->usage = usage_to_string(usage_id);
90 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
92 id->usage_id = usage_id;
93 id->usage = usage_to_string(usage_id);
96 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
98 memcpy(id->label_raw, buf, count);
99 id->label_raw_len = count;
101 #endif
103 #ifdef NOT_NEEDED
104 static size_t strnlen(const char *s, size_t maxlen)
106 size_t i;
107 if (!maxlen) return 0;
108 if (!s) return 0;
109 for (i = 0; *s && i < maxlen; ++s) ++i;
110 return i;
112 #endif
114 #endif
115 #ifdef UTIL2
117 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
119 unsigned i;
121 memcpy(id->label, buf, count);
123 /* remove trailing whitespace */
124 i = strnlen(id->label, count);
125 while (i--) {
126 if (!isspace(id->label[i]))
127 break;
129 id->label[i+1] = '\0';
132 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
134 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
137 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
139 unsigned i;
140 unsigned count = 0;
142 switch (format) {
143 case UUID_DOS:
144 count = 4;
145 break;
146 case UUID_NTFS:
147 case UUID_HFS:
148 count = 8;
149 break;
150 case UUID_DCE:
151 count = 16;
152 break;
153 case UUID_DCE_STRING:
154 /* 36 is ok, id->uuid has one extra byte for NUL */
155 count = VOLUME_ID_UUID_SIZE;
156 break;
158 // memcpy(id->uuid_raw, buf, count);
159 // id->uuid_raw_len = count;
161 /* if set, create string in the same format, the native platform uses */
162 for (i = 0; i < count; i++)
163 if (buf[i] != 0)
164 goto set;
165 return; /* all bytes are zero, leave it empty ("") */
167 set:
168 switch (format) {
169 case UUID_DOS:
170 sprintf(id->uuid, "%02X%02X-%02X%02X",
171 buf[3], buf[2], buf[1], buf[0]);
172 break;
173 case UUID_NTFS:
174 sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X",
175 buf[7], buf[6], buf[5], buf[4],
176 buf[3], buf[2], buf[1], buf[0]);
177 break;
178 case UUID_HFS:
179 sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X",
180 buf[0], buf[1], buf[2], buf[3],
181 buf[4], buf[5], buf[6], buf[7]);
182 break;
183 case UUID_DCE:
184 sprintf(id->uuid,
185 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
186 buf[0], buf[1], buf[2], buf[3],
187 buf[4], buf[5],
188 buf[6], buf[7],
189 buf[8], buf[9],
190 buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
191 break;
192 case UUID_DCE_STRING:
193 memcpy(id->uuid, buf, count);
194 id->uuid[count] = '\0';
195 break;
199 /* Do not use xlseek here. With it, single corrupted filesystem
200 * may result in attempt to seek past device -> exit.
201 * It's better to ignore such fs and continue. */
202 void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
204 uint8_t *dst;
205 unsigned small_off;
206 ssize_t read_len;
208 dbg("get buffer off 0x%llx(%llu), len 0x%zx",
209 (unsigned long long) off, (unsigned long long) off, len);
211 /* check if requested area fits in superblock buffer */
212 if (off + len <= SB_BUFFER_SIZE
213 /* && off <= SB_BUFFER_SIZE - want this paranoid overflow check? */
215 if (id->sbbuf == NULL) {
216 id->sbbuf = xmalloc(SB_BUFFER_SIZE);
218 small_off = off;
219 dst = id->sbbuf;
221 /* check if we need to read */
222 len += off;
223 if (len <= id->sbbuf_len)
224 goto ret; /* we already have it */
226 dbg("read sbbuf len:0x%x", (unsigned) len);
227 id->sbbuf_len = len;
228 off = 0;
229 goto do_read;
232 if (len > SEEK_BUFFER_SIZE) {
233 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
234 return NULL;
236 dst = id->seekbuf;
238 /* check if we need to read */
239 if ((off >= id->seekbuf_off)
240 && ((off + len) <= (id->seekbuf_off + id->seekbuf_len))
242 small_off = off - id->seekbuf_off; /* can't overflow */
243 goto ret; /* we already have it */
246 id->seekbuf_off = off;
247 id->seekbuf_len = len;
248 id->seekbuf = xrealloc(id->seekbuf, len);
249 small_off = 0;
250 dst = id->seekbuf;
251 dbg("read seekbuf off:0x%llx len:0x%zx",
252 (unsigned long long) off, len);
253 do_read:
254 if (lseek(id->fd, off, SEEK_SET) != off) {
255 dbg("seek(0x%llx) failed", (unsigned long long) off);
256 goto err;
258 read_len = full_read(id->fd, dst, len);
259 if (read_len != len) {
260 dbg("requested 0x%x bytes, got 0x%x bytes",
261 (unsigned) len, (unsigned) read_len);
262 err:
263 /* No filesystem can be this tiny. It's most likely
264 * non-associated loop device, empty drive and so on.
265 * Flag it, making it possible to short circuit future
266 * accesses. Rationale:
267 * users complained of slow blkid due to empty floppy drives.
269 if (off < 64*1024)
270 id->error = 1;
271 /* id->seekbuf_len or id->sbbuf_len is wrong now! Fixing. */
272 volume_id_free_buffer(id);
273 return NULL;
275 ret:
276 return dst + small_off;
279 void volume_id_free_buffer(struct volume_id *id)
281 free(id->sbbuf);
282 id->sbbuf = NULL;
283 id->sbbuf_len = 0;
284 free(id->seekbuf);
285 id->seekbuf = NULL;
286 id->seekbuf_len = 0;
287 id->seekbuf_off = 0; /* paranoia */
289 #endif