chiark / gitweb /
6159a5992800edcf9415555eb8f9e0391b79c1e2
[elogind.git] / extras / volume_id / volume_id / util.c
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 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE 1
23 #endif
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <fcntl.h>
36 #include <sys/stat.h>
37 #include <asm/types.h>
38
39 #include "volume_id.h"
40 #include "logging.h"
41 #include "util.h"
42
43 static char *usage_to_string(enum volume_id_usage usage_id)
44 {
45         switch (usage_id) {
46         case VOLUME_ID_FILESYSTEM:
47                 return "filesystem";
48         case VOLUME_ID_PARTITIONTABLE:
49                 return "partitiontable";
50         case VOLUME_ID_OTHER:
51                 return "other";
52         case VOLUME_ID_RAID:
53                 return "raid";
54         case VOLUME_ID_DISKLABEL:
55                 return "disklabel";
56         case VOLUME_ID_CRYPTO:
57                 return "crypto";
58         case VOLUME_ID_UNPROBED:
59                 return "unprobed";
60         case VOLUME_ID_UNUSED:
61                 return "unused";
62         }
63         return NULL;
64 }
65
66 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
67 {
68         part->usage_id = usage_id;
69         part->usage = usage_to_string(usage_id);
70 }
71
72 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
73 {
74         id->usage_id = usage_id;
75         id->usage = usage_to_string(usage_id);
76 }
77
78 void volume_id_set_label_raw(struct volume_id *id, const __u8 *buf, unsigned int count)
79 {
80         memcpy(id->label_raw, buf, count);
81         id->label_raw_len = count;
82 }
83
84 void volume_id_set_label_string(struct volume_id *id, const __u8 *buf, unsigned int count)
85 {
86         unsigned int i;
87
88         memcpy(id->label, buf, count);
89
90         /* remove trailing whitespace */
91         i = strnlen(id->label, count);
92         while (i--) {
93                 if (!isspace(id->label[i]))
94                         break;
95         }
96         id->label[i+1] = '\0';
97 }
98
99 void volume_id_set_label_unicode16(struct volume_id *id, const __u8 *buf, enum endian endianess, unsigned int count)
100 {
101         unsigned int i, j;
102         __u16 c;
103
104         j = 0;
105         for (i = 0; i + 2 <= count; i += 2) {
106                 if (endianess == LE)
107                         c = (buf[i+1] << 8) | buf[i];
108                 else
109                         c = (buf[i] << 8) | buf[i+1];
110                 if (c == 0) {
111                         id->label[j] = '\0';
112                         break;
113                 } else if (c < 0x80) {
114                         id->label[j++] = (__u8) c;
115                 } else if (c < 0x800) {
116                         id->label[j++] = (__u8) (0xc0 | (c >> 6));
117                         id->label[j++] = (__u8) (0x80 | (c & 0x3f));
118                 } else {
119                         id->label[j++] = (__u8) (0xe0 | (c >> 12));
120                         id->label[j++] = (__u8) (0x80 | ((c >> 6) & 0x3f));
121                         id->label[j++] = (__u8) (0x80 | (c & 0x3f));
122                 }
123         }
124 }
125
126 void volume_id_set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format format)
127 {
128         unsigned int i;
129         unsigned int count = 0;
130
131         switch(format) {
132         case UUID_DOS:
133                 count = 4;
134                 break;
135         case UUID_NTFS:
136         case UUID_HFS:
137                 count = 8;
138                 break;
139         case UUID_DCE:
140                 count = 16;
141         }
142         memcpy(id->uuid_raw, buf, count);
143
144         /* if set, create string in the same format, the native platform uses */
145         for (i = 0; i < count; i++)
146                 if (buf[i] != 0)
147                         goto set;
148         return;
149
150 set:
151         switch(format) {
152         case UUID_DOS:
153                 sprintf(id->uuid, "%02X%02X-%02X%02X",
154                         buf[3], buf[2], buf[1], buf[0]);
155                 break;
156         case UUID_NTFS:
157                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
158                         buf[7], buf[6], buf[5], buf[4],
159                         buf[3], buf[2], buf[1], buf[0]);
160                 break;
161         case UUID_HFS:
162                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
163                         buf[0], buf[1], buf[2], buf[3],
164                         buf[4], buf[5], buf[6], buf[7]);
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         }
176 }
177
178 __u8 *volume_id_get_buffer(struct volume_id *id, __u64 off, unsigned int len)
179 {
180         unsigned int buf_len;
181
182         dbg("get buffer off 0x%llx(%llu), len 0x%x", off, off, len);
183         /* check if requested area fits in superblock buffer */
184         if (off + len <= SB_BUFFER_SIZE) {
185                 if (id->sbbuf == NULL) {
186                         id->sbbuf = malloc(SB_BUFFER_SIZE);
187                         if (id->sbbuf == NULL)
188                                 return NULL;
189                 }
190
191                 /* check if we need to read */
192                 if ((off + len) > id->sbbuf_len) {
193                         dbg("read sbbuf len:0x%llx", off + len);
194                         lseek(id->fd, 0, SEEK_SET);
195                         buf_len = read(id->fd, id->sbbuf, off + len);
196                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
197                         id->sbbuf_len = buf_len;
198                         if (buf_len < off + len)
199                                 return NULL;
200                 }
201
202                 return &(id->sbbuf[off]);
203         } else {
204                 if (len > SEEK_BUFFER_SIZE) {
205                         dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
206                         return NULL;
207                 }
208
209                 /* get seek buffer */
210                 if (id->seekbuf == NULL) {
211                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
212                         if (id->seekbuf == NULL)
213                                 return NULL;
214                 }
215
216                 /* check if we need to read */
217                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
218                         dbg("read seekbuf off:0x%llx len:0x%x", off, len);
219                         if (lseek(id->fd, off, SEEK_SET) == -1)
220                                 return NULL;
221                         buf_len = read(id->fd, id->seekbuf, len);
222                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
223                         id->seekbuf_off = off;
224                         id->seekbuf_len = buf_len;
225                         if (buf_len < len) {
226                                 dbg("requested 0x%x bytes, got only 0x%x bytes", len, buf_len);
227                                 return NULL;
228                         }
229                 }
230
231                 return &(id->seekbuf[off - id->seekbuf_off]);
232         }
233 }
234
235 void volume_id_free_buffer(struct volume_id *id)
236 {
237         if (id->sbbuf != NULL) {
238                 free(id->sbbuf);
239                 id->sbbuf = NULL;
240                 id->sbbuf_len = 0;
241         }
242         if (id->seekbuf != NULL) {
243                 free(id->seekbuf);
244                 id->seekbuf = NULL;
245                 id->seekbuf_len = 0;
246         }
247 }