• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-mcu / 1642556678

27 Jan 2025 01:30PM UTC coverage: 25.749%. First build
1642556678

push

gitlab-ci

vpodzime
feat: Support platform-specific or custom allocators

And use Zephyr-specific allocation functions on Zephyr by
default, using either a separate heap (default) or the system
heap.

This means we have to use our memory management functions instead
of the standard ones everywhere, even if they can actually just
call the standard ones.

Ticket: MEN-7808
Changelog: none
Signed-off-by: Vratislav Podzimek <vratislav.podzimek@northern.tech>

64 of 184 new or added lines in 12 files covered. (34.78%)

731 of 2839 relevant lines covered (25.75%)

8.54 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/platform/storage/posix/src/mender-storage.c
1
/**
2
 * @file      mender-storage.c
3
 * @brief     Mender storage interface for Posix platform
4
 *
5
 * Copyright joelguittet and mender-mcu-client contributors
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19

20
#include <unistd.h>
21
#include "mender-alloc.h"
22
#include "mender-log.h"
23
#include "mender-storage.h"
24

25
/**
26
 * @brief Default storage path (working directory)
27
 */
28
#ifndef CONFIG_MENDER_STORAGE_PATH
29
#define CONFIG_MENDER_STORAGE_PATH ""
30
#endif /* CONFIG_MENDER_STORAGE_PATH */
31

32
/**
33
 * @brief NVS Files
34
 */
35
#define MENDER_STORAGE_NVS_PRIVATE_KEY     CONFIG_MENDER_STORAGE_PATH "key.der"
36
#define MENDER_STORAGE_NVS_PUBLIC_KEY      CONFIG_MENDER_STORAGE_PATH "pubkey.der"
37
#define MENDER_STORAGE_NVS_DEPLOYMENT_DATA CONFIG_MENDER_STORAGE_PATH "deployment-data.json"
38
#define MENDER_STORAGE_NVS_PROVIDES        CONFIG_MENDER_STORAGE_PATH "provides.txt"
39
#define MENDER_STORAGE_NVS_ARTICACT_NAME   CONFIG_MENDER_STORAGE_PATH "artifact_name.txt"
40

41
mender_err_t
42
mender_storage_init(void) {
×
43

44
    /* Nothing to do */
45
    return MENDER_OK;
×
46
}
47

48
static mender_err_t
49
mender_storage_write_file(const char *file_path, const void *data, size_t data_length) {
×
50

51
    assert(NULL != file_path);
×
52
    assert(NULL != data);
×
53

54
    FILE *f = fopen(file_path, "wb");
×
55
    if (NULL == f) {
×
56
        mender_log_error("Unable to open file %s for writing", file_path);
×
57
        return MENDER_FAIL;
×
58
    }
59
    if (fwrite(data, sizeof(unsigned char), data_length, f) != data_length) {
×
60
        mender_log_error("Unable to write data to file %s", file_path);
×
61
        fclose(f);
×
62
        return MENDER_FAIL;
×
63
    }
64
    fclose(f);
×
65
    return MENDER_OK;
×
66
}
67

68
static mender_err_t
69
mender_storage_read_file(const char *file_path, void **data, size_t *data_length) {
×
70

71
    assert(NULL != file_path);
×
72
    assert(NULL != data);
×
73
    assert(NULL != data_length);
×
74

75
    FILE *f = fopen(file_path, "rb");
×
76
    if (NULL == f) {
×
77
        return MENDER_NOT_FOUND;
×
78
    }
79
    fseek(f, 0, SEEK_END);
×
80
    long length = ftell(f);
×
81
    if (length <= 0) {
×
82
        mender_log_info("File %s is empty or unavailable", file_path);
×
83
        fclose(f);
×
84
        return MENDER_NOT_FOUND;
×
85
    }
86
    *data_length = (size_t)length;
×
87
    fseek(f, 0, SEEK_SET);
×
NEW
88
    *data = mender_malloc(*data_length + 1);
×
89
    if (NULL == *data) {
×
90
        mender_log_error("Unable to allocate memory");
×
91
        fclose(f);
×
92
        return MENDER_FAIL;
×
93
    }
94
    /* Set last byte to \0 */
95
    ((unsigned char *)*data)[*data_length] = '\0';
×
96
    if (fread(*data, sizeof(unsigned char), *data_length, f) != *data_length) {
×
97
        mender_log_error("Unable to read data from file %s", file_path);
×
NEW
98
        mender_free(*data);
×
99
        fclose(f);
×
100
        return MENDER_FAIL;
×
101
    }
102
    fclose(f);
×
103
    return MENDER_OK;
×
104
}
105

