fslite-sqlite 0.1.0

A transport-independent, async virtual filesystem with a SQLite-backed persistent backend, HTTP adapter, and CLI.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
//! Copy, move, permanent removal, symbolic links, attributes, atomic
//! batches, and their shared subtree and destination-resolution helpers.

use std::collections::{BTreeMap, BTreeSet};

use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use fslite_core::{
    BatchOperation, BatchResult, Capability, ChangeKind, CopyOptions, CreateOptions, FsError,
    FsResult, LinkTarget, MoveOptions, MutationOptions, Node, NodeId, RemoveOptions,
    RequestContext, VirtualPath, WorkspaceId,
};
use rusqlite::params;
use tokio_rusqlite::Connection;

use crate::change;
use crate::content;
use crate::db::{self, now_ms};
use crate::directory;
use crate::resolve::{self, DIRECTORY_KIND, FILE_KIND, RawNode, ResolveOutcome, SYMLINK_KIND};
use crate::trash;

// --- shared destination resolution and subtree removal ---------------------

enum Destination {
    Ready {
        parent_id: String,
        existing: Option<Box<RawNode>>,
    },
    IsRoot,
    ParentNotFound,
    ParentNotDirectory,
}

fn resolve_destination(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    to: &VirtualPath,
) -> rusqlite::Result<Destination> {
    let Some(name) = to.name() else {
        return Ok(Destination::IsRoot);
    };
    let parent_path = to.parent().expect("a named path has a parent");

    match resolve::resolve_following(tx, workspace_id, &parent_path, true)? {
        ResolveOutcome::Found(parent) if parent.kind == DIRECTORY_KIND => {
            let existing = resolve::fetch_child(tx, workspace_id, &parent.id, name)?;
            Ok(Destination::Ready {
                parent_id: parent.id,
                existing: existing.map(Box::new),
            })
        }
        ResolveOutcome::Found(_) => Ok(Destination::ParentNotDirectory),
        _ => Ok(Destination::ParentNotFound),
    }
}

/// Permanently deletes a node and its entire subtree, including every
/// descendant's content generation and chunks.
///
/// Node rows cascade automatically (`nodes.parent_id ... ON DELETE CASCADE`),
/// but content generations do not cascade from the node that references
/// them, so their ids are collected before the cascading delete runs.
pub(crate) fn purge_subtree(tx: &rusqlite::Transaction<'_>, node_id: &str) -> rusqlite::Result<()> {
    let mut stmt = tx.prepare(
        "WITH RECURSIVE subtree(id, content_generation_id) AS ( \
             SELECT id, content_generation_id FROM nodes WHERE id = ?1 \
             UNION ALL \
             SELECT n.id, n.content_generation_id FROM nodes n \
             JOIN subtree s ON n.parent_id = s.id \
         ) \
         SELECT content_generation_id FROM subtree WHERE content_generation_id IS NOT NULL",
    )?;
    let generation_ids: Vec<String> = stmt
        .query_map(params![node_id], |row| row.get(0))?
        .collect::<rusqlite::Result<Vec<_>>>()?;
    drop(stmt);

    tx.execute("DELETE FROM nodes WHERE id = ?1", params![node_id])?;

    for generation_id in generation_ids {
        tx.execute(
            "DELETE FROM content_generations WHERE id = ?1",
            params![generation_id],
        )?;
    }

    Ok(())
}

fn is_same_or_descendant(ancestor: &VirtualPath, candidate: &VirtualPath) -> bool {
    if candidate.as_str() == ancestor.as_str() {
        return true;
    }
    let prefix = if ancestor.as_str() == "/" {
        "/".to_string()
    } else {
        format!("{}/", ancestor.as_str())
    };
    candidate.as_str().starts_with(&prefix)
}

// --- copy --------------------------------------------------------------------

pub(crate) enum CopyOutcome {
    Created(RawNode),
    SourceNotFound,
    SourceLinkLoop,
    SourceNotRecursive,
    RevisionConflict,
    DestIsRoot,
    DestParentNotFound,
    DestParentNotDirectory,
    DestAlreadyExists,
}

pub(crate) fn copy_result(
    outcome: CopyOutcome,
    from: VirtualPath,
    to: VirtualPath,
) -> FsResult<Node> {
    match outcome {
        CopyOutcome::Created(row) => row.into_node(),
        CopyOutcome::SourceNotFound => Err(FsError::not_found(from)),
        CopyOutcome::SourceLinkLoop => Err(FsError::link_loop(from)),
        CopyOutcome::SourceNotRecursive => Err(FsError::wrong_node_type(from)),
        CopyOutcome::RevisionConflict => Err(FsError::revision_conflict(from)),
        CopyOutcome::DestIsRoot | CopyOutcome::DestAlreadyExists => {
            Err(FsError::already_exists(to))
        }
        CopyOutcome::DestParentNotFound => Err(FsError::not_found(to)),
        CopyOutcome::DestParentNotDirectory => Err(FsError::wrong_node_type(to)),
    }
}

