./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
278 / 334
83.2%
Branches:
152 / 210
72.4%
Functions:
22 / 24
91.7%

kfsw-services/src/ftp/ftp_client.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 #include <zephyr/fs/fs.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/sys/byteorder.h>
11
12 #include <kfsw/platform/storage.h>
13 #include <kfsw/services/ftp.h>
14 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_FTP
15 #include <kfsw/services/log.h>
16 #if CONFIG_KFSW_EVENT
17 #include <kfsw/services/event.h>
18 #endif
19
20 #include "ftp_link.h"
21
22 K_MUTEX_DEFINE(kfsw_ftp_client_lock);
23
24 static atomic_t next_request_id;
25 static struct kfsw_ftp_workspace client_workspace;
26
27 172 static int validate_client(uint16_t node)
28 {
29
3/4
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 166 times.
172 if (!kfsw_storage_is_ready() || !kfsw_ftp_link_is_ready()) {
30 6 return -EACCES;
31 }
32
2/2
✓ Branch 1 taken 160 times.
✓ Branch 2 taken 6 times.
166 return (node <= kfsw_ftp_link_max_node()) ? 0 : -EINVAL;
33 }
34
35 314 static bool is_local_node(uint16_t node)
36 {
37 314 return node == kfsw_ftp_link_local_node();
38 }
39
40 /*
41 * A request addressed to this node reads its own FTP root directly, so it
42 * needs storage and a started service but no router, route or connection.
43 */
44 130 static int validate_local(void)
45 {
46
3/4
✓ Branch 1 taken 130 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 128 times.
✓ Branch 5 taken 2 times.
130 return (kfsw_storage_is_ready() && kfsw_ftp_is_started()) ? 0 : -EACCES;
47 }
48
49 200 static uint32_t allocate_request_id(void)
50 {
51 200 uint32_t request_id = (uint32_t)atomic_inc(&next_request_id) + 1U;
52
53
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 return (request_id == 0U) ? (uint32_t)atomic_inc(&next_request_id) + 1U : request_id;
54 }
55
56 224 static int validate_virtual_path(const char *path, bool allow_root, char *scratch,
57 size_t scratch_size, uint16_t *path_size)
58 {
59 size_t size;
60 int result;
61
62
3/4
✓ Branch 0 taken 218 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 218 times.
224 if ((path == NULL) || (path_size == NULL)) {
63 6 return -EINVAL;
64 }
65 218 result = kfsw_ftp_resolve_path(path, allow_root, scratch, scratch_size);
66
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 200 times.
218 if (result != 0) {
67 18 return result;
68 }
69 200 size = strnlen(path, KFSW_FTP_MAX_PATH_SIZE + 1U);
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 200 times.
200 if (size > KFSW_FTP_MAX_PATH_SIZE) {
71 ✗ return -ENAMETOOLONG;
72 }
73 200 *path_size = (uint16_t)size;
74 200 return 0;
75 }
76
77 /*
78 * Receive one response and check that it answers the request in flight. On
79 * success the caller releases the frame; on error it is released here.
80 */
81 84 static int receive_response(struct kfsw_ftp_link *link, uint8_t expected_opcode,
82 uint32_t request_id, struct kfsw_ftp_link_frame *frame)
83 {
84 84 int result = kfsw_ftp_link_receive(link, frame);
85
86
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 70 times.
84 if (result != 0) {
87 14 return result;
88 }
89
2/2
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 8 times.
70 if ((frame->message.opcode != expected_opcode) ||
90
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 52 times.
62 (frame->message.request_id != request_id)) {
91 18 kfsw_ftp_link_release(frame);
92 18 return -EBADMSG;
93 }
94 52 result = kfsw_ftp_wire_status_to_errno(frame->message.status);
95
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 38 times.
52 if (result != 0) {
96 14 kfsw_ftp_link_release(frame);
97 }
98 52 return result;
99 }
100
101 104 static int simple_path_request(uint16_t node, const char *path, bool allow_root,
102 uint8_t request_opcode, uint8_t response_opcode,
103 struct kfsw_ftp_message *response_out)
104 {
105 104 struct kfsw_ftp_link link = {0};
106 104 struct kfsw_ftp_link_frame frame = {0};
107 208 struct kfsw_ftp_message request = {
108 .opcode = request_opcode,
109 104 .request_id = allocate_request_id(),
110 .path = (const uint8_t *)path,
111 };
112 int result;
113
114 104 result = validate_client(node);
115
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 92 times.
104 if (result != 0) {
116 12 return result;
117 }
118 92 result = validate_virtual_path(path, allow_root, client_workspace.path,
119 sizeof(client_workspace.path), &request.path_size);
120
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 76 times.
92 if (result != 0) {
121 16 return result;
122 }
123 76 result = kfsw_ftp_link_connect(&link, node);
124
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 70 times.
76 if (result != 0) {
125 6 return result;
126 }
127 70 result = kfsw_ftp_link_send(&link, &request);
128
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 6 times.
70 if (result == 0) {
129 64 result = receive_response(&link, response_opcode, request.request_id, &frame);
130 }
131
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 8 times.
70 if ((result == 0) && (response_out != NULL)) {
132 14 *response_out = frame.message;
133 }
134 70 kfsw_ftp_link_release(&frame);
135 70 kfsw_ftp_link_close(&link);
136 70 return result;
137 }
138
139 102 int kfsw_ftp_mkdir(uint16_t node, const char *path)
140 {
141 int result;
142
143 102 k_mutex_lock(&kfsw_ftp_client_lock, K_FOREVER);
144
2/2
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 70 times.
102 if (is_local_node(node)) {
145 32 result = validate_local();
146
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (result == 0) {
147 32 result = kfsw_ftp_local_mkdir(path, &client_workspace);
148 }
149 } else {
150 70 result = simple_path_request(node, path, false, KFSW_FTP_OP_MKDIR_REQUEST,
151 KFSW_FTP_OP_MKDIR_RESPONSE, NULL);
152 }
153 102 k_mutex_unlock(&kfsw_ftp_client_lock);
154 102 return result;
155 }
156
157 82 int kfsw_ftp_remove(uint16_t node, const char *path)
158 {
159 int result;
160
161 82 k_mutex_lock(&kfsw_ftp_client_lock, K_FOREVER);
162
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 20 times.
82 if (is_local_node(node)) {
163 62 result = validate_local();
164
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 2 times.
62 if (result == 0) {
165 60 result = kfsw_ftp_local_remove(path, &client_workspace);
166 }
167 } else {
168 20 result = simple_path_request(node, path, false, KFSW_FTP_OP_REMOVE_REQUEST,
169 KFSW_FTP_OP_REMOVE_RESPONSE, NULL);
170 }
171 82 k_mutex_unlock(&kfsw_ftp_client_lock);
172 82 return result;
173 }
174
175 14 static int remote_stat(uint16_t node, const char *path, struct kfsw_ftp_stat *info)
176 {
177 struct kfsw_ftp_message response;
178 14 int result = simple_path_request(node, path, true, KFSW_FTP_OP_STAT_REQUEST,
179 KFSW_FTP_OP_STAT_RESPONSE, &response);
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (result != 0) {
182 ✗ return result;
183 }
184
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 2 times.
14 if ((response.path_size != 0U) || (response.data_size != 0U) ||
185
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 ((response.flags != KFSW_FTP_ENTRY_FILE) &&
186
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 (response.flags != KFSW_FTP_ENTRY_DIRECTORY))) {
187 8 return -EBADMSG;
188 }
189 6 info->type = (enum kfsw_ftp_entry_type)response.flags;
190 6 info->size = response.total_size;
191 6 info->crc32 = response.crc32;
192 6 return 0;
193 }
194
195 42 int kfsw_ftp_stat(uint16_t node, const char *path, struct kfsw_ftp_stat *info)
196 {
197 int result;
198
199
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 30 times.
42 if (info == NULL) {
200 12 return -EINVAL;
201 }
202 30 k_mutex_lock(&kfsw_ftp_client_lock, K_FOREVER);
203
2/2
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 14 times.
30 if (is_local_node(node)) {
204 16 result = validate_local();
205
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (result == 0) {
206 16 result = kfsw_ftp_local_stat(path, &client_workspace, info);
207 }
208 } else {
209 14 result = remote_stat(node, path, info);
210 }
211 30 k_mutex_unlock(&kfsw_ftp_client_lock);
212 30 return result;
213 }
214
215 ✗ static int visit_list_entry(const struct kfsw_ftp_message *response,
216 kfsw_ftp_list_visitor_t visitor, void *context, bool *keep_visiting)
217 {
218 char name[KFSW_FTP_MAX_PATH_SIZE + 1U];
219 ✗ struct kfsw_ftp_entry entry = {
220 .name = name,
221 ✗ .type = (enum kfsw_ftp_entry_type)response->flags,
222 ✗ .size = response->total_size,
223 };
224
225
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if ((response->opcode != KFSW_FTP_OP_LIST_ENTRY) ||
226
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ (response->status != KFSW_FTP_STATUS_OK) || (response->path_size == 0U) ||
227
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (response->data_size != 0U) ||
228
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ ((response->flags != KFSW_FTP_ENTRY_FILE) &&
229
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ (response->flags != KFSW_FTP_ENTRY_DIRECTORY))) {
230 ✗ return -EBADMSG;
231 }
232 ✗ memcpy(name, response->path, response->path_size);
233 ✗ name[response->path_size] = '\0';
234 ✗ *keep_visiting = visitor(&entry, context);
235 ✗ return 0;
236 }
237
238 ✗ static int remote_list(uint16_t node, const char *path, kfsw_ftp_list_visitor_t visitor,
239 void *context)
240 {
241 ✗ struct kfsw_ftp_link link = {0};
242 ✗ struct kfsw_ftp_message request = {
243 .opcode = KFSW_FTP_OP_LIST_REQUEST,
244 ✗ .request_id = allocate_request_id(),
245 .path = (const uint8_t *)path,
246 };
247 ✗ bool keep_visiting = true;
248 int result;
249
250 ✗ result = validate_client(node);
251
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result == 0) {
252 ✗ result = validate_virtual_path(path, true, client_workspace.path,
253 sizeof(client_workspace.path), &request.path_size);
254 }
255
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result == 0) {
256 ✗ result = kfsw_ftp_link_connect(&link, node);
257 }
258
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result == 0) {
259 ✗ result = kfsw_ftp_link_send(&link, &request);
260 }
261
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ while (result == 0) {
262 struct kfsw_ftp_link_frame frame;
263
264 ✗ result = kfsw_ftp_link_receive(&link, &frame);
265
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (result != 0) {
266 ✗ break;
267 }
268
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (frame.message.request_id != request.request_id) {
269 ✗ result = -EBADMSG;
270
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (frame.message.opcode == KFSW_FTP_OP_LIST_END) {
271 ✗ result = kfsw_ftp_wire_status_to_errno(frame.message.status);
272 ✗ kfsw_ftp_link_release(&frame);
273 ✗ break;
274
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (keep_visiting) {
275 ✗ result = visit_list_entry(&frame.message, visitor, context, &keep_visiting);
276 }
277 ✗ kfsw_ftp_link_release(&frame);
278 }
279 ✗ kfsw_ftp_link_close(&link);
280 ✗ return result;
281 }
282
283 24 int kfsw_ftp_list(uint16_t node, const char *path, kfsw_ftp_list_visitor_t visitor, void *context)
284 {
285 int result;
286
287
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 20 times.
24 if (visitor == NULL) {
288 4 return -EINVAL;
289 }
290 20 k_mutex_lock(&kfsw_ftp_client_lock, K_FOREVER);
291
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 if (is_local_node(node)) {
292 20 result = validate_local();
293
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (result == 0) {
294 20 result = kfsw_ftp_local_list(path, &client_workspace, visitor, context);
295 }
296 } else {
297 ✗ result = remote_list(node, path, visitor, context);
298 }
299 20 k_mutex_unlock(&kfsw_ftp_client_lock);
300 20 return result;
301 }
302
303 /*
304 * Resolve both ends of an upload and CRC the local file before the connection
305 * opens, so the server learns the expected size and CRC in the request itself.
306 */
307 36 static int prepare_upload(const char *local_path, const char *remote_path,
308 struct kfsw_ftp_message *request, struct kfsw_ftp_transfer *transfer)
309 {
310 uint16_t remote_path_size;
311 int result;
312
313 36 result = validate_virtual_path(local_path, false, client_workspace.path,
314 sizeof(client_workspace.path), &remote_path_size);
315
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 if (result != 0) {
316 4 return result;
317 }
318 32 result = validate_virtual_path(remote_path, false, client_workspace.temporary_path,
319 sizeof(client_workspace.temporary_path), &remote_path_size);
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (result != 0) {
321 ✗ return result;
322 }
323 32 result = kfsw_ftp_file_crc(client_workspace.path, &client_workspace, &transfer->total_size,
324 &transfer->crc32);
325
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
32 if (result != 0) {
326 4 return result;
327 }
328 28 request->path = (const uint8_t *)remote_path;
329 28 request->path_size = remote_path_size;
330 28 request->total_size = transfer->total_size;
331 28 request->crc32 = transfer->crc32;
332 /* Ask to continue an interrupted upload of this same file. */
333 28 request->flags = KFSW_FTP_FLAG_RESUME;
334 28 return 0;
335 }
336
337 /*
338 * PUT_READY means the server opened its temporary file; PUT_RESULT means it
339 * refused. A ready reply also says where to start, which is non-zero when the
340 * server still holds a partial of this same file.
341 */
342 28 static int await_put_ready(struct kfsw_ftp_link *link, struct kfsw_ftp_transfer *transfer)
343 {
344 struct kfsw_ftp_link_frame frame;
345 28 int result = kfsw_ftp_link_receive(link, &frame);
346
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (result != 0) {
348 ✗ return result;
349 }
350
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
28 if ((frame.message.request_id != transfer->request_id) ||
351
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 ((frame.message.opcode != KFSW_FTP_OP_PUT_READY) &&
352
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 (frame.message.opcode != KFSW_FTP_OP_PUT_RESULT))) {
353 8 result = -EBADMSG;
354 } else {
355 20 result = kfsw_ftp_wire_status_to_errno(frame.message.status);
356
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
20 if ((result == 0) && (frame.message.opcode != KFSW_FTP_OP_PUT_READY)) {
357 ✗ result = -EBADMSG;
358 }
359 }
360
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 12 times.
28 if (result == 0) {
361 /* Past the end is nonsense; equal to it is an empty file, which has
362 * nothing to send and still has to be created.
363 */
364
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (frame.message.offset > transfer->total_size) {
365 4 result = -EBADMSG;
366 } else {
367 12 transfer->offset = frame.message.offset;
368 }
369 }
370 28 kfsw_ftp_link_release(&frame);
371 28 return result;
372 }
373
374 /* The peer echoes what it committed; a disagreement is an integrity failure. */
375 20 static int await_transfer_result(struct kfsw_ftp_link *link, uint8_t result_opcode,
376 const struct kfsw_ftp_transfer *transfer)
377 {
378 struct kfsw_ftp_link_frame frame;
379 20 int result = receive_response(link, result_opcode, transfer->request_id, &frame);
380
381
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 16 times.
20 if (result != 0) {
382 4 return result;
383 }
384
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 if ((frame.message.total_size != transfer->total_size) ||
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 (frame.message.crc32 != transfer->crc32)) {
386 4 result = -EILSEQ;
387 }
388 16 kfsw_ftp_link_release(&frame);
389 16 return result;
390 }
391
392 12 static void report_transfer(struct kfsw_ftp_transfer_result *transfer_result, uint32_t bytes,
393 uint32_t crc32, uint32_t started_ms)
394 {
395 12 transfer_result->bytes = bytes;
396 12 transfer_result->crc32 = crc32;
397 12 transfer_result->duration_ms = k_uptime_get_32() - started_ms;
398 12 }
399
400 /* Record the transfer result as an event. */
401 68 static void record_transfer_event(uint16_t event_id, uint16_t node, uint32_t bytes,
402 uint32_t crc32_or_errno, bool failed)
403 {
404 /* Counted even without the event record. */
405 68 kfsw_ftp_count_transfer(bytes, failed);
406
407 #if CONFIG_KFSW_EVENT
408 uint8_t payload[10];
409
410 ✗ sys_put_be16(node, &payload[0]);
411 ✗ sys_put_be32(bytes, &payload[2]);
412 ✗ sys_put_be32(crc32_or_errno, &payload[6]);
413
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kfsw_event_emit(KFSW_EVENT_SOURCE_FTP, event_id,
414 failed ? KFSW_EVENT_ERROR : KFSW_EVENT_INFO, payload, sizeof(payload));
415 #else
416 ARG_UNUSED(event_id);
417 ARG_UNUSED(node);
418 ARG_UNUSED(bytes);
419 ARG_UNUSED(crc32_or_errno);
420 ARG_UNUSED(failed);
421 #endif
422 68 }
423
424 52 int kfsw_ftp_put(uint16_t node, const char *local_path, const char *remote_path,
425 struct kfsw_ftp_transfer_result *transfer_result)
426 {
427 52 struct kfsw_ftp_link link = {0};
428 104 struct kfsw_ftp_message request = {
429 .opcode = KFSW_FTP_OP_PUT_REQUEST,
430 52 .request_id = allocate_request_id(),
431 };
432 52 struct kfsw_ftp_transfer transfer = {
433 .link = &link,
434 .workspace = &client_workspace,
435 52 .request_id = request.request_id,
436 .data_opcode = KFSW_FTP_OP_PUT_DATA,
437 };
438 uint32_t started_ms;
439 int result;
440
441
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 44 times.
52 if (transfer_result == NULL) {
442 8 return -EINVAL;
443 }
444
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 36 times.
44 if (is_local_node(node)) {
445 8 return -ENOTSUP;
446 }
447 36 memset(transfer_result, 0, sizeof(*transfer_result));
448 36 k_mutex_lock(&kfsw_ftp_client_lock, K_FOREVER);
449 36 started_ms = k_uptime_get_32();
450 36 result = validate_client(node);
451
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (result == 0) {
452 36 result = prepare_upload(local_path, remote_path, &request, &transfer);
453 }
454
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8 times.
36 if (result == 0) {
455 28 result = kfsw_ftp_link_connect(&link, node);
456 }
457
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8 times.
36 if (result == 0) {
458 28 result = kfsw_ftp_link_send(&link, &request);
459 }
460
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8 times.
36 if (result == 0) {
461 28 result = await_put_ready(&link, &transfer);
462 }
463
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
36 if (result == 0) {
464 12 result = kfsw_ftp_transfer_open_source(&transfer, client_workspace.path);
465 }
466
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
36 if (result == 0) {
467 12 result = kfsw_ftp_transfer_send(&transfer);
468 }
469
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 24 times.
36 if (result == 0) {
470 12 result = await_transfer_result(&link, KFSW_FTP_OP_PUT_RESULT, &transfer);
471 }
472 36 kfsw_ftp_link_close(&link);
473
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 if (result == 0) {
474 4 report_transfer(transfer_result, transfer.total_size, transfer.crc32, started_ms);
475 4 record_transfer_event(KFSW_EVENT_FTP_PUT_DONE, node, transfer.total_size,
476 transfer.crc32, false);
477 } else {
478 32 record_transfer_event(KFSW_EVENT_FTP_TRANSFER_FAILED, node, transfer.offset,
479 32 (uint32_t)(-result), true);
480 }
481 36 k_mutex_unlock(&kfsw_ftp_client_lock);
482 36 return result;
483 }
484
485 /*
486 * Resolve both ends of a download, derive the temporary path, and work out
487 * whether an interrupted download of this same file can be continued. An
488 * existing local file stays untouched until the transfer commits.
489 */
490 32 static int prepare_download(const char *remote_path, const char *local_path,
491 struct kfsw_ftp_message *request, uint32_t *partial_crc32)
492 {
493 struct fs_dirent entry;
494 32 uint32_t noted_size = 0U;
495 32 uint32_t noted_crc32 = 0U;
496 uint16_t local_path_size;
497 uint16_t path_size;
498 int result;
499
500 32 result = validate_virtual_path(remote_path, false, client_workspace.temporary_path,
501 sizeof(client_workspace.temporary_path), &path_size);
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (result != 0) {
503 ✗ return result;
504 }
505 32 result = validate_virtual_path(local_path, false, client_workspace.path,
506 sizeof(client_workspace.path), &local_path_size);
507
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 28 times.
32 if (result != 0) {
508 4 return result;
509 }
510 28 request->path = (const uint8_t *)remote_path;
511 28 request->path_size = path_size;
512 28 result = fs_stat(client_workspace.path, &entry);
513
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
28 if ((result == 0) && (entry.type == FS_DIR_ENTRY_DIR)) {
514 ✗ return -EISDIR;
515 }
516
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
28 if ((result != 0) && (result != -ENOENT)) {
517 ✗ return result;
518 }
519 result =
520 28 kfsw_ftp_make_temporary_path(client_workspace.path, client_workspace.temporary_path,
521 sizeof(client_workspace.temporary_path));
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (result != 0) {
523 ✗ return result;
524 }
525
526 /* Only the note says which file the partial beside it belongs to, so the
527 * claim carries that size and CRC32 and the far side decides.
528 */
529
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 20 times.
28 if (kfsw_ftp_partial_note_read(client_workspace.path, &noted_size, &noted_crc32) == 0) {
530 8 (void)kfsw_ftp_partial_resume_point(client_workspace.path, &client_workspace,
531 noted_size, noted_crc32, &request->offset,
532 partial_crc32);
533 }
534
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 20 times.
28 if (request->offset != 0U) {
535 8 request->total_size = noted_size;
536 8 request->crc32 = noted_crc32;
537 8 request->flags = KFSW_FTP_FLAG_RESUME;
538 }
539 28 return 0;
540 }
541
542 /*
543 * GET_INFO carries the expected size and CRC and where the sender starts;
544 * GET_RESULT means it refused the request outright. A start that is neither
545 * zero nor the offset asked for is a reply to some other conversation.
546 */
547 28 static int await_get_info(struct kfsw_ftp_link *link, struct kfsw_ftp_transfer *transfer,
548 uint32_t claimed_offset)
549 {
550 struct kfsw_ftp_link_frame frame;
551 28 int result = kfsw_ftp_link_receive(link, &frame);
552
553
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 24 times.
28 if (result != 0) {
554 4 return result;
555 }
556
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if ((frame.message.request_id != transfer->request_id) ||
557
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 16 times.
24 ((frame.message.opcode != KFSW_FTP_OP_GET_INFO) &&
558
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 (frame.message.opcode != KFSW_FTP_OP_GET_RESULT))) {
559 4 result = -EBADMSG;
560 } else {
561 20 result = kfsw_ftp_wire_status_to_errno(frame.message.status);
562
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
20 if ((result == 0) && (frame.message.opcode == KFSW_FTP_OP_GET_INFO)) {
563 16 transfer->total_size = frame.message.total_size;
564 16 transfer->crc32 = frame.message.crc32;
565
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
16 if ((frame.message.offset != 0U) &&
566
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 (frame.message.offset != claimed_offset)) {
567 4 result = -EBADMSG;
568 } else {
569 12 transfer->offset = frame.message.offset;
570 }
571
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (result == 0) {
572 ✗ result = -EBADMSG;
573 }
574 }
575 24 kfsw_ftp_link_release(&frame);
576 24 return result;
577 }
578
579 /*
580 * This node receives, so this node keeps the partial and the note beside it. A
581 * continued download inherits the CRC32 of what is already on disk; a refused
582 * claim starts again, and then the note has to describe the file now coming in
583 * before any of it is written.
584 */
585 12 static int prepare_partial(struct kfsw_ftp_transfer *transfer, uint32_t partial_crc32)
586 {
587
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 if (transfer->offset != 0U) {
588 4 transfer->actual_crc32 = partial_crc32;
589 4 kfsw_log_info("FTP: continuing %s at %u of %u bytes", client_workspace.path,
590 transfer->offset, transfer->total_size);
591 4 return 0;
592 }
593 8 return kfsw_ftp_partial_note_write(client_workspace.path, transfer->total_size,
594 transfer->crc32);
595 }
596
597 44 int kfsw_ftp_get(uint16_t node, const char *remote_path, const char *local_path,
598 struct kfsw_ftp_transfer_result *transfer_result)
599 {
600 44 struct kfsw_ftp_link link = {0};
601 88 struct kfsw_ftp_message request = {
602 .opcode = KFSW_FTP_OP_GET_REQUEST,
603 44 .request_id = allocate_request_id(),
604 };
605 44 struct kfsw_ftp_transfer transfer = {
606 .link = &link,
607 .workspace = &client_workspace,
608 44 .request_id = request.request_id,
609 .data_opcode = KFSW_FTP_OP_GET_DATA,
610 };
611 44 uint32_t partial_crc32 = 0U;
612 uint32_t started_ms;
613 int result;
614
615
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 36 times.
44 if (transfer_result == NULL) {
616 8 return -EINVAL;
617 }
618
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 32 times.
36 if (is_local_node(node)) {
619 4 return -ENOTSUP;
620 }
621 32 memset(transfer_result, 0, sizeof(*transfer_result));
622 32 k_mutex_lock(&kfsw_ftp_client_lock, K_FOREVER);
623 32 started_ms = k_uptime_get_32();
624 32 result = validate_client(node);
625
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (result == 0) {
626 32 result = prepare_download(remote_path, local_path, &request, &partial_crc32);
627 }
628
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 if (result == 0) {
629 28 result = kfsw_ftp_link_connect(&link, node);
630 }
631
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 if (result == 0) {
632 28 result = kfsw_ftp_link_send(&link, &request);
633 }
634
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 4 times.
32 if (result == 0) {
635 28 result = await_get_info(&link, &transfer, request.offset);
636 }
637
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (result == 0) {
638 12 result = kfsw_ftp_check_space(client_workspace.path,
639 12 transfer.total_size - transfer.offset);
640 }
641
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (result == 0) {
642 12 result = prepare_partial(&transfer, partial_crc32);
643 }
644
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (result == 0) {
645 12 result = kfsw_ftp_transfer_open_sink(&transfer, client_workspace.temporary_path);
646 }
647
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 20 times.
32 if (result == 0) {
648 12 result = kfsw_ftp_transfer_receive(&transfer);
649
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 if (result == 0) {
650 /*
651 * The server's result arrives before the commit, so a
652 * server-side failure is seen while the received data is
653 * still only a temporary file.
654 */
655 8 result = await_transfer_result(&link, KFSW_FTP_OP_GET_RESULT, &transfer);
656 }
657 12 result = kfsw_ftp_transfer_finish(&transfer, client_workspace.path,
658 client_workspace.temporary_path, result);
659 }
660 32 kfsw_ftp_link_close(&link);
661
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
32 if (result == 0) {
662 8 report_transfer(transfer_result, transfer.offset, transfer.actual_crc32,
663 started_ms);
664 8 record_transfer_event(KFSW_EVENT_FTP_GET_DONE, node, transfer.offset,
665 transfer.actual_crc32, false);
666 } else {
667 24 record_transfer_event(KFSW_EVENT_FTP_TRANSFER_FAILED, node, transfer.offset,
668 24 (uint32_t)(-result), true);
669 }
670 32 k_mutex_unlock(&kfsw_ftp_client_lock);
671 32 return result;
672 }
673