-
Notifications
You must be signed in to change notification settings - Fork 20
/
mdata_get.c
88 lines (76 loc) · 1.82 KB
/
mdata_get.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
80
81
82
83
84
85
86
87
88
/*
* 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 char *keyname;
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 for '%s'\n", keyname);
return (MDEC_NOTFOUND);
case MDR_UNKNOWN:
fprintf(stderr, "Error getting metadata for key '%s': %s\n",
keyname, cstr);
return (MDEC_ERROR);
case MDR_INVALID_COMMAND:
fprintf(stderr, "ERROR: host does not support GET\n");
return (MDEC_ERROR);
default:
ABORT("print_response: UNKNOWN RESPONSE\n");
return (MDEC_ERROR);
}
}
int
main(int argc, char **argv)
{
mdata_proto_t *mdp;
mdata_response_t mdr;
string_t *data;
const char *errmsg = NULL;
if (argc < 2) {
errx(MDEC_USAGE_ERROR, "Usage: %s <keyname>", argv[0]);
}
if (proto_init(&mdp, &errmsg) != 0) {
fprintf(stderr, "ERROR: could not initialise protocol: %s\n",
errmsg);
return (MDEC_ERROR);
}
keyname = strdup(argv[1]);
if (proto_execute(mdp, "GET", keyname, &mdr, &data) != 0) {
fprintf(stderr, "ERROR: could not execute GET\n");
return (MDEC_ERROR);
}
return (print_response(mdr, data));
}