pub(crate) async fn copy(
    conn: &Connection,
    workspace_id: WorkspaceId,
    from: VirtualPath,
    to: VirtualPath,
    options: CopyOptions,
    actor_json: String,
) -> FsResult<Node> {
    let workspace_id_str = workspace_id.to_string();
    let from_for_tx = from.clone();
    let to_for_tx = to.clone();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let outcome = copy_tx(
                &tx,
                &workspace_id_str,
                &from_for_tx,
                &to_for_tx,
                options,
                &actor_json,
            )?;
            tx.commit()?;
            Ok(outcome)
        })
        .await
        .map_err(db::map_call_error)?;

    copy_result(outcome, from, to)
}

pub(crate) fn copy_tx(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    from: &VirtualPath,
    to: &VirtualPath,
    options: CopyOptions,
    actor_json: &str,
) -> rusqlite::Result<CopyOutcome> {
    let source = match resolve::resolve_following(tx, workspace_id, from, false)? {
        ResolveOutcome::Found(node) => node,
        ResolveOutcome::NotFound | ResolveOutcome::BrokenLink => {
            return Ok(CopyOutcome::SourceNotFound);
        }
        ResolveOutcome::LinkLoop => return Ok(CopyOutcome::SourceLinkLoop),
    };

    if let Some(expected) = options.expected_revision
        && source.revision != expected.get() as i64
    {
        return Ok(CopyOutcome::RevisionConflict);
    }

    if source.kind == DIRECTORY_KIND && !options.recursive {
        return Ok(CopyOutcome::SourceNotRecursive);
    }

    let (dest_parent_id, existing, dest_name) = match resolve_destination(tx, workspace_id, to)? {
        Destination::Ready {
            parent_id,
            existing,
        } => (parent_id, existing, to.name().expect("checked by Ready")),
        Destination::IsRoot => return Ok(CopyOutcome::DestIsRoot),
        Destination::ParentNotFound => return Ok(CopyOutcome::DestParentNotFound),
        Destination::ParentNotDirectory => return Ok(CopyOutcome::DestParentNotDirectory),
    };

    if let Some(existing_node) = existing {
        if !options.overwrite {
            return Ok(CopyOutcome::DestAlreadyExists);
        }
        purge_subtree(tx, &existing_node.id)?;
    }

    let now = now_ms();
    let new_node = copy_subtree(tx, workspace_id, &source, &dest_parent_id, dest_name, now)?;
    change::append(
        tx,
        workspace_id,
        ChangeKind::Copied,
        Some(&new_node.id),
        Some(from.as_str()),
        Some(to.as_str()),
        Some(1),
        actor_json,
        now,
    )?;

    Ok(CopyOutcome::Created(new_node))
}

/// Iteratively copies `source` (and, if it is a directory, its entire
/// subtree) under `dest_parent_id` as `dest_name`, assigning every copied
/// node a fresh id and every copied file a fresh, independent content
/// generation. Uses an explicit stack rather than Rust-level recursion so
/// traversal depth is not bounded by the call stack.
fn copy_subtree(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    source: &RawNode,
    dest_parent_id: &str,
    dest_name: &str,
    now: i64,
) -> rusqlite::Result<RawNode> {
    let mut stack: Vec<(RawNode, String, String)> = vec![(
        fetch_full(tx, workspace_id, &source.id)?,
        dest_parent_id.to_string(),
        dest_name.to_string(),
    )];
    let mut top: Option<RawNode> = None;

    while let Some((old_node, new_parent_id, new_name)) = stack.pop() {
        let new_id = NodeId::new().to_string();

        let content_generation_id = match old_node.kind {
            DIRECTORY_KIND | SYMLINK_KIND => None,
            FILE_KIND => match &old_node.content_generation_id {
                Some(old_generation_id) => {
                    let new_generation_id = content::create_generation(tx, workspace_id)?;
                    tx.execute(
                        "INSERT INTO content_chunks(generation_id, chunk_index, bytes) \
                         SELECT ?1, chunk_index, bytes FROM content_chunks WHERE generation_id = ?2",
                        params![new_generation_id, old_generation_id],
                    )?;
                    content::finalize_generation(tx, &new_generation_id, old_node.size as u64)?;
                    Some(new_generation_id)
                }
                None => None,
            },
            _ => {
                return Err(rusqlite::Error::InvalidColumnType(
                    4,
                    "kind".to_string(),
                    rusqlite::types::Type::Integer,
                ));
            }
        };

        tx.execute(
            "INSERT INTO nodes(id, workspace_id, parent_id, name, kind, size, revision, \
             created_at_ms, modified_at_ms, accessed_at_ms, content_generation_id, symlink_target) \
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, 1, ?7, ?7, ?7, ?8, ?9)",
            params![
                new_id,
                workspace_id,
                new_parent_id,
                new_name,
                old_node.kind,
                old_node.size,
                now,
                content_generation_id,
                old_node.symlink_target,
            ],
        )?;
        tx.execute(
            "INSERT INTO attributes(node_id, key, value) \
             SELECT ?1, key, value FROM attributes WHERE node_id = ?2",
            params![new_id, old_node.id],
        )?;

        let new_node = RawNode {
            id: new_id.clone(),
            workspace_id: workspace_id.to_string(),
            parent_id: Some(new_parent_id),
            name: new_name,
            kind: old_node.kind,
            size: old_node.size,
            revision: 1,
            created_at_ms: now,
            modified_at_ms: now,
            accessed_at_ms: now,
            content_generation_id,
            symlink_target: old_node.symlink_target.clone(),
        };

        if old_node.kind == DIRECTORY_KIND {
            for child in resolve::fetch_children(tx, workspace_id, &old_node.id)? {
                let child_name = child.name.clone();
                stack.push((child, new_id.clone(), child_name));
            }
        }

        if top.is_none() {
            top = Some(new_node);
        }
    }

    Ok(top.expect("the source node is always copied first"))
}

fn fetch_full(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    id: &str,
) -> rusqlite::Result<RawNode> {
    resolve::fetch_by_id(tx, workspace_id, id)?.ok_or_else(|| rusqlite::Error::QueryReturnedNoRows)
}

// --- move --------------------------------------------------------------------

pub(crate) enum MoveOutcome {
    Moved(RawNode),
    SourceIsRoot,
    SourceNotFound,
    SourceLinkLoop,
    DestInsideSource,
    RevisionConflict,
    DestIsRoot,
    DestParentNotFound,
    DestParentNotDirectory,
    DestAlreadyExists,
}

pub(crate) fn move_result(
    outcome: MoveOutcome,
    from: VirtualPath,
    to: VirtualPath,
) -> FsResult<Node> {
    match outcome {
        MoveOutcome::Moved(row) => row.into_node(),
        MoveOutcome::SourceIsRoot | MoveOutcome::DestInsideSource => {
            Err(FsError::permission_denied(from))
        }
        MoveOutcome::SourceNotFound => Err(FsError::not_found(from)),
        MoveOutcome::SourceLinkLoop => Err(FsError::link_loop(from)),
        MoveOutcome::RevisionConflict => Err(FsError::revision_conflict(from)),
        MoveOutcome::DestIsRoot | MoveOutcome::DestAlreadyExists => {
            Err(FsError::already_exists(to))
        }
        MoveOutcome::DestParentNotFound => Err(FsError::not_found(to)),
        MoveOutcome::DestParentNotDirectory => Err(FsError::wrong_node_type(to)),
    }
}

pub(crate) async fn move_path(
    conn: &Connection,
    workspace_id: WorkspaceId,
    from: VirtualPath,
    to: VirtualPath,
    options: MoveOptions,
    actor_json: String,
) -> FsResult<Node> {
    let workspace_id_str = workspace_id.to_string();
    let from_for_tx = from.clone();
    let to_for_tx = to.clone();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let outcome = move_tx(
                &tx,
                &workspace_id_str,
                &from_for_tx,
                &to_for_tx,
                options,
                &actor_json,
            )?;
            tx.commit()?;
            Ok(outcome)
        })
        .await
        .map_err(db::map_call_error)?;

    move_result(outcome, from, to)
}

