chiark / gitweb /
Make run_directory.c stat the place it is going to try to run.
[elogind.git] / extras / volume_id / libvolume_id / iso9660.c
1 /*
2  * volume_id - reads filesystem label and uuid
3  *
4  * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  *      This program is free software; you can redistribute it and/or modify it
7  *      under the terms of the GNU General Public License as published by the
8  *      Free Software Foundation version 2 of the License.
9  */
10
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE 1
13 #endif
14
15 #ifdef HAVE_CONFIG_H
16 #  include <config.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <ctype.h>
25
26 #include "volume_id.h"
27 #include "logging.h"
28 #include "util.h"
29
30 #define ISO_SUPERBLOCK_OFFSET           0x8000
31 #define ISO_SECTOR_SIZE                 0x800
32 #define ISO_VD_OFFSET                   (ISO_SUPERBLOCK_OFFSET + ISO_SECTOR_SIZE)
33 #define ISO_VD_PRIMARY                  0x1
34 #define ISO_VD_SUPPLEMENTARY            0x2
35 #define ISO_VD_END                      0xff
36 #define ISO_VD_MAX                      16
37
38 struct iso_volume_descriptor {
39         uint8_t         vd_type;
40         uint8_t         vd_id[5];
41         uint8_t         vd_version;
42         uint8_t         flags;
43         uint8_t         system_id[32];
44         uint8_t         volume_id[32];
45         uint8_t         unused[8];
46         uint8_t         space_size[8];
47         uint8_t         escape_sequences[8];
48 } __attribute__((__packed__));
49
50 struct high_sierra_volume_descriptor {
51         uint8_t         foo[8];
52         uint8_t         type;
53         uint8_t         id[4];
54         uint8_t         version;
55 } __attribute__((__packed__));
56
57 int volume_id_probe_iso9660(struct volume_id *id, uint64_t off)
58 {
59         uint8_t *buf;
60         struct iso_volume_descriptor *is;
61         struct high_sierra_volume_descriptor *hs;
62
63         dbg("probing at offset 0x%llx", (unsigned long long) off);
64
65         buf = volume_id_get_buffer(id, off + ISO_SUPERBLOCK_OFFSET, 0x200);
66         if (buf == NULL)
67                 return -1;
68
69         is = (struct iso_volume_descriptor *) buf;
70
71         if (memcmp(is->vd_id, "CD001", 5) == 0) {
72                 int vd_offset;
73                 int i;
74
75                 dbg("read label from PVD");
76                 volume_id_set_label_raw(id, is->volume_id, 32);
77                 volume_id_set_label_string(id, is->volume_id, 32);
78
79                 dbg("looking for SVDs");
80                 vd_offset = ISO_VD_OFFSET;
81                 for (i = 0; i < ISO_VD_MAX; i++) {
82                         uint8_t svd_label[64];
83
84                         is = (struct iso_volume_descriptor *) volume_id_get_buffer(id, off + vd_offset, 0x200);
85                         if (is == NULL || is->vd_type == ISO_VD_END)
86                                 break;
87                         if (is->vd_type != ISO_VD_SUPPLEMENTARY)
88                                 continue;
89
90                         dbg("found SVD at offset 0x%llx", (unsigned long long) (off + vd_offset));
91                         if (memcmp(is->escape_sequences, "%/@", 3) == 0||
92                             memcmp(is->escape_sequences, "%/C", 3) == 0||
93                             memcmp(is->escape_sequences, "%/E", 3) == 0) {
94                                 dbg("Joliet extension found");
95                                 volume_id_set_unicode16((char *)svd_label, sizeof(svd_label), is->volume_id, BE, 32);
96                                 if (memcmp(id->label, svd_label, 16) == 0) {
97                                         dbg("SVD label is identical, use the possibly longer PVD one");
98                                         break;
99                                 }
100
101                                 volume_id_set_label_raw(id, is->volume_id, 32);
102                                 volume_id_set_label_string(id, svd_label, 32);
103                                 strcpy(id->type_version, "Joliet Extension");
104                                 goto found;
105                         }
106                         vd_offset += ISO_SECTOR_SIZE;
107                 }
108                 goto found;
109         }
110
111         hs = (struct high_sierra_volume_descriptor *) buf;
112
113         if (memcmp(hs->id, "CDROM", 5) == 0) {
114                 strcpy(id->type_version, "High Sierra");
115                 goto found;
116         }
117
118         return -1;
119
120 found:
121         volume_id_set_usage(id, VOLUME_ID_FILESYSTEM);
122         id->type = "iso9660";
123
124         return 0;
125 }