kfsw-services/src/ftp/ftp_store.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stddef.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #include <zephyr/fs/fs.h> | ||
| 7 | #include <zephyr/sys/crc.h> | ||
| 8 | |||
| 9 | #include "ftp_internal.h" | ||
| 10 | |||
| 11 | /* | ||
| 12 | * Storage under the FTP root: path resolution, file CRC, the temporary file and | ||
| 13 | * rename, and directory operations. Used by the server and by local requests. | ||
| 14 | */ | ||
| 15 | |||
| 16 | 70 | int kfsw_ftp_file_crc(const char *path, struct kfsw_ftp_workspace *workspace, uint32_t *file_size, | |
| 17 | uint32_t *crc32) | ||
| 18 | { | ||
| 19 | struct fs_file_t file; | ||
| 20 | 70 | uint32_t size = 0U; | |
| 21 | 70 | uint32_t crc = 0U; | |
| 22 | ssize_t bytes_read; | ||
| 23 | int close_result; | ||
| 24 | int result; | ||
| 25 | |||
| 26 | 70 | if ((path == NULL) || (workspace == NULL) || (file_size == NULL) || (crc32 == NULL)) { | |
| 27 | ✗ | return -EINVAL; | |
| 28 | } | ||
| 29 | 70 | fs_file_t_init(&file); | |
| 30 | 70 | result = fs_open(&file, path, FS_O_READ); | |
| 31 | 70 | if (result != 0) { | |
| 32 | 8 | return result; | |
| 33 | } | ||
| 34 | for (;;) { | ||
| 35 | 120 | bytes_read = fs_read(&file, workspace->chunk, sizeof(workspace->chunk)); | |
| 36 | 120 | if (bytes_read < 0) { | |
| 37 | ✗ | result = (int)bytes_read; | |
| 38 | ✗ | break; | |
| 39 | } | ||
| 40 | 120 | if (bytes_read == 0) { | |
| 41 | 62 | break; | |
| 42 | } | ||
| 43 | 58 | if ((uint32_t)bytes_read > UINT32_MAX - size) { | |
| 44 | ✗ | result = -EFBIG; | |
| 45 | ✗ | break; | |
| 46 | } | ||
| 47 | 58 | size += (uint32_t)bytes_read; | |
| 48 | 58 | crc = crc32_ieee_update(crc, workspace->chunk, (size_t)bytes_read); | |
| 49 | } | ||
| 50 | 62 | close_result = fs_close(&file); | |
| 51 | 62 | if (result == 0) { | |
| 52 | 62 | result = close_result; | |
| 53 | } | ||
| 54 | 62 | if (result == 0) { | |
| 55 | 62 | *file_size = size; | |
| 56 | 62 | *crc32 = crc; | |
| 57 | } | ||
| 58 | 62 | return result; | |
| 59 | } | ||
| 60 | |||
| 61 | 28 | int kfsw_ftp_check_space(const char *path, uint32_t bytes) | |
| 62 | { | ||
| 63 | struct fs_statvfs volume; | ||
| 64 | 28 | int result = fs_statvfs(path, &volume); | |
| 65 | |||
| 66 | 28 | if (result != 0) { | |
| 67 | ✗ | return result; | |
| 68 | } | ||
| 69 | 28 | if ((uint64_t)volume.f_bfree * volume.f_frsize < | |
| 70 | 28 | (uint64_t)bytes + CONFIG_KFSW_FTP_SPACE_MARGIN_BYTES) { | |
| 71 | 8 | return -ENOSPC; | |
| 72 | } | ||
| 73 | 20 | return 0; | |
| 74 | } | ||
| 75 | |||
| 76 | 144 | int kfsw_ftp_make_temporary_path(const char *path, char *temporary_path, size_t temporary_path_size) | |
| 77 | { | ||
| 78 | static const char suffix[] = ".part"; | ||
| 79 | size_t path_size; | ||
| 80 | |||
| 81 | 144 | if ((path == NULL) || (temporary_path == NULL)) { | |
| 82 | ✗ | return -EINVAL; | |
| 83 | } | ||
| 84 | 144 | path_size = strnlen(path, temporary_path_size); | |
| 85 | 144 | if ((path_size == temporary_path_size) || | |
| 86 | 144 | (path_size + sizeof(suffix) > temporary_path_size)) { | |
| 87 | ✗ | return -ENAMETOOLONG; | |
| 88 | } | ||
| 89 | 144 | memcpy(temporary_path, path, path_size); | |
| 90 | 144 | memcpy(&temporary_path[path_size], suffix, sizeof(suffix)); | |
| 91 | 144 | return 0; | |
| 92 | } | ||
| 93 | |||
| 94 | /* | ||
| 95 | * A partial upload keeps a note beside it naming the file it will become, so a | ||
| 96 | * partial from a different version is not continued into this one. Chunks | ||
| 97 | * arrive in order, so the partial's own size is the resume point. | ||
| 98 | */ | ||
| 99 | #define KFSW_FTP_PARTIAL_MAGIC 0x4B465450UL /* "KFTP" */ | ||
| 100 | #define KFSW_FTP_PARTIAL_VERSION 1U | ||
| 101 | |||
| 102 | struct partial_note { | ||
| 103 | uint32_t magic; | ||
| 104 | uint32_t version; | ||
| 105 | uint32_t total_size; | ||
| 106 | uint32_t crc32; | ||
| 107 | uint32_t note_crc32; | ||
| 108 | }; | ||
| 109 | |||
| 110 | 56 | static uint32_t note_crc(const struct partial_note *note) | |
| 111 | { | ||
| 112 | 56 | return crc32_ieee((const uint8_t *)note, offsetof(struct partial_note, note_crc32)); | |
| 113 | } | ||
| 114 | |||
| 115 | 98 | static int make_note_path(const char *path, char *note_path, size_t note_path_size) | |
| 116 | { | ||
| 117 | static const char suffix[] = ".map"; | ||
| 118 | size_t path_size; | ||
| 119 | 98 | int result = kfsw_ftp_make_temporary_path(path, note_path, note_path_size); | |
| 120 | |||
| 121 | 98 | if (result != 0) { | |
| 122 | ✗ | return result; | |
| 123 | } | ||
| 124 | 98 | path_size = strnlen(note_path, note_path_size); | |
| 125 | 98 | if (path_size + sizeof(suffix) > note_path_size) { | |
| 126 | ✗ | return -ENAMETOOLONG; | |
| 127 | } | ||
| 128 | 98 | memcpy(¬e_path[path_size], suffix, sizeof(suffix)); | |
| 129 | 98 | return 0; | |
| 130 | } | ||
| 131 | |||
| 132 | 54 | static int read_note(const char *note_path, struct partial_note *note) | |
| 133 | { | ||
| 134 | struct fs_file_t file; | ||
| 135 | ssize_t bytes_read; | ||
| 136 | int close_result; | ||
| 137 | int result; | ||
| 138 | |||
| 139 | 54 | fs_file_t_init(&file); | |
| 140 | 54 | result = fs_open(&file, note_path, FS_O_READ); | |
| 141 | 54 | if (result != 0) { | |
| 142 | 22 | return result; | |
| 143 | } | ||
| 144 | 32 | bytes_read = fs_read(&file, note, sizeof(*note)); | |
| 145 | 32 | close_result = fs_close(&file); | |
| 146 | 32 | if (bytes_read < 0) { | |
| 147 | ✗ | return (int)bytes_read; | |
| 148 | } | ||
| 149 | 32 | if (close_result != 0) { | |
| 150 | ✗ | return close_result; | |
| 151 | } | ||
| 152 | 32 | if ((size_t)bytes_read != sizeof(*note)) { | |
| 153 | ✗ | return -EILSEQ; | |
| 154 | } | ||
| 155 | 32 | if ((note->magic != KFSW_FTP_PARTIAL_MAGIC) || | |
| 156 | 30 | (note->version != KFSW_FTP_PARTIAL_VERSION) || (note->note_crc32 != note_crc(note))) { | |
| 157 | 2 | return -EILSEQ; | |
| 158 | } | ||
| 159 | 30 | return 0; | |
| 160 | } | ||
| 161 | |||
| 162 | 32 | int kfsw_ftp_partial_note_read(const char *path, uint32_t *total_size, uint32_t *crc32) | |
| 163 | { | ||
| 164 | char note_path[KFSW_FTP_FULL_PATH_SIZE]; | ||
| 165 | struct partial_note note; | ||
| 166 | int result; | ||
| 167 | |||
| 168 | 32 | if ((path == NULL) || (total_size == NULL) || (crc32 == NULL)) { | |
| 169 | ✗ | return -EINVAL; | |
| 170 | } | ||
| 171 | 32 | result = make_note_path(path, note_path, sizeof(note_path)); | |
| 172 | 32 | if (result == 0) { | |
| 173 | 32 | result = read_note(note_path, ¬e); | |
| 174 | } | ||
| 175 | 32 | if (result == 0) { | |
| 176 | 12 | *total_size = note.total_size; | |
| 177 | 12 | *crc32 = note.crc32; | |
| 178 | } | ||
| 179 | 32 | return result; | |
| 180 | } | ||
| 181 | |||
| 182 | 22 | int kfsw_ftp_partial_resume_point(const char *path, struct kfsw_ftp_workspace *workspace, | |
| 183 | uint32_t total_size, uint32_t crc32, uint32_t *offset, | ||
| 184 | uint32_t *partial_crc32) | ||
| 185 | { | ||
| 186 | char scratch[KFSW_FTP_FULL_PATH_SIZE]; | ||
| 187 | struct partial_note note; | ||
| 188 | uint32_t partial_size; | ||
| 189 | |||
| 190 | 22 | if ((path == NULL) || (workspace == NULL) || (offset == NULL) || (partial_crc32 == NULL)) { | |
| 191 | ✗ | return -EINVAL; | |
| 192 | } | ||
| 193 | 22 | *offset = 0U; | |
| 194 | 22 | *partial_crc32 = 0U; | |
| 195 | |||
| 196 | 22 | if (make_note_path(path, scratch, sizeof(scratch)) != 0) { | |
| 197 | ✗ | return 0; | |
| 198 | } | ||
| 199 | 22 | if (read_note(scratch, ¬e) != 0) { | |
| 200 | 4 | return 0; | |
| 201 | } | ||
| 202 | /* A note for a different file is no use, however far that upload got. */ | ||
| 203 | 18 | if ((note.total_size != total_size) || (note.crc32 != crc32)) { | |
| 204 | 4 | return 0; | |
| 205 | } | ||
| 206 | 14 | if (kfsw_ftp_make_temporary_path(path, scratch, sizeof(scratch)) != 0) { | |
| 207 | ✗ | return 0; | |
| 208 | } | ||
| 209 | 14 | if (kfsw_ftp_file_crc(scratch, workspace, &partial_size, partial_crc32) != 0) { | |
| 210 | ✗ | return 0; | |
| 211 | } | ||
| 212 | /* Nothing to continue from an empty or already complete partial. */ | ||
| 213 | 14 | if ((partial_size == 0U) || (partial_size >= total_size)) { | |
| 214 | ✗ | *partial_crc32 = 0U; | |
| 215 | ✗ | return 0; | |
| 216 | } | ||
| 217 | 14 | *offset = partial_size; | |
| 218 | 14 | return 0; | |
| 219 | } | ||
| 220 | |||
| 221 | 26 | int kfsw_ftp_partial_note_write(const char *path, uint32_t total_size, uint32_t crc32) | |
| 222 | { | ||
| 223 | char note_path[KFSW_FTP_FULL_PATH_SIZE]; | ||
| 224 | 26 | struct partial_note note = { | |
| 225 | .magic = KFSW_FTP_PARTIAL_MAGIC, | ||
| 226 | .version = KFSW_FTP_PARTIAL_VERSION, | ||
| 227 | .total_size = total_size, | ||
| 228 | .crc32 = crc32, | ||
| 229 | }; | ||
| 230 | struct fs_file_t file; | ||
| 231 | ssize_t written; | ||
| 232 | int close_result; | ||
| 233 | 26 | int result = make_note_path(path, note_path, sizeof(note_path)); | |
| 234 | |||
| 235 | 26 | if (result != 0) { | |
| 236 | ✗ | return result; | |
| 237 | } | ||
| 238 | 26 | note.note_crc32 = note_crc(¬e); | |
| 239 | 26 | fs_file_t_init(&file); | |
| 240 | 26 | result = fs_open(&file, note_path, FS_O_CREATE | FS_O_WRITE | FS_O_TRUNC); | |
| 241 | 26 | if (result != 0) { | |
| 242 | ✗ | return result; | |
| 243 | } | ||
| 244 | 26 | written = fs_write(&file, ¬e, sizeof(note)); | |
| 245 | 26 | if (written < 0) { | |
| 246 | ✗ | result = (int)written; | |
| 247 | 26 | } else if ((size_t)written != sizeof(note)) { | |
| 248 | ✗ | result = -EIO; | |
| 249 | } | ||
| 250 | 26 | if (result == 0) { | |
| 251 | 26 | result = fs_sync(&file); | |
| 252 | } | ||
| 253 | 26 | close_result = fs_close(&file); | |
| 254 | 26 | if (result == 0) { | |
| 255 | 26 | result = close_result; | |
| 256 | } | ||
| 257 | 26 | if (result != 0) { | |
| 258 | ✗ | (void)fs_unlink(note_path); | |
| 259 | } | ||
| 260 | 26 | return result; | |
| 261 | } | ||
| 262 | |||
| 263 | 18 | void kfsw_ftp_partial_note_remove(const char *path) | |
| 264 | { | ||
| 265 | char note_path[KFSW_FTP_FULL_PATH_SIZE]; | ||
| 266 | |||
| 267 | 18 | if (make_note_path(path, note_path, sizeof(note_path)) == 0) { | |
| 268 | 18 | (void)fs_unlink(note_path); | |
| 269 | } | ||
| 270 | 18 | } | |
| 271 | |||
| 272 | 18 | int kfsw_ftp_commit_temporary(const char *path, const char *temporary_path, uint32_t actual_size, | |
| 273 | uint32_t actual_crc32, uint32_t expected_size, | ||
| 274 | uint32_t expected_crc32) | ||
| 275 | { | ||
| 276 | int result; | ||
| 277 | |||
| 278 | 18 | if ((path == NULL) || (temporary_path == NULL)) { | |
| 279 | ✗ | return -EINVAL; | |
| 280 | } | ||
| 281 | 18 | if ((actual_size != expected_size) || (actual_crc32 != expected_crc32)) { | |
| 282 | 4 | result = -EILSEQ; | |
| 283 | } else { | ||
| 284 | 14 | result = fs_rename(temporary_path, path); | |
| 285 | } | ||
| 286 | 18 | if (result != 0) { | |
| 287 | 4 | (void)fs_unlink(temporary_path); | |
| 288 | } | ||
| 289 | /* The partial is gone either way, so its note describes nothing. */ | ||
| 290 | 18 | kfsw_ftp_partial_note_remove(path); | |
| 291 | 18 | return result; | |
| 292 | } | ||
| 293 | |||
| 294 | 36 | int kfsw_ftp_local_mkdir(const char *virtual_path, struct kfsw_ftp_workspace *workspace) | |
| 295 | { | ||
| 296 | int result; | ||
| 297 | |||
| 298 | 36 | if (workspace == NULL) { | |
| 299 | ✗ | return -EINVAL; | |
| 300 | } | ||
| 301 | result = | ||
| 302 | 36 | kfsw_ftp_resolve_write_path(virtual_path, workspace->path, sizeof(workspace->path)); | |
| 303 | 36 | if (result != 0) { | |
| 304 | 8 | return result; | |
| 305 | } | ||
| 306 | 28 | return fs_mkdir(workspace->path); | |
| 307 | } | ||
| 308 | |||
| 309 | 74 | int kfsw_ftp_local_remove(const char *virtual_path, struct kfsw_ftp_workspace *workspace) | |
| 310 | { | ||
| 311 | int result; | ||
| 312 | |||
| 313 | 74 | if (workspace == NULL) { | |
| 314 | ✗ | return -EINVAL; | |
| 315 | } | ||
| 316 | result = | ||
| 317 | 74 | kfsw_ftp_resolve_write_path(virtual_path, workspace->path, sizeof(workspace->path)); | |
| 318 | 74 | if (result != 0) { | |
| 319 | 40 | return result; | |
| 320 | } | ||
| 321 | 34 | result = kfsw_ftp_validate_path(virtual_path, false); | |
| 322 | 34 | if (result != 0) { | |
| 323 | ✗ | return result; | |
| 324 | } | ||
| 325 | 34 | return fs_unlink(workspace->path); | |
| 326 | } | ||
| 327 | |||
| 328 | 26 | int kfsw_ftp_local_stat(const char *virtual_path, struct kfsw_ftp_workspace *workspace, | |
| 329 | struct kfsw_ftp_stat *info) | ||
| 330 | { | ||
| 331 | struct fs_dirent entry; | ||
| 332 | int result; | ||
| 333 | |||
| 334 | 26 | if ((workspace == NULL) || (info == NULL)) { | |
| 335 | ✗ | return -EINVAL; | |
| 336 | } | ||
| 337 | result = | ||
| 338 | 26 | kfsw_ftp_resolve_path(virtual_path, true, workspace->path, sizeof(workspace->path)); | |
| 339 | 26 | if (result != 0) { | |
| 340 | 2 | return result; | |
| 341 | } | ||
| 342 | 24 | result = fs_stat(workspace->path, &entry); | |
| 343 | 24 | if (result != 0) { | |
| 344 | 6 | return result; | |
| 345 | } | ||
| 346 | 18 | if (entry.type == FS_DIR_ENTRY_DIR) { | |
| 347 | 8 | info->type = KFSW_FTP_ENTRY_DIRECTORY; | |
| 348 | 8 | info->size = 0U; | |
| 349 | 8 | info->crc32 = 0U; | |
| 350 | 8 | return 0; | |
| 351 | } | ||
| 352 | 10 | if (entry.type != FS_DIR_ENTRY_FILE) { | |
| 353 | ✗ | return -ENOTSUP; | |
| 354 | } | ||
| 355 | 10 | info->type = KFSW_FTP_ENTRY_FILE; | |
| 356 | 10 | return kfsw_ftp_file_crc(workspace->path, workspace, &info->size, &info->crc32); | |
| 357 | } | ||
| 358 | |||
| 359 | 24 | int kfsw_ftp_local_list(const char *virtual_path, struct kfsw_ftp_workspace *workspace, | |
| 360 | kfsw_ftp_list_visitor_t visitor, void *context) | ||
| 361 | { | ||
| 362 | struct fs_dir_t directory; | ||
| 363 | struct fs_dirent entry; | ||
| 364 | bool listing_root; | ||
| 365 | int close_result; | ||
| 366 | int result; | ||
| 367 | |||
| 368 | 24 | if ((workspace == NULL) || (visitor == NULL)) { | |
| 369 | ✗ | return -EINVAL; | |
| 370 | } | ||
| 371 | result = | ||
| 372 | 24 | kfsw_ftp_resolve_path(virtual_path, true, workspace->path, sizeof(workspace->path)); | |
| 373 | 24 | if (result != 0) { | |
| 374 | ✗ | return result; | |
| 375 | } | ||
| 376 | 24 | listing_root = strcmp(workspace->path, KFSW_FTP_ROOT_PATH) == 0; | |
| 377 | 24 | fs_dir_t_init(&directory); | |
| 378 | 24 | result = fs_opendir(&directory, workspace->path); | |
| 379 | 24 | if (result != 0) { | |
| 380 | 6 | return result; | |
| 381 | } | ||
| 382 | 64 | while (result == 0) { | |
| 383 | struct kfsw_ftp_entry visited; | ||
| 384 | |||
| 385 | 64 | result = fs_readdir(&directory, &entry); | |
| 386 | 64 | if ((result != 0) || (entry.name[0] == '\0')) { | |
| 387 | break; | ||
| 388 | } | ||
| 389 | 46 | if (strnlen(entry.name, KFSW_FTP_MAX_PATH_SIZE + 1U) > KFSW_FTP_MAX_PATH_SIZE) { | |
| 390 | ✗ | result = -ENAMETOOLONG; | |
| 391 | ✗ | break; | |
| 392 | } | ||
| 393 | 30 | if ((entry.type == FS_DIR_ENTRY_FILE) && (entry.size > UINT32_MAX)) { | |
| 394 | ✗ | result = -EFBIG; | |
| 395 | ✗ | break; | |
| 396 | } | ||
| 397 | 46 | visited.name = entry.name; | |
| 398 | 92 | visited.type = (entry.type == FS_DIR_ENTRY_DIR) ? KFSW_FTP_ENTRY_DIRECTORY | |
| 399 | 46 | : KFSW_FTP_ENTRY_FILE; | |
| 400 | 46 | visited.size = (entry.type == FS_DIR_ENTRY_FILE) ? (uint32_t)entry.size : 0U; | |
| 401 | 46 | if (!visitor(&visited, context)) { | |
| 402 | ✗ | listing_root = false; | |
| 403 | ✗ | break; | |
| 404 | } | ||
| 405 | } | ||
| 406 | 18 | close_result = fs_closedir(&directory); | |
| 407 | 18 | if (result != 0) { | |
| 408 | ✗ | return result; | |
| 409 | } | ||
| 410 | 18 | if ((close_result == 0) && listing_root) { | |
| 411 | 10 | (void)kfsw_ftp_list_roots(visitor, context); | |
| 412 | } | ||
| 413 | 18 | return close_result; | |
| 414 | } | ||
| 415 |