pub(crate) fn move_tx(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    from: &VirtualPath,
    to: &VirtualPath,
    options: MoveOptions,
    actor_json: &str,
) -> rusqlite::Result<MoveOutcome> {
    if from.name().is_none() {
        return Ok(MoveOutcome::SourceIsRoot);
    }

    let source = match resolve::resolve_following(tx, workspace_id, from, false)? {
        ResolveOutcome::Found(node) => node,
        ResolveOutcome::NotFound | ResolveOutcome::BrokenLink => {
            return Ok(MoveOutcome::SourceNotFound);
        }
        ResolveOutcome::LinkLoop => return Ok(MoveOutcome::SourceLinkLoop),
    };

    if let Some(expected) = options.expected_revision
        && source.revision != expected.get() as i64
    {
        return Ok(MoveOutcome::RevisionConflict);
    }

    if is_same_or_descendant(from, to) {
        return Ok(MoveOutcome::DestInsideSource);
    }

    let (dest_parent_id, existing, dest_name) = match resolve_destination(tx, workspace_id, to)? {
        Destination::Ready {
            parent_id,
            existing,
        } => (parent_id, existing, to.name().expect("checked by Ready")),
        Destination::IsRoot => return Ok(MoveOutcome::DestIsRoot),
        Destination::ParentNotFound => return Ok(MoveOutcome::DestParentNotFound),
        Destination::ParentNotDirectory => return Ok(MoveOutcome::DestParentNotDirectory),
    };

    if let Some(existing_node) = existing {
        if !options.overwrite {
            return Ok(MoveOutcome::DestAlreadyExists);
        }
        purge_subtree(tx, &existing_node.id)?;
    }

    let now = now_ms();
    let new_revision = source.revision + 1;
    tx.execute(
        "UPDATE nodes SET parent_id = ?2, name = ?3, revision = ?4, modified_at_ms = ?5 WHERE id = ?1",
        params![source.id, dest_parent_id, dest_name, new_revision, now],
    )?;
    change::append(
        tx,
        workspace_id,
        ChangeKind::Moved,
        Some(&source.id),
        Some(from.as_str()),
        Some(to.as_str()),
        Some(new_revision),
        actor_json,
        now,
    )?;

    Ok(MoveOutcome::Moved(RawNode {
        id: source.id,
        workspace_id: workspace_id.to_string(),
        parent_id: Some(dest_parent_id),
        name: dest_name.to_string(),
        kind: source.kind,
        size: source.size,
        revision: new_revision,
        created_at_ms: source.created_at_ms,
        modified_at_ms: now,
        accessed_at_ms: source.accessed_at_ms,
        content_generation_id: source.content_generation_id,
        symlink_target: source.symlink_target,
    }))
}

// --- remove --------------------------------------------------------------------

pub(crate) enum RemoveOutcome {
    Removed,
    IsRoot,
    NotFound,
    LinkLoop,
    DirectoryNotEmpty,
    RevisionConflict,
}

pub(crate) fn remove_result(outcome: RemoveOutcome, path: VirtualPath) -> FsResult<()> {
    match outcome {
        RemoveOutcome::Removed => Ok(()),
        RemoveOutcome::IsRoot => Err(FsError::permission_denied(path)),
        RemoveOutcome::NotFound => Err(FsError::not_found(path)),
        RemoveOutcome::LinkLoop => Err(FsError::link_loop(path)),
        RemoveOutcome::DirectoryNotEmpty => Err(FsError::directory_not_empty(path)),
        RemoveOutcome::RevisionConflict => Err(FsError::revision_conflict(path)),
    }
}

pub(crate) async fn remove(
    conn: &Connection,
    workspace_id: WorkspaceId,
    path: VirtualPath,
    options: RemoveOptions,
    actor_json: String,
) -> FsResult<()> {
    let workspace_id_str = workspace_id.to_string();
    let path_for_tx = path.clone();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let outcome = remove_tx(&tx, &workspace_id_str, &path_for_tx, options, &actor_json)?;
            tx.commit()?;
            Ok(outcome)
        })
        .await
        .map_err(db::map_call_error)?;

    remove_result(outcome, path)
}

pub(crate) fn remove_tx(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    path: &VirtualPath,
    options: RemoveOptions,
    actor_json: &str,
) -> rusqlite::Result<RemoveOutcome> {
    if path.name().is_none() {
        return Ok(RemoveOutcome::IsRoot);
    }

    let node = match resolve::resolve_following(tx, workspace_id, path, false)? {
        ResolveOutcome::Found(node) => node,
        ResolveOutcome::NotFound | ResolveOutcome::BrokenLink => {
            return Ok(RemoveOutcome::NotFound);
        }
        ResolveOutcome::LinkLoop => return Ok(RemoveOutcome::LinkLoop),
    };

    if let Some(expected) = options.expected_revision
        && node.revision != expected.get() as i64
    {
        return Ok(RemoveOutcome::RevisionConflict);
    }

    if node.kind == DIRECTORY_KIND
        && !options.recursive
        && resolve::has_active_children(tx, workspace_id, &node.id)?
    {
        return Ok(RemoveOutcome::DirectoryNotEmpty);
    }

    purge_subtree(tx, &node.id)?;

    let now = now_ms();
    change::append(
        tx,
        workspace_id,
        ChangeKind::Removed,
        Some(&node.id),
        Some(path.as_str()),
        None,
        None,
        actor_json,
        now,
    )?;

    Ok(RemoveOutcome::Removed)
}

// --- symlink / read_link -------------------------------------------------------

pub(crate) enum SymlinkOutcome {
    Created(RawNode),
    Existing(RawNode),
    AlreadyExists,
    WrongNodeType,
    ParentNotFound,
    RevisionConflict,
}

pub(crate) fn symlink_result(outcome: SymlinkOutcome, link: VirtualPath) -> FsResult<Node> {
    match outcome {
        SymlinkOutcome::Created(row) | SymlinkOutcome::Existing(row) => row.into_node(),
        SymlinkOutcome::AlreadyExists => Err(FsError::already_exists(link)),
        SymlinkOutcome::WrongNodeType => Err(FsError::wrong_node_type(link)),
        SymlinkOutcome::ParentNotFound => Err(FsError::not_found(link)),
        SymlinkOutcome::RevisionConflict => Err(FsError::revision_conflict(link)),
    }
}

pub(crate) async fn symlink(
    conn: &Connection,
    workspace_id: WorkspaceId,
    target: LinkTarget,
    link: VirtualPath,
    options: CreateOptions,
    actor_json: String,
) -> FsResult<Node> {
    let Some(_) = link.name() else {
        return Err(FsError::already_exists(link));
    };

    let workspace_id_str = workspace_id.to_string();
    let link_for_tx = link.clone();
    let target_str = target.as_str().to_string();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let outcome = symlink_tx(
                &tx,
                &workspace_id_str,
                &link_for_tx,
                &target_str,
                options,
                &actor_json,
            )?;
            tx.commit()?;
            Ok(outcome)
        })
        .await
        .map_err(db::map_call_error)?;

    symlink_result(outcome, link)
}

pub(crate) fn symlink_tx(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    link: &VirtualPath,
    target_str: &str,
    options: CreateOptions,
    actor_json: &str,
) -> rusqlite::Result<SymlinkOutcome> {
    let name = link.name().expect("checked by caller");
    let parent_path = link.parent().expect("a named path has a parent");

    let parent = match resolve::resolve_following(tx, workspace_id, &parent_path, true)? {
        ResolveOutcome::Found(node) if node.kind == DIRECTORY_KIND => node,
        ResolveOutcome::Found(_) => return Ok(SymlinkOutcome::WrongNodeType),
        _ => return Ok(SymlinkOutcome::ParentNotFound),
    };

    if let Some(existing) = resolve::fetch_child(tx, workspace_id, &parent.id, name)? {
        if existing.kind != SYMLINK_KIND {
            return Ok(SymlinkOutcome::WrongNodeType);
        }
        if !options.exist_ok {
            return Ok(SymlinkOutcome::AlreadyExists);
        }
        if let Some(expected) = options.expected_revision
            && existing.revision != expected.get() as i64
        {
            return Ok(SymlinkOutcome::RevisionConflict);
        }
        return Ok(SymlinkOutcome::Existing(existing));
    }

    let node_id = NodeId::new().to_string();
    let now = now_ms();
    tx.execute(
        "INSERT INTO nodes(id, workspace_id, parent_id, name, kind, size, revision, \
         created_at_ms, modified_at_ms, accessed_at_ms, symlink_target) \
         VALUES (?1, ?2, ?3, ?4, 2, 0, 1, ?5, ?5, ?5, ?6)",
        params![node_id, workspace_id, parent.id, name, now, target_str],
    )?;
    change::append(
        tx,
        workspace_id,
        ChangeKind::Created,
        Some(&node_id),
        None,
        Some(link.as_str()),
        Some(1),
        actor_json,
        now,
    )?;

    Ok(SymlinkOutcome::Created(RawNode {
        id: node_id,
        workspace_id: workspace_id.to_string(),
        parent_id: Some(parent.id),
        name: name.to_string(),
        kind: SYMLINK_KIND,
        size: 0,
        revision: 1,
        created_at_ms: now,
        modified_at_ms: now,
        accessed_at_ms: now,
        content_generation_id: None,
        symlink_target: Some(target_str.to_string()),
    }))
}

pub(crate) async fn read_link(
    conn: &Connection,
    workspace_id: WorkspaceId,
    path: VirtualPath,
) -> FsResult<LinkTarget> {
    let workspace_id_str = workspace_id.to_string();
    let error_path = path.clone();

    let outcome = conn
        .call(move |conn| {
            Ok(resolve::resolve_following(
                conn,
                &workspace_id_str,
                &path,
                false,
            )?)
        })
        .await
        .map_err(db::map_call_error)?;

    match outcome {
        ResolveOutcome::Found(node) if node.kind == SYMLINK_KIND => {
            let target = node.symlink_target.ok_or_else(|| {
                FsError::internal_storage_failure("symlink node missing a stored target")
            })?;
            LinkTarget::parse(&target)
        }
        ResolveOutcome::Found(_) => Err(FsError::wrong_node_type(error_path)),
        ResolveOutcome::NotFound | ResolveOutcome::BrokenLink => {
            Err(FsError::not_found(error_path))
        }
        ResolveOutcome::LinkLoop => Err(FsError::link_loop(error_path)),
    }
}

// --- attributes ----------------------------------------------------------

/// Attribute sizes and counts are quota-controlled, as application metadata
/// rather than POSIX extended attributes.
const MAX_ATTRIBUTE_KEY_BYTES: usize = 256;
const MAX_ATTRIBUTE_VALUE_BYTES: usize = 4096;
const MAX_ATTRIBUTES_PER_NODE: i64 = 64;

