kfsw-services/src/hk/hk_reader.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <errno.h> | ||
| 2 | #include <stdbool.h> | ||
| 3 | #include <stddef.h> | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | #include <zephyr/fs/fs.h> | ||
| 9 | #include <zephyr/sys/byteorder.h> | ||
| 10 | #include <zephyr/sys/crc.h> | ||
| 11 | #include <zephyr/sys/util.h> | ||
| 12 | |||
| 13 | #include <kfsw/platform/storage.h> | ||
| 14 | #include <kfsw/services/hk.h> | ||
| 15 | #define KFSW_LOG_MODULE KFSW_LOG_MODULE_HK | ||
| 16 | #include <kfsw/services/log.h> | ||
| 17 | |||
| 18 | #include "hk_internal.h" | ||
| 19 | |||
| 20 | #if CONFIG_KFSW_HK_STORE | ||
| 21 | |||
| 22 | /* | ||
| 23 | * Reading back what the store wrote. The ring holds the frames themselves, so a | ||
| 24 | * record needs no decoding to be served again: the same sample structure, the | ||
| 25 | * same printer, the same remote reply as a live one. | ||
| 26 | * | ||
| 27 | * An extract is a file of selected records for downlink, which is the only way | ||
| 28 | * a pass produces a dataset rather than whatever the newest sample happened to | ||
| 29 | * be when the ground asked. | ||
| 30 | * | ||
| 31 | * 0 magic "KHKD" | ||
| 32 | * 4 format version | ||
| 33 | * 5 report | ||
| 34 | * 6 record size | ||
| 35 | * 8 record count | ||
| 36 | * 12 first sequence | ||
| 37 | * 14 last sequence | ||
| 38 | * 16 CRC32 over the whole file with these four bytes zero | ||
| 39 | */ | ||
| 40 | #define KFSW_HK_DATASET_MAGIC "KHKD" | ||
| 41 | #define KFSW_HK_DATASET_MAGIC_SIZE 4U | ||
| 42 | #define KFSW_HK_DATASET_VERSION 1U | ||
| 43 | #define KFSW_HK_DATASET_HEADER_SIZE 20U | ||
| 44 | #define KFSW_HK_DATASET_CRC_OFFSET 16U | ||
| 45 | |||
| 46 | #define KFSW_HK_EXTRACT_PATH_MAX 96U | ||
| 47 | #define KFSW_HK_EXTRACT_SUFFIX ".part" | ||
| 48 | |||
| 49 | /* Frame header, as hk.c writes it into every sample. */ | ||
| 50 | #define FRAME_VERSION_OFFSET 0U | ||
| 51 | #define FRAME_REPORT_OFFSET 1U | ||
| 52 | #define FRAME_SEQUENCE_OFFSET 2U | ||
| 53 | #define FRAME_SECONDS_OFFSET 4U | ||
| 54 | #define FRAME_ENTRY_COUNT_OFFSET 8U | ||
| 55 | #define FRAME_FLAGS_OFFSET 9U | ||
| 56 | |||
| 57 | /** An open store file and what its slots were found to hold. */ | ||
| 58 | struct store_reader { | ||
| 59 | struct fs_file_t file; | ||
| 60 | uint16_t record_size; | ||
| 61 | uint32_t capacity; | ||
| 62 | /** Sequence of the newest record found. */ | ||
| 63 | uint16_t newest; | ||
| 64 | /** Slots that hold a record, so also how far back the ring goes. */ | ||
| 65 | uint16_t written; | ||
| 66 | bool open; | ||
| 67 | }; | ||
| 68 | |||
| 69 | 66 | static void reader_close(struct store_reader *reader) | |
| 70 | { | ||
| 71 | 66 | if (reader->open) { | |
| 72 | 66 | (void)fs_close(&reader->file); | |
| 73 | 66 | reader->open = false; | |
| 74 | } | ||
| 75 | 66 | } | |
| 76 | |||
| 77 | /* A slot is this report's when its own header says so and its sequence lands on | ||
| 78 | * the slot it was read from. | ||
| 79 | */ | ||
| 80 | 436 | static bool slot_belongs(const uint8_t *record, uint8_t report, uint32_t capacity, uint32_t slot) | |
| 81 | { | ||
| 82 | uint16_t sequence; | ||
| 83 | |||
| 84 | 436 | if (record[FRAME_VERSION_OFFSET] != KFSW_HK_PROTOCOL_VERSION) { | |
| 85 | ✗ | return false; | |
| 86 | } | ||
| 87 | 436 | if (record[FRAME_REPORT_OFFSET] != report) { | |
| 88 | ✗ | return false; | |
| 89 | } | ||
| 90 | 436 | sequence = sys_get_be16(&record[FRAME_SEQUENCE_OFFSET]); | |
| 91 | 436 | return ((uint32_t)sequence % capacity) == slot; | |
| 92 | } | ||
| 93 | |||
| 94 | 558 | static int read_at(struct fs_file_t *file, off_t offset, uint8_t *data, size_t size) | |
| 95 | { | ||
| 96 | 558 | int result = fs_seek(file, offset, FS_SEEK_SET); | |
| 97 | |||
| 98 | 558 | if (result != 0) { | |
| 99 | ✗ | return result; | |
| 100 | } | ||
| 101 | 1060 | while (size > 0U) { | |
| 102 | 558 | ssize_t read = fs_read(file, data, size); | |
| 103 | |||
| 104 | 558 | if (read < 0) { | |
| 105 | ✗ | return (int)read; | |
| 106 | } | ||
| 107 | 558 | if (read == 0) { | |
| 108 | 56 | return -EBADMSG; | |
| 109 | } | ||
| 110 | 502 | data += read; | |
| 111 | 502 | size -= (size_t)read; | |
| 112 | } | ||
| 113 | 502 | return 0; | |
| 114 | } | ||
| 115 | |||
| 116 | /* | ||
| 117 | * Opens the file and walks every slot once, which is what tells the window | ||
| 118 | * apart: the newest sequence and how many slots hold anything. | ||
| 119 | */ | ||
| 120 | 70 | static int reader_open(uint8_t report, struct store_reader *reader) | |
| 121 | { | ||
| 122 | uint8_t header[KFSW_HK_STORE_HEADER_SIZE]; | ||
| 123 | uint8_t record[CONFIG_KFSW_HK_SAMPLE_BYTES]; | ||
| 124 | char path[64]; | ||
| 125 | 70 | bool have_newest = false; | |
| 126 | int result; | ||
| 127 | |||
| 128 | 70 | *reader = (struct store_reader){0}; | |
| 129 | |||
| 130 | 70 | if (!kfsw_storage_is_ready()) { | |
| 131 | ✗ | return -ENODEV; | |
| 132 | } | ||
| 133 | |||
| 134 | 70 | kfsw_hk_store_path(report, path, sizeof(path)); | |
| 135 | 70 | fs_file_t_init(&reader->file); | |
| 136 | 70 | result = fs_open(&reader->file, path, FS_O_READ); | |
| 137 | 70 | if (result != 0) { | |
| 138 | 4 | return result; | |
| 139 | } | ||
| 140 | 66 | reader->open = true; | |
| 141 | |||
| 142 | 66 | result = read_at(&reader->file, 0, header, sizeof(header)); | |
| 143 | 66 | if (result != 0) { | |
| 144 | ✗ | reader_close(reader); | |
| 145 | ✗ | return result; | |
| 146 | } | ||
| 147 | 66 | if ((memcmp(header, KFSW_HK_STORE_MAGIC, KFSW_HK_STORE_MAGIC_SIZE) != 0) || | |
| 148 | 66 | (header[4] != KFSW_HK_STORE_VERSION) || (header[5] != report)) { | |
| 149 | 2 | reader_close(reader); | |
| 150 | 2 | return -EBADMSG; | |
| 151 | } | ||
| 152 | 64 | reader->record_size = sys_get_be16(&header[6]); | |
| 153 | 64 | reader->capacity = sys_get_be32(&header[8]); | |
| 154 | 64 | if ((reader->record_size == 0U) || | |
| 155 | 64 | (reader->record_size > (uint16_t)CONFIG_KFSW_HK_SAMPLE_BYTES) || | |
| 156 | 64 | (reader->capacity == 0U)) { | |
| 157 | ✗ | reader_close(reader); | |
| 158 | ✗ | return -EBADMSG; | |
| 159 | } | ||
| 160 | |||
| 161 | 330 | for (uint32_t slot = 0U; slot < reader->capacity; slot++) { | |
| 162 | 322 | off_t offset = (off_t)KFSW_HK_STORE_HEADER_SIZE + | |
| 163 | 322 | ((off_t)slot * (off_t)reader->record_size); | |
| 164 | uint16_t sequence; | ||
| 165 | |||
| 166 | 322 | result = read_at(&reader->file, offset, record, reader->record_size); | |
| 167 | 322 | if (result != 0) { | |
| 168 | /* A file shorter than its capacity is the normal case while | ||
| 169 | * the ring is still filling. */ | ||
| 170 | 56 | result = 0; | |
| 171 | 56 | break; | |
| 172 | } | ||
| 173 | 266 | if (record[FRAME_VERSION_OFFSET] == 0U) { | |
| 174 | ✗ | continue; /* Never written. */ | |
| 175 | } | ||
| 176 | 266 | if (!slot_belongs(record, report, reader->capacity, slot)) { | |
| 177 | ✗ | continue; /* Left by a file with a different shape. */ | |
| 178 | } | ||
| 179 | |||
| 180 | 266 | sequence = sys_get_be16(&record[FRAME_SEQUENCE_OFFSET]); | |
| 181 | 266 | reader->written++; | |
| 182 | 266 | if (!have_newest || ((int16_t)(sequence - reader->newest) > 0)) { | |
| 183 | 226 | reader->newest = sequence; | |
| 184 | 226 | have_newest = true; | |
| 185 | } | ||
| 186 | } | ||
| 187 | |||
| 188 | 64 | if (!have_newest) { | |
| 189 | ✗ | reader->written = 0U; | |
| 190 | } | ||
| 191 | 64 | return 0; | |
| 192 | } | ||
| 193 | |||
| 194 | /** Sequence of the oldest record still readable. */ | ||
| 195 | 184 | static uint16_t reader_oldest(const struct store_reader *reader) | |
| 196 | { | ||
| 197 | 184 | return (uint16_t)(reader->newest - (uint16_t)(reader->written - 1U)); | |
| 198 | } | ||
| 199 | |||
| 200 | /* Reads the slot a sequence number selects, and refuses one holding something | ||
| 201 | * else, which is how a gap in a partly filled ring is skipped. | ||
| 202 | */ | ||
| 203 | 170 | static int reader_record(struct store_reader *reader, uint8_t report, uint16_t sequence, | |
| 204 | struct kfsw_hk_sample *sample) | ||
| 205 | { | ||
| 206 | 170 | off_t offset = | |
| 207 | (off_t)KFSW_HK_STORE_HEADER_SIZE + | ||
| 208 | 170 | ((off_t)((uint32_t)sequence % reader->capacity) * (off_t)reader->record_size); | |
| 209 | int result; | ||
| 210 | |||
| 211 | 170 | (void)memset(sample, 0, sizeof(*sample)); | |
| 212 | 170 | result = read_at(&reader->file, offset, sample->data, reader->record_size); | |
| 213 | 170 | if (result != 0) { | |
| 214 | ✗ | return result; | |
| 215 | } | ||
| 216 | 170 | if ((sample->data[FRAME_VERSION_OFFSET] == 0U) || | |
| 217 | 170 | !slot_belongs(sample->data, report, reader->capacity, | |
| 218 | 340 | (uint32_t)sequence % reader->capacity) || | |
| 219 | 170 | (sys_get_be16(&sample->data[FRAME_SEQUENCE_OFFSET]) != sequence)) { | |
| 220 | ✗ | return -ENOENT; | |
| 221 | } | ||
| 222 | |||
| 223 | 170 | sample->length = reader->record_size; | |
| 224 | 170 | sample->sequence = sequence; | |
| 225 | 170 | sample->seconds = sys_get_be32(&sample->data[FRAME_SECONDS_OFFSET]); | |
| 226 | 170 | sample->entry_count = sample->data[FRAME_ENTRY_COUNT_OFFSET]; | |
| 227 | 170 | sample->flags = sample->data[FRAME_FLAGS_OFFSET]; | |
| 228 | 170 | return 0; | |
| 229 | } | ||
| 230 | |||
| 231 | /* Sequence numbers wrap, so a window is a signed difference rather than a | ||
| 232 | * comparison, and a range across the wrap still selects what it should. | ||
| 233 | */ | ||
| 234 | 170 | static bool sequence_within(const struct kfsw_hk_store_filter *filter, uint16_t sequence) | |
| 235 | { | ||
| 236 | 170 | if ((filter->from_sequence != 0U) && ((int16_t)(sequence - filter->from_sequence) < 0)) { | |
| 237 | 40 | return false; | |
| 238 | } | ||
| 239 | 130 | if ((filter->to_sequence != 0U) && ((int16_t)(sequence - filter->to_sequence) > 0)) { | |
| 240 | 6 | return false; | |
| 241 | } | ||
| 242 | 124 | return true; | |
| 243 | } | ||
| 244 | |||
| 245 | 170 | static bool sample_matches(const struct kfsw_hk_store_filter *filter, | |
| 246 | const struct kfsw_hk_sample *sample) | ||
| 247 | { | ||
| 248 | 170 | if (!sequence_within(filter, sample->sequence)) { | |
| 249 | 46 | return false; | |
| 250 | } | ||
| 251 | 124 | if ((filter->from_seconds != 0U) && (sample->seconds < filter->from_seconds)) { | |
| 252 | ✗ | return false; | |
| 253 | } | ||
| 254 | 124 | if ((filter->to_seconds != 0U) && (sample->seconds > filter->to_seconds)) { | |
| 255 | ✗ | return false; | |
| 256 | } | ||
| 257 | 124 | if ((filter->without_flags != 0U) && ((sample->flags & filter->without_flags) != 0U)) { | |
| 258 | 6 | return false; | |
| 259 | } | ||
| 260 | 118 | return true; | |
| 261 | } | ||
| 262 | |||
| 263 | /* | ||
| 264 | * Walks the matching records oldest first. Stops early when the visitor says so, | ||
| 265 | * which is how reading one record avoids reading the rest. | ||
| 266 | */ | ||
| 267 | typedef bool (*record_visitor_t)(const struct kfsw_hk_sample *sample, uint16_t position, | ||
| 268 | void *context); | ||
| 269 | |||
| 270 | 54 | static int walk(const struct kfsw_hk_store_filter *filter, record_visitor_t visitor, void *context, | |
| 271 | uint16_t *matched) | ||
| 272 | { | ||
| 273 | struct store_reader reader; | ||
| 274 | 54 | uint16_t position = 0U; | |
| 275 | int result; | ||
| 276 | |||
| 277 | 54 | if ((filter == NULL) || (filter->report >= CONFIG_KFSW_HK_REPORTS)) { | |
| 278 | 2 | return -EINVAL; | |
| 279 | } | ||
| 280 | |||
| 281 | 52 | result = reader_open(filter->report, &reader); | |
| 282 | 52 | if (result != 0) { | |
| 283 | 2 | return result; | |
| 284 | } | ||
| 285 | |||
| 286 | 200 | for (uint16_t index = 0U; index < reader.written; index++) { | |
| 287 | struct kfsw_hk_sample sample; | ||
| 288 | 170 | uint16_t sequence = (uint16_t)(reader_oldest(&reader) + index); | |
| 289 | |||
| 290 | 170 | if (reader_record(&reader, filter->report, sequence, &sample) != 0) { | |
| 291 | 52 | continue; | |
| 292 | } | ||
| 293 | 170 | if (!sample_matches(filter, &sample)) { | |
| 294 | 52 | continue; | |
| 295 | } | ||
| 296 | 118 | position++; | |
| 297 | 118 | if ((visitor != NULL) && !visitor(&sample, (uint16_t)(position - 1U), context)) { | |
| 298 | 20 | break; | |
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | 50 | reader_close(&reader); | |
| 303 | 50 | *matched = position; | |
| 304 | 50 | return 0; | |
| 305 | } | ||
| 306 | |||
| 307 | 22 | int kfsw_hk_store_query(uint8_t report, struct kfsw_hk_store_info *info) | |
| 308 | { | ||
| 309 | struct store_reader reader; | ||
| 310 | int result; | ||
| 311 | |||
| 312 | 22 | if ((info == NULL) || (report >= CONFIG_KFSW_HK_REPORTS)) { | |
| 313 | 4 | return -EINVAL; | |
| 314 | } | ||
| 315 | |||
| 316 | 18 | result = reader_open(report, &reader); | |
| 317 | 18 | if (result != 0) { | |
| 318 | 4 | return result; | |
| 319 | } | ||
| 320 | |||
| 321 | 14 | *info = (struct kfsw_hk_store_info){ | |
| 322 | 14 | .record_size = reader.record_size, | |
| 323 | 14 | .capacity = reader.capacity, | |
| 324 | 14 | .records = reader.written, | |
| 325 | 14 | .oldest = (reader.written != 0U) ? reader_oldest(&reader) : 0U, | |
| 326 | 14 | .newest = (reader.written != 0U) ? reader.newest : 0U, | |
| 327 | 14 | .interval_ms = kfsw_hk_store_interval(report), | |
| 328 | }; | ||
| 329 | 14 | reader_close(&reader); | |
| 330 | 14 | return 0; | |
| 331 | } | ||
| 332 | |||
| 333 | 16 | int kfsw_hk_store_count(const struct kfsw_hk_store_filter *filter, uint16_t *records) | |
| 334 | { | ||
| 335 | 16 | uint16_t matched = 0U; | |
| 336 | int result; | ||
| 337 | |||
| 338 | 16 | if (records == NULL) { | |
| 339 | ✗ | return -EINVAL; | |
| 340 | } | ||
| 341 | 16 | result = walk(filter, NULL, NULL, &matched); | |
| 342 | 16 | if (result == 0) { | |
| 343 | 12 | *records = matched; | |
| 344 | } | ||
| 345 | 16 | return result; | |
| 346 | } | ||
| 347 | |||
| 348 | struct read_one { | ||
| 349 | struct kfsw_hk_sample *sample; | ||
| 350 | uint16_t wanted; | ||
| 351 | bool found; | ||
| 352 | }; | ||
| 353 | |||
| 354 | 56 | static bool take_one(const struct kfsw_hk_sample *sample, uint16_t position, void *context) | |
| 355 | { | ||
| 356 | 56 | struct read_one *state = context; | |
| 357 | |||
| 358 | 56 | if (position != state->wanted) { | |
| 359 | 36 | return true; | |
| 360 | } | ||
| 361 | 20 | *state->sample = *sample; | |
| 362 | 20 | state->found = true; | |
| 363 | 20 | return false; | |
| 364 | } | ||
| 365 | |||
| 366 | 24 | int kfsw_hk_store_read(const struct kfsw_hk_store_filter *filter, uint16_t index, | |
| 367 | struct kfsw_hk_sample *sample) | ||
| 368 | { | ||
| 369 | 24 | struct read_one state = {.sample = sample, .wanted = index}; | |
| 370 | 24 | uint16_t matched = 0U; | |
| 371 | int result; | ||
| 372 | |||
| 373 | 24 | if (sample == NULL) { | |
| 374 | ✗ | return -EINVAL; | |
| 375 | } | ||
| 376 | 24 | result = walk(filter, take_one, &state, &matched); | |
| 377 | 24 | if (result != 0) { | |
| 378 | ✗ | return result; | |
| 379 | } | ||
| 380 | 24 | return state.found ? 0 : -ENOENT; | |
| 381 | } | ||
| 382 | |||
| 383 | /** The window a filter selects, so the header is complete before a record is written. */ | ||
| 384 | struct window { | ||
| 385 | uint16_t first_sequence; | ||
| 386 | uint16_t last_sequence; | ||
| 387 | }; | ||
| 388 | |||
| 389 | 14 | static bool note_window(const struct kfsw_hk_sample *sample, uint16_t position, void *context) | |
| 390 | { | ||
| 391 | 14 | struct window *state = context; | |
| 392 | |||
| 393 | 14 | if (position == 0U) { | |
| 394 | 6 | state->first_sequence = sample->sequence; | |
| 395 | } | ||
| 396 | 14 | state->last_sequence = sample->sequence; | |
| 397 | 14 | return true; | |
| 398 | } | ||
| 399 | |||
| 400 | struct extract_state { | ||
| 401 | struct fs_file_t file; | ||
| 402 | uint32_t crc; | ||
| 403 | uint16_t record_size; | ||
| 404 | int error; | ||
| 405 | }; | ||
| 406 | |||
| 407 | 14 | static bool write_one(const struct kfsw_hk_sample *sample, uint16_t position, void *context) | |
| 408 | { | ||
| 409 | 14 | struct extract_state *state = context; | |
| 410 | 14 | size_t remaining = state->record_size; | |
| 411 | 14 | const uint8_t *data = sample->data; | |
| 412 | |||
| 413 | ARG_UNUSED(position); | ||
| 414 | |||
| 415 | 28 | while (remaining > 0U) { | |
| 416 | 14 | ssize_t written = fs_write(&state->file, data, remaining); | |
| 417 | |||
| 418 | 14 | if (written <= 0) { | |
| 419 | ✗ | state->error = (written < 0) ? (int)written : -EIO; | |
| 420 | ✗ | return false; | |
| 421 | } | ||
| 422 | 14 | data += written; | |
| 423 | 14 | remaining -= (size_t)written; | |
| 424 | } | ||
| 425 | 14 | state->crc = crc32_ieee_update(state->crc, sample->data, state->record_size); | |
| 426 | 14 | return true; | |
| 427 | } | ||
| 428 | |||
| 429 | 12 | int kfsw_hk_store_extract(const struct kfsw_hk_store_filter *filter, const char *path, | |
| 430 | uint16_t *records) | ||
| 431 | { | ||
| 432 | char temporary_path[KFSW_HK_EXTRACT_PATH_MAX + sizeof(KFSW_HK_EXTRACT_SUFFIX)]; | ||
| 433 | 12 | uint8_t header[KFSW_HK_DATASET_HEADER_SIZE] = {0}; | |
| 434 | uint8_t checksum[sizeof(uint32_t)]; | ||
| 435 | struct kfsw_hk_store_info info; | ||
| 436 | 12 | struct extract_state state = {0}; | |
| 437 | 12 | struct window selection = {0}; | |
| 438 | 12 | uint16_t selected = 0U; | |
| 439 | 12 | uint16_t written = 0U; | |
| 440 | int result; | ||
| 441 | int close_result; | ||
| 442 | |||
| 443 | 12 | if ((filter == NULL) || (path == NULL) || (path[0] == '\0') || | |
| 444 | 8 | (strlen(path) >= (size_t)KFSW_HK_EXTRACT_PATH_MAX)) { | |
| 445 | 4 | return -EINVAL; | |
| 446 | } | ||
| 447 | |||
| 448 | 8 | result = kfsw_hk_store_query(filter->report, &info); | |
| 449 | 8 | if (result != 0) { | |
| 450 | ✗ | return result; | |
| 451 | } | ||
| 452 | |||
| 453 | /* One pass over the selection first, so every header field but the | ||
| 454 | * checksum is known before a record is written. */ | ||
| 455 | 8 | result = walk(filter, note_window, &selection, &selected); | |
| 456 | 8 | if (result != 0) { | |
| 457 | ✗ | return result; | |
| 458 | } | ||
| 459 | 8 | if (selected == 0U) { | |
| 460 | 2 | return -ENOENT; | |
| 461 | } | ||
| 462 | |||
| 463 | /* codechecker_intentional [bugprone-not-null-terminated-result] a binary magic */ | ||
| 464 | 6 | memcpy(header, KFSW_HK_DATASET_MAGIC, KFSW_HK_DATASET_MAGIC_SIZE); | |
| 465 | 6 | header[4] = KFSW_HK_DATASET_VERSION; | |
| 466 | 6 | header[5] = filter->report; | |
| 467 | 6 | sys_put_be16(info.record_size, &header[6]); | |
| 468 | 6 | sys_put_be32(selected, &header[8]); | |
| 469 | 6 | sys_put_be16(selection.first_sequence, &header[12]); | |
| 470 | 6 | sys_put_be16(selection.last_sequence, &header[14]); | |
| 471 | 6 | sys_put_be32(0U, &header[KFSW_HK_DATASET_CRC_OFFSET]); | |
| 472 | |||
| 473 | 6 | (void)snprintf(temporary_path, sizeof(temporary_path), "%s%s", path, | |
| 474 | KFSW_HK_EXTRACT_SUFFIX); | ||
| 475 | /* A temporary left by an interrupted extract would be appended to. */ | ||
| 476 | 6 | (void)fs_unlink(temporary_path); | |
| 477 | |||
| 478 | 6 | state.record_size = info.record_size; | |
| 479 | 6 | state.crc = crc32_ieee_update(0U, header, sizeof(header)); | |
| 480 | 6 | fs_file_t_init(&state.file); | |
| 481 | 6 | result = fs_open(&state.file, temporary_path, FS_O_CREATE | FS_O_WRITE | FS_O_TRUNC); | |
| 482 | 6 | if (result != 0) { | |
| 483 | ✗ | return result; | |
| 484 | } | ||
| 485 | |||
| 486 | 6 | if (fs_write(&state.file, header, sizeof(header)) != (ssize_t)sizeof(header)) { | |
| 487 | ✗ | result = -EIO; | |
| 488 | } | ||
| 489 | 6 | if (result == 0) { | |
| 490 | 6 | result = walk(filter, write_one, &state, &written); | |
| 491 | 6 | if (result == 0) { | |
| 492 | 6 | result = state.error; | |
| 493 | } | ||
| 494 | } | ||
| 495 | 6 | if ((result == 0) && (written != selected)) { | |
| 496 | /* The ring moved between the two passes. */ | ||
| 497 | ✗ | result = -EAGAIN; | |
| 498 | } | ||
| 499 | |||
| 500 | /* The checksum covers what follows it, so it is the one field written | ||
| 501 | * after the records, over the zeros the header was opened with. */ | ||
| 502 | 6 | if (result == 0) { | |
| 503 | 6 | sys_put_be32(state.crc, checksum); | |
| 504 | 6 | result = fs_seek(&state.file, (off_t)KFSW_HK_DATASET_CRC_OFFSET, FS_SEEK_SET); | |
| 505 | 6 | if ((result == 0) && (fs_write(&state.file, checksum, sizeof(checksum)) != | |
| 506 | (ssize_t)sizeof(checksum))) { | ||
| 507 | ✗ | result = -EIO; | |
| 508 | } | ||
| 509 | } | ||
| 510 | 6 | if (result == 0) { | |
| 511 | 6 | result = fs_sync(&state.file); | |
| 512 | } | ||
| 513 | 6 | close_result = fs_close(&state.file); | |
| 514 | 6 | if (result == 0) { | |
| 515 | 6 | result = close_result; | |
| 516 | } | ||
| 517 | 6 | if (result != 0) { | |
| 518 | ✗ | (void)fs_unlink(temporary_path); | |
| 519 | ✗ | return result; | |
| 520 | } | ||
| 521 | |||
| 522 | 6 | result = fs_rename(temporary_path, path); | |
| 523 | 6 | if (result != 0) { | |
| 524 | ✗ | (void)fs_unlink(temporary_path); | |
| 525 | ✗ | return result; | |
| 526 | } | ||
| 527 | |||
| 528 | 6 | kfsw_log_info("HK: extracted %u records of report %u to '%s'", written, filter->report, | |
| 529 | path); | ||
| 530 | 6 | if (records != NULL) { | |
| 531 | 6 | *records = written; | |
| 532 | } | ||
| 533 | 6 | return 0; | |
| 534 | } | ||
| 535 | |||
| 536 | #endif /* CONFIG_KFSW_HK_STORE */ | ||
| 537 |