sim-table-fs 0.1.1

SIM workspace package for sim table fs.
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
use std::{
    collections::BTreeSet,
    path::{Path, PathBuf},
    sync::Arc,
};

use sim_codec::{Input, Output, decode_with_codec, encode_with_codec};
use sim_kernel::{
    Cx, EncodeOptions, Error, Expr, Object, ObjectEncode, ObjectEncoding, ReadPolicy, Result,
    Symbol, Value,
    id::CORE_TABLE_CLASS_ID,
    object::ClassRef,
    table::{Dir, Table},
};

use crate::{
    capabilities::{require_table_fs_read, require_table_fs_write},
    citizen::fs_dir_class_symbol,
    roadmap11::{decode_expr_for_ext, encode_expr_for_ext, infer_ext_from_expr, known_exts},
    table_fs_capability,
};

const DEFAULT_EXT: &str = "siml";

/// A SIM table backed by a host directory rooted at a canonical path.
#[derive(Clone)]
pub struct FsDir {
    root: PathBuf,
}

impl FsDir {
    /// Opens (creating if needed) the directory at `root` as a filesystem table.
    ///
    /// The root is created if it does not exist and then canonicalized; an I/O
    /// failure on either step is reported as an error.
    pub fn open(root: PathBuf) -> Result<Self> {
        std::fs::create_dir_all(&root)
            .map_err(|err| Error::Eval(format!("table/fs: cannot open root: {err}")))?;
        let root = std::fs::canonicalize(&root)
            .map_err(|err| Error::Eval(format!("table/fs: cannot open root: {err}")))?;
        Ok(Self { root })
    }

    fn segment(&self, name: &Symbol) -> Result<PathBuf> {
        let segment = name.name.as_ref();
        // The shared predicate rejects empty/`.`/`..`/`/`/`\`; table-fs keeps the
        // additional, stricter `is_absolute()` guard on top of it.
        if !sim_table_core::is_legal_table_segment(segment) || Path::new(segment).is_absolute() {
            return Err(Error::Eval(format!("table/fs: illegal name {segment:?}")));
        }
        let path = self.root.join(segment);
        self.ensure_within_root(&path)?;
        Ok(path)
    }

    fn ensure_within_root(&self, path: &Path) -> Result<()> {
        let candidate = if path.exists() {
            std::fs::canonicalize(path)
                .map_err(|err| Error::Eval(format!("table/fs: path check {err}")))?
        } else {
            path.to_path_buf()
        };
        if candidate.starts_with(&self.root) {
            Ok(())
        } else {
            Err(Error::Eval(format!(
                "table/fs: path escapes root: {}",
                path.display()
            )))
        }
    }

    pub(crate) fn ensure_internal_path(&self, path: &Path) -> Result<()> {
        self.ensure_within_root(path)
    }

    pub(crate) fn root_path(&self) -> &Path {
        &self.root
    }

    fn leaf_candidates(&self, name: &Symbol) -> Result<Vec<(PathBuf, &'static str)>> {
        let base = self.segment(name)?;
        let mut matches = Vec::new();
        for ext in known_exts() {
            let path = base.with_extension(ext);
            self.ensure_within_root(&path)?;
            if path.is_file() {
                matches.push((path, ext));
            }
        }
        Ok(matches)
    }

    fn leaf_path_for_read(&self, name: &Symbol) -> Result<Option<(PathBuf, &'static str)>> {
        let matches = self.leaf_candidates(name)?;
        match matches.len() {
            0 => Ok(None),
            1 => Ok(matches.into_iter().next()),
            _ => Err(Error::Eval(format!(
                "table/fs: multiple leaf files found for key {name}"
            ))),
        }
    }

    pub(crate) fn read_leaf_expr(
        &self,
        cx: &mut Cx,
        key: &Symbol,
    ) -> Result<(PathBuf, &'static str, Expr)> {
        let Some((path, ext)) = self.leaf_path_for_read(key)? else {
            return Err(Error::Eval(format!("table/fs: {key} is not a file")));
        };
        let expr = self.read_leaf_path(cx, &path, ext)?;
        Ok((path, ext, expr))
    }

    pub(crate) fn read_leaf_path(&self, cx: &mut Cx, path: &Path, ext: &str) -> Result<Expr> {
        self.ensure_internal_path(path)?;
        let bytes =
            std::fs::read(path).map_err(|err| Error::Eval(format!("table/fs: read {err}")))?;
        Ok(match decode_expr_for_ext(ext, &bytes) {
            Some(expr) => expr?,
            None => {
                let codec = Self::codec_for_ext(ext)?;
                Self::decode_expr_bytes(cx, &codec, &bytes)?
            }
        })
    }

    fn codec_for_ext(ext: &str) -> Result<Symbol> {
        match ext {
            "siml" => Ok(Symbol::qualified("codec", "lisp")),
            "simb" => Ok(Symbol::qualified("codec", "binary")),
            "simb64" => Ok(Symbol::qualified("codec", "binary-base64")),
            "simj" => Ok(Symbol::qualified("codec", "json")),
            "sima" => Ok(Symbol::qualified("codec", "algol")),
            other => Err(Error::Eval(format!("table/fs: unknown extension {other}"))),
        }
    }

