|
ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
|
Cached page validation, streamed transfer, and publication. More...
#include <limits.h>#include <stdint.h>#include <stdio.h>#include <string.h>#include <time.h>#include "mdl_fetch.h"#include "mdl_fetch_body_internal.h"#include "mdl_fetch_internal.h"#include "mdl_hash.h"#include "mdl_net.h"#include "mdl_storage.h"#include "mdl_url_guard.h"#include "mdl_urlname.h"#include "ra8_attributes.h"#include "ra8_err.h"Go to the source code of this file.
Data Structures | |
| struct | mdl_page_outcome_t |
| Transfer outcome used to produce one progress event. More... | |
| struct | mdl_page_transfer_t |
| Mutable state for one conditional page transfer. More... | |
Enumerations | |
| enum | mdl_fetch_page_size_t : uint16_t { k_fetch_leaf_bytes = 32 } |
| Local page-path buffer size. More... | |
| enum | mdl_fetch_page_bound_t : uint32_t { k_http_not_modified = 304U , k_http_status_min = 100U , k_http_status_max = 599U , k_ms_per_sec = 1000U , k_ns_per_ms = 1000000U } |
| Status and time conversions used by page accounting. More... | |
Functions | |
| static uint32_t | internal_mdl_fetch_page_max_u32 (uint32_t a, uint32_t b) |
| Larger of two unsigned delay values. | |
| static const char * | internal_mdl_fetch_page_host (const char *url, char *buf, size_t cap) |
| Governor host key for one page URL. | |
| static int64_t | internal_mdl_fetch_mono_ms (const mdl_fetch_ctx_t *ctx) |
| Perform the mono ms step. | |
| static ra8_err_t | internal_mdl_fetch_emit_progress (const mdl_fetch_ctx_t *ctx, const mdl_run_pos_t *pos, size_t page_index, const mdl_page_outcome_t *out) |
| Emit one per-page progress event through the injected sink, if any. | |
| static bool | internal_mdl_fetch_copy_file (mdl_storage_t *storage, const char *src, const char *dst) |
| Copy one file through the validated portable storage transaction. | |
| static bool | internal_mdl_fetch_page_leaf (size_t page_no, const char *url, char *out, size_t cap) |
| Compose the page_NNNN.ext leaf for one page; false if it overran. | |
| static bool | internal_mdl_fetch_try_reuse (mdl_fetch_ctx_t *ctx, uint64_t url_hash, const char *target_abs, const char *target_rel) |
| Reuse an already-held byte-identical page instead of fetching it. | |
| static bool | internal_mdl_fetch_discard_stale_page (mdl_storage_t *storage, const char *guessed_abs, const char *actual_abs) |
| Remove a superseded URL-derived page only after its replacement committed. | |
| static ra8_err_t | internal_mdl_fetch_prepare_page (mdl_fetch_ctx_t *ctx, const char *chapter_url, const char *url, const char *target_abs, const char *target_rel, mdl_page_transfer_t *tx) |
| Prepare and execute the initial conditional page request. | |
| static ra8_err_t | internal_mdl_fetch_resolve_not_modified (mdl_fetch_ctx_t *ctx, const char *url, const char *target_abs, const char *target_rel, mdl_page_transfer_t *tx, bool *out_done) |
| Resolve HTTP 304 by verified reuse or one unconditional retry. | |
| static ra8_err_t | internal_mdl_fetch_publish_page (mdl_fetch_ctx_t *ctx, const char *url, const char *target_abs, mdl_page_transfer_t *tx, size_t *out_bytes) |
| Validate and atomically publish a nonempty staged page. | |
| static ra8_err_t | internal_mdl_fetch_do_fetch_page (mdl_fetch_ctx_t *ctx, const char *chapter_url, const char *url, const char *target_abs, const char *target_rel, size_t *out_bytes, mdl_net_resp_t *out_resp) |
| Perform the do fetch page step. | |
| static ra8_err_t | internal_mdl_fetch_one_page (mdl_fetch_ctx_t *ctx, const char *chapter_url, const char *dest_abs, const char *dest_rel, size_t page_no, size_t idx, mdl_chapter_rec_t *rec, mdl_fetch_stats_t *stats, mdl_page_outcome_t *out) |
Reuse or fetch page idx and checkpoint its outcome. | |
| ra8_err_t | priv_mdl_fetch_chapter_pages (mdl_fetch_ctx_t *ctx, const char *chapter_url, const char *dest_abs, const char *dest_rel, size_t base, mdl_chapter_rec_t *rec, mdl_fetch_stats_t *stats, const mdl_run_pos_t *pos) |
| Fetch every page of one chapter; fail (partial) on the first bad page. | |
Cached page validation, streamed transfer, and publication.
Implements one-page reuse and conditional-fetch state transitions over injected network and atomic storage contracts without allocation.
Definition in file mdl_fetch_page.c.
| enum mdl_fetch_page_bound_t : uint32_t |
Status and time conversions used by page accounting.
Definition at line 32 of file mdl_fetch_page.c.
| enum mdl_fetch_page_size_t : uint16_t |
Local page-path buffer size.
| Enumerator | |
|---|---|
| k_fetch_leaf_bytes | page_NNNN.ext leaf-name bytes. |
Definition at line 27 of file mdl_fetch_page.c.
|
static |
Copy one file through the validated portable storage transaction.
Adapts the canonical storage status to the fetcher's reuse boolean; the underlying operation independently hashes the staged bytes before an atomic create or truthful atomic replacement.
| [in,out] | storage | Initialized storage binding and workspaces. |
| [in] | src | Canonical regular-file source path. |
| [in] | dst | Canonical distinct destination path. |
| true | The destination now contains the exact source snapshot. |
| false | Validation, I/O, capability, or publication failed. |
storage is exclusively owned for the duration of the copy. Definition at line 176 of file mdl_fetch_page.c.
References k_ra8_ok, and mdl_storage_copy_atomic().
Referenced by internal_mdl_fetch_try_reuse().
|
static |
Remove a superseded URL-derived page only after its replacement committed.
Performs discard stale page under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in] | storage | Portable storage binding. |
| [in] | guessed_abs | Original URL-derived destination. |
| [in] | actual_abs | Magic-derived committed destination. |
| true | Paths match, the stale path was absent, or a regular stale file was removed. |
| false | The stale path names a non-regular object or could not be inspected/removed. |
actual_abs is never modified or removed. Definition at line 309 of file mdl_fetch_page.c.
References fw_fs_stat_t::exists, mdl_storage_t::fs, fw_fs_stat(), fw_fs_unlink(), k_fw_fs_node_file, k_ra8_ok, fw_fs_t::names, RA8_INTERNAL, strcmp(), and fw_fs_stat_t::type.
Referenced by internal_mdl_fetch_publish_page().
|
static |
Perform the do fetch page step.
Performs do fetch page under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in,out] | ctx | Borrowed operation context. |
| [in] | chapter_url | Canonical chapter URL. |
| [in] | url | Canonical input URL. |
| [in] | target_abs | Absolute destination path. |
| [in] | target_rel | Library-relative destination path. |
| [out] | out_bytes | Receives the transferred byte count. |
| [out] | out_resp | Receives normalized response metadata. |
| k_ra8_ok | The operation completed. |
| other | Validation, capacity, network, or storage failed. |
Definition at line 563 of file mdl_fetch_page.c.
References internal_mdl_fetch_prepare_page(), internal_mdl_fetch_publish_page(), internal_mdl_fetch_resolve_not_modified(), k_ra8_ok, RA8_INTERNAL, and mdl_page_transfer_t::resp.
Referenced by internal_mdl_fetch_one_page().
|
static |
Emit one per-page progress event through the injected sink, if any.
Snapshots chapter position, page position, bytes, duration, and reuse into a stack event and synchronously invokes the optional callback.
| [in] | ctx | Fetch context containing the optional callback and page count. |
| [in] | pos | Current chapter position and stable identifier. |
| [in] | page_index | Zero-based page index. |
| [in] | out | Completed page outcome to report. |
ctx, pos, and out are non-NULL. page_index is less than the current extracted page count. | k_ra8_ok | The operation completed successfully. |
| other | The originating validation, storage, stream, or network error. |
Definition at line 138 of file mdl_fetch_page.c.
References mdl_page_outcome_t::bytes, mdl_run_pos_t::chapter_id, mdl_run_pos_t::chapter_index, mdl_run_pos_t::chapter_total, mdl_url_list_t::count, mdl_page_outcome_t::elapsed_ms, mdl_fetch_ctx_t::images, k_ra8_ok, mdl_fetch_ctx_t::progress_ctx, mdl_fetch_ctx_t::progress_fn, RA8_INTERNAL, and mdl_page_outcome_t::reused.
Referenced by priv_mdl_fetch_chapter_pages().
|
static |
Perform the mono ms step.
Performs mono ms under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in] | ctx | Borrowed operation context. |
| other | The computed result in the function's declared domain. |
Definition at line 106 of file mdl_fetch_page.c.
References k_ms_per_sec, k_ns_per_ms, mdl_fetch_ctx_t::progress_fn, and RA8_INTERNAL.
Referenced by internal_mdl_fetch_one_page().
|
static |
Reuse or fetch page idx and checkpoint its outcome.
Composes absolute and relative paths, tries verified hash reuse, otherwise performs a governed conditional fetch, records the page, updates statistics, and checkpoints only the completed outcome.
| [in,out] | ctx | Fetch dependencies, persistent state, and page list. |
| [in] | chapter_url | Canonical chapter URL used for diagnostics. |
| [in] | dest_abs | Canonical destination directory. |
| [in] | dest_rel | Library-relative destination directory. |
| [in] | page_no | One-based page number. |
| [in] | idx | Zero-based index in the extracted page URL list. |
| [in,out] | rec | Chapter record receiving completed-page progress. |
| [in,out] | stats | Run counters receiving bytes, reuse, or failure. |
| [out] | out | Receives the bounded page outcome. |
| k_ra8_ok | The page was verified in place, reused, or published and checkpointed. |
| other | Path construction, transfer, validation, or checkpointing failed. |
idx is less than the extracted page count. ctx are exclusively owned. Definition at line 621 of file mdl_fetch_page.c.
References mdl_page_rec_t::etag, mdl_fetch_ctx_t::images, internal_mdl_fetch_do_fetch_page(), internal_mdl_fetch_mono_ms(), internal_mdl_fetch_page_leaf(), internal_mdl_fetch_try_reuse(), k_fetch_leaf_bytes, k_http_not_modified, k_mdl_relpath_max, k_ra8_fail, k_ra8_ok, mdl_page_rec_t::last_modified, mdl_hash_str(), mdl_state_find_page(), mdl_chapter_rec_t::pages_done, mdl_fetch_stats_t::pages_failed, mdl_fetch_stats_t::pages_fetched, mdl_fetch_stats_t::pages_reused, priv_mdl_fetch_checkpoint(), RA8_INTERNAL, mdl_fetch_ctx_t::refetch, mdl_fetch_ctx_t::state, mdl_net_resp_t::status, and mdl_url_list_t::urls.
Referenced by priv_mdl_fetch_chapter_pages().
|
static |
Governor host key for one page URL.
Definition at line 88 of file mdl_fetch_page.c.
References mdl_url_host(), and RA8_INTERNAL.
Referenced by internal_mdl_fetch_prepare_page().
|
static |
Compose the page_NNNN.ext leaf for one page; false if it overran.
Derives the bounded extension from url and renders the one-based page number into caller-owned storage without filesystem access.
| [in] | page_no | One-based page number. |
| [in] | url | Canonical page URL used to derive the extension. |
| [out] | out | Receives a NUL-terminated leaf on success. |
| [in] | cap | Capacity of out in bytes. |
| true | out contains the complete page leaf. |
| false | Formatting failed or cap was insufficient. |
url and out are non-NULL. cap is nonzero and describes writable storage at out. cap. Definition at line 200 of file mdl_fetch_page.c.
References mdl_urlname_ext().
Referenced by internal_mdl_fetch_one_page().
|
static |
Larger of two unsigned delay values.
Performs page max u32 under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in] | a | First unsigned delay operand. |
| [in] | b | Second unsigned delay operand. |
| other | The computed result in the function's declared domain. |
Definition at line 82 of file mdl_fetch_page.c.
References RA8_INTERNAL.
Referenced by internal_mdl_fetch_prepare_page().
|
static |
Prepare and execute the initial conditional page request.
Performs prepare page under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in,out] | ctx | Fetch dependencies and state. |
| [in] | chapter_url | Chapter URL used as the request Referer. |
| [in] | url | Absolute page URL. |
| [in] | target_abs | Final absolute page path. |
| [in] | target_rel | Final relative page path. |
| [out] | tx | Caller-owned transfer state. |
| k_ra8_ok | The operation completed. |
| other | Validation, capacity, network, or storage failed. |
tx is writable. tx. Definition at line 352 of file mdl_fetch_page.c.
References mdl_page_transfer_t::body, mdl_page_rec_t::etag, mdl_page_transfer_t::got, mdl_page_transfer_t::held, mdl_page_transfer_t::host, mdl_site_t::img_delay_max, mdl_site_t::img_delay_min, internal_mdl_fetch_page_host(), internal_mdl_fetch_page_max_u32(), mdl_page_transfer_t::jmax, mdl_page_transfer_t::jmin, k_ra8_fail, k_ra8_ok, mdl_page_rec_t::last_modified, mdl_hash_str(), mdl_session_url_allowed(), mdl_state_find_page(), nullptr, priv_mdl_fetch_body_abort(), priv_mdl_fetch_body_init_image(), priv_mdl_fetch_body_sink(), priv_mdl_fetch_record_fail(), priv_mdl_fetch_with_retry(), RA8_INTERNAL, mdl_page_transfer_t::recorded, mdl_fetch_ctx_t::refetch, mdl_page_transfer_t::req, mdl_page_transfer_t::resp, mdl_fetch_ctx_t::session, mdl_fetch_ctx_t::site, mdl_fetch_ctx_t::state, mdl_net_resp_t::status, mdl_fetch_ctx_t::storage, mdl_fetch_ctx_t::timeout_ms, and mdl_session_t::user_agent.
Referenced by internal_mdl_fetch_do_fetch_page().
|
static |
Validate and atomically publish a nonempty staged page.
Performs publish page under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in,out] | ctx | Fetch dependencies and persistent state. |
| [in] | url | Absolute page URL. |
| [in] | target_abs | URL-derived absolute destination. |
| [in,out] | tx | Completed non-304 transfer state. |
| [out] | out_bytes | Optional published byte count. |
| k_ra8_ok | The operation completed. |
| other | Validation, capacity, network, or storage failed. |
Definition at line 488 of file mdl_fetch_page.c.
References mdl_fetch_body_t::actual_abs, mdl_fetch_body_t::actual_rel, mdl_page_transfer_t::body, mdl_net_resp_t::etag, mdl_page_rec_t::etag, mdl_page_transfer_t::got, mdl_storage_txn_t::hash, mdl_page_transfer_t::held, internal_mdl_fetch_discard_stale_page(), k_http_status_max, k_http_status_min, k_mdl_max_page_recs, k_ra8_err_no_mem, k_ra8_err_protocol_error, k_ra8_fail, k_ra8_ok, mdl_net_resp_t::last_modified, mdl_page_rec_t::last_modified, mdl_hash_str(), mdl_state_add_page(), mdl_state_t::page_rec_count, priv_mdl_fetch_body_abort(), priv_mdl_fetch_body_commit(), priv_mdl_fetch_body_prepare(), priv_mdl_fetch_record_fail(), RA8_INTERNAL, mdl_page_transfer_t::recorded, mdl_page_transfer_t::resp, mdl_fetch_ctx_t::state, mdl_net_resp_t::status, mdl_fetch_ctx_t::storage, and mdl_fetch_body_t::writer.
Referenced by internal_mdl_fetch_do_fetch_page().
|
static |
Resolve HTTP 304 by verified reuse or one unconditional retry.
Performs resolve not modified under the injected network, governor, and storage contracts; dependency failures are propagated before incomplete bytes are published.
| [in,out] | ctx | Fetch dependencies and state. |
| [in] | url | Absolute page URL. |
| [in] | target_abs | URL-derived absolute destination. |
| [in] | target_rel | URL-derived relative destination. |
| [in,out] | tx | Current transfer state. |
| [out] | out_done | Whether verified reuse completed the page. |
| k_ra8_ok | The operation completed. |
| other | Validation, capacity, network, or storage failed. |
Definition at line 419 of file mdl_fetch_page.c.
References mdl_page_transfer_t::body, mdl_page_transfer_t::got, mdl_page_transfer_t::held, mdl_page_transfer_t::host, mdl_net_req_t::if_modified_since, mdl_net_req_t::if_none_match, internal_mdl_fetch_try_reuse(), mdl_page_transfer_t::jmax, mdl_page_transfer_t::jmin, k_http_not_modified, k_ra8_err_invalid_state, k_ra8_err_validation_failed, k_ra8_ok, mdl_hash_str(), mdl_state_note_page_response(), priv_mdl_fetch_body_abort(), priv_mdl_fetch_body_sink(), priv_mdl_fetch_record_fail(), priv_mdl_fetch_with_retry(), RA8_INTERNAL, mdl_page_transfer_t::req, mdl_page_transfer_t::resp, mdl_fetch_ctx_t::state, and mdl_net_resp_t::status.
Referenced by internal_mdl_fetch_do_fetch_page().
|
static |
Reuse an already-held byte-identical page instead of fetching it.
Content-hash dedup: if a page with the same source URL is recorded and its file still verifies, put those bytes at the target position (a no-op when it is already there, else a copy) and record the new location – no network. Returns false to fall through to a fetch.
| [in,out] | ctx | Borrowed operation context. |
| [in] | url_hash | Stable hash of the canonical source URL. |
| [in] | target_abs | Absolute destination path. |
| [in] | target_rel | Library-relative destination path. |
| true | The condition holds or the operation completed. |
| false | Input was rejected or the condition does not hold. |
Definition at line 228 of file mdl_fetch_page.c.
References mdl_page_rec_t::content_hash, mdl_page_rec_t::etag, mdl_page_rec_t::fetched_at, internal_mdl_fetch_copy_file(), k_mdl_relpath_max, k_ra8_ok, mdl_page_rec_t::last_modified, mdl_hash_file(), mdl_state_add_page(), mdl_state_find_page(), RA8_INTERNAL, mdl_page_rec_t::rel_path, mdl_page_rec_t::response_status, mdl_fetch_ctx_t::series_abs_dir, mdl_fetch_ctx_t::state, mdl_fetch_ctx_t::storage, strcmp(), and strrchr().
Referenced by internal_mdl_fetch_one_page(), and internal_mdl_fetch_resolve_not_modified().
| ra8_err_t priv_mdl_fetch_chapter_pages | ( | mdl_fetch_ctx_t * | ctx, |
| const char * | chapter_url, | ||
| const char * | dest_abs, | ||
| const char * | dest_rel, | ||
| size_t | base, | ||
| mdl_chapter_rec_t * | rec, | ||
| mdl_fetch_stats_t * | stats, | ||
| const mdl_run_pos_t * | pos ) |
Fetch every page of one chapter; fail (partial) on the first bad page.
Fetch and checkpoint every extracted page in one chapter.
Definition at line 680 of file mdl_fetch_page.c.
References mdl_url_list_t::count, mdl_fetch_ctx_t::images, internal_mdl_fetch_emit_progress(), internal_mdl_fetch_one_page(), k_ra8_ok, mdl_fetch_ctx_t::progress_error, and RA8_PRIV.
Referenced by internal_mdl_fetch_chapter_pages_and_checkpoint().