summaryrefslogtreecommitdiffstats
path: root/daemon/isoinfo.c
blob: d117b40fc8efbb8dec15e60f2e200bdddf4b37ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* libguestfs - the guestfsd daemon
 * Copyright (C) 2012 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"

GUESTFSD_EXT_CMD(str_isoinfo, isoinfo);

static int
parse_uint32 (uint32_t *ret, const char *str)
{
  uint32_t r;

  if (sscanf (str, "%" SCNu32, &r) != 1) {
    reply_with_error ("cannot parse numeric field from isoinfo: %s", str);
    return -1;
  }

  *ret = r;
  return 0;
}

/* This is always in a fixed format:
 * "2012 03 16 11:05:46.00"
 * or if the field is not present, then:
 * "0000 00 00 00:00:00.00"
 */
static int
parse_time_t (int64_t *ret, const char *str)
{
  struct tm tm;
  time_t r;

  if (STREQ (str, "0000 00 00 00:00:00.00")) {
    *ret = -1;
    return 0;
  }

  if (sscanf (str, "%04d %02d %02d %02d:%02d:%02d",
              &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
              &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6) {
    reply_with_error ("cannot parse date from isoinfo: %s", str);
    return -1;
  }

  /* Adjust fields. */
  tm.tm_year -= 1900;
  tm.tm_mon--;
  tm.tm_isdst = -1;

  /* Convert to time_t. */
  r = timegm (&tm);
  if (r == -1) {
    reply_with_error ("invalid date or time: %s", str);
    return -1;
  }

  *ret = r;
  return 0;
}

static guestfs_int_isoinfo *
parse_isoinfo (char **lines)
{
  guestfs_int_isoinfo *ret;
  size_t i;

  ret = calloc (1, sizeof *ret);
  if (ret == NULL) {
    reply_with_perror ("calloc");
    return NULL;
  }

  /* Default each int field in the struct to -1. */
  ret->iso_volume_space_size = (uint32_t) -1;
  ret->iso_volume_set_size = (uint32_t) -1;
  ret->iso_volume_sequence_number = (uint32_t) -1;
  ret->iso_logical_block_size = (uint32_t) -1;
  ret->iso_volume_creation_t = -1;
  ret->iso_volume_modification_t = -1;
  ret->iso_volume_expiration_t = -1;
  ret->iso_volume_effective_t = -1;

  for (i = 0; lines[i] != NULL; ++i) {
    if (STRPREFIX (lines[i], "System id: ")) {
      ret->iso_system_id = strdup (&lines[i][11]);
      if (ret->iso_system_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Volume id: ")) {
      ret->iso_volume_id = strdup (&lines[i][11]);
      if (ret->iso_volume_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Volume set id: ")) {
      ret->iso_volume_set_id = strdup (&lines[i][15]);
      if (ret->iso_volume_set_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Publisher id: ")) {
      ret->iso_publisher_id = strdup (&lines[i][14]);
      if (ret->iso_publisher_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Data preparer id: ")) {
      ret->iso_data_preparer_id = strdup (&lines[i][18]);
      if (ret->iso_data_preparer_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Application id: ")) {
      ret->iso_application_id = strdup (&lines[i][16]);
      if (ret->iso_application_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Copyright File id: ")) {
      ret->iso_copyright_file_id = strdup (&lines[i][19]);
      if (ret->iso_copyright_file_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Abstract File id: ")) {
      ret->iso_abstract_file_id = strdup (&lines[i][18]);
      if (ret->iso_abstract_file_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Bibliographic File id: ")) {
      ret->iso_bibliographic_file_id = strdup (&lines[i][23]);
      if (ret->iso_bibliographic_file_id == NULL) goto error;
    }
    else if (STRPREFIX (lines[i], "Volume size is: ")) {
      if (parse_uint32 (&ret->iso_volume_space_size, &lines[i][16]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Volume set size is: ")) {
      if (parse_uint32 (&ret->iso_volume_set_size, &lines[i][20]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Volume set sequence number is: ")) {
      if (parse_uint32 (&ret->iso_volume_sequence_number, &lines[i][31]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Logical block size is: ")) {
      if (parse_uint32 (&ret->iso_logical_block_size, &lines[i][23]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Creation Date:     ")) {
      if (parse_time_t (&ret->iso_volume_creation_t, &lines[i][19]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Modification Date: ")) {
      if (parse_time_t (&ret->iso_volume_modification_t, &lines[i][19]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Expiration Date:   ")) {
      if (parse_time_t (&ret->iso_volume_expiration_t, &lines[i][19]) == -1)
        goto error;
    }
    else if (STRPREFIX (lines[i], "Effective Date:    ")) {
      if (parse_time_t (&ret->iso_volume_effective_t, &lines[i][19]) == -1)
        goto error;
    }
  }

  /* Any string fields which were not set above will be NULL.  However
   * we cannot return NULL fields in structs, so we convert these to
   * empty strings here.
   */
  if (ret->iso_system_id == NULL) {
    ret->iso_system_id = strdup ("");
    if (ret->iso_system_id == NULL) goto error;
  }
  if (ret->iso_volume_id == NULL) {
    ret->iso_volume_id = strdup ("");
    if (ret->iso_volume_id == NULL) goto error;
  }
  if (ret->iso_volume_set_id == NULL) {
    ret->iso_volume_set_id = strdup ("");
    if (ret->iso_volume_set_id == NULL) goto error;
  }
  if (ret->iso_publisher_id == NULL) {
    ret->iso_publisher_id = strdup ("");
    if (ret->iso_publisher_id == NULL) goto error;
  }
  if (ret->iso_data_preparer_id == NULL) {
    ret->iso_data_preparer_id = strdup ("");
    if (ret->iso_data_preparer_id == NULL) goto error;
  }
  if (ret->iso_application_id == NULL) {
    ret->iso_application_id = strdup ("");
    if (ret->iso_application_id == NULL) goto error;
  }
  if (ret->iso_copyright_file_id == NULL) {
    ret->iso_copyright_file_id = strdup ("");
    if (ret->iso_copyright_file_id == NULL) goto error;
  }
  if (ret->iso_abstract_file_id == NULL) {
    ret->iso_abstract_file_id = strdup ("");
    if (ret->iso_abstract_file_id == NULL) goto error;
  }
  if (ret->iso_bibliographic_file_id == NULL) {
    ret->iso_bibliographic_file_id = strdup ("");
    if (ret->iso_bibliographic_file_id == NULL) goto error;
  }

  return ret;

 error:
  free (ret->iso_system_id);
  free (ret->iso_volume_id);
  free (ret->iso_volume_set_id);
  free (ret->iso_publisher_id);
  free (ret->iso_data_preparer_id);
  free (ret->iso_application_id);
  free (ret->iso_copyright_file_id);
  free (ret->iso_abstract_file_id);
  free (ret->iso_bibliographic_file_id);
  free (ret);
  return NULL;
}

static guestfs_int_isoinfo *
isoinfo (const char *path)
{
  char *out = NULL, *err = NULL;
  int r;
  char **lines = NULL;
  guestfs_int_isoinfo *ret = NULL;

  /* --debug is necessary to get additional fields, in particular
   * the date & time fields.
   */
  r = command (&out, &err, str_isoinfo, "--debug", "-d", "-i", path, NULL);
  if (r == -1) {
    reply_with_error ("%s", err);
    goto done;
  }

  lines = split_lines (out);
  if (lines == NULL)
    goto done;

  ret = parse_isoinfo (lines);
  if (ret == NULL)
    goto done;

 done:
  free (out);
  free (err);
  if (lines)
    free_strings (lines);

  return ret;
}

guestfs_int_isoinfo *
do_isoinfo_device (const char *device)
{
  return isoinfo (device);
}

guestfs_int_isoinfo *
do_isoinfo (const char *path)
{
  char *buf;
  guestfs_int_isoinfo *ret;

  buf = sysroot_path (path);
  if (!buf) {
    reply_with_perror ("malloc");
    return NULL;
  }

  ret = isoinfo (buf);
  free (buf);

  return ret;
}