util.c 2.96 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * (C) 2019 The University of Chicago
 *
 * See COPYRIGHT in top-level directory.
 */

#include "bake-config.h"
#include "bake.h"
9
#include <stdlib.h>
10
#include <stdio.h>
11
12
#include <inttypes.h>
#include <libpmemobj.h>
13
#include "base64/b64.h"
14

Philip Carns's avatar
Philip Carns committed
15
static char* bake_err_str(int ret)
16
{
Philip Carns's avatar
Philip Carns committed
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
    switch (ret) {
    case BAKE_SUCCESS:
        return "Success";
        break;
    case BAKE_ERR_ALLOCATION:
        return "Error allocating something";
        break;
    case BAKE_ERR_INVALID_ARG:
        return "An argument is invalid";
        break;
    case BAKE_ERR_MERCURY:
        return "An error happend calling a Mercury function";
        break;
    case BAKE_ERR_ARGOBOTS:
        return "An error happened calling an Argobots function";
        break;
    case BAKE_ERR_PMEM:
        return "An error happend calling a PMDK function";
        break;
    case BAKE_ERR_UNKNOWN_TARGET:
        return "Target refered to by id is not know to provider";
        break;
    case BAKE_ERR_UNKNOWN_PROVIDER:
        return "Provider id could not be matched with a provider";
        break;
    case BAKE_ERR_UNKNOWN_REGION:
        return "Region id could not be found";
        break;
    case BAKE_ERR_OUT_OF_BOUNDS:
        return "Attempting an out of bound access";
        break;
    case BAKE_ERR_REMI:
        return "Error related to REMI";
        break;
    case BAKE_ERR_OP_UNSUPPORTED:
        return "Operation not supported";
        break;
    default:
        return "Unknown error";
        break;
57
58
    }
}
Philip Carns's avatar
Philip Carns committed
59
void bake_perror(const char* s, int err)
60
{
Philip Carns's avatar
Philip Carns committed
61
62
63
64
65
66
    char  error_string[256];
    char* p;
    int   ret;
    ret = snprintf(error_string, 256, "%s", s);
    p   = error_string + ret;
    snprintf(p, 256 - ret, " (%d) %s", err, bake_err_str(err));
67
68
69
70
    error_string[255] = '\0';
    fprintf(stderr, "%s\n", error_string);
}

Philip Carns's avatar
Philip Carns committed
71
void bake_print_dbg_region_id_t(char* str, size_t size, bake_region_id_t rid)
72
{
Philip Carns's avatar
Philip Carns committed
73
    PMEMoid* oid;
74
75

    /* NOTE: this is fragile.  Would break if pmemobj format changes. */
Philip Carns's avatar
Philip Carns committed
76
    oid = (PMEMoid*)rid.data;
77

Philip Carns's avatar
Philip Carns committed
78
79
    snprintf(str, size, "%u:%" PRIu64 ":%" PRIu64, rid.type, oid->pool_uuid_lo,
             oid->off);
80
81
82

    return;
}
83
84
85

int bake_target_id_to_string(bake_target_id_t tid, char* str, size_t size)
{
Philip Carns's avatar
Philip Carns committed
86
    if (size < 37) return BAKE_ERR_INVALID_ARG;
87
88
89
90
91
92
    uuid_unparse(tid.id, str);
    return BAKE_SUCCESS;
}

int bake_target_id_from_string(const char* str, bake_target_id_t* tid)
{
Philip Carns's avatar
Philip Carns committed
93
    if (uuid_parse(str, tid->id) != 0) return BAKE_ERR_INVALID_ARG;
94
95
96
97
98
99
    return BAKE_SUCCESS;
}

int bake_region_id_to_string(bake_region_id_t rid, char* str, size_t size)
{
    char* s = bake_b64_encode((const unsigned char*)&rid, sizeof(rid));
Philip Carns's avatar
Philip Carns committed
100
    if (size < strlen(s) + 1) {
101
102
103
        free(s);
        return BAKE_ERR_INVALID_ARG;
    }
Philip Carns's avatar
Philip Carns committed
104
    strcpy(str, s);
105
106
107
108
109
110
111
112
113
114
115
    free(s);
    return BAKE_SUCCESS;
}

int bake_region_id_from_string(const char* str, bake_region_id_t* tid)
{
    unsigned char* data = bake_b64_decode(str, strlen(str));
    memcpy(tid, data, sizeof(*tid));
    free(data);
    return BAKE_SUCCESS;
}