106
mender_err_t
107
mender_storage_set_authentication_keys(unsigned char *private_key, size_t private_key_length, unsigned char *public_key, size_t public_key_length) {
×
108

109
    assert(NULL != private_key);
×
110
    assert(NULL != public_key);
×
111

112
    if (MENDER_OK != mender_storage_write_file(MENDER_STORAGE_NVS_PRIVATE_KEY, private_key, private_key_length)) {
×
113
        return MENDER_FAIL;
×
114
    }
115
    if (MENDER_OK != mender_storage_write_file(MENDER_STORAGE_NVS_PUBLIC_KEY, public_key, public_key_length)) {
×
116
        return MENDER_FAIL;
×
117
    }
118
    return MENDER_OK;
×
119
}
120

121
mender_err_t
122
mender_storage_get_authentication_keys(unsigned char **private_key, size_t *private_key_length, unsigned char **public_key, size_t *public_key_length) {
×
123

124
    assert(NULL != private_key);
×
125
    assert(NULL != private_key_length);
×
126
    assert(NULL != public_key);
×
127
    assert(NULL != public_key_length);
×
128

129
    if (MENDER_OK != mender_storage_read_file(MENDER_STORAGE_NVS_PRIVATE_KEY, (void **)private_key, private_key_length)) {
×
130
        return MENDER_NOT_FOUND;
×
131
    }
132
    if (MENDER_OK != mender_storage_read_file(MENDER_STORAGE_NVS_PUBLIC_KEY, (void **)public_key, public_key_length)) {
×
NEW
133
        mender_free(*private_key);
×
134
        *private_key        = NULL;
×
135
        *private_key_length = 0;
×
136
        return MENDER_NOT_FOUND;
×
137
    }
138
    return MENDER_OK;
×
139
}
140

141
mender_err_t
142
mender_storage_delete_authentication_keys(void) {
×
143

144
    /* Erase keys */
145
    if ((0 != unlink(MENDER_STORAGE_NVS_PRIVATE_KEY)) || (0 != unlink(MENDER_STORAGE_NVS_PUBLIC_KEY))) {
×
146
        mender_log_error("Unable to erase authentication keys");
×
147
        return MENDER_FAIL;
×
148
    }
149

150
    return MENDER_OK;
×
151
}
152

153
mender_err_t
154
mender_storage_set_deployment_data(char *deployment_data) {
×
155
    assert(NULL != deployment_data);
×
156
    size_t deployment_data_length = strlen(deployment_data);
×
157

158
    if (MENDER_OK != mender_storage_write_file(MENDER_STORAGE_NVS_DEPLOYMENT_DATA, deployment_data, deployment_data_length)) {
×
159
        return MENDER_FAIL;
×
160
    }
161
    return MENDER_OK;
×
162
}
163

164
mender_err_t
165
mender_storage_get_deployment_data(char **deployment_data) {
×
166
    assert(NULL != deployment_data);
×
167

168
    size_t deployment_data_length;
169
    if (MENDER_OK != mender_storage_read_file(MENDER_STORAGE_NVS_DEPLOYMENT_DATA, (void **)deployment_data, &deployment_data_length)) {
×
170
        return MENDER_NOT_FOUND;
×
171
    }
172
    return MENDER_OK;
×
173
}
174

