ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
mdl_app_storage.c
Go to the documentation of this file.
1
9#include <stdint.h>
10#include <stdio.h>
11#include <string.h>
12
14
16static const char s_site_descriptor_format[] =
17 "# Copyright (c) 2026 Brighton Sikarskie\n"
18 "# SPDX-License-Identifier: MIT\n"
19 "#\n"
20 "# Site descriptor for %s (%s).\n"
21 "# Generated by mdl --init-site\n"
22 "\n"
23 "name = %s\n"
24 "host = %s\n"
25 "kind = manga\n"
26 "\n"
27 "# --- chapter list (on a series page) ---\n"
28 "chapter_url_contains = /chapter-\n"
29 "# chapter_url_prefix = https://%s/path/to/series/chapters/\n"
30 "chapter_order = asc\n"
31 "\n"
32 "# --- search / discovery ---\n"
33 "search_url = https://%s/?s={q}\n"
34 "search_result_contains = /manga/\n"
35 "browse_url = https://%s/\n"
36 "\n"
37 "# --- page images (on a chapter page) ---\n"
38 "page_img_attr = data-src\n"
39 "page_img_url_contains = /uploads/\n"
40 "\n"
41 "# --- politeness (milliseconds; jittered in [min,max]) ---\n"
42 "img_delay_min = 500\n"
43 "img_delay_max = 1200\n"
44 "chapter_delay_min = 1500\n"
45 "chapter_delay_max = 3000\n";
46
48{
49 if ((storage == nullptr) || (storage->fs == nullptr) || (path == nullptr) || (path[0] != '/')) {
51 }
52 fw_fs_stat_t node = {};
53 ra8_err_t error = fw_fs_stat(&storage->fs->names, path, &node);
54 if (error != k_ra8_ok) {
55 return error;
56 }
57 if (!node.exists) {
58 error = fw_fs_mkdir(&storage->fs->names, path);
59 if ((error != k_ra8_ok) && (error != k_ra8_err_exists)) {
60 return error;
61 }
62 node = (fw_fs_stat_t){};
63 error = fw_fs_stat(&storage->fs->names, path, &node);
64 if (error != k_ra8_ok) {
65 return error;
66 }
67 }
68 if (!node.exists) {
70 }
71 if (node.type == k_fw_fs_node_symlink) {
73 }
75}
76
78 const char* path,
79 bool* out_removed)
80{
81 if ((storage == nullptr) || (storage->fs == nullptr) || (path == nullptr) || (path[0] != '/') ||
82 (out_removed == nullptr)) {
84 }
85 *out_removed = false;
86 fw_fs_stat_t node = {};
87 const ra8_err_t st = fw_fs_stat(&storage->fs->names, path, &node);
88 if (st != k_ra8_ok) {
89 return st;
90 }
91 if (!node.exists) {
92 return k_ra8_ok;
93 }
94 if (node.type == k_fw_fs_node_symlink) {
96 }
97 if (node.type != k_fw_fs_node_file) {
99 }
100 const ra8_err_t removed = fw_fs_unlink(&storage->fs->names, path);
101 if (removed == k_ra8_ok) {
102 *out_removed = true;
103 }
104 return removed;
105}
106
108 const char* destination,
109 const char* url,
110 const char* host,
111 const char* name)
112{
113 if ((storage == nullptr) || (storage->fs == nullptr) || (storage->io_buffer == nullptr) ||
114 (storage->io_buffer_bytes == 0U) || (destination == nullptr) || (url == nullptr) ||
115 (host == nullptr) || (name == nullptr)) {
117 }
118 const char* hptr = (strncmp(host, "www.", 4U) == 0) ? host + 4 : host;
119 const int rendered = snprintf((char*)storage->io_buffer,
120 (size_t)storage->io_buffer_bytes,
122 hptr,
123 url,
124 name,
125 host,
126 host,
127 host,
128 host);
129 if ((rendered < 0) || ((uint64_t)rendered >= (uint64_t)storage->io_buffer_bytes)) {
131 }
132 mdl_storage_txn_t writer = {};
133 ra8_err_t error = mdl_storage_txn_begin_new(&writer, storage, destination);
134 if (error == k_ra8_ok) {
135 error = mdl_storage_txn_write(&writer, storage->io_buffer, (uint32_t)rendered);
136 }
137 if (error == k_ra8_ok) {
138 error = mdl_storage_txn_commit(&writer);
139 }
140 if ((error != k_ra8_ok) && writer.transaction.active) {
141 const ra8_err_t aborted = mdl_storage_txn_abort(&writer);
142 if (aborted != k_ra8_ok) {
143 return aborted;
144 }
145 }
146 return error;
147}
ra8_err_t fw_fs_stat(const fw_fs_namespace_t *names, const char *path, fw_fs_stat_t *out)
Query a path; a miss is success with out->exists == false.
Definition fw_if_fs.c:480
ra8_err_t fw_fs_unlink(const fw_fs_namespace_t *names, const char *path)
Remove one regular file; directories require fw_fs_rmdir.
Definition fw_if_fs.c:575
ra8_err_t fw_fs_mkdir(const fw_fs_namespace_t *names, const char *path)
Create exactly one directory; parents must already exist.
Definition fw_if_fs.c:566
@ k_fw_fs_node_directory
Directory.
@ k_fw_fs_node_file
Regular byte stream.
@ k_fw_fs_node_symlink
Symbolic link, if observable.
static const char s_site_descriptor_format[]
Complete starter descriptor template written by init-site mode.
ra8_err_t priv_mdl_app_storage_unlink_regular(mdl_storage_t *storage, const char *path, bool *out_removed)
Remove one regular file while refusing symbolic or special nodes.
ra8_err_t priv_mdl_app_storage_ensure_directory(mdl_storage_t *storage, const char *path)
Ensure one canonical portable path names a real directory.
ra8_err_t priv_mdl_app_storage_publish_site(mdl_storage_t *storage, const char *destination, const char *url, const char *host, const char *name)
Render and create one starter site descriptor transactionally.
Private portable filesystem operations shared by CLI application modes.
ra8_err_t mdl_storage_txn_write(mdl_storage_txn_t *writer, const uint8_t *bytes, uint32_t length)
Append one complete caller chunk, tolerating bounded short writes.
ra8_err_t mdl_storage_txn_begin_new(mdl_storage_txn_t *writer, mdl_storage_t *storage, const char *destination)
Begin one streamed create-new publication without replacement.
ra8_err_t mdl_storage_txn_abort(mdl_storage_txn_t *writer)
Abort and clear one streamed transaction.
ra8_err_t mdl_storage_txn_commit(mdl_storage_txn_t *writer)
Independently validate and publish a completed streamed transaction.
#define RA8_PRIV
Module-private helper: shared across TUs but only inside one library.
@ k_ra8_err_invalid_arg
Invalid function argument.
Definition ra8_err.h:152
@ k_ra8_err_exists
Item already exists – cannot create again.
Definition ra8_err.h:216
@ k_ra8_err_invalid_state
Module in wrong state for requested operation.
Definition ra8_err.h:161
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
@ k_ra8_err_access_denied
Operation refused because the target is protected against it.
Definition ra8_err.h:276
@ k_ra8_err_invalid_size
Invalid size parameter (too large, too small, or misaligned).
Definition ra8_err.h:167
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
int strncmp(const char *s1, const char *s2, size_t n)
Compare two strings up to a specified length.
Result of a portable metadata query.
bool exists
False means a clean lookup miss.
fw_fs_node_type_t type
Kind of node at the path.
fw_fs_namespace_t names
Namespace operations.
bool active
Begin succeeded.
One non-reentrant downloader filesystem dependency bundle.
Definition mdl_storage.h:40
uint8_t * io_buffer
Caller-owned stream scratch.
Definition mdl_storage.h:44
const fw_fs_t * fs
Injected portable filesystem.
Definition mdl_storage.h:41
uint32_t io_buffer_bytes
Stream scratch extent.
Definition mdl_storage.h:47
Caller-owned streaming publication transaction with running identity.
Definition mdl_storage.h:58
fw_fs_transaction_t transaction
Active private filesystem stage.
Definition mdl_storage.h:60