File: rdbloadsave.c

package info (click to toggle)
redict 7.3.6%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 15,112 kB
  • sloc: ansic: 129,434; tcl: 46,164; makefile: 930; python: 815; ruby: 572; sh: 482; javascript: 30
file content (168 lines) | stat: -rw-r--r-- 5,081 bytes parent folder | download | duplicates (3)
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
// SPDX-FileCopyrightText: 2024 Redict Contributors
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
//
// SPDX-License-Identifier: BSD-3-Clause
// SPDX-License-Identifier: LGPL-3.0-only

#include "redictmodule.h"

#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <memory.h>
#include <errno.h>

/* Sanity tests to verify inputs and return values. */
int sanity(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    REDICTMODULE_NOT_USED(argv);
    REDICTMODULE_NOT_USED(argc);

    RedictModuleRdbStream *s = RedictModule_RdbStreamCreateFromFile("dbnew.rdb");

    /* NULL stream should fail. */
    if (RedictModule_RdbLoad(ctx, NULL, 0) == REDICTMODULE_OK || errno != EINVAL) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        goto out;
    }

    /* Invalid flags should fail. */
    if (RedictModule_RdbLoad(ctx, s, 188) == REDICTMODULE_OK || errno != EINVAL) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        goto out;
    }

    /* Missing file should fail. */
    if (RedictModule_RdbLoad(ctx, s, 0) == REDICTMODULE_OK || errno != ENOENT) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        goto out;
    }

    /* Save RDB file. */
    if (RedictModule_RdbSave(ctx, s, 0) != REDICTMODULE_OK || errno != 0) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        goto out;
    }

    /* Load the saved RDB file. */
    if (RedictModule_RdbLoad(ctx, s, 0) != REDICTMODULE_OK || errno != 0) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        goto out;
    }

    RedictModule_ReplyWithSimpleString(ctx, "OK");

 out:
    RedictModule_RdbStreamFree(s);
    return REDICTMODULE_OK;
}

int cmd_rdbsave(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    if (argc != 2) {
        RedictModule_WrongArity(ctx);
        return REDICTMODULE_OK;
    }

    size_t len;
    const char *filename = RedictModule_StringPtrLen(argv[1], &len);

    char tmp[len + 1];
    memcpy(tmp, filename, len);
    tmp[len] = '\0';

    RedictModuleRdbStream *stream = RedictModule_RdbStreamCreateFromFile(tmp);

    if (RedictModule_RdbSave(ctx, stream, 0) != REDICTMODULE_OK || errno != 0) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        goto out;
    }

    RedictModule_ReplyWithSimpleString(ctx, "OK");

out:
    RedictModule_RdbStreamFree(stream);
    return REDICTMODULE_OK;
}

/* Fork before calling RM_RdbSave(). */
int cmd_rdbsave_fork(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    if (argc != 2) {
        RedictModule_WrongArity(ctx);
        return REDICTMODULE_OK;
    }

    size_t len;
    const char *filename = RedictModule_StringPtrLen(argv[1], &len);

    char tmp[len + 1];
    memcpy(tmp, filename, len);
    tmp[len] = '\0';

    int fork_child_pid = RedictModule_Fork(NULL, NULL);
    if (fork_child_pid < 0) {
        RedictModule_ReplyWithError(ctx, strerror(errno));
        return REDICTMODULE_OK;
    } else if (fork_child_pid > 0) {
        /* parent */
        RedictModule_ReplyWithSimpleString(ctx, "OK");
        return REDICTMODULE_OK;
    }

    RedictModuleRdbStream *stream = RedictModule_RdbStreamCreateFromFile(tmp);

    int ret = 0;
    if (RedictModule_RdbSave(ctx, stream, 0) != REDICTMODULE_OK) {
        ret = errno;
    }
    RedictModule_RdbStreamFree(stream);

    RedictModule_ExitFromChild(ret);
    return REDICTMODULE_OK;
}

int cmd_rdbload(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    if (argc != 2) {
        RedictModule_WrongArity(ctx);
        return REDICTMODULE_OK;
    }

    size_t len;
    const char *filename = RedictModule_StringPtrLen(argv[1], &len);

    char tmp[len + 1];
    memcpy(tmp, filename, len);
    tmp[len] = '\0';

    RedictModuleRdbStream *stream = RedictModule_RdbStreamCreateFromFile(tmp);

    if (RedictModule_RdbLoad(ctx, stream, 0) != REDICTMODULE_OK || errno != 0) {
        RedictModule_RdbStreamFree(stream);
        RedictModule_ReplyWithError(ctx, strerror(errno));
        return REDICTMODULE_OK;
    }

    RedictModule_RdbStreamFree(stream);
    RedictModule_ReplyWithSimpleString(ctx, "OK");
    return REDICTMODULE_OK;
}

int RedictModule_OnLoad(RedictModuleCtx *ctx, RedictModuleString **argv, int argc) {
    REDICTMODULE_NOT_USED(argv);
    REDICTMODULE_NOT_USED(argc);

    if (RedictModule_Init(ctx, "rdbloadsave", 1, REDICTMODULE_APIVER_1) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    if (RedictModule_CreateCommand(ctx, "test.sanity", sanity, "", 0, 0, 0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    if (RedictModule_CreateCommand(ctx, "test.rdbsave", cmd_rdbsave, "", 0, 0, 0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    if (RedictModule_CreateCommand(ctx, "test.rdbsave_fork", cmd_rdbsave_fork, "", 0, 0, 0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    if (RedictModule_CreateCommand(ctx, "test.rdbload", cmd_rdbload, "", 0, 0, 0) == REDICTMODULE_ERR)
        return REDICTMODULE_ERR;

    return REDICTMODULE_OK;
}