175
mender_err_t
176
mender_storage_delete_deployment_data(void) {
×
177

178
    /* Delete deployment data */
179
    if (0 != unlink(MENDER_STORAGE_NVS_DEPLOYMENT_DATA)) {
×
180
        mender_log_error("Unable to delete deployment data");
×
181
        return MENDER_FAIL;
×
182
    }
183

184
    return MENDER_OK;
×
185
}
186

187
#ifdef CONFIG_MENDER_FULL_PARSE_ARTIFACT
188
#ifdef CONFIG_MENDER_PROVIDES_DEPENDS
189

190
mender_err_t
191
mender_storage_set_provides(mender_key_value_list_t *provides) {
×
192
    assert(NULL != provides);
×
193

194
    char *provides_str = NULL;
×
195
    if (MENDER_OK != mender_utils_key_value_list_to_string(provides, &provides_str)) {
×
196
        return MENDER_FAIL;
×
197
    }
198
    size_t provides_str_length = strlen(provides_str);
×
199

200
    if (MENDER_OK != mender_storage_write_file(MENDER_STORAGE_NVS_PROVIDES, provides_str, provides_str_length)) {
×
NEW
201
        mender_free(provides_str);
×
202
        return MENDER_FAIL;
×
203
    }
NEW
204
    mender_free(provides_str);
×
205
    return MENDER_OK;
×
206
}
207

208
mender_err_t
209
mender_storage_get_provides(mender_key_value_list_t **provides) {
×
210

211
    assert(NULL != provides);
×
212

213
    char  *provides_str = NULL;
×
214
    size_t provides_length;
215
    if (MENDER_OK != mender_storage_read_file(MENDER_STORAGE_NVS_PROVIDES, (void **)&provides_str, &provides_length)) {
×
216
        return MENDER_NOT_FOUND;
×
217
    }
218
    if (MENDER_OK != mender_utils_string_to_key_value_list(provides_str, provides)) {
×
219
        mender_log_error("Unable to parse provides");
×
NEW
220
        mender_free(provides_str);
×
221
        return MENDER_FAIL;
×
222
    }
223

NEW
224
    mender_free(provides_str);
×
225
    return MENDER_OK;
×
226
}
227

228
mender_err_t
229
mender_storage_delete_provides(void) {
×
230

231
    /* Delete provides */
232
    if (0 != unlink(MENDER_STORAGE_NVS_PROVIDES)) {
×
233
        mender_log_error("Unable to delete provides");
×
234
        return MENDER_FAIL;
×
235
    }
236

237
    return MENDER_OK;
×
238
}
239

240
#endif /*CONFIG_MENDER_FULL_PARSE_ARTIFACT*/
241
#endif /*CONFIG_MENDER_PROVIDES_DEPENDS*/
242

243
mender_err_t
244
mender_storage_set_artifact_name(const char *artifact_name) {
×
245

246
    assert(NULL != artifact_name);
×
247

248
    size_t artifact_name_str_length = strlen(artifact_name);
×
249

250
    if (MENDER_OK != mender_storage_write_file(MENDER_STORAGE_NVS_ARTICACT_NAME, artifact_name, artifact_name_str_length)) {
×
251
        return MENDER_FAIL;
×
252
    }
253
    return MENDER_OK;
×
254
}
255

256
mender_err_t
257
mender_storage_get_artifact_name(const char **artifact_name) {
×
258

259
    assert(NULL != artifact_name);
×
260

261
    size_t       artifact_name_length;
262
    mender_err_t ret = mender_storage_read_file(MENDER_STORAGE_NVS_ARTICACT_NAME, (void **)artifact_name, &artifact_name_length);
×
263
    if (MENDER_OK != ret) {
×
264
        if (MENDER_NOT_FOUND == ret) {
×
265
            *artifact_name = "unknown";
×
266
            return MENDER_OK;
×
267
        } else {
268
            mender_log_error("Unable to read artifact_name");
×
269
        }
270
    }
271

272
    return ret;
×
273
}
274

275
mender_err_t
276
mender_storage_exit(void) {
×
277

278
    /* Nothing to do */
279
    return MENDER_OK;
×
280
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc