-
Notifications
You must be signed in to change notification settings - Fork 20
/
mdata_list.c
79 lines (70 loc) · 1.67 KB
/
mdata_list.c
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
/*
* See LICENSE file for copyright and license details.
*
* Copyright (c) 2013 Joyent, Inc.
* Copyright (c) 2024 MNX Cloud, Inc.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "common.h"
#include "dynstr.h"
#include "plat.h"
#include "proto.h"
typedef enum mdata_exit_codes {
MDEC_SUCCESS = 0,
MDEC_NOTFOUND = 1,
MDEC_ERROR = 2,
MDEC_USAGE_ERROR = 3,
MDEC_TRY_AGAIN = 10
} mdata_exit_codes_t;
static int
print_response(mdata_response_t mdr, string_t *data)
{
const char *cstr = dynstr_cstr(data);
size_t len = dynstr_len(data);
switch (mdr) {
case MDR_SUCCESS:
fprintf(stdout, "%s", cstr);
if (len >= 1 && cstr[len - 1] != '\n')
fprintf(stdout, "\n");
return (MDEC_SUCCESS);
case MDR_NOTFOUND:
fprintf(stderr, "No metadata\n");
return (MDEC_NOTFOUND);
case MDR_UNKNOWN:
fprintf(stderr, "Error getting metadata: %s\n", cstr);
return (MDEC_ERROR);
case MDR_INVALID_COMMAND:
fprintf(stderr, "ERROR: host does not support KEYS\n");
return (MDEC_ERROR);
default:
ABORT("print_response: UNKNOWN RESPONSE\n");
return (MDEC_ERROR);
}
}
int
main(int argc __UNUSED, char **argv __UNUSED)
{
mdata_proto_t *mdp;
mdata_response_t mdr;
string_t *data;
const char *errmsg = NULL;
if (proto_init(&mdp, &errmsg) != 0) {
fprintf(stderr, "ERROR: could not initialise protocol: %s\n",
errmsg);
return (MDEC_ERROR);
}
if (proto_execute(mdp, "KEYS", NULL, &mdr, &data) != 0) {
fprintf(stderr, "ERROR: could not execute KEYS\n");
return (MDEC_ERROR);
}
return (print_response(mdr, data));
}