pub(crate) enum AttributeOutcome {
    Updated(Box<RawNode>, BTreeMap<String, serde_json::Value>),
    NotFound,
    LinkLoop,
    InvalidKey,
    QuotaExceeded,
    RevisionConflict,
}

/// Converts an [`AttributeOutcome`] into its public result. Unlike other
/// node conversions, this attaches the node's current attribute map, since
/// [`RawNode::into_node`] otherwise always reports an empty one (populating
/// it universally would require every node-fetching call site across the
/// crate to query the `attributes` table; only the attribute-mutation
/// results carry it today).
pub(crate) fn attribute_result(outcome: AttributeOutcome, path: VirtualPath) -> FsResult<Node> {
    match outcome {
        AttributeOutcome::Updated(row, attributes) => {
            let mut node = row.into_node()?;
            node.attributes = attributes;
            Ok(node)
        }
        AttributeOutcome::NotFound => Err(FsError::not_found(path)),
        AttributeOutcome::LinkLoop => Err(FsError::link_loop(path)),
        AttributeOutcome::InvalidKey => Err(FsError::invalid_path_or_name(path)),
        AttributeOutcome::QuotaExceeded => Err(FsError::quota_exceeded(path)),
        AttributeOutcome::RevisionConflict => Err(FsError::revision_conflict(path)),
    }
}

/// Attribute values are arbitrary bytes, but [`Node::attributes`] is a JSON
/// map; each value is base64url-encoded into a JSON string so it round-trips
/// without assuming the bytes are valid UTF-8.
pub(crate) fn build_attributes_map(
    conn: &rusqlite::Connection,
    node_id: &str,
) -> rusqlite::Result<BTreeMap<String, serde_json::Value>> {
    let mut stmt =
        conn.prepare("SELECT key, value FROM attributes WHERE node_id = ?1 ORDER BY key")?;
    let rows = stmt.query_map(params![node_id], |row| {
        let key: String = row.get(0)?;
        let value: Vec<u8> = row.get(1)?;
        Ok((key, value))
    })?;

    let mut map = BTreeMap::new();
    for row in rows {
        let (key, value) = row?;
        map.insert(
            key,
            serde_json::Value::String(URL_SAFE_NO_PAD.encode(value)),
        );
    }
    Ok(map)
}

pub(crate) fn set_attribute_tx(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    path: &VirtualPath,
    key: &str,
    value: &[u8],
    options: MutationOptions,
    actor_json: &str,
) -> rusqlite::Result<AttributeOutcome> {
    let node = match resolve::resolve_following(tx, workspace_id, path, false)? {
        ResolveOutcome::Found(node) => node,
        ResolveOutcome::NotFound | ResolveOutcome::BrokenLink => {
            return Ok(AttributeOutcome::NotFound);
        }
        ResolveOutcome::LinkLoop => return Ok(AttributeOutcome::LinkLoop),
    };

    if key.is_empty() || key.len() > MAX_ATTRIBUTE_KEY_BYTES {
        return Ok(AttributeOutcome::InvalidKey);
    }
    if value.len() > MAX_ATTRIBUTE_VALUE_BYTES {
        return Ok(AttributeOutcome::QuotaExceeded);
    }
    if let Some(expected) = options.expected_revision
        && node.revision != expected.get() as i64
    {
        return Ok(AttributeOutcome::RevisionConflict);
    }

    let already_exists: bool = tx.query_row(
        "SELECT EXISTS(SELECT 1 FROM attributes WHERE node_id = ?1 AND key = ?2)",
        params![node.id, key],
        |row| row.get(0),
    )?;
    if !already_exists {
        let count: i64 = tx.query_row(
            "SELECT COUNT(*) FROM attributes WHERE node_id = ?1",
            params![node.id],
            |row| row.get(0),
        )?;
        if count >= MAX_ATTRIBUTES_PER_NODE {
            return Ok(AttributeOutcome::QuotaExceeded);
        }
    }

    tx.execute(
        "INSERT INTO attributes(node_id, key, value) VALUES (?1, ?2, ?3) \
         ON CONFLICT(node_id, key) DO UPDATE SET value = excluded.value",
        params![node.id, key, value],
    )?;

    let now = now_ms();
    let new_revision = node.revision + 1;
    tx.execute(
        "UPDATE nodes SET revision = ?2, modified_at_ms = ?3 WHERE id = ?1",
        params![node.id, new_revision, now],
    )?;
    change::append(
        tx,
        workspace_id,
        ChangeKind::AttributeSet,
        Some(&node.id),
        None,
        Some(path.as_str()),
        Some(new_revision),
        actor_json,
        now,
    )?;

    let updated =
        resolve::fetch_by_id(tx, workspace_id, &node.id)?.expect("the node was just updated");
    let attributes = build_attributes_map(tx, &node.id)?;
    Ok(AttributeOutcome::Updated(Box::new(updated), attributes))
}

pub(crate) async fn set_attribute(
    conn: &Connection,
    workspace_id: WorkspaceId,
    path: VirtualPath,
    key: String,
    value: Vec<u8>,
    options: MutationOptions,
    actor_json: String,
) -> FsResult<Node> {
    let workspace_id_str = workspace_id.to_string();
    let path_for_tx = path.clone();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let outcome = set_attribute_tx(
                &tx,
                &workspace_id_str,
                &path_for_tx,
                &key,
                &value,
                options,
                &actor_json,
            )?;
            tx.commit()?;
            Ok(outcome)
        })
        .await
        .map_err(db::map_call_error)?;

    attribute_result(outcome, path)
}

pub(crate) fn remove_attribute_tx(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    path: &VirtualPath,
    key: &str,
    options: MutationOptions,
    actor_json: &str,
) -> rusqlite::Result<AttributeOutcome> {
    let node = match resolve::resolve_following(tx, workspace_id, path, false)? {
        ResolveOutcome::Found(node) => node,
        ResolveOutcome::NotFound | ResolveOutcome::BrokenLink => {
            return Ok(AttributeOutcome::NotFound);
        }
        ResolveOutcome::LinkLoop => return Ok(AttributeOutcome::LinkLoop),
    };

    if let Some(expected) = options.expected_revision
        && node.revision != expected.get() as i64
    {
        return Ok(AttributeOutcome::RevisionConflict);
    }

    let deleted = tx.execute(
        "DELETE FROM attributes WHERE node_id = ?1 AND key = ?2",
        params![node.id, key],
    )?;

    let final_node = if deleted > 0 {
        let now = now_ms();
        let new_revision = node.revision + 1;
        tx.execute(
            "UPDATE nodes SET revision = ?2, modified_at_ms = ?3 WHERE id = ?1",
            params![node.id, new_revision, now],
        )?;
        change::append(
            tx,
            workspace_id,
            ChangeKind::AttributeRemoved,
            Some(&node.id),
            None,
            Some(path.as_str()),
            Some(new_revision),
            actor_json,
            now,
        )?;
        resolve::fetch_by_id(tx, workspace_id, &node.id)?.expect("the node was just updated")
    } else {
        node
    };

    let attributes = build_attributes_map(tx, &final_node.id)?;
    Ok(AttributeOutcome::Updated(Box::new(final_node), attributes))
}

pub(crate) async fn remove_attribute(
    conn: &Connection,
    workspace_id: WorkspaceId,
    path: VirtualPath,
    key: String,
    options: MutationOptions,
    actor_json: String,
) -> FsResult<Node> {
    let workspace_id_str = workspace_id.to_string();
    let path_for_tx = path.clone();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let outcome = remove_attribute_tx(
                &tx,
                &workspace_id_str,
                &path_for_tx,
                &key,
                options,
                &actor_json,
            )?;
            tx.commit()?;
            Ok(outcome)
        })
        .await
        .map_err(db::map_call_error)?;

    attribute_result(outcome, path)
}

// --- batch -----------------------------------------------------------------

enum BatchStepOutcome {
    Result(BatchResult),
    Failed(FsError),
}

enum BatchOutcome {
    Completed(Vec<BatchResult>),
    Failed { index: usize, error: FsError },
}

fn required_capability(operation: &BatchOperation) -> Capability {
    match operation {
        BatchOperation::Mkdir { .. }
        | BatchOperation::Touch { .. }
        | BatchOperation::Copy { .. }
        | BatchOperation::Move { .. }
        | BatchOperation::Symlink { .. }
        | BatchOperation::SetAttribute { .. }
        | BatchOperation::RemoveAttribute { .. } => Capability::Write,
        BatchOperation::Remove { .. } | BatchOperation::Purge { .. } => Capability::Delete,
        BatchOperation::Trash { .. } | BatchOperation::Restore { .. } => Capability::TrashRestore,
    }
}

fn to_step<T>(result: FsResult<T>, wrap: impl FnOnce(T) -> BatchResult) -> BatchStepOutcome {
    match result {
        Ok(value) => BatchStepOutcome::Result(wrap(value)),
        Err(err) => BatchStepOutcome::Failed(err),
    }
}

