aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/libxlu_disk.c
blob: 18fe386dbe2f0adff504af5863c2e30d4c09975d (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
#include "libxl_osdeps.h" /* must come before any other headers */
#include "libxlu_internal.h"
#include "libxlu_disk_l.h"
#include "libxlu_disk_i.h"
#include "libxlu_cfg_i.h"

void xlu__disk_err(DiskParseContext *dpc, const char *erroneous,
                   const char *message) {
    fprintf(dpc->cfg->report,
            "%s: config parsing error in disk specification: %s"
            "%s%s%s"
            " in `%s'\n",
            dpc->cfg->config_source, message,
            erroneous?": near `":"", erroneous?erroneous:"", erroneous?"'":"",
            dpc->spec);
    if (!dpc->err) dpc->err= EINVAL;
}

static int dpc_prep(DiskParseContext *dpc, const char *spec) {
    int e;

    dpc->spec = spec;

    e = xlu__disk_yylex_init_extra(dpc, &dpc->scanner);
    if (e) goto fail;

    dpc->buf = xlu__disk_yy_scan_bytes(spec, strlen(spec), dpc->scanner);
    if (!dpc->buf) { e = ENOMEM; goto fail; }

    return 0;

 fail:
    fprintf(dpc->cfg->report, "cannot init disk scanner: %s\n",
            strerror(errno));
    return e;
}

static void dpc_dispose(DiskParseContext *dpc) {
    if (dpc->buf) {
        xlu__disk_yy_delete_buffer(dpc->buf, dpc->scanner);
        dpc->buf = 0;
    }
    if (dpc->scanner) {
        xlu__disk_yylex_destroy(dpc->scanner);
        dpc->scanner = 0;
    }
}

int xlu_disk_parse(XLU_Config *cfg,
                   int nspecs, const char *const *specs,
                   libxl_device_disk *disk) {
    DiskParseContext dpc;
    int i, e;

    memset(&dpc,0,sizeof(dpc));
    dpc.cfg = cfg;
    dpc.scanner = 0;
    dpc.disk = disk;

    disk->readwrite = 1;

    for (i=0; i<nspecs; i++) {
        e = dpc_prep(&dpc, specs[i]);
        if (e) { dpc.err = e; goto x_err; }

        xlu__disk_yylex(dpc.scanner);
        assert(!e);
        if (dpc.err) goto x_err;

        dpc_dispose(&dpc);
    }

    if (disk->format == LIBXL_DISK_FORMAT_UNKNOWN) {
        disk->format = LIBXL_DISK_FORMAT_RAW;
    }
    if (disk->is_cdrom) {
        disk->removable = 1;
        disk->readwrite = 0;
        if (!disk->pdev_path || !strcmp(disk->pdev_path, ""))
            disk->format = LIBXL_DISK_FORMAT_EMPTY;
    }

    if (!disk->vdev) {
        xlu__disk_err(&dpc,0, "no vdev specified");
        goto x_err;
    }
    if (!disk->pdev_path && !disk->removable) {
        xlu__disk_err(&dpc,0,"no target specified (and device not removable)");
        goto x_err;
    }

 x_err:
    dpc_dispose(&dpc);
    return dpc.err;
}

/*
 * Local variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */