| 2334 | }; |
| 2335 | |
| 2336 | cbm_db_writer_t *cbm_writer_open(const char *path) { |
| 2337 | cbm_db_writer_t *w = (cbm_db_writer_t *)calloc(CBM_ALLOC_ONE, sizeof(*w)); |
| 2338 | if (!w) { |
| 2339 | return NULL; |
| 2340 | } |
| 2341 | int n = snprintf(w->wc.final_path, sizeof(w->wc.final_path), "%s", path); |
| 2342 | if (n < 0 || (size_t)n >= sizeof(w->wc.final_path) || |
| 2343 | make_writer_temp_path(path, w, w->wc.temp_path, sizeof(w->wc.temp_path)) != 0) { |
| 2344 | /* Both conditions are truncation and neither sets errno; name the |
| 2345 | * reason rather than let the caller report a stale one. */ |
| 2346 | free(w); |
| 2347 | errno = ENAMETOOLONG; |
| 2348 | return NULL; |
| 2349 | } |
| 2350 | FILE *fp = cbm_fopen(w->wc.temp_path, "wb"); |
| 2351 | if (!fp) { |
| 2352 | /* The cleanup below unlinks a file that was never created, so it |
| 2353 | * fails and leaves ENOENT behind — which reads as a missing path |
| 2354 | * when the real answer is that the directory refused the create. |
| 2355 | * That is the #1620 case, so carry the open's reason across it. */ |
| 2356 | int open_errno = errno; |
| 2357 | (void)cbm_unlink(w->wc.temp_path); |
| 2358 | free(w); |
| 2359 | errno = open_errno; |
| 2360 | return NULL; |
| 2361 | } |
| 2362 | w->wc.fp = fp; |
| 2363 | w->wc.next_page = FIRST_DATA_PAGE; |
| 2364 | /* Nodes are never page 1 (page 1 is sqlite_master, written at finalize). */ |
| 2365 | pb_init(&w->nodes_pb, fp, FIRST_DATA_PAGE, false); |
| 2366 | return w; |
| 2367 | } |
| 2368 | |
| 2369 | int cbm_writer_append_nodes(cbm_db_writer_t *w, const CBMDumpNode *nodes, int count) { |
| 2370 | if (!w) { |
no test coverage detected