Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 1 | /* |
| 2 | * volume_id - reads filesystem label and uuid |
| 3 | * |
| 4 | * Copyright (C) 2005 Kay Sievers <kay.sievers@vrfy.org> |
| 5 | * |
| 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. |
| 10 | * |
| 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. |
| 15 | * |
| 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 |
| 19 | */ |
| 20 | |
| 21 | #include "volume_id_internal.h" |
| 22 | |
| 23 | void volume_id_set_unicode16(char *str, size_t len, const uint8_t *buf, enum endian endianess, size_t count) |
| 24 | { |
| 25 | unsigned i, j; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 26 | unsigned c; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 27 | |
| 28 | j = 0; |
| 29 | for (i = 0; i + 2 <= count; i += 2) { |
| 30 | if (endianess == LE) |
| 31 | c = (buf[i+1] << 8) | buf[i]; |
| 32 | else |
| 33 | c = (buf[i] << 8) | buf[i+1]; |
| 34 | if (c == 0) { |
| 35 | str[j] = '\0'; |
| 36 | break; |
| 37 | } else if (c < 0x80) { |
| 38 | if (j+1 >= len) |
| 39 | break; |
| 40 | str[j++] = (uint8_t) c; |
| 41 | } else if (c < 0x800) { |
| 42 | if (j+2 >= len) |
| 43 | break; |
| 44 | str[j++] = (uint8_t) (0xc0 | (c >> 6)); |
| 45 | str[j++] = (uint8_t) (0x80 | (c & 0x3f)); |
| 46 | } else { |
| 47 | if (j+3 >= len) |
| 48 | break; |
| 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)); |
| 52 | } |
| 53 | } |
| 54 | str[j] = '\0'; |
| 55 | } |
| 56 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 57 | #ifdef UNUSED |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 58 | static const char *usage_to_string(enum volume_id_usage usage_id) |
| 59 | { |
| 60 | switch (usage_id) { |
| 61 | case VOLUME_ID_FILESYSTEM: |
| 62 | return "filesystem"; |
| 63 | case VOLUME_ID_PARTITIONTABLE: |
| 64 | return "partitiontable"; |
| 65 | case VOLUME_ID_OTHER: |
| 66 | return "other"; |
| 67 | case VOLUME_ID_RAID: |
| 68 | return "raid"; |
| 69 | case VOLUME_ID_DISKLABEL: |
| 70 | return "disklabel"; |
| 71 | case VOLUME_ID_CRYPTO: |
| 72 | return "crypto"; |
| 73 | case VOLUME_ID_UNPROBED: |
| 74 | return "unprobed"; |
| 75 | case VOLUME_ID_UNUSED: |
| 76 | return "unused"; |
| 77 | } |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id) |
| 82 | { |
| 83 | part->usage_id = usage_id; |
| 84 | part->usage = usage_to_string(usage_id); |
| 85 | } |
| 86 | |
| 87 | void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id) |
| 88 | { |
| 89 | id->usage_id = usage_id; |
| 90 | id->usage = usage_to_string(usage_id); |
| 91 | } |
| 92 | |
| 93 | void volume_id_set_label_raw(struct volume_id *id, const uint8_t *buf, size_t count) |
| 94 | { |
| 95 | memcpy(id->label_raw, buf, count); |
| 96 | id->label_raw_len = count; |
| 97 | } |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 98 | #endif |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 99 | |
| 100 | #ifdef NOT_NEEDED |
| 101 | static size_t strnlen(const char *s, size_t maxlen) |
| 102 | { |
| 103 | size_t i; |
| 104 | if (!maxlen) return 0; |
| 105 | if (!s) return 0; |
| 106 | for (i = 0; *s && i < maxlen; ++s) ++i; |
| 107 | return i; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | void volume_id_set_label_string(struct volume_id *id, const uint8_t *buf, size_t count) |
| 112 | { |
| 113 | unsigned i; |
| 114 | |
| 115 | memcpy(id->label, buf, count); |
| 116 | |
| 117 | /* remove trailing whitespace */ |
| 118 | i = strnlen(id->label, count); |
| 119 | while (i--) { |
| 120 | if (!isspace(id->label[i])) |
| 121 | break; |
| 122 | } |
| 123 | id->label[i+1] = '\0'; |
| 124 | } |
| 125 | |
| 126 | void volume_id_set_label_unicode16(struct volume_id *id, const uint8_t *buf, enum endian endianess, size_t count) |
| 127 | { |
| 128 | volume_id_set_unicode16(id->label, sizeof(id->label), buf, endianess, count); |
| 129 | } |
| 130 | |
| 131 | void volume_id_set_uuid(struct volume_id *id, const uint8_t *buf, enum uuid_format format) |
| 132 | { |
| 133 | unsigned i; |
| 134 | unsigned count = 0; |
| 135 | |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 136 | switch (format) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 137 | case UUID_DOS: |
| 138 | count = 4; |
| 139 | break; |
| 140 | case UUID_NTFS: |
| 141 | case UUID_HFS: |
| 142 | count = 8; |
| 143 | break; |
| 144 | case UUID_DCE: |
| 145 | count = 16; |
| 146 | break; |
| 147 | case UUID_DCE_STRING: |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 148 | /* 36 is ok, id->uuid has one extra byte for NUL */ |
| 149 | count = VOLUME_ID_UUID_SIZE; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 150 | break; |
| 151 | } |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 152 | // memcpy(id->uuid_raw, buf, count); |
| 153 | // id->uuid_raw_len = count; |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 154 | |
| 155 | /* if set, create string in the same format, the native platform uses */ |
| 156 | for (i = 0; i < count; i++) |
| 157 | if (buf[i] != 0) |
| 158 | goto set; |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 159 | return; /* all bytes are zero, leave it empty ("") */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 160 | |
| 161 | set: |
Denis Vlasenko | c5b7372 | 2008-03-17 09:21:26 +0000 | [diff] [blame] | 162 | switch (format) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 163 | case UUID_DOS: |
| 164 | sprintf(id->uuid, "%02X%02X-%02X%02X", |
| 165 | buf[3], buf[2], buf[1], buf[0]); |
| 166 | break; |
| 167 | case UUID_NTFS: |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 168 | sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X", |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 169 | buf[7], buf[6], buf[5], buf[4], |
| 170 | buf[3], buf[2], buf[1], buf[0]); |
| 171 | break; |
| 172 | case UUID_HFS: |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 173 | sprintf(id->uuid, "%02X%02X%02X%02X%02X%02X%02X%02X", |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 174 | buf[0], buf[1], buf[2], buf[3], |
| 175 | buf[4], buf[5], buf[6], buf[7]); |
| 176 | break; |
| 177 | case UUID_DCE: |
| 178 | sprintf(id->uuid, |
| 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], |
| 181 | buf[4], buf[5], |
| 182 | buf[6], buf[7], |
| 183 | buf[8], buf[9], |
Denis Vlasenko | d5e3059 | 2008-10-12 11:17:49 +0000 | [diff] [blame^] | 184 | buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]); |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 185 | break; |
| 186 | case UUID_DCE_STRING: |
| 187 | memcpy(id->uuid, buf, count); |
| 188 | id->uuid[count] = '\0'; |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
Denis Vlasenko | d5e3059 | 2008-10-12 11:17:49 +0000 | [diff] [blame^] | 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. */ |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 196 | void *volume_id_get_buffer(struct volume_id *id, uint64_t off, size_t len) |
| 197 | { |
| 198 | ssize_t buf_len; |
| 199 | |
| 200 | dbg("get buffer off 0x%llx(%llu), len 0x%zx", (unsigned long long) off, (unsigned long long) off, len); |
| 201 | /* check if requested area fits in superblock buffer */ |
| 202 | if (off + len <= SB_BUFFER_SIZE) { |
| 203 | if (id->sbbuf == NULL) { |
| 204 | id->sbbuf = xmalloc(SB_BUFFER_SIZE); |
| 205 | } |
| 206 | |
| 207 | /* check if we need to read */ |
| 208 | if ((off + len) > id->sbbuf_len) { |
| 209 | dbg("read sbbuf len:0x%llx", (unsigned long long) (off + len)); |
Denis Vlasenko | d5e3059 | 2008-10-12 11:17:49 +0000 | [diff] [blame^] | 210 | if (lseek(id->fd, 0, SEEK_SET) != 0) { |
| 211 | dbg("seek(0) failed"); |
| 212 | return NULL; |
| 213 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 214 | buf_len = full_read(id->fd, id->sbbuf, off + len); |
| 215 | if (buf_len < 0) { |
| 216 | dbg("read failed (%s)", strerror(errno)); |
| 217 | return NULL; |
| 218 | } |
| 219 | dbg("got 0x%zx (%zi) bytes", buf_len, buf_len); |
| 220 | id->sbbuf_len = buf_len; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 221 | if ((uint64_t)buf_len < off + len) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 222 | dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len); |
| 223 | return NULL; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return &(id->sbbuf[off]); |
| 228 | } |
| 229 | |
| 230 | if (len > SEEK_BUFFER_SIZE) { |
| 231 | dbg("seek buffer too small %d", SEEK_BUFFER_SIZE); |
| 232 | return NULL; |
| 233 | } |
| 234 | |
| 235 | /* get seek buffer */ |
| 236 | if (id->seekbuf == NULL) { |
| 237 | id->seekbuf = xmalloc(SEEK_BUFFER_SIZE); |
| 238 | } |
| 239 | |
| 240 | /* check if we need to read */ |
| 241 | if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) { |
| 242 | dbg("read seekbuf off:0x%llx len:0x%zx", (unsigned long long) off, len); |
Denis Vlasenko | d5e3059 | 2008-10-12 11:17:49 +0000 | [diff] [blame^] | 243 | if (lseek(id->fd, off, SEEK_SET) != off) { |
| 244 | dbg("seek(0x%llx) failed", (unsigned long long) off); |
| 245 | return NULL; |
| 246 | } |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 247 | buf_len = full_read(id->fd, id->seekbuf, len); |
| 248 | if (buf_len < 0) { |
| 249 | dbg("read failed (%s)", strerror(errno)); |
| 250 | return NULL; |
| 251 | } |
| 252 | dbg("got 0x%zx (%zi) bytes", buf_len, buf_len); |
| 253 | id->seekbuf_off = off; |
| 254 | id->seekbuf_len = buf_len; |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 255 | if ((size_t)buf_len < len) { |
Denis Vlasenko | de7684a | 2008-02-18 21:08:49 +0000 | [diff] [blame] | 256 | dbg("requested 0x%zx bytes, got only 0x%zx bytes", len, buf_len); |
| 257 | return NULL; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | return &(id->seekbuf[off - id->seekbuf_off]); |
| 262 | } |
| 263 | |
| 264 | void volume_id_free_buffer(struct volume_id *id) |
| 265 | { |
| 266 | free(id->sbbuf); |
| 267 | id->sbbuf = NULL; |
| 268 | id->sbbuf_len = 0; |
| 269 | free(id->seekbuf); |
| 270 | id->seekbuf = NULL; |
| 271 | id->seekbuf_len = 0; |
| 272 | } |