    fn decode_expr_bytes(cx: &mut Cx, codec: &Symbol, bytes: &[u8]) -> Result<Expr> {
        decode_with_codec(
            cx,
            codec,
            Input::Bytes(bytes.to_vec()),
            ReadPolicy::default(),
        )
    }

    fn encode_expr_bytes(cx: &mut Cx, codec: &Symbol, expr: &Expr) -> Result<Vec<u8>> {
        match encode_with_codec(cx, codec, expr, EncodeOptions::default())? {
            Output::Text(text) => Ok(text.into_bytes()),
            Output::Bytes(bytes) => Ok(bytes),
        }
    }

    pub(crate) fn encode_leaf_expr(cx: &mut Cx, ext: &str, expr: &Expr) -> Result<Vec<u8>> {
        match encode_expr_for_ext(ext, expr) {
            Some(bytes) => bytes,
            None => {
                let codec = Symbol::qualified("codec", "lisp");
                Self::encode_expr_bytes(cx, &codec, expr)
            }
        }
    }
}

impl Object for FsDir {
    fn display(&self, _cx: &mut Cx) -> Result<String> {
        Ok(format!("table/fs[{}]", self.root.display()))
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

impl sim_kernel::ObjectCompat for FsDir {
    fn class(&self, cx: &mut Cx) -> Result<ClassRef> {
        let symbol = fs_dir_class_symbol();
        if let Some(value) = cx.registry().class_by_symbol(&symbol) {
            return Ok(value.clone());
        }
        let symbol = Symbol::qualified("core", "Table");
        if let Some(value) = cx.registry().class_by_symbol(&symbol) {
            return Ok(value.clone());
        }
        cx.factory().class_stub(CORE_TABLE_CLASS_ID, symbol)
    }
    fn as_expr(&self, cx: &mut Cx) -> Result<Expr> {
        self.as_table_expr(cx)
    }
    fn truth(&self, cx: &mut Cx) -> Result<bool> {
        Ok(!self.is_empty(cx)?)
    }
    fn as_table_impl(&self) -> Option<&dyn Table> {
        Some(self)
    }
    fn as_dir(&self) -> Option<&dyn Dir> {
        Some(self)
    }
    fn as_object_encoder(&self) -> Option<&dyn ObjectEncode> {
        Some(self)
    }
}

impl ObjectEncode for FsDir {
    fn object_encoding(&self, _cx: &mut Cx) -> Result<ObjectEncoding> {
        Ok(ObjectEncoding::Constructor {
            class: fs_dir_class_symbol(),
            args: vec![
                Expr::Symbol(Symbol::new("v0")),
                Expr::String(self.root.display().to_string()),
            ],
        })
    }
}

impl sim_citizen::Citizen for FsDir {
    fn citizen_symbol() -> Symbol {
        fs_dir_class_symbol()
    }

    fn citizen_version() -> u32 {
        0
    }

    fn citizen_arity() -> usize {
        1
    }

    fn citizen_fields() -> &'static [&'static str] {
        &["root"]
    }
}

impl Table for FsDir {
    fn backend_symbol(&self) -> Symbol {
        Symbol::qualified("table", "fs")
    }

    fn get(&self, cx: &mut Cx, key: Symbol) -> Result<Value> {
        require_table_fs_read(cx)?;
        match self.leaf_path_for_read(&key)? {
            Some(_) => {
                let (_, _, expr) = self.read_leaf_expr(cx, &key)?;
                cx.factory().expr(expr)
            }
            None => cx.factory().nil(),
        }
    }

    fn set(&self, cx: &mut Cx, key: Symbol, value: Value) -> Result<()> {
        require_table_fs_write(cx)?;
        let base = self.segment(&key)?;
        if base.is_dir() {
            return Err(Error::Eval(format!("table/fs: {key} is a directory")));
        }
        let existing_leaf = self.leaf_path_for_read(&key)?;
        for (path, _) in self.leaf_candidates(&key)? {
            if Some(path.clone()) != existing_leaf.as_ref().map(|(path, _)| path.clone())
                && path.extension().and_then(|ext| ext.to_str()) != Some(DEFAULT_EXT)
            {
                std::fs::remove_file(&path)
                    .map_err(|err| Error::Eval(format!("table/fs: write {err}")))?;
            }
        }
        let expr = value.object().as_expr(cx)?;
        let ext = existing_leaf
            .as_ref()
            .map(|(_, ext)| *ext)
            .or_else(|| infer_ext_from_expr(&expr))
            .unwrap_or(DEFAULT_EXT);
        let path = base.with_extension(ext);
        self.ensure_within_root(&path)?;
        let bytes = Self::encode_leaf_expr(cx, ext, &expr)?;
        std::fs::write(&path, bytes)
            .map_err(|err| Error::Eval(format!("table/fs: write {err}")))?;
        Ok(())
    }

