aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/libxl_xshelp.c
blob: 8db2e5bb69a21d4e2be6d8f3d9da1f9db8ebb0f7 (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
/*
 * Copyright (C) 2009      Citrix Ltd.
 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * 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 Lesser General Public License for more details.
 */

#include "libxl_osdeps.h"

#include <string.h>
#include <stddef.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>

#include "libxl.h"
#include "libxl_internal.h"

int xs_writev(struct xs_handle *xsh, xs_transaction_t t, char *dir, char *kvs[])
{
    char *path;
    int i;

    if (!kvs)
        return 0;

    for (i = 0; kvs[i] != NULL; i += 2) {
        if (asprintf(&path, "%s/%s", dir, kvs[i]) < 0)
            return -1;
        if (path && kvs[i + 1]) {
            int length = strlen(kvs[i + 1]);
            xs_write(xsh, t, path, kvs[i + 1], length);
        }
        free(path);
    }
    return 0;
}

char **libxl_xs_kvs_of_flexarray(libxl_ctx *ctx, flexarray_t *array, int length)
{
    char **kvs;
    int i;

    kvs = libxl_calloc(ctx, length + 2, sizeof(char *));
    if (kvs) {
        for (i = 0; i < length; i += 2) {
            void *ptr;

            flexarray_get(array, i, &ptr);
            kvs[i] = (char *) ptr;
            flexarray_get(array, i + 1, &ptr);
            kvs[i + 1] = (char *) ptr;
        }
        kvs[i] = NULL;
        kvs[i + 1] = NULL;
    }
    return kvs;
}

int libxl_xs_writev(libxl_ctx *ctx, xs_transaction_t t,
                    char *dir, char *kvs[])
{
    char *path;
    int i;

    if (!kvs)
        return 0;

    for (i = 0; kvs[i] != NULL; i += 2) {
        path = libxl_sprintf(ctx, "%s/%s", dir, kvs[i]);
        if (path && kvs[i + 1]) {
            int length = strlen(kvs[i + 1]);
            xs_write(ctx->xsh, t, path, kvs[i + 1], length);
        }
        libxl_free(ctx, path);
    }
    return 0;
}

int libxl_xs_write(libxl_ctx *ctx, xs_transaction_t t,
                   char *path, char *fmt, ...)
{
    char *s;
    va_list ap;
    int ret;
    va_start(ap, fmt);
    ret = vasprintf(&s, fmt, ap);
    va_end(ap);

    if (ret == -1) {
        return -1;
    }
    xs_write(ctx->xsh, t, path, s, ret);
    free(s);
    return 0;
}

char * libxl_xs_read(libxl_ctx *ctx, xs_transaction_t t, char *path)
{
    unsigned int len;
    char *ptr;

    ptr = xs_read(ctx->xsh, t, path, &len);
    if (ptr != NULL) {
        libxl_ptr_add(ctx, ptr);
        return ptr;
    }
    return 0;
}

char *libxl_xs_get_dompath(libxl_ctx *ctx, uint32_t domid)
{
    char *s = xs_get_domain_path(ctx->xsh, domid);
    if (!s) {
        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to get dompath for %" PRIu32,
                     domid);
        return NULL;
    }
    libxl_ptr_add(ctx, s);
    return s;
}

char **libxl_xs_directory(libxl_ctx *ctx, xs_transaction_t t, char *path, unsigned int *nb)
{
    char **ret = NULL;
    ret = xs_directory(ctx->xsh, XBT_NULL, path, nb);
    libxl_ptr_add(ctx, ret);
    return ret;
}