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 void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count)
29 for (i = 0; i + 2 <= count; i += 2) {
31 c = (buf[i+1] << 8) | buf[i];
33 c = (buf[i] << 8) | buf[i+1];
41 uint8_t topbits = 0xc0;
45 /* 110yyyxx 10xxxxxx */
49 /* 1110yyyy 10yyyyxx 10xxxxxx */
50 str[j++] = (uint8_t) (0xe0 | (c >> 12));
53 str[j++] = (uint8_t) (topbits | ((c >> 6) & 0x3f));
54 c = 0x80 | (c & 0x3f);
56 str[j++] = (uint8_t) c;
62 static const char *usage_to_string(enum volume_id_usage usage_id)
65 case VOLUME_ID_FILESYSTEM:
67 case VOLUME_ID_PARTITIONTABLE:
68 return "partitiontable";
73 case VOLUME_ID_DISKLABEL:
75 case VOLUME_ID_CRYPTO:
77 case VOLUME_ID_UNPROBED:
79 case VOLUME_ID_UNUSED:
85 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
87 part->usage_id = usage_id;
88 part->usage = usage_to_string(usage_id);
91 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
93 id->usage_id = usage_id;
94 id->usage = usage_to_string(usage_id);
97 void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count)
99 memcpy(id->label_raw, buf, count);
100 id->label_raw_len = count;
105 static size_t strnlen(const char *s, size_t maxlen)
108 if (!maxlen) return 0;
110 for (i = 0; *s && i < maxlen; ++s) ++i;
115 void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count)
119 memcpy(id->label, buf, count);
121 /* remove trailing whitespace */
122 i = strnlen(id->label, count);
124 if (!isspace(id->label[i]))
127 id->label[i+1] = '\0';
130 void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count)
132 volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count);
135 void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format)
150 case UUID_DCE_STRING:
151 /* 36 is ok, id->uuid has one extra byte for NUL */
152 count = VOLUME_ID_UUID_SIZE;
155 // memcpy(id->uuid_raw, buf, count);
156 // id->uuid_raw_len = count;
158 /* if set, create string in the same format, the native platform uses */
159 for (i = 0; i < count; i++)
162 return; /* all bytes are zero, leave it empty ("") */
167 sprintf(id->uuid, "%02X%02X-%02X%02X",
168 buf[3], buf[2], buf[1], buf[0]);
171 sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X",
172 buf[7], buf[6], buf[5], buf[4],
173 buf[3], buf[2], buf[1], buf[0]);
177 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
178 buf[0], buf[1], buf[2], buf[3],
182 buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
184 case UUID_DCE_STRING:
185 memcpy(id->uuid, buf, count);
186 id->uuid[count] = '\0';
191 /* Do not use xlseek here. With it, single corrupted filesystem
192 * may result in attempt to seek past device -> exit.
193 * It's better to ignore such fs and continue. */
194 void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len)
200 dbg("get buffer off 0x%llx(%llu), len 0x%zx",
201 (unsigned long long) off, (unsigned long long) off, len);
203 /* check if requested area fits in superblock buffer */
204 if (off + len <= SB_BUFFER_SIZE
205 /* && off <= SB_BUFFER_SIZE - want this paranoid overflow check? */
207 if (id->sbbuf == NULL) {
208 id->sbbuf = xmalloc(SB_BUFFER_SIZE);
213 /* check if we need to read */
215 if (len <= id->sbbuf_len)
216 goto ret; /* we already have it */
218 dbg("read sbbuf len:0x%x", (unsigned) len);
224 if (len > SEEK_BUFFER_SIZE) {
225 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
230 /* check if we need to read */
231 if ((off >= id->seekbuf_off)
232 && ((off + len) <= (id->seekbuf_off + id->seekbuf_len))
234 small_off = off - id->seekbuf_off; /* can't overflow */
235 goto ret; /* we already have it */
238 id->seekbuf_off = off;
239 id->seekbuf_len = len;
240 id->seekbuf = xrealloc(id->seekbuf, len);
243 dbg("read seekbuf off:0x%llx len:0x%zx",
244 (unsigned long long) off, len);
246 if (lseek(id->fd, off, SEEK_SET) != off) {
247 dbg("seek(0x%llx) failed", (unsigned long long) off);
250 read_len = full_read(id->fd, dst, len);
251 if (read_len != len) {
252 dbg("requested 0x%x bytes, got 0x%x bytes",
253 (unsigned) len, (unsigned) read_len);
255 /* No filesystem can be this tiny. It's most likely
256 * non-associated loop device, empty drive and so on.
257 * Flag it, making it possible to short circuit future
258 * accesses. Rationale:
259 * users complained of slow blkid due to empty floppy drives.
263 /* id->seekbuf_len or id->sbbuf_len is wrong now! Fixing. */
264 volume_id_free_buffer(id);
268 return dst + small_off;
271 void volume_id_free_buffer(struct volume_id *id)
279 id->seekbuf_off = 0; /* paranoia */