    fn has(&self, cx: &mut Cx, key: Symbol) -> Result<bool> {
        require_table_fs_read(cx)?;
        let path = self.segment(&key)?;
        Ok(path.is_dir() || self.leaf_path_for_read(&key)?.is_some())
    }

    fn del(&self, cx: &mut Cx, key: Symbol) -> Result<Value> {
        require_table_fs_write(cx)?;
        match self.leaf_path_for_read(&key)? {
            Some((path, ext)) => {
                let bytes = std::fs::read(&path).unwrap_or_default();
                std::fs::remove_file(&path)
                    .map_err(|err| Error::Eval(format!("table/fs: del {err}")))?;
                let expr = match decode_expr_for_ext(ext, &bytes) {
                    Some(expr) => expr,
                    None => {
                        let codec = Self::codec_for_ext(ext)?;
                        Self::decode_expr_bytes(cx, &codec, &bytes)
                    }
                };
                match expr {
                    Ok(expr) => cx.factory().expr(expr),
                    Err(_) => cx.factory().nil(),
                }
            }
            None => cx.factory().nil(),
        }
    }

    fn keys(&self, cx: &mut Cx) -> Result<Vec<Symbol>> {
        require_table_fs_read(cx)?;
        let mut keys = BTreeSet::new();
        let entries = std::fs::read_dir(&self.root)
            .map_err(|err| Error::Eval(format!("table/fs: read_dir {err}")))?;
        for entry in entries {
            let entry = entry.map_err(|err| Error::Eval(format!("table/fs: {err}")))?;
            let path = entry.path();
            self.ensure_within_root(&path)?;
            let name = entry.file_name().to_string_lossy().to_string();
            if name.starts_with('.') {
                continue;
            }
            if path.is_dir() {
                keys.insert(Symbol::new(name));
                continue;
            }
            let Some(stem) = known_exts().into_iter().find_map(|ext| {
                name.strip_suffix(&format!(".{ext}"))
                    .map(std::borrow::ToOwned::to_owned)
            }) else {
                continue;
            };
            keys.insert(Symbol::new(stem));
        }
        Ok(keys.into_iter().collect())
    }

    fn entries(&self, cx: &mut Cx) -> Result<Vec<(Symbol, Value)>> {
        require_table_fs_read(cx)?;
        let mut entries = Vec::new();
        for key in self.keys(cx)? {
            if self.is_dir(cx, key.clone())? {
                continue;
            }
            entries.push((key.clone(), self.get(cx, key)?));
        }
        Ok(entries)
    }

    fn len(&self, cx: &mut Cx) -> Result<usize> {
        Ok(self.entries(cx)?.len())
    }

    fn clear(&self, cx: &mut Cx) -> Result<()> {
        require_table_fs_write(cx)?;
        for key in self.keys(cx)? {
            if !self.is_dir(cx, key.clone())? {
                let _ = self.del(cx, key)?;
            }
        }
        Ok(())
    }
}

impl Dir for FsDir {
    fn mkdir(&self, cx: &mut Cx, name: Symbol) -> Result<Value> {
        require_table_fs_write(cx)?;
        let path = self.segment(&name)?;
        if self.leaf_path_for_read(&name)?.is_some() {
            return Err(Error::Eval(format!("table/fs: {name} is a file")));
        }
        std::fs::create_dir_all(&path)
            .map_err(|err| Error::Eval(format!("table/fs: mkdir {err}")))?;
        cx.factory().opaque(Arc::new(Self::open(path)?))
    }

    fn opendir(&self, cx: &mut Cx, name: Symbol) -> Result<Option<Value>> {
        require_table_fs_read(cx)?;
        let path = self.segment(&name)?;
        if path.is_dir() {
            Ok(Some(cx.factory().opaque(Arc::new(Self::open(path)?))?))
        } else if path.exists() || self.leaf_path_for_read(&name)?.is_some() {
            Err(Error::Eval(format!("table/fs: {name} is not a directory")))
        } else {
            Ok(None)
        }
    }

    fn rmdir(&self, cx: &mut Cx, name: Symbol) -> Result<Value> {
        require_table_fs_write(cx)?;
        let path = self.segment(&name)?;
        if !path.is_dir() {
            return Err(Error::Eval(format!("table/fs: {name} is not a directory")));
        }
        std::fs::remove_dir_all(&path)
            .map_err(|err| Error::Eval(format!("table/fs: rmdir {err}")))?;
        cx.factory().nil()
    }

    fn is_dir(&self, cx: &mut Cx, name: Symbol) -> Result<bool> {
        require_table_fs_read(cx)?;
        Ok(self.segment(&name)?.is_dir())
    }
}

/// Opens a filesystem table at `root` and returns it as a runtime table value.
///
/// Requires the table-fs capability; the returned value wraps an [`FsDir`].
pub fn install_fs_dir_lib(cx: &mut Cx, root: &str) -> Result<Value> {
    cx.require(&table_fs_capability())?;
    let dir = FsDir::open(PathBuf::from(root))?;
    cx.factory().opaque(Arc::new(dir))
}