/// Executes one [`BatchOperation`] against the shared batch transaction,
/// using each operation's transaction-level `_tx` helper directly rather
/// than the async wrapper (which would try to acquire another connection
/// call from within this one and deadlock).
fn execute_batch_operation(
    tx: &rusqlite::Transaction<'_>,
    workspace_id: &str,
    operation: BatchOperation,
    actor_json: &str,
) -> rusqlite::Result<BatchStepOutcome> {
    Ok(match operation {
        BatchOperation::Mkdir { path, options } => {
            if path.name().is_none() {
                return Ok(BatchStepOutcome::Failed(FsError::already_exists(path)));
            }
            let segments: Vec<String> = path.segments().map(str::to_owned).collect();
            let outcome = directory::mkdir_tx(tx, workspace_id, &segments, options, actor_json)?;
            to_step(directory::mkdir_result(outcome, path), BatchResult::Node)
        }
        BatchOperation::Touch { path, options } => {
            let outcome = content::touch_tx(tx, workspace_id, &path, options, actor_json)?;
            to_step(content::touch_result(outcome, path), BatchResult::Node)
        }
        BatchOperation::Copy { from, to, options } => {
            let outcome = copy_tx(tx, workspace_id, &from, &to, options, actor_json)?;
            to_step(copy_result(outcome, from, to), BatchResult::Node)
        }
        BatchOperation::Move { from, to, options } => {
            let outcome = move_tx(tx, workspace_id, &from, &to, options, actor_json)?;
            to_step(move_result(outcome, from, to), BatchResult::Node)
        }
        BatchOperation::Remove { path, options } => {
            let outcome = remove_tx(tx, workspace_id, &path, options, actor_json)?;
            to_step(remove_result(outcome, path), |()| BatchResult::Unit)
        }
        BatchOperation::Symlink {
            target,
            link,
            options,
        } => {
            if link.name().is_none() {
                return Ok(BatchStepOutcome::Failed(FsError::already_exists(link)));
            }
            let target_str = target.as_str().to_string();
            let outcome = symlink_tx(tx, workspace_id, &link, &target_str, options, actor_json)?;
            to_step(symlink_result(outcome, link), BatchResult::Node)
        }
        BatchOperation::Trash { path, options } => {
            let outcome = trash::trash_tx(tx, workspace_id, &path, options, actor_json)?;
            let actor_metadata = serde_json::from_str(actor_json).unwrap_or_default();
            to_step(
                trash::trash_result(outcome, path, actor_metadata),
                BatchResult::Trash,
            )
        }
        BatchOperation::Restore {
            trash: trash_id,
            destination,
            options,
        } => {
            let trash_id_str = trash_id.to_string();
            let outcome = trash::restore_tx(
                tx,
                workspace_id,
                &trash_id_str,
                destination.as_ref(),
                options,
                actor_json,
            )?;
            to_step(trash::restore_result(outcome, trash_id), BatchResult::Node)
        }
        BatchOperation::Purge { trash: trash_id } => {
            let trash_id_str = trash_id.to_string();
            let outcome = trash::purge_tx(tx, workspace_id, &trash_id_str, actor_json)?;
            to_step(trash::purge_result(outcome, trash_id), |()| {
                BatchResult::Unit
            })
        }
        BatchOperation::SetAttribute {
            path,
            key,
            value,
            options,
        } => {
            let outcome =
                set_attribute_tx(tx, workspace_id, &path, &key, &value, options, actor_json)?;
            to_step(attribute_result(outcome, path), BatchResult::Node)
        }
        BatchOperation::RemoveAttribute { path, key, options } => {
            let outcome = remove_attribute_tx(tx, workspace_id, &path, &key, options, actor_json)?;
            to_step(attribute_result(outcome, path), BatchResult::Node)
        }
    })
}

pub(crate) async fn batch(
    conn: &Connection,
    ctx: &RequestContext,
    operations: Vec<BatchOperation>,
    actor_json: String,
) -> FsResult<Vec<BatchResult>> {
    let workspace_id_str = ctx.workspace_id.to_string();
    let capabilities: BTreeSet<Capability> = ctx.capabilities.clone();

    let outcome = conn
        .call(move |conn| {
            let tx = conn.transaction()?;
            let mut results = Vec::with_capacity(operations.len());

            for (index, operation) in operations.into_iter().enumerate() {
                let required = required_capability(&operation);
                if !capabilities.contains(&required) {
                    return Ok(BatchOutcome::Failed {
                        index,
                        error: FsError::permission_denied(format!(
                            "batch operation {index} requires {required:?}"
                        )),
                    });
                }

                match execute_batch_operation(&tx, &workspace_id_str, operation, &actor_json)? {
                    BatchStepOutcome::Result(result) => results.push(result),
                    BatchStepOutcome::Failed(error) => {
                        return Ok(BatchOutcome::Failed { index, error });
                    }
                }
            }

            tx.commit()?;
            Ok(BatchOutcome::Completed(results))
        })
        .await
        .map_err(db::map_call_error)?;

    match outcome {
        BatchOutcome::Completed(results) => Ok(results),
        BatchOutcome::Failed { index, error } => {
            let details = serde_json::json!({ "index": index });
            Err(FsError::new(
                error.code(),
                error.message().to_string(),
                details,
            ))
        }
    }
}