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];
37 } else if (c
< 0x80) {
40 str
[j
++] = (uint8_t) c
;
41 } else if (c
< 0x800) {
44 str
[j
++] = (uint8_t) (0xc0 | (c
>> 6));
45 str
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
49 str
[j
++] = (uint8_t) (0xe0 | (c
>> 12));
50 str
[j
++] = (uint8_t) (0x80 | ((c
>> 6) & 0x3f));
51 str
[j
++] = (uint8_t) (0x80 | (c
& 0x3f));
58 static const char *usage_to_string(enum volume_id_usage usage_id
)
61 case VOLUME_ID_FILESYSTEM
:
63 case VOLUME_ID_PARTITIONTABLE
:
64 return "partitiontable";
69 case VOLUME_ID_DISKLABEL
:
71 case VOLUME_ID_CRYPTO
:
73 case VOLUME_ID_UNPROBED
:
75 case VOLUME_ID_UNUSED
:
81 void volume_id_set_usage_part(struct volume_id_partition
*part
, enum volume_id_usage usage_id
)
83 part
->usage_id
= usage_id
;
84 part
->usage
= usage_to_string(usage_id
);
87 void volume_id_set_usage(struct volume_id
*id
, enum volume_id_usage usage_id
)
89 id
->usage_id
= usage_id
;
90 id
->usage
= usage_to_string(usage_id
);
93 void volume_id_set_label_raw(struct volume_id
*id
, const uint8_t *buf
, size_t count
)
95 memcpy(id
->label_raw
, buf
, count
);
96 id
->label_raw_len
= count
;
101 static size_t strnlen(const char *s
, size_t maxlen
)
104 if (!maxlen
) return 0;
106 for (i
= 0; *s
&& i
< maxlen
; ++s
) ++i
;
111 void volume_id_set_label_string(struct volume_id
*id
, const uint8_t *buf
, size_t count
)
115 memcpy(id
->label
, buf
, count
);
117 /* remove trailing whitespace */
118 i
= strnlen(id
->label
, count
);
120 if (!isspace(id
->label
[i
]))
123 id
->label
[i
+1] = '\0';
126 void volume_id_set_label_unicode16(struct volume_id
*id
, const uint8_t *buf
, enum endian endianess
, size_t count
)
128 volume_id_set_unicode16(id
->label
, sizeof(id
->label
), buf
, endianess
, count
);
131 void volume_id_set_uuid(struct volume_id
*id
, const uint8_t *buf
, enum uuid_format format
)
147 case UUID_DCE_STRING
:
148 /* 36 is ok, id->uuid has one extra byte for NUL */
149 count
= VOLUME_ID_UUID_SIZE
;
152 // memcpy(id->uuid_raw, buf, count);
153 // id->uuid_raw_len = count;
155 /* if set, create string in the same format, the native platform uses */
156 for (i
= 0; i
< count
; i
++)
159 return; /* all bytes are zero, leave it empty ("") */
164 sprintf(id
->uuid
, "%02X%02X-%02X%02X",
165 buf
[3], buf
[2], buf
[1], buf
[0]);
168 sprintf(id
->uuid
, "%02X%02X%02X%02X%02X%02X%02X%02X",
169 buf
[7], buf
[6], buf
[5], buf
[4],
170 buf
[3], buf
[2], buf
[1], buf
[0]);
173 sprintf(id
->uuid
, "%02X%02X%02X%02X%02X%02X%02X%02X",
174 buf
[0], buf
[1], buf
[2], buf
[3],
175 buf
[4], buf
[5], buf
[6], buf
[7]);
179 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
180 buf
[0], buf
[1], buf
[2], buf
[3],
184 buf
[10], buf
[11], buf
[12], buf
[13], buf
[14], buf
[15]);
186 case UUID_DCE_STRING
:
187 memcpy(id
->uuid
, buf
, count
);
188 id
->uuid
[count
] = '\0';
193 /* Do not use xlseek here. With it, single corrupted filesystem
194 * may result in attempt to seek past device -> exit.
195 * It's better to ignore such fs and continue. */
196 void *volume_id_get_buffer(struct volume_id
*id
, uint64_t off
, size_t len
)
202 dbg("get buffer off 0x%llx(%llu), len 0x%zx",
203 (unsigned long long) off
, (unsigned long long) off
, len
);
205 /* check if requested area fits in superblock buffer */
206 if (off
+ len
<= SB_BUFFER_SIZE
207 /* && off <= SB_BUFFER_SIZE - want this paranoid overflow check? */
209 if (id
->sbbuf
== NULL
) {
210 id
->sbbuf
= xmalloc(SB_BUFFER_SIZE
);
215 /* check if we need to read */
217 if (len
<= id
->sbbuf_len
)
218 goto ret
; /* we already have it */
220 dbg("read sbbuf len:0x%x", (unsigned) len
);
226 if (len
> SEEK_BUFFER_SIZE
) {
227 dbg("seek buffer too small %d", SEEK_BUFFER_SIZE
);
232 /* check if we need to read */
233 if ((off
>= id
->seekbuf_off
)
234 && ((off
+ len
) <= (id
->seekbuf_off
+ id
->seekbuf_len
))
236 small_off
= off
- id
->seekbuf_off
; /* can't overflow */
237 goto ret
; /* we already have it */
240 id
->seekbuf_off
= off
;
241 id
->seekbuf_len
= len
;
242 id
->seekbuf
= xrealloc(id
->seekbuf
, len
);
245 dbg("read seekbuf off:0x%llx len:0x%zx",
246 (unsigned long long) off
, len
);
248 if (lseek(id
->fd
, off
, SEEK_SET
) != off
) {
249 dbg("seek(0x%llx) failed", (unsigned long long) off
);
252 read_len
= full_read(id
->fd
, dst
, len
);
253 if (read_len
!= len
) {
254 dbg("requested 0x%x bytes, got 0x%x bytes",
255 (unsigned) len
, (unsigned) read_len
);
257 /* No filesystem can be this tiny. It's most likely
258 * non-associated loop device, empty drive and so on.
259 * Flag it, making it possible to short circuit future
260 * accesses. Rationale:
261 * users complained of slow blkid due to empty floppy drives.
265 /* id->seekbuf_len or id->sbbuf_len is wrong now! Fixing. */
266 volume_id_free_buffer(id
);
270 return dst
+ small_off
;
273 void volume_id_free_buffer(struct volume_id
*id
)
281 id
->seekbuf_off
= 0; /* paranoia */