summaryrefslogtreecommitdiff
path: root/tools/misc/xenhypfs.c
blob: df398b07bdc0ddb553ae3bb3d254a8859082d844 (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
#define _GNU_SOURCE
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xenhypfs.h>

static struct xenhypfs_handle *hdl;

static int usage(void)
{
    fprintf(stderr, "usage: xenhypfs ls <path>\n");
    fprintf(stderr, "       xenhypfs cat [-b] <path>\n");
    fprintf(stderr, "       xenhypfs write <path> <val>\n");
    fprintf(stderr, "       xenhypfs tree\n");

    return 1;
}

static void xenhypfs_print_escaped(char *string)
{
    char *c;

    for (c = string; *c; c++) {
        if (isgraph(*c) || isspace(*c))
            printf("%c", *c);
        else
            printf("\\x%02x", *c);
    }
    printf("\n");
}

static int xenhypfs_cat(int argc, char *argv[])
{
    int ret = 0;
    char *result;
    char *path;
    bool bin = false;

    switch (argc) {
    case 1:
        path = argv[0];
        break;

    case 2:
        if (strcmp(argv[0], "-b"))
            return usage();
        bin = true;
        path = argv[1];
        break;

    default:
        return usage();
    }

    result = xenhypfs_read(hdl, path);
    if (!result) {
        perror("could not read");
        ret = 3;
    } else {
        if (!bin)
            printf("%s\n", result);
        else
            xenhypfs_print_escaped(result);
        free(result);
    }

    return ret;
}

static int xenhypfs_wr(char *path, char *val)
{
    int ret;

    ret = xenhypfs_write(hdl, path, val);
    if (ret) {
        perror("could not write");
        ret = 3;
    }

    return ret;
}

static const char *xenhypfs_type(struct xenhypfs_dirent *ent)
{
    const char *res;

    switch (ent->type) {
    case xenhypfs_type_dir:
        res = "<dir>   ";
        break;
    case xenhypfs_type_blob:
        res = "<blob>  ";
        break;
    case xenhypfs_type_string:
        res = "<string>";
        break;
    case xenhypfs_type_uint:
        res = "<uint>  ";
        break;
    case xenhypfs_type_int:
        res = "<int>   ";
        break;
    case xenhypfs_type_bool:
        res = "<bool>  ";
        break;
    default:
        res = "<\?\?\?>   ";
        break;
    }

    return res;
}

static int xenhypfs_ls(char *path)
{
    struct xenhypfs_dirent *ent;
    unsigned int n, i;
    int ret = 0;

    ent = xenhypfs_readdir(hdl, path, &n);
    if (!ent) {
        perror("could not read dir");
        ret = 3;
    } else {
        for (i = 0; i < n; i++)
            printf("%s r%c %s\n", xenhypfs_type(ent + i),
                   (ent[i].flags & XENHYPFS_FLAG_WRITABLE) ? 'w' : '-',
                   ent[i].name);

        free(ent);
    }

    return ret;
}

static int xenhypfs_tree_sub(const char *path, unsigned int depth)
{
    struct xenhypfs_dirent *ent;
    unsigned int n, i;
    int ret = 0;
    char *p;

    ent = xenhypfs_readdir(hdl, path, &n);
    if (!ent)
        return 2;

    for (i = 0; i < n; i++) {
        printf("%*s%s%s\n", depth * 2, "", ent[i].name,
               ent[i].type == xenhypfs_type_dir ? "/" : "");
        if (ent[i].type == xenhypfs_type_dir) {
            if (asprintf(&p, "%s%s%s", path, (depth == 1) ? "" : "/",
                         ent[i].name) < 0) {
                ret = 2;
                break;
            }
            if (xenhypfs_tree_sub(p, depth + 1))
                ret = 2;
            free(p);
        }
    }

    free(ent);

    return ret;
}

static int xenhypfs_tree(void)
{
    printf("/\n");

    return xenhypfs_tree_sub("/", 1);
}

int main(int argc, char *argv[])
{
    int ret;

    hdl = xenhypfs_open(NULL, 0);

    if (!hdl) {
        fprintf(stderr, "Could not open libxenhypfs\n");
        ret = 2;
    } else if (argc >= 3 && !strcmp(argv[1], "cat"))
        ret = xenhypfs_cat(argc - 2, argv + 2);
    else if (argc == 3 && !strcmp(argv[1], "ls"))
        ret = xenhypfs_ls(argv[2]);
    else if (argc == 4 && !strcmp(argv[1], "write"))
        ret = xenhypfs_wr(argv[2], argv[3]);
    else if (argc == 2 && !strcmp(argv[1], "tree"))
        ret = xenhypfs_tree();
    else
        ret = usage();

    xenhypfs_close(hdl);

    return ret;
}