chiark / gitweb /
[PATCH] udev_volume_id: new version of volume_id
[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_UNPROBED:
57                 return "unprobed";
58         case VOLUME_ID_UNUSED:
59                 return "unused";
60         }
61         return NULL;
62 }
63
64 void volume_id_set_usage_part(struct volume_id_partition *part, enum volume_id_usage usage_id)
65 {
66         part->usage_id = usage_id;
67         part->usage = usage_to_string(usage_id);
68 }
69
70 void volume_id_set_usage(struct volume_id *id, enum volume_id_usage usage_id)
71 {
72         id->usage_id = usage_id;
73         id->usage = usage_to_string(usage_id);
74 }
75
76 void volume_id_set_label_raw(struct volume_id *id, const __u8 *buf, unsigned int count)
77 {
78         memcpy(id->label_raw, buf, count);
79         id->label_raw_len = count;
80 }
81
82 void volume_id_set_label_string(struct volume_id *id, const __u8 *buf, unsigned int count)
83 {
84         unsigned int i;
85
86         memcpy(id->label, buf, count);
87
88         /* remove trailing whitespace */
89         i = strnlen(id->label, count);
90         while (i--) {
91                 if (!isspace(id->label[i]))
92                         break;
93         }
94         id->label[i+1] = '\0';
95 }
96
97 void volume_id_set_label_unicode16(struct volume_id *id, const __u8 *buf, enum endian endianess, unsigned int count)
98 {
99         unsigned int i, j;
100         __u16 c;
101
102         j = 0;
103         for (i = 0; i + 2 <= count; i += 2) {
104                 if (endianess == LE)
105                         c = (buf[i+1] << 8) | buf[i];
106                 else
107                         c = (buf[i] << 8) | buf[i+1];
108                 if (c == 0) {
109                         id->label[j] = '\0';
110                         break;
111                 } else if (c < 0x80) {
112                         id->label[j++] = (__u8) c;
113                 } else if (c < 0x800) {
114                         id->label[j++] = (__u8) (0xc0 | (c >> 6));
115                         id->label[j++] = (__u8) (0x80 | (c & 0x3f));
116                 } else {
117                         id->label[j++] = (__u8) (0xe0 | (c >> 12));
118                         id->label[j++] = (__u8) (0x80 | ((c >> 6) & 0x3f));
119                         id->label[j++] = (__u8) (0x80 | (c & 0x3f));
120                 }
121         }
122 }
123
124 void volume_id_set_uuid(struct volume_id *id, const __u8 *buf, enum uuid_format format)
125 {
126         unsigned int i;
127         unsigned int count = 0;
128
129         switch(format) {
130         case UUID_DOS:
131                 count = 4;
132                 break;
133         case UUID_NTFS:
134         case UUID_HFS:
135                 count = 8;
136                 break;
137         case UUID_DCE:
138                 count = 16;
139         }
140         memcpy(id->uuid_raw, buf, count);
141
142         /* if set, create string in the same format, the native platform uses */
143         for (i = 0; i < count; i++)
144                 if (buf[i] != 0)
145                         goto set;
146         return;
147
148 set:
149         switch(format) {
150         case UUID_DOS:
151                 sprintf(id->uuid, "%02X%02X-%02X%02X",
152                         buf[3], buf[2], buf[1], buf[0]);
153                 break;
154         case UUID_NTFS:
155                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
156                         buf[7], buf[6], buf[5], buf[4],
157                         buf[3], buf[2], buf[1], buf[0]);
158                 break;
159         case UUID_HFS:
160                 sprintf(id->uuid,"%02X%02X%02X%02X%02X%02X%02X%02X",
161                         buf[0], buf[1], buf[2], buf[3],
162                         buf[4], buf[5], buf[6], buf[7]);
163                 break;
164         case UUID_DCE:
165                 sprintf(id->uuid,
166                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
167                         buf[0], buf[1], buf[2], buf[3],
168                         buf[4], buf[5],
169                         buf[6], buf[7],
170                         buf[8], buf[9],
171                         buf[10], buf[11], buf[12], buf[13], buf[14],buf[15]);
172                 break;
173         }
174 }
175
176 __u8 *volume_id_get_buffer(struct volume_id *id, __u64 off, unsigned int len)
177 {
178         unsigned int buf_len;
179
180         dbg("get buffer off 0x%llx(%llu), len 0x%x", off, off, len);
181         /* check if requested area fits in superblock buffer */
182         if (off + len <= SB_BUFFER_SIZE) {
183                 if (id->sbbuf == NULL) {
184                         id->sbbuf = malloc(SB_BUFFER_SIZE);
185                         if (id->sbbuf == NULL)
186                                 return NULL;
187                 }
188
189                 /* check if we need to read */
190                 if ((off + len) > id->sbbuf_len) {
191                         dbg("read sbbuf len:0x%llx", off + len);
192                         lseek(id->fd, 0, SEEK_SET);
193                         buf_len = read(id->fd, id->sbbuf, off + len);
194                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
195                         id->sbbuf_len = buf_len;
196                         if (buf_len < off + len)
197                                 return NULL;
198                 }
199
200                 return &(id->sbbuf[off]);
201         } else {
202                 if (len > SEEK_BUFFER_SIZE) {
203                         dbg("seek buffer too small %d", SEEK_BUFFER_SIZE);
204                         return NULL;
205                 }
206
207                 /* get seek buffer */
208                 if (id->seekbuf == NULL) {
209                         id->seekbuf = malloc(SEEK_BUFFER_SIZE);
210                         if (id->seekbuf == NULL)
211                                 return NULL;
212                 }
213
214                 /* check if we need to read */
215                 if ((off < id->seekbuf_off) || ((off + len) > (id->seekbuf_off + id->seekbuf_len))) {
216                         dbg("read seekbuf off:0x%llx len:0x%x", off, len);
217                         if (lseek(id->fd, off, SEEK_SET) == -1)
218                                 return NULL;
219                         buf_len = read(id->fd, id->seekbuf, len);
220                         dbg("got 0x%x (%i) bytes", buf_len, buf_len);
221                         id->seekbuf_off = off;
222                         id->seekbuf_len = buf_len;
223                         if (buf_len < len) {
224                                 dbg("requested 0x%x bytes, got only 0x%x bytes", len, buf_len);
225                                 return NULL;
226                         }
227                 }
228
229                 return &(id->seekbuf[off - id->seekbuf_off]);
230         }
231 }
232
233 void volume_id_free_buffer(struct volume_id *id)
234 {
235         if (id->sbbuf != NULL) {
236                 free(id->sbbuf);
237                 id->sbbuf = NULL;
238                 id->sbbuf_len = 0;
239         }
240         if (id->seekbuf != NULL) {
241                 free(id->seekbuf);
242                 id->seekbuf = NULL;
243                 id->seekbuf_len = 0;
244         }
245 }