Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / busybox / util-linux / volume_id / util.c
blob1a68436ca2be6d02e3c849babd4cba5a253c9d43
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 break;
37 if (j+1 >= len)
38 break;
39 if (c < 0x80) {
40 /* 0xxxxxxx */
41 } else {
42 uint8_t topbits = 0xc0;
43 if (j+2 >= len)
44 break;
45 if (c < 0x800) {
46 /* 110yyyxx 10xxxxxx */
47 } else {
48 if (j+3 >= len)
49 break;
50 /* 1110yyyy 10yyyyxx 10xxxxxx */
51 str[j++] = (uint8_t) (0xe0 | (c >> 12));
52 topbits = 0x80;
54 str[j++] = (uint8_t) (topbits | ((c >> 6) & 0x3f));
55 c = 0x80 | (c & 0x3f);
57 str[j++] = (uint8_t) c;
59 str[j] = '\0';
61 #endif
63 #ifndef UTIL2
64 #ifdef UNUSED
65 static const char *usage_to_string(enum volume_id_usage usage_id)
67 switch (usage_id) {
68 case VOLUME_ID_FILESYSTEM:
69 return "filesystem";
70 case VOLUME_ID_PARTITIONTABLE:
71 return "partitiontable";
72 case VOLUME_ID_OTHER:
73 return "other";
74 case VOLUME_ID_RAID:
75 return "raid";
76 case VOLUME_ID_DISKLABEL:
77 return "disklabel";
78 case VOLUME_ID_CRYPTO:
79 return "crypto";
80 case VOLUME_ID_UNPROBED:
81 return "unprobed";
82 case VOLUME_ID_UNUSED:
83 return "unused";
85 return NULL;
88 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
90 part->usage_id = usage_id;
91 part->usage = usage_to_string(usage_id);
94 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
96 id->usage_id = usage_id;
97 id->usage = usage_to_string(usage_id);
100 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
102 memcpy(id->label_raw, buf, count);
103 id->label_raw_len = count;
105 #endif
107 #ifdef NOT_NEEDED
108 static size_t strnlen(const char *s, size_t maxlen)
110 size_t i;
111 if (!maxlen) return 0;
112 if (!s) return 0;
113 for (i = 0; *s && i < maxlen; ++s) ++i;
114 return i;
116 #endif
118 #endif
119 #ifdef UTIL2
121 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
123 unsigned i;
125 memcpy(id->label, buf, count);
127 /* remove trailing whitespace */
128 i = strnlen(id->label, count);
129 while (i--) {
130 if (!isspace(id->label[i]))
131 break;
133 id->label[i+1] = '\0';
136 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
138 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
141 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
143 unsigned i;
144 unsigned count = (format == UUID_DCE_STRING ? VOLUME_ID_UUID_SIZE : 4 << format);
146 // memcpy(id->uuid_raw, buf, count);
147 // id->uuid_raw_len = count;
149 /* if set, create string in the same format, the native platform uses */
150 for (i = 0; i < count; i++)
151 if (buf[i] != 0)
152 goto set;
153 return; /* all bytes are zero, leave it empty ("") */
155 set:
156 switch (format) {
157 case UUID_DOS:
158 sprintf(id->uuid, "%02X%02X-%02X%02X",
159 buf[3], buf[2], buf[1], buf[0]);
160 break;
161 case UUID_NTFS:
162 sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X",
163 buf[7], buf[6], buf[5], buf[4],
164 buf[3], buf[2], buf[1], buf[0]);
165 break;
166 case UUID_DCE:
167 sprintf(id->uuid,
168 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
169 buf[0], buf[1], buf[2], buf[3],
170 buf[4], buf[5],
171 buf[6], buf[7],
172 buf[8], buf[9],
173 buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
174 break;
175 case UUID_DCE_STRING:
176 memcpy(id->uuid, buf, count);
177 id->uuid[count] = '\0';
178 break;
182 /* Do not use xlseek here. With it, single corrupted filesystem
183 * may result in attempt to seek past device -> exit.
184 * It's better to ignore such fs and continue. */
185 void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
187 uint8_t *dst;
188 unsigned small_off;
189 ssize_t read_len;
191 dbg("get buffer off 0x%llx(%llu), len 0x%zx",
192 (unsigned long long) off, (unsigned long long) off, len);
194 /* check if requested area fits in superblock buffer */
195 if (off + len <= SB_BUFFER_SIZE
196 /* && off <= SB_BUFFER_SIZE - want this paranoid overflow check? */
198 if (id->sbbuf == NULL) {
199 id->sbbuf = xmalloc(SB_BUFFER_SIZE);
201 small_off = off;
202 dst = id->sbbuf;
204 /* check if we need to read */
205 len += off;
206 if (len <= id->sbbuf_len)
207 goto ret; /* we already have it */
209 dbg("read sbbuf len:0x%x", (unsigned) len);
210 id->sbbuf_len = len;
211 off = 0;
212 goto do_read;
215 if (len > SEEK_BUFFER_SIZE) {
216 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
217 return NULL;
219 dst = id->seekbuf;
221 /* check if we need to read */
222 if ((off >= id->seekbuf_off)
223 && ((off + len) <= (id->seekbuf_off + id->seekbuf_len))
225 small_off = off - id->seekbuf_off; /* can't overflow */
226 goto ret; /* we already have it */
229 id->seekbuf_off = off;
230 id->seekbuf_len = len;
231 id->seekbuf = xrealloc(id->seekbuf, len);
232 small_off = 0;
233 dst = id->seekbuf;
234 dbg("read seekbuf off:0x%llx len:0x%zx",
235 (unsigned long long) off, len);
236 do_read:
237 if (lseek(id->fd, off, SEEK_SET) != off) {
238 dbg("seek(0x%llx) failed", (unsigned long long) off);
239 goto err;
241 read_len = full_read(id->fd, dst, len);
242 if (read_len != len) {
243 dbg("requested 0x%x bytes, got 0x%x bytes",
244 (unsigned) len, (unsigned) read_len);
245 err:
246 /* No filesystem can be this tiny. It's most likely
247 * non-associated loop device, empty drive and so on.
248 * Flag it, making it possible to short circuit future
249 * accesses. Rationale:
250 * users complained of slow blkid due to empty floppy drives.
252 if (off < 64*1024)
253 id->error = 1;
254 /* id->seekbuf_len or id->sbbuf_len is wrong now! Fixing. */
255 volume_id_free_buffer(id);
256 return NULL;
258 ret:
259 return dst + small_off;
262 void volume_id_free_buffer(struct volume_id *id)
264 free(id->sbbuf);
265 id->sbbuf = NULL;
266 id->sbbuf_len = 0;
267 free(id->seekbuf);
268 id->seekbuf = NULL;
269 id->seekbuf_len = 0;
270 id->seekbuf_off = 0; /* paranoia */
272 #endif