@native-sdk/cli 0.9.0 → 0.9.2
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/build/app.zig +357 -33
- package/build.zig +234 -11
- package/native-sdk.d.ts +8 -0
- package/package.json +10 -10
- package/packages/core/compile-surface/core.ts +70 -1
- package/packages/core/package-lock.json +11 -11
- package/packages/core/package.json +2 -2
- package/packages/core/scripts/merge_static_archives.mjs +78 -0
- package/packages/core/scripts/run_external_core_compiler.mjs +52 -7
- package/packages/core/scripts/run_external_service_compiler.mjs +53 -13
- package/packages/core/scripts/stage_external_services.mjs +1 -1
- package/packages/core/sdk/core.d.ts +75 -0
- package/packages/core/sdk/core.ts +170 -13
- package/packages/core/sdk/events.d.ts +53 -0
- package/packages/core/sdk/events.ts +78 -7
- package/packages/core/src/checker.ts +470 -2
- package/packages/core/src/cli.ts +13 -0
- package/packages/core/src/contract.ts +3 -1
- package/packages/core/src/devhost.ts +30 -1
- package/packages/core/src/devhost_journal.mjs +10 -8
- package/packages/core/src/diagnostics.ts +99 -8
- package/packages/core/src/frontend.ts +6 -0
- package/packages/core/src/infer.ts +24 -0
- package/skill-data/automation/SKILL.md +1 -1
- package/skill-data/native-ui/SKILL.md +18 -12
- package/skill-data/ts-core/SKILL.md +54 -21
- package/skill-data/ts-services/SKILL.md +4 -4
- package/skill-data/ts-services/references/service-surface.md +2 -1
- package/skill-data/zig/SKILL.md +1 -1
- package/src/app_runner/root.zig +122 -6
- package/src/app_runner/ts_core_main.zig +9 -4
- package/src/app_runner/ts_core_mobile.zig +241 -0
- package/src/app_runner/window_placement.zig +63 -0
- package/src/automation/protocol.zig +5 -1
- package/src/automation/snapshot.zig +8 -6
- package/src/embed/app_exports.zig +3 -1
- package/src/embed/conversions.zig +1 -0
- package/src/embed/host.zig +10 -3
- package/src/embed/root.zig +2 -0
- package/src/embed/tests.zig +41 -1
- package/src/embed/types.zig +2 -0
- package/src/embed/ui_host.zig +94 -4
- package/src/platform/android/NativeSdkActivity.java +49 -4
- package/src/platform/android/android_host.c +3 -3
- package/src/platform/android/native_sdk_app.h +1 -1
- package/src/platform/ios/apple_image_fit.h +38 -0
- package/src/platform/ios/files.zig +1 -0
- package/src/platform/ios/native_sdk_app.h +1 -1
- package/src/platform/ios/uikit_host.m +28 -5
- package/src/platform/linux/cef_host.cpp +17 -3
- package/src/platform/linux/gtk_host.c +243 -15
- package/src/platform/linux/gtk_host.h +5 -4
- package/src/platform/linux/root.zig +91 -11
- package/src/platform/macos/appkit_host.h +18 -11
- package/src/platform/macos/appkit_host.m +402 -128
- package/src/platform/macos/cef_host.mm +342 -122
- package/src/platform/macos/image_fit_test.m +44 -0
- package/src/platform/macos/root.zig +231 -39
- package/src/platform/null_platform.zig +251 -54
- package/src/platform/null_platform_tests.zig +97 -4
- package/src/platform/root.zig +12 -0
- package/src/platform/types.zig +174 -35
- package/src/platform/windows/root.zig +42 -13
- package/src/platform/windows/webview2_host.cpp +72 -11
- package/src/primitives/app_manifest/root.zig +3 -0
- package/src/primitives/app_manifest/tests.zig +24 -0
- package/src/primitives/app_manifest/types.zig +13 -0
- package/src/primitives/app_manifest/validation.zig +24 -0
- package/src/primitives/canvas/a11y_audit.zig +9 -7
- package/src/primitives/canvas/a11y_audit_tests.zig +24 -0
- package/src/primitives/canvas/events.zig +50 -0
- package/src/primitives/canvas/layout_audit.zig +6 -6
- package/src/primitives/canvas/markdown.zig +4 -0
- package/src/primitives/canvas/markdown_tests.zig +1 -0
- package/src/primitives/canvas/png.zig +138 -0
- package/src/primitives/canvas/root.zig +13 -2
- package/src/primitives/canvas/text_span_tests.zig +5 -3
- package/src/primitives/canvas/text_spans.zig +2 -0
- package/src/primitives/canvas/text_tests.zig +20 -0
- package/src/primitives/canvas/ui.zig +55 -2
- package/src/primitives/canvas/ui_markup.zig +40 -13
- package/src/primitives/canvas/ui_markup_binary.zig +8 -6
- package/src/primitives/canvas/ui_markup_binary_tests.zig +14 -4
- package/src/primitives/canvas/ui_markup_compiled.zig +5 -1
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +52 -0
- package/src/primitives/canvas/ui_markup_tests.zig +11 -2
- package/src/primitives/canvas/ui_markup_view.zig +8 -2
- package/src/primitives/canvas/ui_markup_view_tests.zig +49 -4
- package/src/primitives/canvas/ui_schema.zig +17 -13
- package/src/primitives/canvas/ui_tests.zig +47 -0
- package/src/primitives/canvas/widget_access.zig +3 -3
- package/src/primitives/canvas/widget_anchored_tests.zig +106 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +580 -32
- package/src/primitives/canvas/widget_invalidation.zig +33 -6
- package/src/primitives/canvas/widget_layout.zig +333 -126
- package/src/primitives/canvas/widget_layout_tests.zig +169 -0
- package/src/primitives/canvas/widget_metrics.zig +7 -8
- package/src/primitives/canvas/widget_render.zig +76 -44
- package/src/primitives/canvas/widget_render_controls.zig +110 -32
- package/src/primitives/canvas/widget_render_style.zig +58 -18
- package/src/primitives/canvas/widget_render_surfaces.zig +45 -14
- package/src/primitives/canvas/widget_routing.zig +40 -34
- package/src/primitives/canvas/widget_runtime.zig +11 -2
- package/src/primitives/canvas/widget_runtime_tests.zig +10 -14
- package/src/primitives/canvas/widget_semantics.zig +8 -3
- package/src/primitives/canvas/widget_semantics_tests.zig +6 -2
- package/src/primitives/canvas/widget_tree.zig +99 -5
- package/src/primitives/canvas/widgets.zig +20 -7
- package/src/root.zig +10 -0
- package/src/runtime/api.zig +14 -0
- package/src/runtime/automation_commands.zig +35 -2
- package/src/runtime/automation_snapshot.zig +32 -24
- package/src/runtime/automation_widget_dispatch.zig +48 -6
- package/src/runtime/bridge_permission_tests.zig +35 -1
- package/src/runtime/bridge_responses.zig +1 -1
- package/src/runtime/builtin_bridge.zig +16 -3
- package/src/runtime/canvas_frame_packet_tests.zig +22 -13
- package/src/runtime/canvas_image_tests.zig +146 -2
- package/src/runtime/canvas_images.zig +35 -16
- package/src/runtime/canvas_limits.zig +13 -3
- package/src/runtime/canvas_widget_accessibility_tests.zig +279 -4
- package/src/runtime/canvas_widget_anchored_tests.zig +54 -0
- package/src/runtime/canvas_widget_control_tests.zig +31 -1
- package/src/runtime/canvas_widget_events.zig +151 -26
- package/src/runtime/canvas_widget_floating_tests.zig +48 -0
- package/src/runtime/canvas_widget_runtime.zig +322 -102
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +34 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +12 -10
- package/src/runtime/canvas_widget_scroll_tests.zig +193 -2
- package/src/runtime/canvas_widget_semantics_tests.zig +337 -3
- package/src/runtime/canvas_widget_split_tree_tests.zig +55 -0
- package/src/runtime/canvas_widget_state.zig +33 -7
- package/src/runtime/core.zig +28 -6
- package/src/runtime/effects.zig +1337 -49
- package/src/runtime/effects_file_tests.zig +587 -0
- package/src/runtime/effects_image_tests.zig +91 -0
- package/src/runtime/effects_notification_tests.zig +11 -0
- package/src/runtime/file_access.zig +400 -0
- package/src/runtime/file_effect_crash_helper.zig +28 -0
- package/src/runtime/file_effect_crash_tests.zig +37 -0
- package/src/runtime/flow.zig +21 -9
- package/src/runtime/gpu_surface_events.zig +25 -2
- package/src/runtime/native_command_event_tests.zig +44 -2
- package/src/runtime/pty.zig +5 -2
- package/src/runtime/root.zig +7 -0
- package/src/runtime/session_blobs.zig +4 -1
- package/src/runtime/session_journal.zig +56 -5
- package/src/runtime/session_record.zig +38 -0
- package/src/runtime/session_replay.zig +110 -15
- package/src/runtime/session_tests.zig +192 -4
- package/src/runtime/shell_layout_tests.zig +12 -0
- package/src/runtime/state.zig +10 -0
- package/src/runtime/system_services.zig +129 -36
- package/src/runtime/ts_core_host.zig +281 -11
- package/src/runtime/ts_core_host_tests.zig +142 -1
- package/src/runtime/ts_ui_app.zig +461 -8
- package/src/runtime/ui_app.zig +330 -112
- package/src/runtime/ui_app_tests.zig +251 -25
- package/src/runtime/ui_app_window_tests.zig +74 -8
- package/src/runtime/validation.zig +20 -0
- package/src/runtime/view.zig +3 -0
- package/src/runtime/view_widget_control.zig +18 -0
- package/src/runtime/view_widget_scroll.zig +112 -18
- package/src/runtime/view_widget_tree.zig +110 -13
- package/src/runtime/widget_bridge.zig +2 -0
- package/src/runtime/window_command_bridge_tests.zig +86 -0
- package/src/runtime/window_storage.zig +16 -1
- package/src/runtime/window_views.zig +1 -0
- package/src/tooling/ios.zig +3 -0
- package/src/tooling/manifest.zig +90 -2
- package/src/tooling/package.zig +67 -2
- package/src/tooling/raw_manifest.zig +10 -0
- package/src/tooling/templates.zig +156 -18
- package/src/tooling/ts_core.zig +83 -0
- package/src/tooling/web_engine.zig +1 -1
- package/tools/corewire/emit.zig +3 -3
- package/tools/corewire/emit_facade.zig +65 -5
- package/tools/corewire/emit_profile.zig +2 -2
- package/tools/corewire/emit_service.zig +2 -2
- package/tools/corewire/service_contract.zig +4 -4
- package/tools/corewire/sidecar.zig +4 -4
package/src/runtime/effects.zig
CHANGED
|
@@ -78,6 +78,7 @@ const persist_store = @import("persist_store.zig");
|
|
|
78
78
|
const record_store = @import("record_store.zig");
|
|
79
79
|
const relational_store = @import("relational_store.zig");
|
|
80
80
|
const credentials_store = @import("credentials_store.zig");
|
|
81
|
+
const file_access = @import("file_access.zig");
|
|
81
82
|
const pty_transport = @import("pty.zig");
|
|
82
83
|
|
|
83
84
|
/// Maximum in-flight effects (spawn slots / worker threads).
|
|
@@ -179,6 +180,13 @@ pub const max_effect_file_path_bytes: usize = 1024;
|
|
|
179
180
|
/// write would corrupt the file on disk, which truncation flags cannot
|
|
180
181
|
/// undo.
|
|
181
182
|
pub const max_effect_file_bytes: usize = 1024 * 1024;
|
|
183
|
+
/// Streaming reads use a stable 256-KiB delivery granularity. Writes accept
|
|
184
|
+
/// any chunk through the shared 1-MiB payload ceiling.
|
|
185
|
+
pub const effect_file_stream_chunk_bytes: usize = 256 * 1024;
|
|
186
|
+
/// Streaming file operations own a separate worker/slot family, so a large
|
|
187
|
+
/// import or export cannot consume the sixteen general effect slots.
|
|
188
|
+
pub const max_effect_file_streams: usize = 4;
|
|
189
|
+
pub const FileAccessBinding = file_access.Binding;
|
|
182
190
|
/// Model persistence owns a separate payload family and bound; it does not
|
|
183
191
|
/// consume a file-effect slot or inherit the raw-file cap.
|
|
184
192
|
pub const max_effect_persist_snapshot_bytes: usize = persist_store.max_snapshot_bytes;
|
|
@@ -594,8 +602,24 @@ pub const EffectResponse = struct {
|
|
|
594
602
|
dropped_before: u32 = 0,
|
|
595
603
|
};
|
|
596
604
|
|
|
597
|
-
/// Which file operation a file effect performs.
|
|
598
|
-
|
|
605
|
+
/// Which file operation a file effect performs. Values are append-only: the
|
|
606
|
+
/// enum is journaled as part of deterministic record/replay.
|
|
607
|
+
pub const EffectFileOp = enum {
|
|
608
|
+
read,
|
|
609
|
+
write,
|
|
610
|
+
append,
|
|
611
|
+
stat,
|
|
612
|
+
read_stream,
|
|
613
|
+
write_stream_open,
|
|
614
|
+
write_stream_chunk,
|
|
615
|
+
write_stream_close,
|
|
616
|
+
delete,
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
/// Whether a file result is an ordinary one-shot terminal or one delivery
|
|
620
|
+
/// in a streaming read. A read stream emits `chunk` zero or more times and
|
|
621
|
+
/// exactly one `done` or terminal error.
|
|
622
|
+
pub const EffectFileEvent = enum { terminal, chunk, done };
|
|
599
623
|
|
|
600
624
|
/// The terminal outcome of one file effect. Every started file effect
|
|
601
625
|
/// delivers exactly one Msg carrying one of these — failure is never
|
|
@@ -605,7 +629,7 @@ pub const EffectFileOutcome = enum {
|
|
|
605
629
|
/// The operation completed. Reads carry the whole file in `bytes`;
|
|
606
630
|
/// writes wrote every byte (parent directories created as needed).
|
|
607
631
|
ok,
|
|
608
|
-
/// The file does not exist (reads only — writes create the path).
|
|
632
|
+
/// The file does not exist (reads and deletes only — writes create the path).
|
|
609
633
|
not_found,
|
|
610
634
|
/// The OS refused: permissions, the path names a directory, disk
|
|
611
635
|
/// errors, an unwritable parent — anything but absence.
|
|
@@ -620,21 +644,37 @@ pub const EffectFileOutcome = enum {
|
|
|
620
644
|
/// `cancel(key)` ended it before the result was delivered. The
|
|
621
645
|
/// operation itself may still have completed on disk.
|
|
622
646
|
cancelled,
|
|
647
|
+
/// A write-stream chunk/close named no live sink.
|
|
648
|
+
sink_missing,
|
|
649
|
+
/// A chunk or close arrived while the sink still had an earlier
|
|
650
|
+
/// operation in flight.
|
|
651
|
+
out_of_order,
|
|
652
|
+
/// The filesystem explicitly reported exhausted capacity/quota.
|
|
653
|
+
disk_full,
|
|
623
654
|
};
|
|
624
655
|
|
|
625
656
|
/// Payload for file-effect Msg constructors. Exactly one is delivered
|
|
626
|
-
/// per
|
|
657
|
+
/// per one-shot file effect — terminal, nothing for that key after
|
|
627
658
|
/// it. `bytes` is a read's content (binary-safe), valid only during
|
|
628
659
|
/// the `update` call that receives it — copy what the model keeps.
|
|
629
660
|
/// Writes always deliver empty `bytes`.
|
|
630
661
|
pub const EffectFileResult = struct {
|
|
631
662
|
key: u64,
|
|
632
663
|
op: EffectFileOp = .read,
|
|
664
|
+
event: EffectFileEvent = .terminal,
|
|
633
665
|
outcome: EffectFileOutcome = .ok,
|
|
634
666
|
/// Read contents: the whole file for `.ok`, the first
|
|
635
667
|
/// `max_effect_file_bytes` for `.truncated`, `""` otherwise (and
|
|
636
|
-
/// always for writes).
|
|
668
|
+
/// always for writes and deletes).
|
|
637
669
|
bytes: []const u8 = "",
|
|
670
|
+
/// Stream total bytes on `.done`; stat size on a successful `.stat`.
|
|
671
|
+
total: u64 = 0,
|
|
672
|
+
/// Stat modification time in Unix milliseconds (inside JavaScript's
|
|
673
|
+
/// exact-integer window for contemporary dates). Zero for a missing file.
|
|
674
|
+
mtime_ms: i64 = 0,
|
|
675
|
+
/// Stat existence bit. `.stat` of an absent path is a successful result
|
|
676
|
+
/// with `exists=false`, size/time zero.
|
|
677
|
+
exists: bool = false,
|
|
638
678
|
/// Loop-side terminal notices evicted from the pending ring to make
|
|
639
679
|
/// room before this one (only under extreme rejection bursts).
|
|
640
680
|
/// Never silently zero when something was lost.
|
|
@@ -937,17 +977,16 @@ pub fn imageCachePartialPath(buffer: []u8, cache_path: []const u8, generation: u
|
|
|
937
977
|
/// `.rejected` image result Msg.
|
|
938
978
|
pub const max_effect_image_path_bytes: usize = max_effect_file_path_bytes;
|
|
939
979
|
|
|
940
|
-
/// Maximum ENCODED source bytes one `loadImage` accepts —
|
|
941
|
-
///
|
|
942
|
-
///
|
|
943
|
-
///
|
|
944
|
-
///
|
|
945
|
-
///
|
|
946
|
-
///
|
|
947
|
-
///
|
|
948
|
-
|
|
949
|
-
pub const max_effect_image_bytes: usize =
|
|
950
|
-
canvas_limits.max_registered_canvas_image_pixel_bytes / 4;
|
|
980
|
+
/// Maximum ENCODED source bytes one `loadImage` accepts — independent of
|
|
981
|
+
/// the decoded-pixel target now that codecs decode-to-fit. Eight MiB covers
|
|
982
|
+
/// typical phone JPEG/HEIC photos while keeping costs fixed and loud: every
|
|
983
|
+
/// in-flight load owns a transient `bound + 1` fetch/read buffer, recorded
|
|
984
|
+
/// source bytes grow the session blob store, and URL cache entries may reach
|
|
985
|
+
/// this size. Unlike fetch bodies there is no truncated delivery — a cut
|
|
986
|
+
/// image can never decode, so over-bound sources fail whole with
|
|
987
|
+
/// `.too_large`. The bound stays flat when an app raises its pixel budget.
|
|
988
|
+
pub const max_effect_image_source_bytes: usize = 8 * 1024 * 1024;
|
|
989
|
+
pub const max_effect_image_bytes: usize = max_effect_image_source_bytes;
|
|
951
990
|
|
|
952
991
|
/// Bytes of an image effect result's content address in the session
|
|
953
992
|
/// journal: the first half of the source bytes' SHA-256, the
|
|
@@ -991,9 +1030,10 @@ pub const EffectImageOutcome = enum(u8) {
|
|
|
991
1030
|
http_status,
|
|
992
1031
|
/// `cancel(id)` ended the load before its result was delivered.
|
|
993
1032
|
cancelled,
|
|
994
|
-
/// The source bytes exceed `
|
|
995
|
-
/// pixels
|
|
996
|
-
///
|
|
1033
|
+
/// The source bytes exceed `max_effect_image_source_bytes`. A conforming
|
|
1034
|
+
/// codec fits decoded pixels to the app's registered-image budget;
|
|
1035
|
+
/// `error.ImageTooLarge` is retained only as a defensive codec-contract
|
|
1036
|
+
/// violation and maps here too.
|
|
997
1037
|
too_large,
|
|
998
1038
|
/// The host has no image codec (`error.UnsupportedService`), or no
|
|
999
1039
|
/// image registry is bound to this channel.
|
|
@@ -2064,7 +2104,18 @@ pub const EffectResultRecord = struct {
|
|
|
2064
2104
|
status: u16 = 0,
|
|
2065
2105
|
fetch_outcome: EffectFetchOutcome = .ok,
|
|
2066
2106
|
file_op: EffectFileOp = .read,
|
|
2107
|
+
file_event: EffectFileEvent = .terminal,
|
|
2067
2108
|
file_outcome: EffectFileOutcome = .ok,
|
|
2109
|
+
file_total: u64 = 0,
|
|
2110
|
+
file_mtime_ms: i64 = 0,
|
|
2111
|
+
file_exists: bool = false,
|
|
2112
|
+
/// True only for a loop-side admission/protocol result that deterministically
|
|
2113
|
+
/// regenerates under replay. Executor/start failures keep this false even
|
|
2114
|
+
/// when their closed outcome is `.rejected`. The retained field name preserves
|
|
2115
|
+
/// the journal layout and also covers `.sink_missing` and `.out_of_order`.
|
|
2116
|
+
file_rejected_admission: bool = false,
|
|
2117
|
+
file_blob_hash: [effect_image_blob_hash_len]u8 = @splat(0),
|
|
2118
|
+
file_blob_len: u64 = 0,
|
|
2068
2119
|
clipboard_op: EffectClipboardOp = .write,
|
|
2069
2120
|
clipboard_outcome: EffectClipboardOutcome = .ok,
|
|
2070
2121
|
timer_timestamp_ns: u64 = 0,
|
|
@@ -2570,7 +2621,17 @@ const FileWorkerContext = struct {
|
|
|
2570
2621
|
buffer: []u8,
|
|
2571
2622
|
payload_len: usize,
|
|
2572
2623
|
read_len: usize = 0,
|
|
2624
|
+
stat_size: u64 = 0,
|
|
2625
|
+
stat_mtime_ms: i64 = 0,
|
|
2626
|
+
stat_exists: bool = false,
|
|
2573
2627
|
outcome: EffectFileOutcome = .io_failed,
|
|
2628
|
+
/// App-directory exemptions carry authority as an already-open parent
|
|
2629
|
+
/// directory. External paths granted by the manifest leave this null and
|
|
2630
|
+
/// use `path_storage` directly.
|
|
2631
|
+
parent: ?std.Io.Dir = null,
|
|
2632
|
+
basename_storage: [max_effect_file_path_bytes]u8 = undefined,
|
|
2633
|
+
basename_len: usize = 0,
|
|
2634
|
+
missing: bool = false,
|
|
2574
2635
|
/// Copy of the effect's path: the slot's copy lives inside the
|
|
2575
2636
|
/// channel, which the owner frees right after an abandoning
|
|
2576
2637
|
/// teardown returns.
|
|
@@ -2584,6 +2645,41 @@ const FileWorkerContext = struct {
|
|
|
2584
2645
|
fn payload(ctx: *const FileWorkerContext) []const u8 {
|
|
2585
2646
|
return ctx.buffer[0..ctx.payload_len];
|
|
2586
2647
|
}
|
|
2648
|
+
|
|
2649
|
+
fn basename(ctx: *const FileWorkerContext) []const u8 {
|
|
2650
|
+
return ctx.basename_storage[0..ctx.basename_len];
|
|
2651
|
+
}
|
|
2652
|
+
};
|
|
2653
|
+
|
|
2654
|
+
/// Process-lived context for one streamed file syscall. Unlike the stream
|
|
2655
|
+
/// table slot, this may outlive the Effects owner after a bounded teardown:
|
|
2656
|
+
/// cancellation transfers the atomic sink and chunk bytes here before the
|
|
2657
|
+
/// worker starts, then the commit/abandon handshake decides whether results
|
|
2658
|
+
/// may move back into the slot.
|
|
2659
|
+
const FileStreamWorkerContext = struct {
|
|
2660
|
+
mutex: SpinMutex = .{},
|
|
2661
|
+
abandoned: bool = false,
|
|
2662
|
+
committed: bool = false,
|
|
2663
|
+
interrupt: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
2664
|
+
done: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
2665
|
+
op: EffectFileOp,
|
|
2666
|
+
parent: ?std.Io.Dir = null,
|
|
2667
|
+
path_storage: [max_effect_file_path_bytes]u8 = undefined,
|
|
2668
|
+
path_len: usize = 0,
|
|
2669
|
+
offset: u64 = 0,
|
|
2670
|
+
total: u64 = 0,
|
|
2671
|
+
read_file: ?std.Io.File = null,
|
|
2672
|
+
chunk: ?[]u8 = null,
|
|
2673
|
+
atomic: ?std.Io.File.Atomic = null,
|
|
2674
|
+
atomic_dest: ?[]u8 = null,
|
|
2675
|
+
read_buffer: ?[]u8 = null,
|
|
2676
|
+
read_len: usize = 0,
|
|
2677
|
+
event: EffectFileEvent = .terminal,
|
|
2678
|
+
outcome: EffectFileOutcome = .io_failed,
|
|
2679
|
+
|
|
2680
|
+
fn path(ctx: *const FileStreamWorkerContext) []const u8 {
|
|
2681
|
+
return ctx.path_storage[0..ctx.path_len];
|
|
2682
|
+
}
|
|
2587
2683
|
};
|
|
2588
2684
|
|
|
2589
2685
|
/// Map a fetch-side error onto the delivered failure taxonomy. The
|
|
@@ -2993,6 +3089,48 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2993
3089
|
bytes: []const u8 = "",
|
|
2994
3090
|
};
|
|
2995
3091
|
|
|
3092
|
+
pub const AppendFileOptions = struct {
|
|
3093
|
+
key: u64,
|
|
3094
|
+
path: []const u8,
|
|
3095
|
+
bytes: []const u8,
|
|
3096
|
+
on_result: ?FileMsgFn = null,
|
|
3097
|
+
};
|
|
3098
|
+
|
|
3099
|
+
pub const StatFileOptions = struct {
|
|
3100
|
+
key: u64,
|
|
3101
|
+
path: []const u8,
|
|
3102
|
+
on_result: ?FileMsgFn = null,
|
|
3103
|
+
};
|
|
3104
|
+
|
|
3105
|
+
pub const DeleteFileOptions = struct {
|
|
3106
|
+
key: u64,
|
|
3107
|
+
path: []const u8,
|
|
3108
|
+
on_result: ?FileMsgFn = null,
|
|
3109
|
+
};
|
|
3110
|
+
|
|
3111
|
+
pub const ReadFileStreamOptions = struct {
|
|
3112
|
+
key: u64,
|
|
3113
|
+
path: []const u8,
|
|
3114
|
+
on_result: ?FileMsgFn = null,
|
|
3115
|
+
};
|
|
3116
|
+
|
|
3117
|
+
pub const WriteFileStreamOptions = struct {
|
|
3118
|
+
key: u64,
|
|
3119
|
+
path: []const u8,
|
|
3120
|
+
on_result: ?FileMsgFn = null,
|
|
3121
|
+
};
|
|
3122
|
+
|
|
3123
|
+
pub const WriteFileChunkOptions = struct {
|
|
3124
|
+
key: u64,
|
|
3125
|
+
bytes: []const u8,
|
|
3126
|
+
on_result: ?FileMsgFn = null,
|
|
3127
|
+
};
|
|
3128
|
+
|
|
3129
|
+
pub const WriteFileCloseOptions = struct {
|
|
3130
|
+
key: u64,
|
|
3131
|
+
on_result: ?FileMsgFn = null,
|
|
3132
|
+
};
|
|
3133
|
+
|
|
2996
3134
|
pub const WriteClipboardOptions = struct {
|
|
2997
3135
|
/// Caller-chosen identity, stored in the model. Shares the
|
|
2998
3136
|
/// key space and the `max_effects` slots with spawns,
|
|
@@ -3782,6 +3920,14 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3782
3920
|
/// `line_len`.
|
|
3783
3921
|
file_op: EffectFileOp = .read,
|
|
3784
3922
|
file_outcome: EffectFileOutcome = .ok,
|
|
3923
|
+
file_event: EffectFileEvent = .terminal,
|
|
3924
|
+
file_total: u64 = 0,
|
|
3925
|
+
file_mtime_ms: i64 = 0,
|
|
3926
|
+
file_exists: bool = false,
|
|
3927
|
+
file_rejected_admission: bool = false,
|
|
3928
|
+
/// Streaming-file table generation. Zero means the ordinary
|
|
3929
|
+
/// general-slot file family.
|
|
3930
|
+
file_stream_generation: u64 = 0,
|
|
3785
3931
|
/// `.clipboard` entries: the operation and its terminal
|
|
3786
3932
|
/// outcome. A read's text stays in the slot's heap buffer
|
|
3787
3933
|
/// (taken at drain, like a fetch body) with its length in
|
|
@@ -3863,7 +4009,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3863
4009
|
retire_generation: u64 = 0,
|
|
3864
4010
|
},
|
|
3865
4011
|
response: struct { response: EffectResponse, response_fn: ?ResponseMsgFn },
|
|
3866
|
-
file: struct { result: EffectFileResult, file_fn: ?FileMsgFn },
|
|
4012
|
+
file: struct { result: EffectFileResult, file_fn: ?FileMsgFn, stream_generation: u64 = 0, rejected_admission: bool = false },
|
|
3867
4013
|
clipboard: struct { result: EffectClipboardResult, clipboard_fn: ?ClipboardMsgFn },
|
|
3868
4014
|
timer: struct { timer: EffectTimer, timer_fn: ?TimerMsgFn },
|
|
3869
4015
|
/// `.host` terminals produced on the loop thread: rejections
|
|
@@ -4303,6 +4449,37 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4303
4449
|
execution: credentials_store.Execution = .{ .outcome = .rejected },
|
|
4304
4450
|
};
|
|
4305
4451
|
|
|
4452
|
+
const FileStreamState = enum(u8) { idle, reading, sink_open, sink_busy, sink_closing };
|
|
4453
|
+
|
|
4454
|
+
/// Long-lived read stream / atomic write sink. It has a dedicated
|
|
4455
|
+
/// table and never consumes the general spawn/fetch/file slots.
|
|
4456
|
+
const FileStreamSlot = struct {
|
|
4457
|
+
state: FileStreamState = .idle,
|
|
4458
|
+
fake: bool = false,
|
|
4459
|
+
key: u64 = 0,
|
|
4460
|
+
generation: u64 = 0,
|
|
4461
|
+
op: EffectFileOp = .read_stream,
|
|
4462
|
+
on_result: ?FileMsgFn = null,
|
|
4463
|
+
path_storage: [max_effect_file_path_bytes]u8 = undefined,
|
|
4464
|
+
path_len: usize = 0,
|
|
4465
|
+
total: u64 = 0,
|
|
4466
|
+
offset: u64 = 0,
|
|
4467
|
+
chunk_pending: bool = false,
|
|
4468
|
+
cancelling: bool = false,
|
|
4469
|
+
cancelled: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
4470
|
+
worker_thread: ?std.Thread = null,
|
|
4471
|
+
worker_ctx: ?*FileStreamWorkerContext = null,
|
|
4472
|
+
read_file: ?std.Io.File = null,
|
|
4473
|
+
atomic: ?std.Io.File.Atomic = null,
|
|
4474
|
+
atomic_dest: ?[]u8 = null,
|
|
4475
|
+
parent: ?std.Io.Dir = null,
|
|
4476
|
+
chunk: ?[]u8 = null,
|
|
4477
|
+
|
|
4478
|
+
fn path(slot: *const FileStreamSlot) []const u8 {
|
|
4479
|
+
return slot.path_storage[0..slot.path_len];
|
|
4480
|
+
}
|
|
4481
|
+
};
|
|
4482
|
+
|
|
4306
4483
|
const Slot = struct {
|
|
4307
4484
|
state: std.atomic.Value(SlotState) = std.atomic.Value(SlotState).init(.idle),
|
|
4308
4485
|
generation: u32 = 0,
|
|
@@ -4697,6 +4874,10 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4697
4874
|
/// Capability-installed Tier-3 relational database. Queries execute
|
|
4698
4875
|
/// synchronously on the loop thread and stage bounded page results.
|
|
4699
4876
|
relational_store_binding: ?RelationalStoreBinding = null,
|
|
4877
|
+
/// Raw-file path policy installed by the runtime. Null preserves the
|
|
4878
|
+
/// embedding API's legacy unrestricted behavior; shipping app
|
|
4879
|
+
/// runners always bind an explicit policy.
|
|
4880
|
+
file_access_binding: ?file_access.Binding = null,
|
|
4700
4881
|
/// Window-action mirror: counts and the last requested label,
|
|
4701
4882
|
/// observable in tests (`windowActionState`).
|
|
4702
4883
|
window_action_state: WindowActionState = .{},
|
|
@@ -4714,6 +4895,10 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4714
4895
|
/// Injectable so tests pin the interrupt and abandon paths
|
|
4715
4896
|
/// with a tiny bound.
|
|
4716
4897
|
file_join_deadline_ms: u64 = default_effect_file_join_deadline_ms,
|
|
4898
|
+
/// Deterministic failure seam for raw-file tests. Real runtimes leave
|
|
4899
|
+
/// this null. The next blocking raw-file operation consumes it and
|
|
4900
|
+
/// reports the same closed outcome the OS error would classify to.
|
|
4901
|
+
test_next_file_error: ?anyerror = null,
|
|
4717
4902
|
/// Best-effort interruption switch: halfway through the file
|
|
4718
4903
|
/// deadline, teardown cancels a stuck file worker's blocking
|
|
4719
4904
|
/// task (the threaded io interrupts the blocked syscall —
|
|
@@ -4798,6 +4983,8 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4798
4983
|
/// the non-wrapping guarantee without 2^32 opens.
|
|
4799
4984
|
channel_generation: u64 = 0,
|
|
4800
4985
|
slots: [total_effect_slots]Slot = [_]Slot{.{}} ** total_effect_slots,
|
|
4986
|
+
file_stream_slots: [max_effect_file_streams]FileStreamSlot = [_]FileStreamSlot{.{}} ** max_effect_file_streams,
|
|
4987
|
+
next_file_stream_generation: u64 = 1,
|
|
4801
4988
|
db_slots: [max_db_effects]DbSlot = [_]DbSlot{.{}} ** max_db_effects,
|
|
4802
4989
|
next_db_generation: u64 = 1,
|
|
4803
4990
|
db_revision: u64 = 0,
|
|
@@ -5365,6 +5552,17 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
5365
5552
|
}
|
|
5366
5553
|
if (io_threaded_supported) {
|
|
5367
5554
|
if (self.io_threaded) |threaded| {
|
|
5555
|
+
// Stream workers use the same bounded
|
|
5556
|
+
// commit/abandon protocol as whole-file workers.
|
|
5557
|
+
for (&self.file_stream_slots) |*stream| {
|
|
5558
|
+
if (stream.state != .idle) self.retireFileStream(stream);
|
|
5559
|
+
}
|
|
5560
|
+
if (self.abandoned_file_workers > 0 and self.io_threaded != null) {
|
|
5561
|
+
// A detached stream worker still owns a task inside
|
|
5562
|
+
// this executor. Preserve it exactly like the
|
|
5563
|
+
// whole-file/spawn abandon path below.
|
|
5564
|
+
self.io_threaded = null;
|
|
5565
|
+
}
|
|
5368
5566
|
// Wait for every worker's terminal post, then JOIN
|
|
5369
5567
|
// every worker thread. For fetch workers the wait
|
|
5370
5568
|
// is unbounded on purpose: a worker holds pointers
|
|
@@ -5706,6 +5904,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
5706
5904
|
self.credentials_store_binding = null;
|
|
5707
5905
|
self.relational_store_binding = null;
|
|
5708
5906
|
self.system_services = null;
|
|
5907
|
+
self.file_access_binding = null;
|
|
5709
5908
|
}
|
|
5710
5909
|
|
|
5711
5910
|
/// Discard every queued completion, freeing heap line payloads
|
|
@@ -6089,6 +6288,40 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6089
6288
|
if (self.relational_store_binding == null) self.relational_store_binding = binding;
|
|
6090
6289
|
}
|
|
6091
6290
|
|
|
6291
|
+
/// Install the runtime's raw-file confinement policy. The binding's
|
|
6292
|
+
/// roots and their strings must outlive this Effects instance.
|
|
6293
|
+
pub fn bindFileAccess(self: *Self, binding: file_access.Binding) void {
|
|
6294
|
+
if (self.file_access_binding == null) self.file_access_binding = binding;
|
|
6295
|
+
}
|
|
6296
|
+
|
|
6297
|
+
fn resolvedFileAccess(self: *Self, path: []const u8, options: file_access.ResolveOptions) ?file_access.Resolved {
|
|
6298
|
+
const binding = self.file_access_binding orelse return .{
|
|
6299
|
+
.path = self.allocator.dupe(u8, path) catch return null,
|
|
6300
|
+
.decision = .allow,
|
|
6301
|
+
};
|
|
6302
|
+
const io = self.ensureIo() catch return null;
|
|
6303
|
+
var resolved = file_access.resolveForOperation(self.allocator, io, binding, path, options) catch return null;
|
|
6304
|
+
return switch (resolved.decision) {
|
|
6305
|
+
.allow => resolved,
|
|
6306
|
+
.reject => blk: {
|
|
6307
|
+
resolved.deinit(self.allocator, io);
|
|
6308
|
+
break :blk null;
|
|
6309
|
+
},
|
|
6310
|
+
.warn => blk: {
|
|
6311
|
+
if (comptime builtin.os.tag != .freestanding) {
|
|
6312
|
+
std.debug.print("native warning: raw file effect path '{s}' is outside this app's directories; add the `filesystem` permission before enforcement\n", .{path});
|
|
6313
|
+
}
|
|
6314
|
+
break :blk resolved;
|
|
6315
|
+
},
|
|
6316
|
+
};
|
|
6317
|
+
}
|
|
6318
|
+
|
|
6319
|
+
fn takeTestFileFailure(self: *Self) ?EffectFileOutcome {
|
|
6320
|
+
const err = self.test_next_file_error orelse return null;
|
|
6321
|
+
self.test_next_file_error = null;
|
|
6322
|
+
return fileOpFailure(err);
|
|
6323
|
+
}
|
|
6324
|
+
|
|
6092
6325
|
/// Switch this channel into session-replay mode: the fake
|
|
6093
6326
|
/// executor (no processes, no network, no file or pasteboard
|
|
6094
6327
|
/// I/O — requests park in their slots) with journaled results as
|
|
@@ -6099,6 +6332,10 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6099
6332
|
self.replay = true;
|
|
6100
6333
|
}
|
|
6101
6334
|
|
|
6335
|
+
pub fn replayArmed(self: *const Self) bool {
|
|
6336
|
+
return self.replay;
|
|
6337
|
+
}
|
|
6338
|
+
|
|
6102
6339
|
/// End-of-journal consistency check (the `.finish` replay
|
|
6103
6340
|
/// control): every journaled video cascade resolution must have
|
|
6104
6341
|
/// been consumed by the load it named, and none may have paired
|
|
@@ -6806,10 +7043,13 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6806
7043
|
/// outcome `.rejected` on the next drain. File effects share the
|
|
6807
7044
|
/// `max_effects` slots and the key space with spawns and fetches.
|
|
6808
7045
|
pub fn writeFile(self: *Self, options: WriteFileOptions) void {
|
|
6809
|
-
if (options.
|
|
7046
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes or
|
|
7047
|
+
options.bytes.len > max_effect_file_bytes)
|
|
7048
|
+
{
|
|
6810
7049
|
return self.rejectFile(options.key, .write, options.on_result);
|
|
6811
7050
|
}
|
|
6812
|
-
self.
|
|
7051
|
+
var access = self.resolvedFileAccess(options.path, .{ .create_parents = true }) orelse return self.rejectFileExternal(options.key, .write, options.on_result);
|
|
7052
|
+
self.startFile(options.key, .write, &access, options.bytes, options.on_result);
|
|
6813
7053
|
}
|
|
6814
7054
|
|
|
6815
7055
|
/// Read a whole file on a worker thread and deliver it as
|
|
@@ -6820,15 +7060,223 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6820
7060
|
/// snapshot must not parse as whole). The bytes are drain
|
|
6821
7061
|
/// scratch: copy what the model keeps.
|
|
6822
7062
|
pub fn readFile(self: *Self, options: ReadFileOptions) void {
|
|
6823
|
-
|
|
7063
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes) {
|
|
7064
|
+
return self.rejectFile(options.key, .read, options.on_result);
|
|
7065
|
+
}
|
|
7066
|
+
var access = self.resolvedFileAccess(options.path, .{}) orelse return self.rejectFileExternal(options.key, .read, options.on_result);
|
|
7067
|
+
self.startFile(options.key, .read, &access, "", options.on_result);
|
|
7068
|
+
}
|
|
7069
|
+
|
|
7070
|
+
/// Append one bounded payload to a file. Unlike a streaming sink this
|
|
7071
|
+
/// intentionally has no atomic-finalize contract: it is the log-file
|
|
7072
|
+
/// case, where each accepted payload extends the visible file.
|
|
7073
|
+
pub fn appendFile(self: *Self, options: AppendFileOptions) void {
|
|
7074
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes or
|
|
7075
|
+
options.bytes.len > max_effect_file_bytes)
|
|
7076
|
+
{
|
|
7077
|
+
return self.rejectFile(options.key, .append, options.on_result);
|
|
7078
|
+
}
|
|
7079
|
+
var access = self.resolvedFileAccess(options.path, .{ .create_parents = true }) orelse return self.rejectFileExternal(options.key, .append, options.on_result);
|
|
7080
|
+
self.startFile(options.key, .append, &access, options.bytes, options.on_result);
|
|
7081
|
+
}
|
|
7082
|
+
|
|
7083
|
+
/// Stat a path without first allocating a whole-file read. Absence is
|
|
7084
|
+
/// a successful result with `exists=false`.
|
|
7085
|
+
pub fn statFile(self: *Self, options: StatFileOptions) void {
|
|
7086
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes) {
|
|
7087
|
+
return self.rejectFile(options.key, .stat, options.on_result);
|
|
7088
|
+
}
|
|
7089
|
+
var access = self.resolvedFileAccess(options.path, .{}) orelse return self.rejectFileExternal(options.key, .stat, options.on_result);
|
|
7090
|
+
self.startFile(options.key, .stat, &access, "", options.on_result);
|
|
7091
|
+
}
|
|
7092
|
+
|
|
7093
|
+
/// Delete one file. A final symlink is unlinked without deleting its
|
|
7094
|
+
/// target. Absence is explicit (`.not_found`), while a directory or
|
|
7095
|
+
/// another OS refusal is `.io_failed`. The operation uses the same
|
|
7096
|
+
/// worker, key space, path policy, and exactly-one result callback as
|
|
7097
|
+
/// the other one-shot file effects.
|
|
7098
|
+
pub fn deleteFile(self: *Self, options: DeleteFileOptions) void {
|
|
7099
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes) {
|
|
7100
|
+
return self.rejectFile(options.key, .delete, options.on_result);
|
|
7101
|
+
}
|
|
7102
|
+
var access = self.resolvedFileAccess(options.path, .{ .preserve_final_component = true }) orelse return self.rejectFileExternal(options.key, .delete, options.on_result);
|
|
7103
|
+
self.startFile(options.key, .delete, &access, "", options.on_result);
|
|
7104
|
+
}
|
|
7105
|
+
|
|
7106
|
+
/// Stream a file as 256-KiB chunks followed by one `.done(total)`.
|
|
7107
|
+
/// Reads own `max_effect_file_streams`; no whole-file allocation and
|
|
7108
|
+
/// no total-size ceiling is involved.
|
|
7109
|
+
pub fn readFileStream(self: *Self, options: ReadFileStreamOptions) void {
|
|
7110
|
+
// Read streams join the file-style key family: reissuing a live
|
|
7111
|
+
// read key silently retires the predecessor before opening the
|
|
7112
|
+
// replacement. A sink under the key still owns it loudly — never
|
|
7113
|
+
// splice a read over a half-written export.
|
|
7114
|
+
if (self.findFileStream(options.key)) |existing| {
|
|
7115
|
+
if (self.file_stream_slots[existing].op != .read_stream) {
|
|
7116
|
+
return self.rejectFile(options.key, .read_stream, options.on_result);
|
|
7117
|
+
}
|
|
7118
|
+
self.retireFileStream(&self.file_stream_slots[existing]);
|
|
7119
|
+
}
|
|
7120
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes or
|
|
7121
|
+
self.keyOccupiedUntilDelivery(options.key))
|
|
7122
|
+
{
|
|
7123
|
+
return self.rejectFile(options.key, .read_stream, options.on_result);
|
|
7124
|
+
}
|
|
7125
|
+
var access = self.resolvedFileAccess(options.path, .{}) orelse return self.rejectFileExternal(options.key, .read_stream, options.on_result);
|
|
7126
|
+
const access_io = self.ensureIo() catch unreachable;
|
|
7127
|
+
defer access.deinit(self.allocator, access_io);
|
|
7128
|
+
if (access.path.len > max_effect_file_path_bytes) return self.rejectFileExternal(options.key, .read_stream, options.on_result);
|
|
7129
|
+
if (self.takeTestFileFailure()) |outcome| {
|
|
7130
|
+
return self.deliverLoopFile(.{ .key = options.key, .op = .read_stream, .outcome = outcome }, options.on_result);
|
|
7131
|
+
}
|
|
7132
|
+
const index = self.findIdleFileStream() orelse return self.rejectFile(options.key, .read_stream, options.on_result);
|
|
7133
|
+
const slot = &self.file_stream_slots[index];
|
|
7134
|
+
slot.* = .{
|
|
7135
|
+
.state = .reading,
|
|
7136
|
+
.fake = self.executor == .fake,
|
|
7137
|
+
.key = options.key,
|
|
7138
|
+
.generation = self.mintFileStreamGeneration(),
|
|
7139
|
+
.op = .read_stream,
|
|
7140
|
+
.on_result = options.on_result,
|
|
7141
|
+
};
|
|
7142
|
+
const stream_path = if (access.parent != null) access.basename else access.path;
|
|
7143
|
+
@memcpy(slot.path_storage[0..stream_path.len], stream_path);
|
|
7144
|
+
slot.path_len = stream_path.len;
|
|
7145
|
+
slot.parent = access.parent;
|
|
7146
|
+
access.parent = null;
|
|
7147
|
+
if (access.missing) {
|
|
7148
|
+
slot.fake = false;
|
|
7149
|
+
return self.deliverLoopFileStream(.{ .key = slot.key, .op = .read_stream, .outcome = .not_found }, slot.on_result, slot.generation, false);
|
|
7150
|
+
}
|
|
7151
|
+
if (!slot.fake) self.startReadFileStreamChunk(index);
|
|
7152
|
+
}
|
|
7153
|
+
|
|
7154
|
+
/// Open an atomic write sink. Chunks write into an unnamed/sibling
|
|
7155
|
+
/// temporary file; only close syncs and atomically replaces `path`.
|
|
7156
|
+
pub fn writeFileStream(self: *Self, options: WriteFileStreamOptions) void {
|
|
7157
|
+
if (options.path.len == 0 or options.path.len > max_effect_file_path_bytes or
|
|
7158
|
+
self.keyOccupiedUntilDelivery(options.key) or
|
|
7159
|
+
self.findFileStream(options.key) != null)
|
|
7160
|
+
{
|
|
7161
|
+
return self.rejectFile(options.key, .write_stream_open, options.on_result);
|
|
7162
|
+
}
|
|
7163
|
+
var access = self.resolvedFileAccess(options.path, .{ .create_parents = true }) orelse return self.rejectFileExternal(options.key, .write_stream_open, options.on_result);
|
|
7164
|
+
const access_io = self.ensureIo() catch unreachable;
|
|
7165
|
+
defer access.deinit(self.allocator, access_io);
|
|
7166
|
+
if (access.path.len > max_effect_file_path_bytes) return self.rejectFileExternal(options.key, .write_stream_open, options.on_result);
|
|
7167
|
+
if (self.takeTestFileFailure()) |outcome| {
|
|
7168
|
+
return self.deliverLoopFile(.{ .key = options.key, .op = .write_stream_open, .outcome = outcome }, options.on_result);
|
|
7169
|
+
}
|
|
7170
|
+
const index = self.findIdleFileStream() orelse return self.rejectFile(options.key, .write_stream_open, options.on_result);
|
|
7171
|
+
const slot = &self.file_stream_slots[index];
|
|
7172
|
+
slot.* = .{
|
|
7173
|
+
.state = if (self.executor == .fake) .sink_open else .sink_busy,
|
|
7174
|
+
.fake = self.executor == .fake,
|
|
7175
|
+
.key = options.key,
|
|
7176
|
+
.generation = self.mintFileStreamGeneration(),
|
|
7177
|
+
.op = .write_stream_open,
|
|
7178
|
+
.on_result = options.on_result,
|
|
7179
|
+
.chunk_pending = true,
|
|
7180
|
+
};
|
|
7181
|
+
@memcpy(slot.path_storage[0..if (access.parent != null) access.basename.len else access.path.len], if (access.parent != null) access.basename else access.path);
|
|
7182
|
+
slot.path_len = if (access.parent != null) access.basename.len else access.path.len;
|
|
7183
|
+
slot.parent = access.parent;
|
|
7184
|
+
access.parent = null;
|
|
7185
|
+
if (slot.fake) return;
|
|
7186
|
+
const io = self.ensureIo() catch return self.failFileStreamOpen(index, .io_failed);
|
|
7187
|
+
const ctx = self.createFileStreamContext(slot) orelse return self.failFileStreamOpen(index, .rejected);
|
|
7188
|
+
slot.worker_ctx = ctx;
|
|
7189
|
+
slot.worker_thread = std.Thread.spawn(.{}, fileStreamWorkerMain, .{ self, index, slot.generation, io, ctx }) catch {
|
|
7190
|
+
slot.worker_ctx = null;
|
|
7191
|
+
moveFileStreamResourcesToSlot(slot, ctx);
|
|
7192
|
+
destroyFileStreamContext(ctx, io);
|
|
7193
|
+
return self.failFileStreamOpen(index, .rejected);
|
|
7194
|
+
};
|
|
6824
7195
|
}
|
|
6825
7196
|
|
|
6826
|
-
fn
|
|
7197
|
+
pub fn writeFileChunk(self: *Self, options: WriteFileChunkOptions) void {
|
|
7198
|
+
const index = self.findFileStream(options.key) orelse {
|
|
7199
|
+
return self.deliverLoopFileAdmission(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .sink_missing }, options.on_result);
|
|
7200
|
+
};
|
|
7201
|
+
const slot = &self.file_stream_slots[index];
|
|
7202
|
+
if (slot.state != .sink_open or slot.chunk_pending) {
|
|
7203
|
+
return self.deliverLoopFileAdmission(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .out_of_order }, options.on_result);
|
|
7204
|
+
}
|
|
7205
|
+
if (options.bytes.len > max_effect_file_bytes) {
|
|
7206
|
+
return self.deliverLoopFileAdmission(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .rejected }, options.on_result);
|
|
7207
|
+
}
|
|
7208
|
+
if (self.takeTestFileFailure()) |outcome| {
|
|
7209
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_chunk, .outcome = outcome }, options.on_result, slot.generation, false);
|
|
7210
|
+
}
|
|
7211
|
+
slot.chunk_pending = true;
|
|
7212
|
+
slot.state = .sink_busy;
|
|
7213
|
+
slot.op = .write_stream_chunk;
|
|
7214
|
+
slot.on_result = options.on_result;
|
|
7215
|
+
if (slot.fake) return;
|
|
7216
|
+
slot.chunk = self.allocator.dupe(u8, options.bytes) catch {
|
|
7217
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .rejected }, options.on_result, slot.generation, false);
|
|
7218
|
+
};
|
|
7219
|
+
const io = self.ensureIo() catch {
|
|
7220
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .io_failed }, options.on_result, slot.generation, false);
|
|
7221
|
+
};
|
|
7222
|
+
const ctx = self.createFileStreamContext(slot) orelse return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .rejected }, options.on_result, slot.generation, false);
|
|
7223
|
+
slot.worker_ctx = ctx;
|
|
7224
|
+
slot.worker_thread = std.Thread.spawn(.{}, fileStreamWorkerMain, .{ self, index, slot.generation, io, ctx }) catch {
|
|
7225
|
+
slot.worker_ctx = null;
|
|
7226
|
+
moveFileStreamResourcesToSlot(slot, ctx);
|
|
7227
|
+
destroyFileStreamContext(ctx, io);
|
|
7228
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_chunk, .outcome = .rejected }, options.on_result, slot.generation, false);
|
|
7229
|
+
};
|
|
7230
|
+
}
|
|
7231
|
+
|
|
7232
|
+
pub fn writeFileClose(self: *Self, options: WriteFileCloseOptions) void {
|
|
7233
|
+
const index = self.findFileStream(options.key) orelse {
|
|
7234
|
+
return self.deliverLoopFileAdmission(.{ .key = options.key, .op = .write_stream_close, .outcome = .sink_missing }, options.on_result);
|
|
7235
|
+
};
|
|
7236
|
+
const slot = &self.file_stream_slots[index];
|
|
7237
|
+
if (slot.state != .sink_open or slot.chunk_pending) {
|
|
7238
|
+
return self.deliverLoopFileAdmission(.{ .key = options.key, .op = .write_stream_close, .outcome = .out_of_order }, options.on_result);
|
|
7239
|
+
}
|
|
7240
|
+
if (self.takeTestFileFailure()) |outcome| {
|
|
7241
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_close, .outcome = outcome }, options.on_result, slot.generation, false);
|
|
7242
|
+
}
|
|
7243
|
+
slot.state = .sink_closing;
|
|
7244
|
+
slot.chunk_pending = true;
|
|
7245
|
+
slot.op = .write_stream_close;
|
|
7246
|
+
slot.on_result = options.on_result;
|
|
7247
|
+
if (slot.fake) return;
|
|
7248
|
+
const io = self.ensureIo() catch {
|
|
7249
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_close, .outcome = .io_failed }, options.on_result, slot.generation, false);
|
|
7250
|
+
};
|
|
7251
|
+
const ctx = self.createFileStreamContext(slot) orelse return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_close, .outcome = .rejected }, options.on_result, slot.generation, false);
|
|
7252
|
+
slot.worker_ctx = ctx;
|
|
7253
|
+
slot.worker_thread = std.Thread.spawn(.{}, fileStreamWorkerMain, .{ self, index, slot.generation, io, ctx }) catch {
|
|
7254
|
+
slot.worker_ctx = null;
|
|
7255
|
+
moveFileStreamResourcesToSlot(slot, ctx);
|
|
7256
|
+
destroyFileStreamContext(ctx, io);
|
|
7257
|
+
return self.deliverLoopFileStream(.{ .key = options.key, .op = .write_stream_close, .outcome = .rejected }, options.on_result, slot.generation, false);
|
|
7258
|
+
};
|
|
7259
|
+
}
|
|
7260
|
+
|
|
7261
|
+
fn failFileStreamOpen(self: *Self, index: usize, outcome: EffectFileOutcome) void {
|
|
7262
|
+
const slot = &self.file_stream_slots[index];
|
|
7263
|
+
const result: EffectFileResult = .{ .key = slot.key, .op = .write_stream_open, .outcome = outcome };
|
|
7264
|
+
const file_fn = slot.on_result;
|
|
7265
|
+
self.retireFileStream(slot);
|
|
7266
|
+
self.deliverLoopFile(result, file_fn);
|
|
7267
|
+
}
|
|
7268
|
+
|
|
7269
|
+
fn startFile(self: *Self, key: u64, op: EffectFileOp, access: *file_access.Resolved, bytes: []const u8, on_result: ?FileMsgFn) void {
|
|
6827
7270
|
self.reclaimSlots();
|
|
7271
|
+
defer access.deinit(self.allocator, self.ensureIo() catch unreachable);
|
|
7272
|
+
const file_path = access.path;
|
|
6828
7273
|
if (file_path.len == 0 or file_path.len > max_effect_file_path_bytes) {
|
|
6829
|
-
return self.
|
|
7274
|
+
return self.rejectFileExternal(key, op, on_result);
|
|
6830
7275
|
}
|
|
6831
7276
|
if (self.keyOccupiedUntilDelivery(key)) return self.rejectFile(key, op, on_result);
|
|
7277
|
+
if (self.takeTestFileFailure()) |outcome| {
|
|
7278
|
+
return self.deliverLoopFile(.{ .key = key, .op = op, .outcome = outcome }, on_result);
|
|
7279
|
+
}
|
|
6832
7280
|
const slot_index = self.findIdleSlot() orelse return self.rejectFile(key, op, on_result);
|
|
6833
7281
|
|
|
6834
7282
|
const slot = &self.slots[slot_index];
|
|
@@ -6839,9 +7287,14 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6839
7287
|
// delivery space the drain hands to `update`); a real
|
|
6840
7288
|
// worker's blocking phase gets its own process-lived copy
|
|
6841
7289
|
// in `FileWorkerContext.buffer` below.
|
|
6842
|
-
const buffer_len =
|
|
7290
|
+
const buffer_len = switch (op) {
|
|
7291
|
+
.write, .append => bytes.len,
|
|
7292
|
+
.read => max_effect_file_bytes + 1,
|
|
7293
|
+
.stat, .delete => 0,
|
|
7294
|
+
else => unreachable,
|
|
7295
|
+
};
|
|
6843
7296
|
const buffer = self.allocator.alloc(u8, buffer_len) catch {
|
|
6844
|
-
return self.
|
|
7297
|
+
return self.rejectFileExternal(key, op, on_result);
|
|
6845
7298
|
};
|
|
6846
7299
|
slot.generation = self.next_generation;
|
|
6847
7300
|
self.next_generation +%= 1;
|
|
@@ -6865,7 +7318,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6865
7318
|
}
|
|
6866
7319
|
if (slot.fetch_buffer) |old| self.allocator.free(old);
|
|
6867
7320
|
slot.fetch_buffer = buffer;
|
|
6868
|
-
if (op == .write) {
|
|
7321
|
+
if (op == .write or op == .append) {
|
|
6869
7322
|
@memcpy(buffer[0..bytes.len], bytes);
|
|
6870
7323
|
slot.payload_len = bytes.len;
|
|
6871
7324
|
} else {
|
|
@@ -6879,7 +7332,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6879
7332
|
|
|
6880
7333
|
const io = self.ensureIo() catch {
|
|
6881
7334
|
self.releaseFetchSlot(slot);
|
|
6882
|
-
return self.
|
|
7335
|
+
return self.rejectFileExternal(key, op, on_result);
|
|
6883
7336
|
};
|
|
6884
7337
|
// The worker's blocking phase touches only this out-of-line
|
|
6885
7338
|
// context (path copy, op, its own data buffer), never the
|
|
@@ -6893,28 +7346,36 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
6893
7346
|
// process-lifetime seam.
|
|
6894
7347
|
const ctx = process_allocator.create(FileWorkerContext) catch {
|
|
6895
7348
|
self.releaseFetchSlot(slot);
|
|
6896
|
-
return self.
|
|
7349
|
+
return self.rejectFileExternal(key, op, on_result);
|
|
6897
7350
|
};
|
|
6898
7351
|
const worker_buffer = process_allocator.alloc(u8, buffer_len) catch {
|
|
6899
7352
|
process_allocator.destroy(ctx);
|
|
6900
7353
|
self.releaseFetchSlot(slot);
|
|
6901
|
-
return self.
|
|
7354
|
+
return self.rejectFileExternal(key, op, on_result);
|
|
6902
7355
|
};
|
|
6903
7356
|
ctx.* = .{
|
|
6904
7357
|
.op = op,
|
|
6905
7358
|
.buffer = worker_buffer,
|
|
6906
7359
|
.payload_len = slot.payload_len,
|
|
7360
|
+
.parent = access.parent,
|
|
7361
|
+
.missing = access.missing,
|
|
6907
7362
|
};
|
|
6908
|
-
|
|
7363
|
+
access.parent = null;
|
|
7364
|
+
if (op == .write or op == .append) @memcpy(worker_buffer[0..bytes.len], bytes);
|
|
6909
7365
|
@memcpy(ctx.path_storage[0..file_path.len], file_path);
|
|
6910
7366
|
ctx.path_len = file_path.len;
|
|
7367
|
+
if (access.basename.len > 0) {
|
|
7368
|
+
@memcpy(ctx.basename_storage[0..access.basename.len], access.basename);
|
|
7369
|
+
ctx.basename_len = access.basename.len;
|
|
7370
|
+
}
|
|
6911
7371
|
slot.file_ctx = ctx;
|
|
6912
7372
|
const thread = std.Thread.spawn(.{}, fileWorkerMain, .{ self, slot_index, slot.generation, io, ctx }) catch {
|
|
7373
|
+
if (ctx.parent) |parent| parent.close(io);
|
|
6913
7374
|
process_allocator.free(worker_buffer);
|
|
6914
7375
|
process_allocator.destroy(ctx);
|
|
6915
7376
|
slot.file_ctx = null;
|
|
6916
7377
|
self.releaseFetchSlot(slot);
|
|
6917
|
-
return self.
|
|
7378
|
+
return self.rejectFileExternal(key, op, on_result);
|
|
6918
7379
|
};
|
|
6919
7380
|
slot.worker_thread = thread;
|
|
6920
7381
|
}
|
|
@@ -9556,6 +10017,28 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
9556
10017
|
/// Host-request slots keep their own contract instead: a cancel
|
|
9557
10018
|
/// aimed at one drops it silently (`cancelHostRequest`).
|
|
9558
10019
|
pub fn cancel(self: *Self, key: u64) void {
|
|
10020
|
+
if (self.findFileStream(key)) |stream_index| {
|
|
10021
|
+
const stream = &self.file_stream_slots[stream_index];
|
|
10022
|
+
if (stream.op == .read_stream) {
|
|
10023
|
+
// File-style reads cancel silently. A queued chunk from
|
|
10024
|
+
// the retired generation is generation-filtered at drain.
|
|
10025
|
+
self.retireFileStream(stream);
|
|
10026
|
+
return;
|
|
10027
|
+
}
|
|
10028
|
+
if (stream.fake and self.replay) {
|
|
10029
|
+
// The recorded `.cancelled` terminal is the executor
|
|
10030
|
+
// truth under replay. Keep the fake sink parked so that
|
|
10031
|
+
// record can feed and retire this exact occupancy.
|
|
10032
|
+
stream.cancelling = true;
|
|
10033
|
+
return;
|
|
10034
|
+
}
|
|
10035
|
+
const file_fn = stream.on_result;
|
|
10036
|
+
const op = stream.op;
|
|
10037
|
+
const key_copy = stream.key;
|
|
10038
|
+
self.retireFileStream(stream);
|
|
10039
|
+
self.deliverLoopFile(.{ .key = key_copy, .op = op, .outcome = .cancelled }, file_fn);
|
|
10040
|
+
return;
|
|
10041
|
+
}
|
|
9559
10042
|
const slot_index = self.findActiveSlot(key) orelse {
|
|
9560
10043
|
// The worker may have finished with its exit still in
|
|
9561
10044
|
// the queue: mark the finished generation cancelled so
|
|
@@ -10850,6 +11333,9 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
10850
11333
|
pub fn activeCount(self: *Self) usize {
|
|
10851
11334
|
self.reclaimSlots();
|
|
10852
11335
|
var count: usize = 0;
|
|
11336
|
+
for (&self.file_stream_slots) |*slot| {
|
|
11337
|
+
if (slot.state != .idle) count += 1;
|
|
11338
|
+
}
|
|
10853
11339
|
for (&self.slots) |*slot| {
|
|
10854
11340
|
if (slot.state.load(.acquire) == .running) count += 1;
|
|
10855
11341
|
}
|
|
@@ -11035,6 +11521,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
11035
11521
|
return response_fn(entry.response);
|
|
11036
11522
|
},
|
|
11037
11523
|
.file => |entry| {
|
|
11524
|
+
if (entry.stream_generation != 0) self.advanceFileStreamAfterDelivery(entry.result, entry.stream_generation);
|
|
11038
11525
|
const file_fn = entry.file_fn orelse continue;
|
|
11039
11526
|
self.journalNote(.{
|
|
11040
11527
|
.kind = .file,
|
|
@@ -11042,7 +11529,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
11042
11529
|
.payload = entry.result.bytes,
|
|
11043
11530
|
.dropped = entry.result.dropped_before,
|
|
11044
11531
|
.file_op = entry.result.op,
|
|
11532
|
+
.file_event = entry.result.event,
|
|
11045
11533
|
.file_outcome = entry.result.outcome,
|
|
11534
|
+
.file_total = entry.result.total,
|
|
11535
|
+
.file_mtime_ms = entry.result.mtime_ms,
|
|
11536
|
+
.file_exists = entry.result.exists,
|
|
11537
|
+
.file_rejected_admission = entry.rejected_admission,
|
|
11046
11538
|
});
|
|
11047
11539
|
return file_fn(entry.result);
|
|
11048
11540
|
},
|
|
@@ -11497,6 +11989,49 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
11497
11989
|
return response_fn(response);
|
|
11498
11990
|
},
|
|
11499
11991
|
.file => {
|
|
11992
|
+
if (entry.file_stream_generation != 0) {
|
|
11993
|
+
if (self.drain_heap_line) |old| {
|
|
11994
|
+
self.allocator.free(old);
|
|
11995
|
+
self.drain_heap_line = null;
|
|
11996
|
+
}
|
|
11997
|
+
if (entry.heap_line) |heap| {
|
|
11998
|
+
self.drain_heap_line = heap;
|
|
11999
|
+
entry.heap_line = null;
|
|
12000
|
+
}
|
|
12001
|
+
const stream_index = self.findFileStream(entry.key) orelse continue;
|
|
12002
|
+
const stream = &self.file_stream_slots[stream_index];
|
|
12003
|
+
if (stream.generation != entry.file_stream_generation) continue;
|
|
12004
|
+
if (stream.worker_thread) |thread| {
|
|
12005
|
+
thread.join();
|
|
12006
|
+
stream.worker_thread = null;
|
|
12007
|
+
}
|
|
12008
|
+
if (stream.worker_ctx) |ctx| {
|
|
12009
|
+
destroyFileStreamContext(ctx, self.ensureIo() catch unreachable);
|
|
12010
|
+
stream.worker_ctx = null;
|
|
12011
|
+
}
|
|
12012
|
+
const bytes: []const u8 = if (self.drain_heap_line) |heap| heap[0..entry.line_len] else "";
|
|
12013
|
+
const result: EffectFileResult = .{
|
|
12014
|
+
.key = entry.key,
|
|
12015
|
+
.op = entry.file_op,
|
|
12016
|
+
.event = entry.file_event,
|
|
12017
|
+
.outcome = entry.file_outcome,
|
|
12018
|
+
.bytes = bytes,
|
|
12019
|
+
.total = entry.file_total,
|
|
12020
|
+
};
|
|
12021
|
+
self.advanceFileStreamAfterDelivery(result, entry.file_stream_generation);
|
|
12022
|
+
self.journalNote(.{
|
|
12023
|
+
.kind = .file,
|
|
12024
|
+
.key = result.key,
|
|
12025
|
+
.payload = result.bytes,
|
|
12026
|
+
.file_op = result.op,
|
|
12027
|
+
.file_event = result.event,
|
|
12028
|
+
.file_outcome = result.outcome,
|
|
12029
|
+
.file_total = result.total,
|
|
12030
|
+
.file_rejected_admission = entry.file_rejected_admission,
|
|
12031
|
+
});
|
|
12032
|
+
const file_fn = entry.file_fn orelse continue;
|
|
12033
|
+
return file_fn(result);
|
|
12034
|
+
}
|
|
11500
12035
|
// One terminal per file occupancy, mirroring
|
|
11501
12036
|
// `.response`: a mismatched generation means the
|
|
11502
12037
|
// occupant was already retired.
|
|
@@ -11526,8 +12061,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
11526
12061
|
.{
|
|
11527
12062
|
.key = entry.key,
|
|
11528
12063
|
.op = entry.file_op,
|
|
12064
|
+
.event = entry.file_event,
|
|
11529
12065
|
.outcome = entry.file_outcome,
|
|
11530
12066
|
.bytes = bytes,
|
|
12067
|
+
.total = entry.file_total,
|
|
12068
|
+
.mtime_ms = entry.file_mtime_ms,
|
|
12069
|
+
.exists = entry.file_exists,
|
|
11531
12070
|
.dropped_before = entry.dropped_before,
|
|
11532
12071
|
};
|
|
11533
12072
|
self.journalNote(.{
|
|
@@ -11536,7 +12075,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
11536
12075
|
.payload = result.bytes,
|
|
11537
12076
|
.dropped = result.dropped_before,
|
|
11538
12077
|
.file_op = result.op,
|
|
12078
|
+
.file_event = result.event,
|
|
11539
12079
|
.file_outcome = result.outcome,
|
|
12080
|
+
.file_total = result.total,
|
|
12081
|
+
.file_mtime_ms = result.mtime_ms,
|
|
12082
|
+
.file_exists = result.exists,
|
|
12083
|
+
.file_rejected_admission = entry.file_rejected_admission,
|
|
11540
12084
|
});
|
|
11541
12085
|
return file_fn(result);
|
|
11542
12086
|
},
|
|
@@ -12474,6 +13018,34 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
12474
13018
|
/// lands through the pending ring — a read whose bytes were
|
|
12475
13019
|
/// lost that way reports `.truncated`, never silently.
|
|
12476
13020
|
pub fn feedFileResult(self: *Self, key: u64, outcome: EffectFileOutcome, bytes: []const u8) error{EffectNotFound}!void {
|
|
13021
|
+
const slot_index = self.findActiveFakeSlot(key, .file) orelse return error.EffectNotFound;
|
|
13022
|
+
return self.feedFileResultDetailed(.{
|
|
13023
|
+
.key = key,
|
|
13024
|
+
.op = self.slots[slot_index].file_op,
|
|
13025
|
+
.outcome = outcome,
|
|
13026
|
+
.bytes = bytes,
|
|
13027
|
+
});
|
|
13028
|
+
}
|
|
13029
|
+
|
|
13030
|
+
pub const FeedFileResult = struct {
|
|
13031
|
+
key: u64,
|
|
13032
|
+
op: EffectFileOp,
|
|
13033
|
+
event: EffectFileEvent = .terminal,
|
|
13034
|
+
outcome: EffectFileOutcome,
|
|
13035
|
+
bytes: []const u8 = "",
|
|
13036
|
+
total: u64 = 0,
|
|
13037
|
+
mtime_ms: i64 = 0,
|
|
13038
|
+
exists: bool = false,
|
|
13039
|
+
};
|
|
13040
|
+
|
|
13041
|
+
pub fn feedFileResultDetailed(self: *Self, fed: FeedFileResult) error{EffectNotFound}!void {
|
|
13042
|
+
// Streaming-family replay/fakes park in their own table.
|
|
13043
|
+
if (fed.op == .read_stream or fed.op == .write_stream_open or fed.op == .write_stream_chunk or fed.op == .write_stream_close) {
|
|
13044
|
+
return self.feedFileStreamResult(fed);
|
|
13045
|
+
}
|
|
13046
|
+
const key = fed.key;
|
|
13047
|
+
const outcome = fed.outcome;
|
|
13048
|
+
const bytes = fed.bytes;
|
|
12477
13049
|
const slot_index = self.findActiveFakeSlot(key, .file) orelse return error.EffectNotFound;
|
|
12478
13050
|
const slot = &self.slots[slot_index];
|
|
12479
13051
|
const buffer = slot.fetch_buffer orelse return error.EffectNotFound;
|
|
@@ -12492,8 +13064,12 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
12492
13064
|
.generation = slot.generation,
|
|
12493
13065
|
.key = slot.key,
|
|
12494
13066
|
.line_len = @intCast(delivered_len),
|
|
12495
|
-
.file_op =
|
|
13067
|
+
.file_op = fed.op,
|
|
13068
|
+
.file_event = fed.event,
|
|
12496
13069
|
.file_outcome = delivered_outcome,
|
|
13070
|
+
.file_total = fed.total,
|
|
13071
|
+
.file_mtime_ms = fed.mtime_ms,
|
|
13072
|
+
.file_exists = fed.exists,
|
|
12497
13073
|
.file_fn = slot.on_file,
|
|
12498
13074
|
};
|
|
12499
13075
|
slot.state.store(.draining, .release);
|
|
@@ -12510,6 +13086,54 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
12510
13086
|
self.wakeHost();
|
|
12511
13087
|
}
|
|
12512
13088
|
|
|
13089
|
+
fn feedFileStreamResult(self: *Self, fed: FeedFileResult) error{EffectNotFound}!void {
|
|
13090
|
+
const index = self.findFileStream(fed.key) orelse return error.EffectNotFound;
|
|
13091
|
+
const slot = &self.file_stream_slots[index];
|
|
13092
|
+
if (!slot.fake) return error.EffectNotFound;
|
|
13093
|
+
if (fed.op == .read_stream) {
|
|
13094
|
+
if (slot.state != .reading) return error.EffectNotFound;
|
|
13095
|
+
if (fed.event == .chunk) {
|
|
13096
|
+
if (fed.bytes.len > effect_file_stream_chunk_bytes) return error.EffectNotFound;
|
|
13097
|
+
const copy = self.allocator.dupe(u8, fed.bytes) catch return error.EffectNotFound;
|
|
13098
|
+
var entry: Entry = .{
|
|
13099
|
+
.kind = .file,
|
|
13100
|
+
.slot_index = @intCast(index),
|
|
13101
|
+
.key = fed.key,
|
|
13102
|
+
.line_len = @intCast(copy.len),
|
|
13103
|
+
.file_op = fed.op,
|
|
13104
|
+
.file_event = fed.event,
|
|
13105
|
+
.file_outcome = fed.outcome,
|
|
13106
|
+
.file_total = fed.total,
|
|
13107
|
+
.file_stream_generation = slot.generation,
|
|
13108
|
+
.file_fn = slot.on_result,
|
|
13109
|
+
.heap_line = copy,
|
|
13110
|
+
};
|
|
13111
|
+
if (!self.enqueue(&entry)) {
|
|
13112
|
+
self.allocator.free(copy);
|
|
13113
|
+
return error.EffectNotFound;
|
|
13114
|
+
}
|
|
13115
|
+
self.wakeHost();
|
|
13116
|
+
return;
|
|
13117
|
+
}
|
|
13118
|
+
self.deliverLoopFileStream(.{
|
|
13119
|
+
.key = fed.key,
|
|
13120
|
+
.op = fed.op,
|
|
13121
|
+
.event = fed.event,
|
|
13122
|
+
.outcome = fed.outcome,
|
|
13123
|
+
.total = fed.total,
|
|
13124
|
+
}, slot.on_result, slot.generation, false);
|
|
13125
|
+
return;
|
|
13126
|
+
}
|
|
13127
|
+
if (fed.op != slot.op) return error.EffectNotFound;
|
|
13128
|
+
self.deliverLoopFileStream(.{
|
|
13129
|
+
.key = fed.key,
|
|
13130
|
+
.op = fed.op,
|
|
13131
|
+
.event = fed.event,
|
|
13132
|
+
.outcome = fed.outcome,
|
|
13133
|
+
.total = fed.total,
|
|
13134
|
+
}, slot.on_result, slot.generation, false);
|
|
13135
|
+
}
|
|
13136
|
+
|
|
12513
13137
|
/// Number of recorded (still-active) fake clipboard requests.
|
|
12514
13138
|
pub fn pendingClipboardCount(self: *Self) usize {
|
|
12515
13139
|
var count: usize = 0;
|
|
@@ -12967,6 +13591,17 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
12967
13591
|
}
|
|
12968
13592
|
|
|
12969
13593
|
fn rejectFile(self: *Self, key: u64, op: EffectFileOp, file_fn: ?FileMsgFn) void {
|
|
13594
|
+
self.deliverLoopFileAdmission(.{
|
|
13595
|
+
.key = key,
|
|
13596
|
+
.op = op,
|
|
13597
|
+
.outcome = .rejected,
|
|
13598
|
+
}, file_fn);
|
|
13599
|
+
}
|
|
13600
|
+
|
|
13601
|
+
/// Queue a rejection that replay cannot re-derive (path-policy or
|
|
13602
|
+
/// executor/resource truth). Unlike deterministic admission gates,
|
|
13603
|
+
/// the journal must feed this terminal to retire replay's fake slot.
|
|
13604
|
+
fn rejectFileExternal(self: *Self, key: u64, op: EffectFileOp, file_fn: ?FileMsgFn) void {
|
|
12970
13605
|
self.deliverLoopFile(.{
|
|
12971
13606
|
.key = key,
|
|
12972
13607
|
.op = op,
|
|
@@ -13874,8 +14509,19 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
13874
14509
|
/// (file rejections, fake cancels, feed fallbacks) for the next
|
|
13875
14510
|
/// drain. Bytes here are always empty.
|
|
13876
14511
|
fn deliverLoopFile(self: *Self, result: EffectFileResult, file_fn: ?FileMsgFn) void {
|
|
13877
|
-
|
|
13878
|
-
|
|
14512
|
+
self.deliverLoopFileStream(result, file_fn, 0, false);
|
|
14513
|
+
}
|
|
14514
|
+
|
|
14515
|
+
fn deliverLoopFileAdmission(self: *Self, result: EffectFileResult, file_fn: ?FileMsgFn) void {
|
|
14516
|
+
self.deliverLoopFileStream(result, file_fn, 0, true);
|
|
14517
|
+
}
|
|
14518
|
+
|
|
14519
|
+
fn deliverLoopFileStream(self: *Self, result: EffectFileResult, file_fn: ?FileMsgFn, stream_generation: u64, rejected_admission: bool) void {
|
|
14520
|
+
if (file_fn == null) {
|
|
14521
|
+
if (stream_generation != 0) self.advanceFileStreamAfterDelivery(result, stream_generation);
|
|
14522
|
+
return;
|
|
14523
|
+
}
|
|
14524
|
+
self.deliverPending(.{ .file = .{ .result = result, .file_fn = file_fn, .stream_generation = stream_generation, .rejected_admission = rejected_admission } });
|
|
13879
14525
|
}
|
|
13880
14526
|
|
|
13881
14527
|
/// Push onto the loop-side pending ring. When the ring is full
|
|
@@ -14527,6 +15173,147 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
14527
15173
|
return null;
|
|
14528
15174
|
}
|
|
14529
15175
|
|
|
15176
|
+
fn findIdleFileStream(self: *Self) ?usize {
|
|
15177
|
+
for (&self.file_stream_slots, 0..) |*slot, index| {
|
|
15178
|
+
if (slot.state == .idle) return index;
|
|
15179
|
+
}
|
|
15180
|
+
return null;
|
|
15181
|
+
}
|
|
15182
|
+
|
|
15183
|
+
fn findFileStream(self: *Self, key: u64) ?usize {
|
|
15184
|
+
for (&self.file_stream_slots, 0..) |*slot, index| {
|
|
15185
|
+
if (slot.state != .idle and slot.key == key) return index;
|
|
15186
|
+
}
|
|
15187
|
+
return null;
|
|
15188
|
+
}
|
|
15189
|
+
|
|
15190
|
+
fn mintFileStreamGeneration(self: *Self) u64 {
|
|
15191
|
+
const generation = self.next_file_stream_generation;
|
|
15192
|
+
self.next_file_stream_generation +%= 1;
|
|
15193
|
+
if (self.next_file_stream_generation == 0) self.next_file_stream_generation = 1;
|
|
15194
|
+
return generation;
|
|
15195
|
+
}
|
|
15196
|
+
|
|
15197
|
+
fn retireFileStream(self: *Self, slot: *FileStreamSlot) void {
|
|
15198
|
+
slot.cancelled.store(true, .release);
|
|
15199
|
+
if (slot.worker_thread) |thread| {
|
|
15200
|
+
const ctx = slot.worker_ctx;
|
|
15201
|
+
const io = self.ensureIo() catch null;
|
|
15202
|
+
const deadline_ms = self.file_join_deadline_ms;
|
|
15203
|
+
var waited_ms: u64 = 0;
|
|
15204
|
+
while (ctx != null and !ctx.?.done.load(.acquire) and waited_ms < deadline_ms) : (waited_ms += 1) {
|
|
15205
|
+
if (self.file_join_interrupt and waited_ms == deadline_ms / 2) ctx.?.interrupt.store(true, .release);
|
|
15206
|
+
if (io) |value| std.Io.sleep(value, std.Io.Duration.fromMilliseconds(1), .awake) catch break;
|
|
15207
|
+
}
|
|
15208
|
+
var abandoned = false;
|
|
15209
|
+
if (ctx) |worker_ctx| {
|
|
15210
|
+
worker_ctx.mutex.lock();
|
|
15211
|
+
if (!worker_ctx.committed and !worker_ctx.done.load(.acquire)) {
|
|
15212
|
+
worker_ctx.abandoned = true;
|
|
15213
|
+
abandoned = true;
|
|
15214
|
+
}
|
|
15215
|
+
worker_ctx.mutex.unlock();
|
|
15216
|
+
}
|
|
15217
|
+
if (abandoned) {
|
|
15218
|
+
thread.detach();
|
|
15219
|
+
slot.worker_thread = null;
|
|
15220
|
+
slot.worker_ctx = null;
|
|
15221
|
+
self.abandoned_file_workers += 1;
|
|
15222
|
+
} else {
|
|
15223
|
+
thread.join();
|
|
15224
|
+
slot.worker_thread = null;
|
|
15225
|
+
if (slot.worker_ctx) |worker_ctx| {
|
|
15226
|
+
if (io) |value| destroyFileStreamContext(worker_ctx, value);
|
|
15227
|
+
slot.worker_ctx = null;
|
|
15228
|
+
}
|
|
15229
|
+
}
|
|
15230
|
+
}
|
|
15231
|
+
if (slot.atomic) |*atomic| {
|
|
15232
|
+
const io = self.ensureIo() catch null;
|
|
15233
|
+
if (io) |value| atomic.deinit(value);
|
|
15234
|
+
slot.atomic = null;
|
|
15235
|
+
}
|
|
15236
|
+
if (slot.read_file) |file| {
|
|
15237
|
+
if (self.ensureIo() catch null) |value| file.close(value);
|
|
15238
|
+
slot.read_file = null;
|
|
15239
|
+
}
|
|
15240
|
+
if (slot.atomic_dest) |dest| {
|
|
15241
|
+
process_allocator.free(dest);
|
|
15242
|
+
slot.atomic_dest = null;
|
|
15243
|
+
}
|
|
15244
|
+
if (slot.parent) |parent| {
|
|
15245
|
+
if (self.ensureIo() catch null) |value| parent.close(value);
|
|
15246
|
+
slot.parent = null;
|
|
15247
|
+
}
|
|
15248
|
+
if (slot.chunk) |bytes| {
|
|
15249
|
+
self.allocator.free(bytes);
|
|
15250
|
+
slot.chunk = null;
|
|
15251
|
+
}
|
|
15252
|
+
slot.* = .{};
|
|
15253
|
+
}
|
|
15254
|
+
|
|
15255
|
+
fn advanceFileStreamAfterDelivery(self: *Self, result: EffectFileResult, generation: u64) void {
|
|
15256
|
+
const index = self.findFileStream(result.key) orelse return;
|
|
15257
|
+
const slot = &self.file_stream_slots[index];
|
|
15258
|
+
if (slot.generation != generation) return;
|
|
15259
|
+
if (result.op == .read_stream) {
|
|
15260
|
+
if (result.event == .chunk and result.outcome == .ok) {
|
|
15261
|
+
slot.chunk_pending = false;
|
|
15262
|
+
if (!slot.fake) self.startReadFileStreamChunk(index);
|
|
15263
|
+
} else {
|
|
15264
|
+
self.retireFileStream(slot);
|
|
15265
|
+
}
|
|
15266
|
+
return;
|
|
15267
|
+
}
|
|
15268
|
+
if (result.op == .write_stream_open) {
|
|
15269
|
+
slot.chunk_pending = false;
|
|
15270
|
+
slot.state = .sink_open;
|
|
15271
|
+
if (result.outcome != .ok) self.retireFileStream(slot);
|
|
15272
|
+
} else if (result.op == .write_stream_chunk) {
|
|
15273
|
+
slot.chunk_pending = false;
|
|
15274
|
+
if (slot.chunk) |bytes| {
|
|
15275
|
+
self.allocator.free(bytes);
|
|
15276
|
+
slot.chunk = null;
|
|
15277
|
+
}
|
|
15278
|
+
slot.state = .sink_open;
|
|
15279
|
+
if (result.outcome != .ok and result.outcome != .rejected and result.outcome != .out_of_order) {
|
|
15280
|
+
self.retireFileStream(slot);
|
|
15281
|
+
}
|
|
15282
|
+
} else if (result.op == .write_stream_close) {
|
|
15283
|
+
self.retireFileStream(slot);
|
|
15284
|
+
}
|
|
15285
|
+
}
|
|
15286
|
+
|
|
15287
|
+
/// Test/replay seam: clear the open acknowledgment so the fake sink
|
|
15288
|
+
/// can accept its first chunk before that acknowledgment is fed.
|
|
15289
|
+
pub fn acknowledgeFakeFileStreamOpen(self: *Self, key: u64) error{EffectNotFound}!void {
|
|
15290
|
+
const index = self.findFileStream(key) orelse return error.EffectNotFound;
|
|
15291
|
+
const slot = &self.file_stream_slots[index];
|
|
15292
|
+
if (!slot.fake or slot.op != .write_stream_open) return error.EffectNotFound;
|
|
15293
|
+
slot.chunk_pending = false;
|
|
15294
|
+
}
|
|
15295
|
+
|
|
15296
|
+
fn startReadFileStreamChunk(self: *Self, index: usize) void {
|
|
15297
|
+
const slot = &self.file_stream_slots[index];
|
|
15298
|
+
if (slot.state != .reading or slot.chunk_pending or slot.worker_thread != null) return;
|
|
15299
|
+
slot.chunk_pending = true;
|
|
15300
|
+
const io = self.ensureIo() catch {
|
|
15301
|
+
return self.deliverLoopFileStream(.{ .key = slot.key, .op = .read_stream, .outcome = .io_failed }, slot.on_result, slot.generation, false);
|
|
15302
|
+
};
|
|
15303
|
+
const ctx = self.createFileStreamContext(slot) orelse {
|
|
15304
|
+
slot.chunk_pending = false;
|
|
15305
|
+
return self.deliverLoopFileStream(.{ .key = slot.key, .op = .read_stream, .outcome = .rejected }, slot.on_result, slot.generation, false);
|
|
15306
|
+
};
|
|
15307
|
+
slot.worker_ctx = ctx;
|
|
15308
|
+
slot.worker_thread = std.Thread.spawn(.{}, fileStreamWorkerMain, .{ self, index, slot.generation, io, ctx }) catch {
|
|
15309
|
+
slot.worker_ctx = null;
|
|
15310
|
+
moveFileStreamResourcesToSlot(slot, ctx);
|
|
15311
|
+
destroyFileStreamContext(ctx, io);
|
|
15312
|
+
slot.chunk_pending = false;
|
|
15313
|
+
return self.deliverLoopFileStream(.{ .key = slot.key, .op = .read_stream, .outcome = .rejected }, slot.on_result, slot.generation, false);
|
|
15314
|
+
};
|
|
15315
|
+
}
|
|
15316
|
+
|
|
14530
15317
|
fn findIdleStoreSlot(self: *Self) ?usize {
|
|
14531
15318
|
for (self.slots[max_effects .. max_effects + max_store_effects], max_effects..) |*slot, index| {
|
|
14532
15319
|
if (slot.state.load(.acquire) == .idle) return index;
|
|
@@ -14563,6 +15350,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
14563
15350
|
/// update — so reissuing a key from its own terminal handler
|
|
14564
15351
|
/// is always accepted.
|
|
14565
15352
|
fn keyOccupiedUntilDelivery(self: *Self, key: u64) bool {
|
|
15353
|
+
if (self.findFileStream(key) != null) return true;
|
|
14566
15354
|
if (self.findActiveSlot(key) != null) return true;
|
|
14567
15355
|
if (self.findUndeliveredTerminalSlot(key) != null) return true;
|
|
14568
15356
|
if (self.stagedImageOccupiesKey(key)) return true;
|
|
@@ -15551,6 +16339,10 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
15551
16339
|
};
|
|
15552
16340
|
superviseWorkerTask(io, &future, &ctx.done, &ctx.interrupt);
|
|
15553
16341
|
}
|
|
16342
|
+
if (ctx.parent) |parent| {
|
|
16343
|
+
parent.close(io);
|
|
16344
|
+
ctx.parent = null;
|
|
16345
|
+
}
|
|
15554
16346
|
ctx.mutex.lock();
|
|
15555
16347
|
if (ctx.abandoned) {
|
|
15556
16348
|
// Teardown gave up on this worker: the channel may
|
|
@@ -15581,7 +16373,13 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
15581
16373
|
}
|
|
15582
16374
|
}
|
|
15583
16375
|
slot.body_len = ctx.read_len;
|
|
15584
|
-
self.
|
|
16376
|
+
self.postFileDetailed(slot, @intCast(slot_index), generation, io, .{
|
|
16377
|
+
.outcome = outcome,
|
|
16378
|
+
.read_len = ctx.read_len,
|
|
16379
|
+
.total = ctx.stat_size,
|
|
16380
|
+
.mtime_ms = ctx.stat_mtime_ms,
|
|
16381
|
+
.exists = ctx.stat_exists,
|
|
16382
|
+
});
|
|
15585
16383
|
slot.state.store(.draining, .release);
|
|
15586
16384
|
self.wakeHost();
|
|
15587
16385
|
}
|
|
@@ -15616,24 +16414,62 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
15616
16414
|
}
|
|
15617
16415
|
|
|
15618
16416
|
fn runFileOp(ctx: *FileWorkerContext, io: std.Io) EffectFileOutcome {
|
|
15619
|
-
|
|
15620
|
-
const
|
|
16417
|
+
if (ctx.missing) return if (ctx.op == .stat) .ok else .not_found;
|
|
16418
|
+
const dir = ctx.parent orelse std.Io.Dir.cwd();
|
|
16419
|
+
const file_path = if (ctx.parent != null) ctx.basename() else ctx.path();
|
|
15621
16420
|
switch (ctx.op) {
|
|
15622
16421
|
.write => {
|
|
15623
|
-
if (
|
|
15624
|
-
|
|
16422
|
+
if (ctx.parent == null) {
|
|
16423
|
+
if (std.fs.path.dirname(file_path)) |parent| {
|
|
16424
|
+
dir.createDirPath(io, parent) catch |err| return fileOpFailure(err);
|
|
16425
|
+
}
|
|
16426
|
+
dir.writeFile(io, .{ .sub_path = file_path, .data = ctx.payload() }) catch |err| return fileOpFailure(err);
|
|
16427
|
+
return .ok;
|
|
16428
|
+
}
|
|
16429
|
+
var atomic = dir.createFileAtomic(io, file_path, .{ .make_path = ctx.parent == null, .replace = true }) catch |err| return fileOpFailure(err);
|
|
16430
|
+
defer atomic.deinit(io);
|
|
16431
|
+
atomic.file.writePositionalAll(io, ctx.payload(), 0) catch |err| return fileOpFailure(err);
|
|
16432
|
+
atomic.file.sync(io) catch |err| return fileOpFailure(err);
|
|
16433
|
+
atomic.replace(io) catch |err| return fileOpFailure(err);
|
|
16434
|
+
return .ok;
|
|
16435
|
+
},
|
|
16436
|
+
.append => {
|
|
16437
|
+
if (ctx.parent == null) {
|
|
16438
|
+
if (std.fs.path.dirname(file_path)) |parent| {
|
|
16439
|
+
dir.createDirPath(io, parent) catch |err| return fileOpFailure(err);
|
|
16440
|
+
}
|
|
16441
|
+
}
|
|
16442
|
+
var file = dir.openFile(io, file_path, .{
|
|
16443
|
+
.mode = .read_write,
|
|
16444
|
+
.allow_directory = false,
|
|
16445
|
+
.follow_symlinks = false,
|
|
16446
|
+
.resolve_beneath = ctx.parent != null,
|
|
16447
|
+
.lock = .exclusive,
|
|
16448
|
+
}) catch |err| switch (err) {
|
|
16449
|
+
error.FileNotFound => dir.createFile(io, file_path, .{
|
|
16450
|
+
.read = true,
|
|
16451
|
+
.truncate = false,
|
|
16452
|
+
.exclusive = true,
|
|
16453
|
+
.lock = .exclusive,
|
|
16454
|
+
.resolve_beneath = ctx.parent != null,
|
|
16455
|
+
}) catch |create_err| return fileOpFailure(create_err),
|
|
16456
|
+
else => return fileOpFailure(err),
|
|
16457
|
+
};
|
|
16458
|
+
defer file.close(io);
|
|
16459
|
+
const stat = file.stat(io) catch |err| return fileOpFailure(err);
|
|
16460
|
+
if (comptime builtin.os.tag == .windows) {
|
|
16461
|
+
appendFileWindows(file, io, ctx.payload()) catch |err| return fileOpFailure(err);
|
|
16462
|
+
} else {
|
|
16463
|
+
file.writePositionalAll(io, ctx.payload(), stat.size) catch |err| return fileOpFailure(err);
|
|
15625
16464
|
}
|
|
15626
|
-
cwd.writeFile(io, .{
|
|
15627
|
-
.sub_path = file_path,
|
|
15628
|
-
.data = ctx.payload(),
|
|
15629
|
-
}) catch |err| return fileOpFailure(err);
|
|
15630
16465
|
return .ok;
|
|
15631
16466
|
},
|
|
15632
16467
|
.read => {
|
|
15633
|
-
var file =
|
|
16468
|
+
var file = dir.openFile(io, file_path, .{ .resolve_beneath = ctx.parent != null }) catch |err| {
|
|
15634
16469
|
return if (err == error.FileNotFound) .not_found else fileOpFailure(err);
|
|
15635
16470
|
};
|
|
15636
16471
|
defer file.close(io);
|
|
16472
|
+
if (ctx.parent != null and !openedFileIsInsideDir(io, dir, file)) return .io_failed;
|
|
15637
16473
|
// The buffer has one byte past the delivery bound:
|
|
15638
16474
|
// filling it proves the file is over-bound.
|
|
15639
16475
|
const len = file.readPositionalAll(io, ctx.buffer, 0) catch |err| return fileOpFailure(err);
|
|
@@ -15644,6 +16480,29 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
15644
16480
|
ctx.read_len = len;
|
|
15645
16481
|
return .ok;
|
|
15646
16482
|
},
|
|
16483
|
+
.stat => {
|
|
16484
|
+
var file = dir.openFile(io, file_path, .{ .path_only = true, .resolve_beneath = ctx.parent != null }) catch |err| {
|
|
16485
|
+
if (err == error.FileNotFound) {
|
|
16486
|
+
ctx.stat_exists = false;
|
|
16487
|
+
return .ok;
|
|
16488
|
+
}
|
|
16489
|
+
return fileOpFailure(err);
|
|
16490
|
+
};
|
|
16491
|
+
defer file.close(io);
|
|
16492
|
+
if (ctx.parent != null and !openedFileIsInsideDir(io, dir, file)) return .io_failed;
|
|
16493
|
+
const stat = file.stat(io) catch |err| return fileOpFailure(err);
|
|
16494
|
+
ctx.stat_exists = true;
|
|
16495
|
+
ctx.stat_size = stat.size;
|
|
16496
|
+
ctx.stat_mtime_ms = @intCast(@divFloor(stat.mtime.nanoseconds, std.time.ns_per_ms));
|
|
16497
|
+
return .ok;
|
|
16498
|
+
},
|
|
16499
|
+
.delete => {
|
|
16500
|
+
dir.deleteFile(io, file_path) catch |err| {
|
|
16501
|
+
return if (err == error.FileNotFound) .not_found else fileOpFailure(err);
|
|
16502
|
+
};
|
|
16503
|
+
return .ok;
|
|
16504
|
+
},
|
|
16505
|
+
else => unreachable,
|
|
15647
16506
|
}
|
|
15648
16507
|
}
|
|
15649
16508
|
|
|
@@ -15651,7 +16510,375 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
15651
16510
|
/// teardown cancels the task); every other failure stays the
|
|
15652
16511
|
/// blanket `.io_failed`.
|
|
15653
16512
|
fn fileOpFailure(err: anyerror) EffectFileOutcome {
|
|
15654
|
-
return
|
|
16513
|
+
return switch (err) {
|
|
16514
|
+
error.Canceled => .cancelled,
|
|
16515
|
+
error.NoSpaceLeft, error.DiskQuota => .disk_full,
|
|
16516
|
+
else => .io_failed,
|
|
16517
|
+
};
|
|
16518
|
+
}
|
|
16519
|
+
|
|
16520
|
+
/// Windows' Zig 0.16 threaded positional writer can surface
|
|
16521
|
+
/// STATUS_PENDING for the asynchronous handle `follow_symlinks=false`
|
|
16522
|
+
/// deliberately opens, while its streaming writer rejects the same
|
|
16523
|
+
/// post-seek handle. NT defines `ByteOffset = -1` as the atomic
|
|
16524
|
+
/// write-to-end sentinel. Give every write its own completion event:
|
|
16525
|
+
/// neither the stack IOSB nor the caller's payload may die while the
|
|
16526
|
+
/// kernel can still reach them. The handle is already exclusively
|
|
16527
|
+
/// locked by the caller, and this loop preserves the
|
|
16528
|
+
/// full-payload-or-error contract.
|
|
16529
|
+
fn appendFileWindows(file: std.Io.File, io: std.Io, bytes: []const u8) !void {
|
|
16530
|
+
if (comptime builtin.os.tag != .windows) unreachable;
|
|
16531
|
+
const windows = std.os.windows;
|
|
16532
|
+
var at: usize = 0;
|
|
16533
|
+
while (at < bytes.len) {
|
|
16534
|
+
var event: windows.HANDLE = undefined;
|
|
16535
|
+
switch (windows.ntdll.NtCreateEvent(
|
|
16536
|
+
&event,
|
|
16537
|
+
.{
|
|
16538
|
+
.SPECIFIC = .{ .EVENT = .{ .MODIFY_STATE = true } },
|
|
16539
|
+
.STANDARD = .{ .SYNCHRONIZE = true },
|
|
16540
|
+
},
|
|
16541
|
+
null,
|
|
16542
|
+
.Synchronization,
|
|
16543
|
+
.FALSE,
|
|
16544
|
+
)) {
|
|
16545
|
+
.SUCCESS => {},
|
|
16546
|
+
else => return error.InputOutput,
|
|
16547
|
+
}
|
|
16548
|
+
defer windows.CloseHandle(event);
|
|
16549
|
+
|
|
16550
|
+
var iosb: windows.IO_STATUS_BLOCK = .{
|
|
16551
|
+
.u = .{ .Status = .PENDING },
|
|
16552
|
+
.Information = 0,
|
|
16553
|
+
};
|
|
16554
|
+
const end_offset: windows.LARGE_INTEGER = -1;
|
|
16555
|
+
const len: windows.ULONG = @intCast(@min(bytes.len - at, std.math.maxInt(windows.ULONG)));
|
|
16556
|
+
const status = windows.ntdll.NtWriteFile(
|
|
16557
|
+
file.handle,
|
|
16558
|
+
event,
|
|
16559
|
+
null,
|
|
16560
|
+
null,
|
|
16561
|
+
&iosb,
|
|
16562
|
+
bytes[at..].ptr,
|
|
16563
|
+
len,
|
|
16564
|
+
&end_offset,
|
|
16565
|
+
null,
|
|
16566
|
+
);
|
|
16567
|
+
if (status == .PENDING) {
|
|
16568
|
+
wait: while (true) {
|
|
16569
|
+
// A short kernel wait plus Io.checkCancel makes this
|
|
16570
|
+
// direct NT call participate in the surrounding
|
|
16571
|
+
// concurrent task's cancellation protocol without
|
|
16572
|
+
// depending on Threaded's private alertable-syscall
|
|
16573
|
+
// machinery.
|
|
16574
|
+
const poll_interval_100ns: windows.LARGE_INTEGER = -50_000;
|
|
16575
|
+
switch (windows.ntdll.NtWaitForSingleObject(event, .FALSE, &poll_interval_100ns)) {
|
|
16576
|
+
windows.NTSTATUS.WAIT_0 => break :wait,
|
|
16577
|
+
.TIMEOUT => std.Io.checkCancel(io) catch {
|
|
16578
|
+
// The threaded Io cancels a concurrent task
|
|
16579
|
+
// through this check. Cancel this exact
|
|
16580
|
+
// request, then wait non-alertably until
|
|
16581
|
+
// the kernel has dropped every reference to
|
|
16582
|
+
// IOSB and payload before reporting it.
|
|
16583
|
+
var cancel_iosb: windows.IO_STATUS_BLOCK = undefined;
|
|
16584
|
+
_ = windows.ntdll.NtCancelIoFileEx(file.handle, &iosb, &cancel_iosb);
|
|
16585
|
+
switch (windows.ntdll.NtWaitForSingleObject(event, .FALSE, null)) {
|
|
16586
|
+
windows.NTSTATUS.WAIT_0 => {},
|
|
16587
|
+
else => unreachable,
|
|
16588
|
+
}
|
|
16589
|
+
return error.Canceled;
|
|
16590
|
+
},
|
|
16591
|
+
else => {
|
|
16592
|
+
var cancel_iosb: windows.IO_STATUS_BLOCK = undefined;
|
|
16593
|
+
_ = windows.ntdll.NtCancelIoFileEx(file.handle, &iosb, &cancel_iosb);
|
|
16594
|
+
switch (windows.ntdll.NtWaitForSingleObject(event, .FALSE, null)) {
|
|
16595
|
+
windows.NTSTATUS.WAIT_0 => {},
|
|
16596
|
+
else => unreachable,
|
|
16597
|
+
}
|
|
16598
|
+
return error.InputOutput;
|
|
16599
|
+
},
|
|
16600
|
+
}
|
|
16601
|
+
}
|
|
16602
|
+
} else {
|
|
16603
|
+
// A failed immediate submission does not promise to fill
|
|
16604
|
+
// the IOSB; make the returned status authoritative.
|
|
16605
|
+
iosb.u.Status = status;
|
|
16606
|
+
}
|
|
16607
|
+
switch (iosb.u.Status) {
|
|
16608
|
+
.SUCCESS => {},
|
|
16609
|
+
.DISK_FULL => return error.NoSpaceLeft,
|
|
16610
|
+
.ACCESS_DENIED => return error.AccessDenied,
|
|
16611
|
+
.FILE_LOCK_CONFLICT => return error.LockViolation,
|
|
16612
|
+
.CANCELLED => return error.Canceled,
|
|
16613
|
+
.PENDING => return error.InputOutput,
|
|
16614
|
+
else => return error.InputOutput,
|
|
16615
|
+
}
|
|
16616
|
+
if (iosb.Information == 0 or iosb.Information > len) return error.InputOutput;
|
|
16617
|
+
at += iosb.Information;
|
|
16618
|
+
}
|
|
16619
|
+
}
|
|
16620
|
+
|
|
16621
|
+
fn openedFileIsInsideDir(io: std.Io, dir: std.Io.Dir, file: std.Io.File) bool {
|
|
16622
|
+
var dir_path: [std.Io.Dir.max_path_bytes]u8 = undefined;
|
|
16623
|
+
var file_path: [std.Io.Dir.max_path_bytes]u8 = undefined;
|
|
16624
|
+
const dir_len = dir.realPath(io, &dir_path) catch return false;
|
|
16625
|
+
const file_len = file.realPath(io, &file_path) catch return false;
|
|
16626
|
+
const root = dir_path[0..dir_len];
|
|
16627
|
+
const target = file_path[0..file_len];
|
|
16628
|
+
return target.len > root.len and std.mem.eql(u8, root, target[0..root.len]) and
|
|
16629
|
+
(target[root.len] == '/' or (builtin.os.tag == .windows and target[root.len] == '\\'));
|
|
16630
|
+
}
|
|
16631
|
+
|
|
16632
|
+
pub fn failNextFileOperationForTest(self: *Self, err: anyerror) void {
|
|
16633
|
+
if (comptime !builtin.is_test) @compileError("failNextFileOperationForTest is available only in test builds");
|
|
16634
|
+
self.test_next_file_error = err;
|
|
16635
|
+
}
|
|
16636
|
+
|
|
16637
|
+
fn createFileStreamContext(self: *Self, slot: *FileStreamSlot) ?*FileStreamWorkerContext {
|
|
16638
|
+
const ctx = process_allocator.create(FileStreamWorkerContext) catch return null;
|
|
16639
|
+
const chunk = if (slot.chunk) |bytes|
|
|
16640
|
+
process_allocator.dupe(u8, bytes) catch {
|
|
16641
|
+
process_allocator.destroy(ctx);
|
|
16642
|
+
return null;
|
|
16643
|
+
}
|
|
16644
|
+
else
|
|
16645
|
+
null;
|
|
16646
|
+
ctx.* = .{ .op = slot.op, .offset = slot.offset, .total = slot.total, .chunk = chunk };
|
|
16647
|
+
@memcpy(ctx.path_storage[0..slot.path_len], slot.path());
|
|
16648
|
+
ctx.path_len = slot.path_len;
|
|
16649
|
+
ctx.parent = slot.parent;
|
|
16650
|
+
slot.parent = null;
|
|
16651
|
+
ctx.read_file = slot.read_file;
|
|
16652
|
+
slot.read_file = null;
|
|
16653
|
+
if (slot.atomic) |*atomic| {
|
|
16654
|
+
ctx.atomic = atomic.*;
|
|
16655
|
+
atomic.* = undefined;
|
|
16656
|
+
slot.atomic = null;
|
|
16657
|
+
if (slot.atomic_dest) |dest| {
|
|
16658
|
+
ctx.atomic_dest = dest;
|
|
16659
|
+
slot.atomic_dest = null;
|
|
16660
|
+
ctx.atomic.?.dest_sub_path = dest;
|
|
16661
|
+
}
|
|
16662
|
+
}
|
|
16663
|
+
if (slot.chunk) |bytes| {
|
|
16664
|
+
self.allocator.free(bytes);
|
|
16665
|
+
slot.chunk = null;
|
|
16666
|
+
}
|
|
16667
|
+
return ctx;
|
|
16668
|
+
}
|
|
16669
|
+
|
|
16670
|
+
/// Move durable resources back into the owner without allocating. A
|
|
16671
|
+
/// rejected chunk attempt stays context-owned and is discarded; its
|
|
16672
|
+
/// retry supplies the bytes again while the atomic sink survives.
|
|
16673
|
+
fn moveFileStreamResourcesToSlot(slot: *FileStreamSlot, ctx: *FileStreamWorkerContext) void {
|
|
16674
|
+
std.debug.assert(slot.parent == null and slot.read_file == null and slot.atomic == null and slot.atomic_dest == null);
|
|
16675
|
+
slot.parent = ctx.parent;
|
|
16676
|
+
ctx.parent = null;
|
|
16677
|
+
slot.read_file = ctx.read_file;
|
|
16678
|
+
ctx.read_file = null;
|
|
16679
|
+
if (ctx.atomic) |*atomic| {
|
|
16680
|
+
slot.atomic = atomic.*;
|
|
16681
|
+
atomic.* = undefined;
|
|
16682
|
+
ctx.atomic = null;
|
|
16683
|
+
if (ctx.atomic_dest) |dest| {
|
|
16684
|
+
slot.atomic_dest = dest;
|
|
16685
|
+
ctx.atomic_dest = null;
|
|
16686
|
+
slot.atomic.?.dest_sub_path = dest;
|
|
16687
|
+
}
|
|
16688
|
+
}
|
|
16689
|
+
}
|
|
16690
|
+
|
|
16691
|
+
fn destroyFileStreamContext(ctx: *FileStreamWorkerContext, io: std.Io) void {
|
|
16692
|
+
if (ctx.read_buffer) |bytes| process_allocator.free(bytes);
|
|
16693
|
+
if (ctx.chunk) |bytes| process_allocator.free(bytes);
|
|
16694
|
+
if (ctx.read_file) |file| file.close(io);
|
|
16695
|
+
if (ctx.atomic) |*atomic| atomic.deinit(io);
|
|
16696
|
+
if (ctx.atomic_dest) |dest| process_allocator.free(dest);
|
|
16697
|
+
if (ctx.parent) |parent| parent.close(io);
|
|
16698
|
+
process_allocator.destroy(ctx);
|
|
16699
|
+
}
|
|
16700
|
+
|
|
16701
|
+
fn fileStreamWorkerMain(self: *Self, index: usize, generation: u64, io: std.Io, ctx: *FileStreamWorkerContext) void {
|
|
16702
|
+
supervise: {
|
|
16703
|
+
var future = std.Io.concurrent(io, fileStreamTask, .{ ctx, io }) catch {
|
|
16704
|
+
fileStreamTask(ctx, io);
|
|
16705
|
+
break :supervise;
|
|
16706
|
+
};
|
|
16707
|
+
superviseWorkerTask(io, &future, &ctx.done, &ctx.interrupt);
|
|
16708
|
+
}
|
|
16709
|
+
ctx.mutex.lock();
|
|
16710
|
+
if (ctx.abandoned) {
|
|
16711
|
+
ctx.mutex.unlock();
|
|
16712
|
+
return;
|
|
16713
|
+
}
|
|
16714
|
+
ctx.committed = true;
|
|
16715
|
+
ctx.mutex.unlock();
|
|
16716
|
+
const slot = &self.file_stream_slots[index];
|
|
16717
|
+
if (slot.generation != generation or slot.cancelled.load(.acquire)) return;
|
|
16718
|
+
moveFileStreamResourcesToSlot(slot, ctx);
|
|
16719
|
+
slot.offset = ctx.offset;
|
|
16720
|
+
slot.total = ctx.total;
|
|
16721
|
+
const buffer = if (ctx.read_buffer) |worker_buffer| copy: {
|
|
16722
|
+
const delivery = self.allocator.dupe(u8, worker_buffer[0..ctx.read_len]) catch {
|
|
16723
|
+
process_allocator.free(worker_buffer);
|
|
16724
|
+
ctx.read_buffer = null;
|
|
16725
|
+
ctx.read_len = 0;
|
|
16726
|
+
ctx.event = .terminal;
|
|
16727
|
+
ctx.outcome = .rejected;
|
|
16728
|
+
break :copy null;
|
|
16729
|
+
};
|
|
16730
|
+
process_allocator.free(worker_buffer);
|
|
16731
|
+
ctx.read_buffer = null;
|
|
16732
|
+
break :copy delivery;
|
|
16733
|
+
} else null;
|
|
16734
|
+
if (ctx.op == .read_stream) {
|
|
16735
|
+
self.postFileStreamWorker(index, generation, buffer, ctx.read_len, ctx.event, ctx.outcome, io);
|
|
16736
|
+
} else {
|
|
16737
|
+
if (buffer) |bytes| self.allocator.free(bytes);
|
|
16738
|
+
self.postWriteFileStreamWorker(index, generation, ctx.op, ctx.outcome, io);
|
|
16739
|
+
}
|
|
16740
|
+
}
|
|
16741
|
+
|
|
16742
|
+
fn fileStreamTask(ctx: *FileStreamWorkerContext, io: std.Io) void {
|
|
16743
|
+
defer ctx.done.store(true, .release);
|
|
16744
|
+
const dir = ctx.parent orelse std.Io.Dir.cwd();
|
|
16745
|
+
switch (ctx.op) {
|
|
16746
|
+
.read_stream => {
|
|
16747
|
+
const buffer = process_allocator.alloc(u8, effect_file_stream_chunk_bytes) catch {
|
|
16748
|
+
ctx.outcome = .rejected;
|
|
16749
|
+
return;
|
|
16750
|
+
};
|
|
16751
|
+
ctx.read_buffer = buffer;
|
|
16752
|
+
if (ctx.read_file == null) {
|
|
16753
|
+
ctx.read_file = dir.openFile(io, ctx.path(), .{ .resolve_beneath = ctx.parent != null }) catch |err| {
|
|
16754
|
+
ctx.outcome = if (err == error.FileNotFound) .not_found else fileOpFailure(err);
|
|
16755
|
+
return;
|
|
16756
|
+
};
|
|
16757
|
+
if (ctx.parent != null and !openedFileIsInsideDir(io, dir, ctx.read_file.?)) {
|
|
16758
|
+
ctx.read_file.?.close(io);
|
|
16759
|
+
ctx.read_file = null;
|
|
16760
|
+
ctx.outcome = .io_failed;
|
|
16761
|
+
return;
|
|
16762
|
+
}
|
|
16763
|
+
}
|
|
16764
|
+
const len = ctx.read_file.?.readPositionalAll(io, buffer, ctx.offset) catch |err| {
|
|
16765
|
+
ctx.outcome = fileOpFailure(err);
|
|
16766
|
+
return;
|
|
16767
|
+
};
|
|
16768
|
+
if (len == 0) {
|
|
16769
|
+
ctx.event = .done;
|
|
16770
|
+
ctx.outcome = .ok;
|
|
16771
|
+
process_allocator.free(buffer);
|
|
16772
|
+
ctx.read_buffer = null;
|
|
16773
|
+
return;
|
|
16774
|
+
}
|
|
16775
|
+
ctx.offset += len;
|
|
16776
|
+
ctx.total += len;
|
|
16777
|
+
ctx.read_len = len;
|
|
16778
|
+
ctx.event = .chunk;
|
|
16779
|
+
ctx.outcome = .ok;
|
|
16780
|
+
},
|
|
16781
|
+
.write_stream_open => {
|
|
16782
|
+
ctx.atomic = dir.createFileAtomic(io, ctx.path(), .{ .make_path = ctx.parent == null, .replace = true }) catch |err| {
|
|
16783
|
+
ctx.outcome = fileOpFailure(err);
|
|
16784
|
+
return;
|
|
16785
|
+
};
|
|
16786
|
+
ctx.atomic_dest = process_allocator.dupe(u8, ctx.atomic.?.dest_sub_path) catch {
|
|
16787
|
+
ctx.atomic.?.deinit(io);
|
|
16788
|
+
ctx.atomic = null;
|
|
16789
|
+
ctx.outcome = .rejected;
|
|
16790
|
+
return;
|
|
16791
|
+
};
|
|
16792
|
+
ctx.atomic.?.dest_sub_path = ctx.atomic_dest.?;
|
|
16793
|
+
ctx.outcome = .ok;
|
|
16794
|
+
},
|
|
16795
|
+
.write_stream_chunk => {
|
|
16796
|
+
const bytes = ctx.chunk orelse {
|
|
16797
|
+
ctx.outcome = .sink_missing;
|
|
16798
|
+
return;
|
|
16799
|
+
};
|
|
16800
|
+
const atomic = &(ctx.atomic orelse {
|
|
16801
|
+
ctx.outcome = .sink_missing;
|
|
16802
|
+
return;
|
|
16803
|
+
});
|
|
16804
|
+
atomic.file.writePositionalAll(io, bytes, ctx.total) catch |err| {
|
|
16805
|
+
ctx.outcome = fileOpFailure(err);
|
|
16806
|
+
return;
|
|
16807
|
+
};
|
|
16808
|
+
ctx.total += bytes.len;
|
|
16809
|
+
ctx.outcome = .ok;
|
|
16810
|
+
},
|
|
16811
|
+
.write_stream_close => {
|
|
16812
|
+
const atomic = &(ctx.atomic orelse {
|
|
16813
|
+
ctx.outcome = .sink_missing;
|
|
16814
|
+
return;
|
|
16815
|
+
});
|
|
16816
|
+
atomic.file.sync(io) catch |err| {
|
|
16817
|
+
ctx.outcome = fileOpFailure(err);
|
|
16818
|
+
return;
|
|
16819
|
+
};
|
|
16820
|
+
atomic.replace(io) catch |err| {
|
|
16821
|
+
ctx.outcome = fileOpFailure(err);
|
|
16822
|
+
return;
|
|
16823
|
+
};
|
|
16824
|
+
ctx.outcome = .ok;
|
|
16825
|
+
},
|
|
16826
|
+
else => unreachable,
|
|
16827
|
+
}
|
|
16828
|
+
}
|
|
16829
|
+
|
|
16830
|
+
fn postWriteFileStreamWorker(self: *Self, index: usize, generation: u64, op: EffectFileOp, outcome: EffectFileOutcome, io: std.Io) void {
|
|
16831
|
+
const slot = &self.file_stream_slots[index];
|
|
16832
|
+
if (slot.cancelled.load(.acquire) or slot.generation != generation) return;
|
|
16833
|
+
var entry: Entry = .{
|
|
16834
|
+
.kind = .file,
|
|
16835
|
+
.slot_index = @intCast(index),
|
|
16836
|
+
.key = slot.key,
|
|
16837
|
+
.file_op = op,
|
|
16838
|
+
.file_event = .terminal,
|
|
16839
|
+
.file_outcome = outcome,
|
|
16840
|
+
// Writer open/chunk/close results are acknowledgements, not
|
|
16841
|
+
// read progress. The running total stays internal to the sink.
|
|
16842
|
+
.file_total = 0,
|
|
16843
|
+
.file_stream_generation = generation,
|
|
16844
|
+
.file_fn = slot.on_result,
|
|
16845
|
+
};
|
|
16846
|
+
while (!self.enqueue(&entry)) {
|
|
16847
|
+
if (self.shutdown.load(.acquire) or slot.cancelled.load(.acquire)) return;
|
|
16848
|
+
self.wakeHost();
|
|
16849
|
+
std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake) catch {};
|
|
16850
|
+
}
|
|
16851
|
+
self.wakeHost();
|
|
16852
|
+
}
|
|
16853
|
+
|
|
16854
|
+
fn postFileStreamWorker(self: *Self, index: usize, generation: u64, buffer: ?[]u8, len: usize, event: EffectFileEvent, outcome: EffectFileOutcome, io: std.Io) void {
|
|
16855
|
+
const slot = &self.file_stream_slots[index];
|
|
16856
|
+
if (slot.cancelled.load(.acquire) or slot.generation != generation) {
|
|
16857
|
+
if (buffer) |heap| self.allocator.free(heap);
|
|
16858
|
+
return;
|
|
16859
|
+
}
|
|
16860
|
+
var entry: Entry = .{
|
|
16861
|
+
.kind = .file,
|
|
16862
|
+
.slot_index = @intCast(index),
|
|
16863
|
+
.key = slot.key,
|
|
16864
|
+
.line_len = @intCast(len),
|
|
16865
|
+
.file_op = .read_stream,
|
|
16866
|
+
.file_event = event,
|
|
16867
|
+
.file_outcome = outcome,
|
|
16868
|
+
.file_total = slot.total,
|
|
16869
|
+
.file_stream_generation = generation,
|
|
16870
|
+
.file_fn = slot.on_result,
|
|
16871
|
+
.heap_line = buffer,
|
|
16872
|
+
};
|
|
16873
|
+
while (!self.enqueue(&entry)) {
|
|
16874
|
+
if (self.shutdown.load(.acquire) or slot.cancelled.load(.acquire)) {
|
|
16875
|
+
if (entry.heap_line) |heap| self.allocator.free(heap);
|
|
16876
|
+
return;
|
|
16877
|
+
}
|
|
16878
|
+
self.wakeHost();
|
|
16879
|
+
std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake) catch {};
|
|
16880
|
+
}
|
|
16881
|
+
self.wakeHost();
|
|
15655
16882
|
}
|
|
15656
16883
|
|
|
15657
16884
|
// ------------------------------------------------------ image worker
|
|
@@ -15951,14 +17178,33 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
15951
17178
|
/// The terminal file result must never be dropped: retry until
|
|
15952
17179
|
/// the loop thread drains space, giving up only on shutdown.
|
|
15953
17180
|
fn postFile(self: *Self, slot: *Slot, slot_index: u16, generation: u32, io: std.Io, outcome: EffectFileOutcome, read_len: usize) void {
|
|
17181
|
+
self.postFileDetailed(slot, slot_index, generation, io, .{ .outcome = outcome, .read_len = read_len });
|
|
17182
|
+
}
|
|
17183
|
+
|
|
17184
|
+
const FilePost = struct {
|
|
17185
|
+
outcome: EffectFileOutcome,
|
|
17186
|
+
read_len: usize = 0,
|
|
17187
|
+
event: EffectFileEvent = .terminal,
|
|
17188
|
+
total: u64 = 0,
|
|
17189
|
+
mtime_ms: i64 = 0,
|
|
17190
|
+
exists: bool = false,
|
|
17191
|
+
stream_generation: u64 = 0,
|
|
17192
|
+
};
|
|
17193
|
+
|
|
17194
|
+
fn postFileDetailed(self: *Self, slot: *Slot, slot_index: u16, generation: u32, io: std.Io, result: FilePost) void {
|
|
15954
17195
|
var entry: Entry = .{
|
|
15955
17196
|
.kind = .file,
|
|
15956
17197
|
.slot_index = slot_index,
|
|
15957
17198
|
.generation = generation,
|
|
15958
17199
|
.key = slot.key,
|
|
15959
|
-
.line_len = @intCast(read_len),
|
|
17200
|
+
.line_len = @intCast(result.read_len),
|
|
15960
17201
|
.file_op = slot.file_op,
|
|
15961
|
-
.file_outcome = outcome,
|
|
17202
|
+
.file_outcome = result.outcome,
|
|
17203
|
+
.file_event = result.event,
|
|
17204
|
+
.file_total = result.total,
|
|
17205
|
+
.file_mtime_ms = result.mtime_ms,
|
|
17206
|
+
.file_exists = result.exists,
|
|
17207
|
+
.file_stream_generation = result.stream_generation,
|
|
15962
17208
|
.file_fn = slot.on_file,
|
|
15963
17209
|
};
|
|
15964
17210
|
while (!self.enqueue(&entry)) {
|
|
@@ -16076,3 +17322,45 @@ test "fetch errors map onto the documented taxonomy" {
|
|
|
16076
17322
|
try std.testing.expectEqual(EffectFetchOutcome.rejected, classifyFetchError(error.UnsupportedUriScheme));
|
|
16077
17323
|
try std.testing.expectEqual(EffectFetchOutcome.protocol_failed, classifyFetchError(error.HttpHeadersInvalid));
|
|
16078
17324
|
}
|
|
17325
|
+
|
|
17326
|
+
test "read stream keeps one file generation open across chunks" {
|
|
17327
|
+
const io = std.testing.io;
|
|
17328
|
+
var tmp = std.testing.tmpDir(.{});
|
|
17329
|
+
defer tmp.cleanup();
|
|
17330
|
+
const TestMsg = union(enum) { result: EffectFileResult };
|
|
17331
|
+
const Channel = Effects(TestMsg);
|
|
17332
|
+
|
|
17333
|
+
const file_len = effect_file_stream_chunk_bytes * 2;
|
|
17334
|
+
const original = try std.testing.allocator.alloc(u8, file_len);
|
|
17335
|
+
defer std.testing.allocator.free(original);
|
|
17336
|
+
@memset(original, 'A');
|
|
17337
|
+
try tmp.dir.writeFile(io, .{ .sub_path = "stream.bin", .data = original });
|
|
17338
|
+
|
|
17339
|
+
const ctx = try process_allocator.create(FileStreamWorkerContext);
|
|
17340
|
+
ctx.* = .{ .op = .read_stream };
|
|
17341
|
+
defer Channel.destroyFileStreamContext(ctx, io);
|
|
17342
|
+
var path_buffer: [256]u8 = undefined;
|
|
17343
|
+
const path = try std.fmt.bufPrint(&path_buffer, ".zig-cache/tmp/{s}/stream.bin", .{tmp.sub_path[0..]});
|
|
17344
|
+
@memcpy(ctx.path_storage[0..path.len], path);
|
|
17345
|
+
ctx.path_len = path.len;
|
|
17346
|
+
|
|
17347
|
+
Channel.fileStreamTask(ctx, io);
|
|
17348
|
+
try std.testing.expectEqual(effect_file_stream_chunk_bytes, ctx.read_len);
|
|
17349
|
+
try std.testing.expect(std.mem.allEqual(u8, ctx.read_buffer.?[0..ctx.read_len], 'A'));
|
|
17350
|
+
process_allocator.free(ctx.read_buffer.?);
|
|
17351
|
+
ctx.read_buffer = null;
|
|
17352
|
+
ctx.read_len = 0;
|
|
17353
|
+
|
|
17354
|
+
// Replace the pathname between chunks. A stream is one open-file
|
|
17355
|
+
// generation, so its next positional read must still see the original.
|
|
17356
|
+
const replacement = try std.testing.allocator.alloc(u8, file_len);
|
|
17357
|
+
defer std.testing.allocator.free(replacement);
|
|
17358
|
+
@memset(replacement, 'B');
|
|
17359
|
+
var atomic = try tmp.dir.createFileAtomic(io, "stream.bin", .{ .replace = true });
|
|
17360
|
+
try atomic.file.writePositionalAll(io, replacement, 0);
|
|
17361
|
+
try atomic.replace(io);
|
|
17362
|
+
|
|
17363
|
+
Channel.fileStreamTask(ctx, io);
|
|
17364
|
+
try std.testing.expectEqual(effect_file_stream_chunk_bytes, ctx.read_len);
|
|
17365
|
+
try std.testing.expect(std.mem.allEqual(u8, ctx.read_buffer.?[0..ctx.read_len], 'A'));
|
|
17366
|
+
}
|