@mikrojs/native 0.21.1-next.20260928214050 → 0.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +21 -0
- package/dist/runtime/result/native-result.node-shim.d.ts +2 -0
- package/dist/runtime/result/native-result.node-shim.d.ts.map +1 -1
- package/dist/runtime/result/native-result.node-shim.js +57 -0
- package/dist/runtime/result/native-result.node-shim.js.map +1 -1
- package/dist/runtime/result/types.d.ts +33 -0
- package/dist/runtime/result/types.d.ts.map +1 -1
- package/include/mikrojs/app_store.h +16 -0
- package/include/mikrojs/ble_port.h +50 -0
- package/include/mikrojs/build_install.h +36 -0
- package/include/mikrojs/device_store.h +84 -0
- package/include/mikrojs/http_native.h +52 -0
- package/include/mikrojs/mikrojs.h +19 -9
- package/include/mikrojs/ota.h +75 -0
- package/include/mikrojs/ota_env.h +1 -1
- package/include/mikrojs/platform.h +35 -0
- package/include/mikrojs/private.h +13 -3
- package/include/mikrojs/utils.h +17 -7
- package/package.json +4 -4
- package/prebuilds/darwin-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-arm64/mikrojs.napi.node +0 -0
- package/prebuilds/linux-x64/mikrojs.napi.node +0 -0
- package/runtime/http/server-impl.ts +5 -60
- package/runtime/http/server.ts +0 -1
- package/runtime/internal.d.ts +2 -0
- package/runtime/modules.json +1 -1
- package/runtime/result/native-result.node-shim.ts +60 -0
- package/runtime/result/result.ts +1 -1
- package/runtime/result/types.ts +61 -0
- package/runtime/sys/sys.ts +1 -2
- package/runtime/sys/types.ts +5 -2
- package/src/mik_app_store.cpp +32 -0
- package/src/mik_ble.cpp +1573 -0
- package/src/mik_build_install.cpp +785 -0
- package/src/mik_config.cpp +330 -0
- package/src/mik_console.cpp +15 -24
- package/src/mik_deploy.cpp +759 -0
- package/src/mik_ota.cpp +1403 -0
- package/src/mik_ota_client_module.cpp +1142 -0
- package/src/mik_ota_env.cpp +318 -0
- package/src/mik_repl.cpp +15 -6
- package/src/mik_result.cpp +159 -0
- package/src/mik_sys.cpp +57 -52
- package/src/mik_test.cpp +1334 -0
- package/src/mik_test_supervisor.cpp +151 -0
- package/src/mik_udp.cpp +4 -1
- package/src/mik_watchdog.cpp +4 -5
- package/src/mik_wifi_client.cpp +1 -2
- package/src/mikrojs.cpp +15 -6
- package/src/modules.cpp +2 -1
- package/src/utils.cpp +1 -2
- package/runtime/test/test.ts +0 -686
|
@@ -0,0 +1,785 @@
|
|
|
1
|
+
// Build install: streaming gunzip -> untar of a build archive, and the swap
|
|
2
|
+
// that makes it the live app. Pure libc plus tinfl; the host test in
|
|
3
|
+
// firmware/components/mikrojs/test/ota_host/ compiles this file.
|
|
4
|
+
|
|
5
|
+
#include "mikrojs/build_install.h"
|
|
6
|
+
|
|
7
|
+
#include <cerrno>
|
|
8
|
+
#include <climits>
|
|
9
|
+
#include <cstdint>
|
|
10
|
+
#include <cstdio>
|
|
11
|
+
#include <cstdlib>
|
|
12
|
+
#include <cstring>
|
|
13
|
+
#include <dirent.h>
|
|
14
|
+
#include <sys/stat.h>
|
|
15
|
+
#include <unistd.h>
|
|
16
|
+
|
|
17
|
+
#include "mikrojs/app_store.h"
|
|
18
|
+
#include "mikrojs/platform.h"
|
|
19
|
+
|
|
20
|
+
// tinfl comes from miniz: the ESP32 mask ROM has it (the header comes with the
|
|
21
|
+
// always-available `esp_rom` dependency). A target without ROM miniz compiles
|
|
22
|
+
// it in.
|
|
23
|
+
#include "miniz.h"
|
|
24
|
+
|
|
25
|
+
namespace {
|
|
26
|
+
|
|
27
|
+
constexpr size_t kChunk = 4096;
|
|
28
|
+
constexpr size_t kTarBlock = 512;
|
|
29
|
+
constexpr size_t kTarName = 100; // ustar name field width
|
|
30
|
+
constexpr int kMaxNameDepth = 8; // path components a member may create
|
|
31
|
+
|
|
32
|
+
// The boot install and the deploy-time verify run on the main task with no
|
|
33
|
+
// MIK_Loop pass in between, so flash-bound loops feed the task watchdog here.
|
|
34
|
+
void feed_watchdog(void) {
|
|
35
|
+
const MIKPlatform* platform = MIK_GetPlatform();
|
|
36
|
+
if (platform && platform->feed_watchdog) platform->feed_watchdog();
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ── SHA-256 (FIPS 180-4) ─────────────────────────────────────────────────────
|
|
40
|
+
// Self-contained and host-validated against the standard vectors, so the build
|
|
41
|
+
// checksum is verified identically on host and device with no mbedTLS dependency
|
|
42
|
+
// to wire into the component. The offer checksum and `mikro app pack` both use
|
|
43
|
+
// SHA-256 over the whole .tgz.
|
|
44
|
+
struct Sha256 {
|
|
45
|
+
uint32_t h[8];
|
|
46
|
+
uint64_t len; // total bytes hashed
|
|
47
|
+
uint8_t buf[64];
|
|
48
|
+
size_t buflen;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
inline uint32_t sha256_ror(uint32_t v, int b) {
|
|
52
|
+
return (v >> b) | (v << (32 - b));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
void sha256_block(Sha256* c, const uint8_t* p) {
|
|
56
|
+
static const uint32_t K[64] = {
|
|
57
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4,
|
|
58
|
+
0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe,
|
|
59
|
+
0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
|
|
60
|
+
0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
|
61
|
+
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc,
|
|
62
|
+
0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b,
|
|
63
|
+
0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116,
|
|
64
|
+
0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
65
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7,
|
|
66
|
+
0xc67178f2};
|
|
67
|
+
uint32_t w[64];
|
|
68
|
+
for (int i = 0; i < 16; i++) {
|
|
69
|
+
w[i] = ((uint32_t)p[i * 4] << 24) | ((uint32_t)p[i * 4 + 1] << 16) |
|
|
70
|
+
((uint32_t)p[i * 4 + 2] << 8) | p[i * 4 + 3];
|
|
71
|
+
}
|
|
72
|
+
for (int i = 16; i < 64; i++) {
|
|
73
|
+
uint32_t s0 = sha256_ror(w[i - 15], 7) ^ sha256_ror(w[i - 15], 18) ^ (w[i - 15] >> 3);
|
|
74
|
+
uint32_t s1 = sha256_ror(w[i - 2], 17) ^ sha256_ror(w[i - 2], 19) ^ (w[i - 2] >> 10);
|
|
75
|
+
w[i] = w[i - 16] + s0 + w[i - 7] + s1;
|
|
76
|
+
}
|
|
77
|
+
uint32_t a = c->h[0], b = c->h[1], cc = c->h[2], d = c->h[3];
|
|
78
|
+
uint32_t e = c->h[4], f = c->h[5], g = c->h[6], hh = c->h[7];
|
|
79
|
+
for (int i = 0; i < 64; i++) {
|
|
80
|
+
uint32_t S1 = sha256_ror(e, 6) ^ sha256_ror(e, 11) ^ sha256_ror(e, 25);
|
|
81
|
+
uint32_t ch = (e & f) ^ ((~e) & g);
|
|
82
|
+
uint32_t t1 = hh + S1 + ch + K[i] + w[i];
|
|
83
|
+
uint32_t S0 = sha256_ror(a, 2) ^ sha256_ror(a, 13) ^ sha256_ror(a, 22);
|
|
84
|
+
uint32_t maj = (a & b) ^ (a & cc) ^ (b & cc);
|
|
85
|
+
uint32_t t2 = S0 + maj;
|
|
86
|
+
hh = g;
|
|
87
|
+
g = f;
|
|
88
|
+
f = e;
|
|
89
|
+
e = d + t1;
|
|
90
|
+
d = cc;
|
|
91
|
+
cc = b;
|
|
92
|
+
b = a;
|
|
93
|
+
a = t1 + t2;
|
|
94
|
+
}
|
|
95
|
+
c->h[0] += a;
|
|
96
|
+
c->h[1] += b;
|
|
97
|
+
c->h[2] += cc;
|
|
98
|
+
c->h[3] += d;
|
|
99
|
+
c->h[4] += e;
|
|
100
|
+
c->h[5] += f;
|
|
101
|
+
c->h[6] += g;
|
|
102
|
+
c->h[7] += hh;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
void sha256_init(Sha256* c) {
|
|
106
|
+
c->h[0] = 0x6a09e667;
|
|
107
|
+
c->h[1] = 0xbb67ae85;
|
|
108
|
+
c->h[2] = 0x3c6ef372;
|
|
109
|
+
c->h[3] = 0xa54ff53a;
|
|
110
|
+
c->h[4] = 0x510e527f;
|
|
111
|
+
c->h[5] = 0x9b05688c;
|
|
112
|
+
c->h[6] = 0x1f83d9ab;
|
|
113
|
+
c->h[7] = 0x5be0cd19;
|
|
114
|
+
c->len = 0;
|
|
115
|
+
c->buflen = 0;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
void sha256_update(Sha256* c, const uint8_t* p, size_t n) {
|
|
119
|
+
c->len += n;
|
|
120
|
+
while (n) {
|
|
121
|
+
size_t take = 64 - c->buflen;
|
|
122
|
+
if (take > n) take = n;
|
|
123
|
+
memcpy(c->buf + c->buflen, p, take);
|
|
124
|
+
c->buflen += take;
|
|
125
|
+
p += take;
|
|
126
|
+
n -= take;
|
|
127
|
+
if (c->buflen == 64) {
|
|
128
|
+
sha256_block(c, c->buf);
|
|
129
|
+
c->buflen = 0;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
void sha256_final(Sha256* c, uint8_t out[32]) {
|
|
135
|
+
uint64_t bits = c->len * 8; // captured before padding
|
|
136
|
+
uint8_t pad = 0x80;
|
|
137
|
+
sha256_update(c, &pad, 1);
|
|
138
|
+
uint8_t zero = 0;
|
|
139
|
+
while (c->buflen != 56) sha256_update(c, &zero, 1);
|
|
140
|
+
uint8_t lenb[8];
|
|
141
|
+
for (int i = 0; i < 8; i++) lenb[i] = (uint8_t)(bits >> (56 - 8 * i));
|
|
142
|
+
sha256_update(c, lenb, 8);
|
|
143
|
+
for (int i = 0; i < 8; i++) {
|
|
144
|
+
out[i * 4] = c->h[i] >> 24;
|
|
145
|
+
out[i * 4 + 1] = c->h[i] >> 16;
|
|
146
|
+
out[i * 4 + 2] = c->h[i] >> 8;
|
|
147
|
+
out[i * 4 + 3] = (uint8_t)c->h[i];
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// Hash a whole file into `out` (65 bytes: 64 lowercase hex + NUL). Hashing the
|
|
152
|
+
// staged file (not an in-memory running hash) is deliberate: a partial download
|
|
153
|
+
// can resume across a reboot, and the verify still works because it reads the
|
|
154
|
+
// bytes back off flash. Returns false on any I/O error.
|
|
155
|
+
bool sha256_file(const char* path, char out[65]) {
|
|
156
|
+
FILE* f = fopen(path, "rb");
|
|
157
|
+
if (!f) return false;
|
|
158
|
+
unsigned char* buf = (unsigned char*)malloc(kChunk);
|
|
159
|
+
if (!buf) {
|
|
160
|
+
fclose(f);
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
Sha256 c;
|
|
164
|
+
sha256_init(&c);
|
|
165
|
+
size_t n;
|
|
166
|
+
while ((n = fread(buf, 1, kChunk, f)) > 0) {
|
|
167
|
+
feed_watchdog();
|
|
168
|
+
sha256_update(&c, buf, n);
|
|
169
|
+
}
|
|
170
|
+
bool read_err = ferror(f) != 0;
|
|
171
|
+
free(buf);
|
|
172
|
+
fclose(f);
|
|
173
|
+
if (read_err) return false;
|
|
174
|
+
uint8_t d[32];
|
|
175
|
+
sha256_final(&c, d);
|
|
176
|
+
static const char* H = "0123456789abcdef";
|
|
177
|
+
for (int i = 0; i < 32; i++) {
|
|
178
|
+
out[i * 2] = H[d[i] >> 4];
|
|
179
|
+
out[i * 2 + 1] = H[d[i] & 0xf];
|
|
180
|
+
}
|
|
181
|
+
out[64] = 0;
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// ── CRC-32 (gzip/zlib polynomial) ────────────────────────────────────────────
|
|
186
|
+
// Bit-by-bit so it needs no lookup table; one-shot over the decompressed build
|
|
187
|
+
// at install time, so the loop cost is irrelevant. Self-contained rather than
|
|
188
|
+
// miniz's mz_crc32 so it can't be compiled out by ESP-IDF's miniz config.
|
|
189
|
+
uint32_t crc32_update(uint32_t crc, const unsigned char* p, size_t n) {
|
|
190
|
+
crc = crc ^ 0xFFFFFFFFu;
|
|
191
|
+
for (size_t i = 0; i < n; i++) {
|
|
192
|
+
crc ^= p[i];
|
|
193
|
+
for (int k = 0; k < 8; k++) crc = (crc >> 1) ^ (0xEDB88320u & (0u - (crc & 1u)));
|
|
194
|
+
}
|
|
195
|
+
return crc ^ 0xFFFFFFFFu;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Keep the last 8 bytes seen across a byte stream — the gzip trailer, captured
|
|
199
|
+
// as the file is read so it needs neither fseek (unreliable on some VFS) nor
|
|
200
|
+
// tinfl's end-of-stream input accounting (the esp32c6 ROM miniz mis-reports it).
|
|
201
|
+
void tail8_push(unsigned char tail[8], size_t* len, const unsigned char* p, size_t n) {
|
|
202
|
+
if (n >= 8) {
|
|
203
|
+
memcpy(tail, p + n - 8, 8);
|
|
204
|
+
*len = 8;
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (n == 0) return;
|
|
208
|
+
size_t keep = (*len + n > 8) ? (8 - n) : *len; // bytes of the old tail to retain
|
|
209
|
+
memmove(tail, tail + (*len - keep), keep);
|
|
210
|
+
memcpy(tail + keep, p, n);
|
|
211
|
+
*len = keep + n;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
long octal(const char* p, int len) {
|
|
215
|
+
long v = 0;
|
|
216
|
+
for (int i = 0; i < len && p[i] >= '0' && p[i] <= '7'; i++) v = (v << 3) + (p[i] - '0');
|
|
217
|
+
return v;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// Create every parent directory of `path` (the last component is the leaf and
|
|
221
|
+
// is left for the caller to create as a file or dir). EEXIST is fine. Makes the
|
|
222
|
+
// streaming untar robust whether or not the archive carries explicit dir
|
|
223
|
+
// entries. littlefs may ignore the mode argument; nothing here depends on it.
|
|
224
|
+
void mkdir_parents(const char* path) {
|
|
225
|
+
char tmp[512];
|
|
226
|
+
size_t n = strlen(path);
|
|
227
|
+
if (n >= sizeof(tmp)) return;
|
|
228
|
+
memcpy(tmp, path, n + 1);
|
|
229
|
+
for (size_t i = 1; i < n; i++) {
|
|
230
|
+
if (tmp[i] == '/') {
|
|
231
|
+
tmp[i] = 0;
|
|
232
|
+
mkdir(tmp, 0755);
|
|
233
|
+
tmp[i] = '/';
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// ── Streaming ustar extraction ───────────────────────────────────────────────
|
|
239
|
+
// Fed arbitrary byte chunks (the gunzip output), it reassembles 512-byte tar
|
|
240
|
+
// blocks across chunk boundaries and writes members to disk as they arrive, so
|
|
241
|
+
// no intermediate `.tar` ever lands on the app filesystem. Members are expected
|
|
242
|
+
// to be prefixed `app/...` (the build is `tar -C build app`), so dest_dir
|
|
243
|
+
// receives `app/...` and mik__app_commit renames dest_dir/app -> /app.
|
|
244
|
+
struct Untar {
|
|
245
|
+
char dest[256];
|
|
246
|
+
unsigned char block[kTarBlock];
|
|
247
|
+
size_t fill; // bytes accumulated toward the current 512-block
|
|
248
|
+
enum { kHeader, kFile, kSkip } mode;
|
|
249
|
+
FILE* cur;
|
|
250
|
+
long remaining; // file content bytes left to write
|
|
251
|
+
long skip_blocks; // 512-blocks left to discard (unknown entry types)
|
|
252
|
+
bool failed;
|
|
253
|
+
bool done; // saw the end-of-archive marker
|
|
254
|
+
const char* err;
|
|
255
|
+
MIKBuildErr kind; // corrupt for a malformed archive, transient for I/O
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
void untar_init(Untar* u, const char* dest) {
|
|
259
|
+
snprintf(u->dest, sizeof(u->dest), "%s", dest);
|
|
260
|
+
u->fill = 0;
|
|
261
|
+
u->mode = Untar::kHeader;
|
|
262
|
+
u->cur = nullptr;
|
|
263
|
+
u->remaining = 0;
|
|
264
|
+
u->skip_blocks = 0;
|
|
265
|
+
u->failed = false;
|
|
266
|
+
u->done = false;
|
|
267
|
+
u->err = nullptr;
|
|
268
|
+
u->kind = MIK_BUILD_ERR_CORRUPT;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
// Rejects member names that would escape dest_dir. The checksum is no defence
|
|
272
|
+
// here: it arrives in the same offer as the URL, so whoever serves the offer
|
|
273
|
+
// controls both. Without this, a member named `../../app/main.js` writes
|
|
274
|
+
// straight over the live app (or the rollback baseline) during boot reconcile,
|
|
275
|
+
// before any JS gets to run.
|
|
276
|
+
bool unsafe_member_name(const unsigned char* block) {
|
|
277
|
+
const char* n = (const char*)block;
|
|
278
|
+
if (n[0] == '/' || n[0] == 0) return true;
|
|
279
|
+
const size_t len = strnlen(n, kTarName);
|
|
280
|
+
// A name filling the field without a terminator is truncated by the %.100s
|
|
281
|
+
// below into a different path than the archive declared.
|
|
282
|
+
if (len == kTarName) return true;
|
|
283
|
+
// The ustar `prefix` field is not honored when building the path, so a
|
|
284
|
+
// member relying on it would silently extract somewhere else.
|
|
285
|
+
if (memcmp(block + 257, "ustar", 5) == 0 && block[345] != 0) return true;
|
|
286
|
+
// Depth is capped because every directory level created here is a level
|
|
287
|
+
// rmtree/rmdir_recursive later recurses over, and those run from boot
|
|
288
|
+
// recovery where a stack overflow costs the device permanently. `a/` twice
|
|
289
|
+
// per level fits ~49 levels in this field; real builds are 2-3 deep.
|
|
290
|
+
int depth = 0;
|
|
291
|
+
for (size_t i = 0; i < len; i++) {
|
|
292
|
+
if (n[i] == '/' && ++depth > kMaxNameDepth) return true;
|
|
293
|
+
}
|
|
294
|
+
for (size_t i = 0; i + 1 < len; i++) {
|
|
295
|
+
const bool at_start = (i == 0 || n[i - 1] == '/');
|
|
296
|
+
if (!at_start || n[i] != '.' || n[i + 1] != '.') continue;
|
|
297
|
+
if (i + 2 == len || n[i + 2] == '/') return true;
|
|
298
|
+
}
|
|
299
|
+
return false;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
void untar_block(Untar* u) {
|
|
303
|
+
if (u->mode == Untar::kHeader) {
|
|
304
|
+
if (u->block[0] == 0) { // zero block = end of archive
|
|
305
|
+
u->done = true;
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
if (unsafe_member_name(u->block)) {
|
|
309
|
+
u->failed = true;
|
|
310
|
+
u->err = "unsafe path in build archive";
|
|
311
|
+
u->kind = MIK_BUILD_ERR_CORRUPT;
|
|
312
|
+
return;
|
|
313
|
+
}
|
|
314
|
+
char name[256];
|
|
315
|
+
snprintf(name, sizeof(name), "%s/%.100s", u->dest, (const char*)u->block);
|
|
316
|
+
long size = octal((const char*)u->block + 124, 12);
|
|
317
|
+
char type = u->block[156];
|
|
318
|
+
// A negative size would make `want` below wrap to a huge size_t and read
|
|
319
|
+
// past the 512-byte block.
|
|
320
|
+
if (size < 0) {
|
|
321
|
+
u->failed = true;
|
|
322
|
+
u->err = "bad member size in build archive";
|
|
323
|
+
u->kind = MIK_BUILD_ERR_CORRUPT;
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
if (type == '5') { // directory
|
|
328
|
+
mkdir_parents(name);
|
|
329
|
+
mkdir(name, 0755);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
if (type == '0' || type == 0) { // regular file
|
|
333
|
+
mkdir_parents(name);
|
|
334
|
+
u->cur = fopen(name, "wb");
|
|
335
|
+
if (!u->cur) {
|
|
336
|
+
u->failed = true;
|
|
337
|
+
u->err = "create extracted file";
|
|
338
|
+
u->kind = MIK_BUILD_ERR_TRANSIENT;
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
u->remaining = size;
|
|
342
|
+
if (size == 0) { // no data blocks follow
|
|
343
|
+
if (fclose(u->cur) != 0) {
|
|
344
|
+
u->failed = true;
|
|
345
|
+
u->err = "close extracted file";
|
|
346
|
+
u->kind = MIK_BUILD_ERR_TRANSIENT;
|
|
347
|
+
}
|
|
348
|
+
u->cur = nullptr;
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
u->mode = Untar::kFile;
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
// Other entry types (symlink/longname/etc.) aren't produced by our
|
|
355
|
+
// builds — discard their data blocks.
|
|
356
|
+
u->skip_blocks = (size + kTarBlock - 1) / kTarBlock;
|
|
357
|
+
if (u->skip_blocks > 0) u->mode = Untar::kSkip;
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
if (u->mode == Untar::kFile) {
|
|
362
|
+
size_t want = u->remaining < (long)kTarBlock ? (size_t)u->remaining : kTarBlock;
|
|
363
|
+
if (fwrite(u->block, 1, want, u->cur) != want) {
|
|
364
|
+
fclose(u->cur);
|
|
365
|
+
u->cur = nullptr;
|
|
366
|
+
u->failed = true;
|
|
367
|
+
u->err = "write extracted file";
|
|
368
|
+
u->kind = MIK_BUILD_ERR_TRANSIENT;
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
u->remaining -= (long)want; // trailing bytes of this block are padding
|
|
372
|
+
if (u->remaining <= 0) {
|
|
373
|
+
// littlefs surfaces ENOSPC at close, not at write, so an unchecked
|
|
374
|
+
// fclose here would promote a silently truncated app file.
|
|
375
|
+
if (fclose(u->cur) != 0) {
|
|
376
|
+
u->failed = true;
|
|
377
|
+
u->err = "close extracted file";
|
|
378
|
+
u->kind = MIK_BUILD_ERR_TRANSIENT;
|
|
379
|
+
}
|
|
380
|
+
u->cur = nullptr;
|
|
381
|
+
u->mode = Untar::kHeader;
|
|
382
|
+
}
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// kSkip
|
|
387
|
+
if (--u->skip_blocks <= 0) u->mode = Untar::kHeader;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
void untar_feed(Untar* u, const unsigned char* data, size_t n) {
|
|
391
|
+
while (n > 0 && !u->failed && !u->done) {
|
|
392
|
+
size_t take = kTarBlock - u->fill;
|
|
393
|
+
if (take > n) take = n;
|
|
394
|
+
memcpy(u->block + u->fill, data, take);
|
|
395
|
+
u->fill += take;
|
|
396
|
+
data += take;
|
|
397
|
+
n -= take;
|
|
398
|
+
if (u->fill == kTarBlock) {
|
|
399
|
+
u->fill = 0;
|
|
400
|
+
untar_block(u);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
// Release the open member handle, if any. Split out from untar_finish because
|
|
406
|
+
// the failure path has to close it too, and must not overwrite the error that
|
|
407
|
+
// caused the failure with untar_finish's own.
|
|
408
|
+
void untar_close(Untar* u) {
|
|
409
|
+
if (u->cur) {
|
|
410
|
+
fclose(u->cur);
|
|
411
|
+
u->cur = nullptr;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
bool untar_finish(Untar* u, const char** err) {
|
|
416
|
+
if (u->cur) {
|
|
417
|
+
// Still open means the archive ended mid-member: the app file on disk is
|
|
418
|
+
// short. Reporting success here would promote a truncated app.
|
|
419
|
+
if (fclose(u->cur) != 0 && !u->failed) {
|
|
420
|
+
u->err = "close extracted file";
|
|
421
|
+
u->kind = MIK_BUILD_ERR_TRANSIENT;
|
|
422
|
+
} else if (!u->failed) {
|
|
423
|
+
u->err = "build archive ended mid-file (truncated)";
|
|
424
|
+
u->kind = MIK_BUILD_ERR_CORRUPT;
|
|
425
|
+
}
|
|
426
|
+
u->failed = true;
|
|
427
|
+
u->cur = nullptr;
|
|
428
|
+
}
|
|
429
|
+
if (u->failed) {
|
|
430
|
+
*err = u->err;
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// ── Streaming gunzip -> untar ────────────────────────────────────────────────
|
|
437
|
+
// Inflate in_path with miniz's low-level tinfl and feed the decompressed bytes
|
|
438
|
+
// straight into the untar state machine, so the .tar never hits disk. The gzip
|
|
439
|
+
// trailer (CRC-32 + ISIZE) is still verified — a corrupt or truncated download
|
|
440
|
+
// decompresses to garbage that won't match, so it is rejected before any swap.
|
|
441
|
+
bool unpack_tgz(const char* in_path, const char* dest_dir, const char** err, MIKBuildErr* kind) {
|
|
442
|
+
*kind = MIK_BUILD_ERR_TRANSIENT;
|
|
443
|
+
FILE* in = fopen(in_path, "rb");
|
|
444
|
+
if (!in) {
|
|
445
|
+
*err = "open .tgz";
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// Parse the gzip header, honoring the optional fields the FLG byte advertises
|
|
450
|
+
// (FEXTRA/FNAME/FCOMMENT/FHCRC) instead of assuming a fixed 10-byte header.
|
|
451
|
+
// Magic(2) CM(1) FLG(1) MTIME(4) XFL(1) OS(1) = 10 fixed bytes, then extras.
|
|
452
|
+
int b0 = fgetc(in), b1 = fgetc(in), cm = fgetc(in), flg = fgetc(in);
|
|
453
|
+
if (b0 != 0x1f || b1 != 0x8b || cm == EOF || flg == EOF) {
|
|
454
|
+
fclose(in);
|
|
455
|
+
*err = "not a gzip stream";
|
|
456
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
457
|
+
return false;
|
|
458
|
+
}
|
|
459
|
+
for (int i = 0; i < 6; i++) fgetc(in); // MTIME + XFL + OS
|
|
460
|
+
bool hdr_ok = true;
|
|
461
|
+
if (flg & 0x04) { // FEXTRA: 2-byte LE length, then that many bytes
|
|
462
|
+
int xl = fgetc(in), xh = fgetc(in);
|
|
463
|
+
if (xl == EOF || xh == EOF) {
|
|
464
|
+
hdr_ok = false;
|
|
465
|
+
} else {
|
|
466
|
+
for (int i = 0, xlen = xl | (xh << 8); i < xlen && hdr_ok; i++) {
|
|
467
|
+
if (fgetc(in) == EOF) hdr_ok = false;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
if (hdr_ok && (flg & 0x08)) { // FNAME
|
|
472
|
+
int c;
|
|
473
|
+
do {
|
|
474
|
+
c = fgetc(in);
|
|
475
|
+
} while (c != 0 && c != EOF);
|
|
476
|
+
if (c == EOF) hdr_ok = false;
|
|
477
|
+
}
|
|
478
|
+
if (hdr_ok && (flg & 0x10)) { // FCOMMENT
|
|
479
|
+
int c;
|
|
480
|
+
do {
|
|
481
|
+
c = fgetc(in);
|
|
482
|
+
} while (c != 0 && c != EOF);
|
|
483
|
+
if (c == EOF) hdr_ok = false;
|
|
484
|
+
}
|
|
485
|
+
if (hdr_ok && (flg & 0x02)) { // FHCRC
|
|
486
|
+
if (fgetc(in) == EOF || fgetc(in) == EOF) hdr_ok = false;
|
|
487
|
+
}
|
|
488
|
+
if (!hdr_ok) {
|
|
489
|
+
fclose(in);
|
|
490
|
+
*err = "bad gzip header";
|
|
491
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
// Everything here goes on the HEAP, not the stack: this runs deep inside the
|
|
496
|
+
// QuickJS / boot call chain where a multi-KB stack array would overflow.
|
|
497
|
+
// That includes the decompressor itself -- tinfl_decompressor is ~11 KiB
|
|
498
|
+
// against the ESP-IDF ROM miniz (three 3.5 KiB huffman tables), which alone
|
|
499
|
+
// is half the main task stack. TINFL_LZ_DICT_SIZE (32 KiB) is the sliding
|
|
500
|
+
// window tinfl requires.
|
|
501
|
+
tinfl_decompressor* inflator = (tinfl_decompressor*)malloc(sizeof(tinfl_decompressor));
|
|
502
|
+
unsigned char* window = (unsigned char*)malloc(TINFL_LZ_DICT_SIZE);
|
|
503
|
+
unsigned char* inbuf = (unsigned char*)malloc(kChunk);
|
|
504
|
+
if (!inflator || !window || !inbuf) {
|
|
505
|
+
free(inflator);
|
|
506
|
+
free(window);
|
|
507
|
+
free(inbuf);
|
|
508
|
+
fclose(in);
|
|
509
|
+
*err = "oom (inflate buffers)";
|
|
510
|
+
*kind = MIK_BUILD_ERR_OOM;
|
|
511
|
+
return false;
|
|
512
|
+
}
|
|
513
|
+
tinfl_init(inflator);
|
|
514
|
+
|
|
515
|
+
Untar untar;
|
|
516
|
+
untar_init(&untar, dest_dir);
|
|
517
|
+
|
|
518
|
+
// Single streaming loop (validated on host against real `tar -czf` builds).
|
|
519
|
+
// NOTE: do NOT gate the loop on TINFL_FLAG_HAS_MORE_INPUT — once input drains
|
|
520
|
+
// while that flag is set, tinfl returns NEEDS_MORE_INPUT with zero progress
|
|
521
|
+
// and the loop spins forever. Drive it off `avail_in`/`eof` only.
|
|
522
|
+
size_t window_pos = 0;
|
|
523
|
+
const unsigned char* next_in = inbuf;
|
|
524
|
+
size_t avail_in = 0;
|
|
525
|
+
bool eof = false;
|
|
526
|
+
bool ok = true;
|
|
527
|
+
uint32_t crc = 0; // running CRC-32 of the decompressed output
|
|
528
|
+
uint32_t total_out = 0; // uncompressed byte count (mod 2^32 == gzip ISIZE)
|
|
529
|
+
unsigned char tail[8]; // rolling last-8-bytes of the file == the gzip trailer
|
|
530
|
+
size_t tail_len = 0;
|
|
531
|
+
for (;;) {
|
|
532
|
+
feed_watchdog();
|
|
533
|
+
if (avail_in == 0 && !eof) {
|
|
534
|
+
size_t in_n = fread(inbuf, 1, kChunk, in);
|
|
535
|
+
// A read error also returns 0. Distinguish it from real EOF: treating
|
|
536
|
+
// an I/O glitch as a short stream classifies it corrupt below, which
|
|
537
|
+
// makes the policy blacklist a perfectly good build forever.
|
|
538
|
+
if (in_n == 0 && ferror(in)) {
|
|
539
|
+
*err = "read staged build (i/o error)";
|
|
540
|
+
*kind = MIK_BUILD_ERR_TRANSIENT;
|
|
541
|
+
ok = false;
|
|
542
|
+
break;
|
|
543
|
+
}
|
|
544
|
+
next_in = inbuf;
|
|
545
|
+
avail_in = in_n;
|
|
546
|
+
tail8_push(tail, &tail_len, inbuf, in_n); // remember the file's last 8 bytes
|
|
547
|
+
if (in_n == 0) eof = true;
|
|
548
|
+
}
|
|
549
|
+
size_t in_used = avail_in;
|
|
550
|
+
size_t out_used = TINFL_LZ_DICT_SIZE - window_pos;
|
|
551
|
+
int flags = eof ? 0 : TINFL_FLAG_HAS_MORE_INPUT;
|
|
552
|
+
tinfl_status st = tinfl_decompress(inflator, next_in, &in_used, window,
|
|
553
|
+
window + window_pos, &out_used, flags);
|
|
554
|
+
next_in += in_used;
|
|
555
|
+
avail_in -= in_used;
|
|
556
|
+
if (out_used) {
|
|
557
|
+
untar_feed(&untar, window + window_pos, out_used);
|
|
558
|
+
if (untar.failed) {
|
|
559
|
+
*err = untar.err;
|
|
560
|
+
*kind = untar.kind;
|
|
561
|
+
ok = false;
|
|
562
|
+
break;
|
|
563
|
+
}
|
|
564
|
+
crc = crc32_update(crc, window + window_pos, out_used);
|
|
565
|
+
total_out += (uint32_t)out_used;
|
|
566
|
+
}
|
|
567
|
+
window_pos = (window_pos + out_used) & (TINFL_LZ_DICT_SIZE - 1);
|
|
568
|
+
if (st == TINFL_STATUS_DONE) break;
|
|
569
|
+
if (st < TINFL_STATUS_DONE) {
|
|
570
|
+
*err = "inflate error";
|
|
571
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
572
|
+
ok = false;
|
|
573
|
+
break;
|
|
574
|
+
}
|
|
575
|
+
if (st == TINFL_STATUS_NEEDS_MORE_INPUT && eof) {
|
|
576
|
+
*err = "truncated gzip";
|
|
577
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
578
|
+
ok = false;
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
// tinfl stops at the end of the DEFLATE stream, so the 8-byte trailer may be
|
|
584
|
+
// sitting unread in the file. Drain whatever's left through the same rolling
|
|
585
|
+
// tail so `tail` holds the file's true final 8 bytes.
|
|
586
|
+
if (ok) {
|
|
587
|
+
unsigned char drain[64];
|
|
588
|
+
size_t dn;
|
|
589
|
+
while ((dn = fread(drain, 1, sizeof(drain), in)) > 0) {
|
|
590
|
+
tail8_push(tail, &tail_len, drain, dn);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
// Verify the gzip trailer from `tail` (the file's last 8 bytes) rather than
|
|
595
|
+
// from tinfl's leftover input: the esp32c6 ROM tinfl mis-accounts its input
|
|
596
|
+
// consumption at end-of-stream (it draws the trailer into its bit-buffer
|
|
597
|
+
// lookahead and reports it consumed), so the post-DONE buffer is unreliable
|
|
598
|
+
// and a valid build would wrongly read as "missing trailer". `crc`/
|
|
599
|
+
// `total_out` come from the decoded output, so they're independent of that.
|
|
600
|
+
if (ok) {
|
|
601
|
+
if (tail_len < 8) {
|
|
602
|
+
*err = "gzip: missing trailer (truncated)";
|
|
603
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
604
|
+
ok = false;
|
|
605
|
+
} else {
|
|
606
|
+
uint32_t want_crc = (uint32_t)tail[0] | ((uint32_t)tail[1] << 8) |
|
|
607
|
+
((uint32_t)tail[2] << 16) | ((uint32_t)tail[3] << 24);
|
|
608
|
+
uint32_t want_size = (uint32_t)tail[4] | ((uint32_t)tail[5] << 8) |
|
|
609
|
+
((uint32_t)tail[6] << 16) | ((uint32_t)tail[7] << 24);
|
|
610
|
+
if (crc != want_crc) {
|
|
611
|
+
*err = "gzip: crc mismatch (corrupt build)";
|
|
612
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
613
|
+
ok = false;
|
|
614
|
+
} else if (total_out != want_size) {
|
|
615
|
+
*err = "gzip: size mismatch (corrupt build)";
|
|
616
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
617
|
+
ok = false;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
if (ok && !untar_finish(&untar, err)) {
|
|
623
|
+
*kind = untar.kind;
|
|
624
|
+
ok = false;
|
|
625
|
+
}
|
|
626
|
+
// untar_finish is skipped on the failure path above, and it is the only
|
|
627
|
+
// other owner of `cur`. Without this an aborted unpack (inflate error, crc
|
|
628
|
+
// mismatch, truncation) strands an open littlefs handle -- on a tree
|
|
629
|
+
// install_build is about to rmtree -- once per attempt, and nothing ever
|
|
630
|
+
// reclaims it. No-op when untar_finish already closed it.
|
|
631
|
+
untar_close(&untar);
|
|
632
|
+
|
|
633
|
+
free(inflator);
|
|
634
|
+
free(window);
|
|
635
|
+
free(inbuf);
|
|
636
|
+
fclose(in);
|
|
637
|
+
return ok;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
/* `stat` succeeds for any node, so a guard that means "a directory is here"
|
|
641
|
+
* has to say so: an archive carrying a regular file named `app` would
|
|
642
|
+
* otherwise be accepted as an unpacked build. */
|
|
643
|
+
bool dir_exists(const char* path) {
|
|
644
|
+
struct stat st;
|
|
645
|
+
return stat(path, &st) == 0 && S_ISDIR(st.st_mode);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
// Uncompressed size from the gzip ISIZE trailer (last 4 bytes), or -1 when it
|
|
649
|
+
// can't be read. Only a hint for the free-space precheck: it is attacker-
|
|
650
|
+
// influenced and mod 2^32, so nothing may trust it -- the real check is the
|
|
651
|
+
// crc/size verification against the decoded output in unpack_tgz.
|
|
652
|
+
long gzip_isize(const char* path) {
|
|
653
|
+
FILE* f = fopen(path, "rb");
|
|
654
|
+
if (!f) return -1;
|
|
655
|
+
unsigned char t[4];
|
|
656
|
+
long got = -1;
|
|
657
|
+
if (fseek(f, -4, SEEK_END) == 0 && fread(t, 1, 4, f) == 4) {
|
|
658
|
+
uint32_t v = (uint32_t)t[0] | ((uint32_t)t[1] << 8) | ((uint32_t)t[2] << 16) |
|
|
659
|
+
((uint32_t)t[3] << 24);
|
|
660
|
+
if (v <= LONG_MAX) got = (long)v;
|
|
661
|
+
}
|
|
662
|
+
fclose(f);
|
|
663
|
+
return got;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// Deepest tree rmtree will descend into. kMaxNameDepth caps what untar will
|
|
667
|
+
// create, so this is only reachable through state an older firmware left
|
|
668
|
+
// behind. The bound matters because rmtree runs from boot reconcile: a stack
|
|
669
|
+
// overflow there reboots into the same call and never reaches the install
|
|
670
|
+
// budget, so the device would never recover. Past the bound the tree stays on
|
|
671
|
+
// disk and fails the next install instead.
|
|
672
|
+
constexpr int kMaxRmDepth = 32;
|
|
673
|
+
|
|
674
|
+
// `path` is a mutable buffer of `cap` bytes; each level appends into it and
|
|
675
|
+
// truncates on the way out, so a frame costs a few dozen bytes rather than a
|
|
676
|
+
// 512-byte path of its own.
|
|
677
|
+
void rmtree_at(char* path, size_t cap, int depth) {
|
|
678
|
+
DIR* d = opendir(path);
|
|
679
|
+
if (!d) {
|
|
680
|
+
unlink(path);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
const size_t base = strlen(path);
|
|
684
|
+
dirent* ent;
|
|
685
|
+
while ((ent = readdir(d)) != nullptr) {
|
|
686
|
+
if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) continue;
|
|
687
|
+
feed_watchdog();
|
|
688
|
+
const int n = snprintf(path + base, cap - base, "/%s", ent->d_name);
|
|
689
|
+
if (n < 0 || (size_t)n >= cap - base) {
|
|
690
|
+
path[base] = 0; // would truncate to a different path; skip it
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
struct stat st;
|
|
694
|
+
if (!stat(path, &st) && S_ISDIR(st.st_mode)) {
|
|
695
|
+
if (depth < kMaxRmDepth) rmtree_at(path, cap, depth + 1);
|
|
696
|
+
} else {
|
|
697
|
+
unlink(path);
|
|
698
|
+
}
|
|
699
|
+
path[base] = 0;
|
|
700
|
+
}
|
|
701
|
+
closedir(d);
|
|
702
|
+
rmdir(path);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
void rmtree(const char* path) {
|
|
706
|
+
char buf[512];
|
|
707
|
+
const size_t n = strlen(path);
|
|
708
|
+
if (n >= sizeof(buf)) return;
|
|
709
|
+
memcpy(buf, path, n + 1);
|
|
710
|
+
rmtree_at(buf, sizeof(buf), 0);
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
} // namespace
|
|
714
|
+
|
|
715
|
+
bool mik__sha256_file(const char* path, char out[65]) {
|
|
716
|
+
return sha256_file(path, out);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
bool mik__unpack_tgz(const char* tgz, const char* dest_dir, const char** err, MIKBuildErr* kind) {
|
|
720
|
+
return unpack_tgz(tgz, dest_dir, err, kind);
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
// ── install (gunzip -> untar -> atomic swap) ─────────────────────────────────
|
|
724
|
+
// Unpacks `tgz` into <base>/.deploy-tmp/app and promotes it to /app via the
|
|
725
|
+
// shared app-store engine (crash-safe: an interrupted swap is rolled back by
|
|
726
|
+
// MIK_DeployRecover at the next boot). Reusing the deploy staging dir means
|
|
727
|
+
// MIK_DeployRecover also cleans up an unpack interrupted by a brownout.
|
|
728
|
+
bool mik__install_build(const char* fs_base, const char* tgz, const char** err,
|
|
729
|
+
MIKBuildErr* kind) {
|
|
730
|
+
char deploy_tmp[256];
|
|
731
|
+
char deploy_old[256];
|
|
732
|
+
// A truncated path would name another directory for rmtree to delete.
|
|
733
|
+
if (strlen(fs_base) + sizeof("/.deploy-tmp") > sizeof(deploy_tmp)) {
|
|
734
|
+
*err = "app filesystem path too long";
|
|
735
|
+
*kind = MIK_BUILD_ERR_TRANSIENT;
|
|
736
|
+
return false;
|
|
737
|
+
}
|
|
738
|
+
snprintf(deploy_tmp, sizeof(deploy_tmp), "%s/.deploy-tmp", fs_base);
|
|
739
|
+
snprintf(deploy_old, sizeof(deploy_old), "%s/.deploy-old", fs_base);
|
|
740
|
+
rmtree(deploy_tmp);
|
|
741
|
+
rmtree(deploy_old);
|
|
742
|
+
// Reject an oversized build up front instead of failing partway through the
|
|
743
|
+
// unpack and burning an install attempt. Prefer the gzip ISIZE trailer (the
|
|
744
|
+
// unpacked tar size) over the compressed size, which understates the real
|
|
745
|
+
// requirement several-fold and lets through builds that cannot fit.
|
|
746
|
+
long need = gzip_isize(tgz);
|
|
747
|
+
if (need < 0) need = mik__file_size(tgz);
|
|
748
|
+
long total = 0;
|
|
749
|
+
long free_bytes = 0;
|
|
750
|
+
if (need > 0 && mik__fs_space(&total, &free_bytes) && free_bytes < need) {
|
|
751
|
+
*err = "not enough free space to install build";
|
|
752
|
+
*kind = MIK_BUILD_ERR_TRANSIENT;
|
|
753
|
+
return false;
|
|
754
|
+
}
|
|
755
|
+
if (mkdir(deploy_tmp, 0755) != 0 && errno != EEXIST) {
|
|
756
|
+
*err = "mkdir stage";
|
|
757
|
+
*kind = MIK_BUILD_ERR_TRANSIENT;
|
|
758
|
+
return false;
|
|
759
|
+
}
|
|
760
|
+
if (!unpack_tgz(tgz, deploy_tmp, err, kind)) {
|
|
761
|
+
rmtree(deploy_tmp);
|
|
762
|
+
return false;
|
|
763
|
+
}
|
|
764
|
+
// mik__app_commit reports OK when nothing was staged -- correct for the
|
|
765
|
+
// serial deploy path, but here it would turn an archive with no `app/`
|
|
766
|
+
// member into a silent no-op that still promotes and records the checksum
|
|
767
|
+
// as installed, so the device never retries. A build that unpacks to
|
|
768
|
+
// nothing is a bad build.
|
|
769
|
+
char staged_app[512];
|
|
770
|
+
snprintf(staged_app, sizeof(staged_app), "%s/app", deploy_tmp);
|
|
771
|
+
if (!dir_exists(staged_app)) {
|
|
772
|
+
*err = "build archive contains no app/ directory";
|
|
773
|
+
*kind = MIK_BUILD_ERR_CORRUPT;
|
|
774
|
+
rmtree(deploy_tmp);
|
|
775
|
+
return false;
|
|
776
|
+
}
|
|
777
|
+
MIKAppCommitResult r = mik__app_commit(fs_base, false);
|
|
778
|
+
if (r != MIK_APP_COMMIT_OK) {
|
|
779
|
+
*err = r == MIK_APP_COMMIT_STASH_FAILED ? "stash old app failed" : "swap new app failed";
|
|
780
|
+
*kind = MIK_BUILD_ERR_TRANSIENT;
|
|
781
|
+
rmtree(deploy_tmp);
|
|
782
|
+
return false;
|
|
783
|
+
}
|
|
784
|
+
return true;
|
|
785
|
+
}
|