binoc-sdk 0.1.0

Plugin SDK and ABI for Binoc dataset-diff plugins
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
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Mutex;

use crate::types::{ArtifactDescriptor, ArtifactFormat, ArtifactSubject};
use crate::{BinocError, BinocResult, DataAccess, ItemRef};

/// In-process DataAccess backed by the local filesystem, temp directories,
/// and a filesystem-backed artifact store under `data_root/.artifacts/`.
///
/// Construction modes:
///
/// - [`Self::new`] — unrestricted paths (tests, ad-hoc tooling).
/// - [`Self::new_for_diff`] — paths must stay under the two snapshot trees or
///   session workspace (used by the controller).
/// - [`Self::for_plugin`] — shares the host's `data_root` for artifact access,
///   plus a pre-allocated workspace for expansion (C ABI plugins).
/// - [`Self::with_data_root`] — shares an existing `data_root` for artifact
///   reads only (no expansion workspace).
pub struct LocalDataAccess {
    _session_dir: Option<tempfile::TempDir>,
    data_root: PathBuf,
    external_root: Option<PathBuf>,
    workspace_counter: AtomicU32,
    workspaces: Mutex<Vec<tempfile::TempDir>>,
    provide_dir: Mutex<Option<tempfile::TempDir>>,
    path_policy: PathPolicy,
}

enum PathPolicy {
    Unrestricted,
    Restricted {
        snapshot_a: PathBuf,
        snapshot_b: PathBuf,
        extra_allowed: Mutex<Vec<PathBuf>>,
    },
}

fn artifacts_dir(data_root: &Path) -> PathBuf {
    data_root.join(".artifacts")
}

fn safe_name(s: &str) -> String {
    s.bytes()
        .map(|b| {
            if b.is_ascii_alphanumeric() || b == b'-' || b == b'_' || b == b'.' {
                (b as char).to_string()
            } else {
                format!("%{b:02x}")
            }
        })
        .collect()
}

fn subject_dir_name(subject: ArtifactSubject) -> &'static str {
    match subject {
        ArtifactSubject::Left => "left",
        ArtifactSubject::Right => "right",
        ArtifactSubject::Pair => "pair",
    }
}

/// True when `path` is `root` or a descendant (component-wise).
fn path_is_within(path: &Path, root: &Path) -> bool {
    path.starts_with(root)
}

fn item_ref_from_physical(physical: &Path, logical: &str) -> ItemRef {
    ItemRef {
        logical_path: logical.to_string(),
        is_dir: physical.is_dir(),
        content_hash: None,
        media_type: None,
        handle: physical.to_string_lossy().to_string(),
    }
}

impl LocalDataAccess {
    pub fn new() -> Self {
        let session = tempfile::tempdir().expect("failed to create session temp dir");
        let data_root = session.path().to_path_buf();
        Self {
            _session_dir: Some(session),
            data_root,
            external_root: None,
            workspace_counter: AtomicU32::new(0),
            workspaces: Mutex::new(Vec::new()),
            provide_dir: Mutex::new(None),
            path_policy: PathPolicy::Unrestricted,
        }
    }

    /// Session-backed access with path confinement: filesystem reads and
    /// `register_local` targets must lie under the snapshot roots, the session
    /// `data_root`, or a workspace / provide directory created by this instance.
    pub fn new_for_diff(snapshot_a: &Path, snapshot_b: &Path) -> BinocResult<Self> {
        let session = tempfile::tempdir().map_err(BinocError::Io)?;
        let data_root = session.path().to_path_buf();
        let snap_a = std::fs::canonicalize(snapshot_a).map_err(BinocError::Io)?;
        let snap_b = std::fs::canonicalize(snapshot_b).map_err(BinocError::Io)?;
        let data_root_canon = std::fs::canonicalize(&data_root).map_err(BinocError::Io)?;
        Ok(Self {
            _session_dir: Some(session),
            data_root,
            external_root: None,
            workspace_counter: AtomicU32::new(0),
            workspaces: Mutex::new(Vec::new()),
            provide_dir: Mutex::new(None),
            path_policy: PathPolicy::Restricted {
                snapshot_a: snap_a,
                snapshot_b: snap_b,
                extra_allowed: Mutex::new(vec![data_root_canon]),
            },
        })
    }

    /// Create a LocalDataAccess for a plugin running across the C ABI.
    /// Shares the host's `data_root` for cache access and uses `workspace`
    /// for expansion (provide, workspace calls).
    pub fn for_plugin(data_root: PathBuf, workspace: PathBuf) -> Self {
        Self {
            _session_dir: None,
            data_root,
            external_root: Some(workspace),
            workspace_counter: AtomicU32::new(0),
            workspaces: Mutex::new(Vec::new()),
            provide_dir: Mutex::new(None),
            path_policy: PathPolicy::Unrestricted,
        }
    }

    /// Create a LocalDataAccess that can only read from an existing data_root
    /// cache. No workspace for expansion. Used during extract-only access.
    pub fn with_data_root(data_root: PathBuf) -> Self {
        Self {
            _session_dir: None,
            data_root,
            external_root: None,
            workspace_counter: AtomicU32::new(0),
            workspaces: Mutex::new(Vec::new()),
            provide_dir: Mutex::new(None),
            path_policy: PathPolicy::Unrestricted,
        }
    }

    fn record_allowed_if_restricted(&self, path: &Path) -> BinocResult<()> {
        if let PathPolicy::Restricted { extra_allowed, .. } = &self.path_policy {
            let c = std::fs::canonicalize(path).map_err(BinocError::Io)?;
            extra_allowed.lock().unwrap().push(c);
        }
        Ok(())
    }

    fn enforce_path_policy_resolved(&self, resolved: &Path) -> BinocResult<()> {
        match &self.path_policy {
            PathPolicy::Unrestricted => Ok(()),
            PathPolicy::Restricted {
                snapshot_a,
                snapshot_b,
                extra_allowed,
            } => {
                if path_is_within(resolved, snapshot_a) || path_is_within(resolved, snapshot_b) {
                    return Ok(());
                }
                let roots = extra_allowed.lock().unwrap();
                for root in roots.iter() {
                    if path_is_within(resolved, root) {
                        return Ok(());
                    }
                }
                Err(BinocError::PathPolicy(format!(
                    "path must stay under snapshot directories or session workspace: {}",
                    resolved.display()
                )))
            }
        }
    }

    /// Enforce policy for a path that must already exist on disk (e.g. `register_local`).
    fn enforce_path_policy(&self, physical: &Path) -> BinocResult<()> {
        let resolved = std::fs::canonicalize(physical).map_err(BinocError::Io)?;
        self.enforce_path_policy_resolved(&resolved)
    }

    /// Enforce policy before reading; allows missing leaf paths under an allowed directory.
    fn enforce_policy_for_read_path(&self, path: &Path) -> BinocResult<()> {
        match &self.path_policy {
            PathPolicy::Unrestricted => Ok(()),
            PathPolicy::Restricted { .. } => {
                if let Ok(c) = std::fs::canonicalize(path) {
                    return self.enforce_path_policy_resolved(&c);
                }
                let mut probe: Option<&Path> = Some(path);
                while let Some(p) = probe {
                    if p.as_os_str().is_empty() {
                        break;
                    }
                    if p.exists() {
                        let base = std::fs::canonicalize(p).map_err(BinocError::Io)?;
                        self.enforce_path_policy_resolved(&base)?;
                        return Ok(());
                    }
                    probe = p.parent();
                }
                Err(BinocError::PathPolicy(format!(
                    "cannot resolve path under session: {}",
                    path.display()
                )))
            }
        }
    }

    fn ensure_provide_dir(&self) -> BinocResult<PathBuf> {
        if let Some(root) = &self.external_root {
            let d = root.join("_provide");
            std::fs::create_dir_all(&d).map_err(BinocError::Io)?;
            self.record_allowed_if_restricted(&d)?;
            return Ok(d);
        }
        let mut guard = self.provide_dir.lock().unwrap();
        if guard.is_none() {
            let dir = tempfile::tempdir().map_err(BinocError::Io)?;
            self.record_allowed_if_restricted(dir.path())?;
            *guard = Some(dir);
        }
        Ok(guard.as_ref().unwrap().path().to_path_buf())
    }
}

impl Default for LocalDataAccess {
    fn default() -> Self {
        Self::new()
    }
}

impl DataAccess for LocalDataAccess {
    fn read_bytes(&self, item: &ItemRef) -> BinocResult<Vec<u8>> {
        let p = Path::new(&item.handle);
        self.enforce_policy_for_read_path(p)?;
        std::fs::read(p).map_err(BinocError::Io)
    }

    fn open_read(&self, item: &ItemRef) -> BinocResult<Box<dyn std::io::Read + Send>> {
        let p = Path::new(&item.handle);
        self.enforce_policy_for_read_path(p)?;
        let file = std::fs::File::open(p).map_err(BinocError::Io)?;
        Ok(Box::new(file))
    }

    fn local_path(&self, item: &ItemRef) -> BinocResult<PathBuf> {
        let p = PathBuf::from(&item.handle);
        self.enforce_policy_for_read_path(&p)?;
        Ok(p)
    }

    fn provide(&self, logical_path: &str, content: &[u8]) -> BinocResult<ItemRef> {
        let dir = self.ensure_provide_dir()?;
        let safe_name = logical_path.replace(['/', '\\'], "_");
        let file_path = dir.join(&safe_name);
        std::fs::write(&file_path, content).map_err(BinocError::Io)?;
        self.enforce_path_policy(&file_path)?;
        Ok(item_ref_from_physical(&file_path, logical_path))
    }

    fn workspace(&self) -> BinocResult<PathBuf> {
        if let Some(root) = &self.external_root {
            let n = self.workspace_counter.fetch_add(1, Ordering::Relaxed);
            let subdir = root.join(format!("ws-{n}"));
            std::fs::create_dir_all(&subdir).map_err(BinocError::Io)?;
            self.record_allowed_if_restricted(&subdir)?;
            return Ok(subdir);
        }
        let dir = tempfile::tempdir().map_err(BinocError::Io)?;
        let path = dir.path().to_path_buf();
        self.record_allowed_if_restricted(&path)?;
        self.workspaces.lock().unwrap().push(dir);
        Ok(path)
    }

    fn register_local(&self, physical: &Path, logical: &str) -> BinocResult<ItemRef> {
        self.enforce_path_policy(physical)?;
        Ok(item_ref_from_physical(physical, logical))
    }

    fn publish_artifact(
        &self,
        format: &ArtifactFormat,
        subject: ArtifactSubject,
        producer: &str,
        data: &[u8],
    ) -> BinocResult<ArtifactDescriptor> {
        let dir = artifacts_dir(&self.data_root)
            .join(safe_name(&format.package))
            .join(safe_name(&format.name))
            .join(format!("v{}", format.version))
            .join(subject_dir_name(subject));
        std::fs::create_dir_all(&dir).map_err(BinocError::Io)?;
        let handle = dir.join(safe_name(producer)).to_string_lossy().to_string();
        std::fs::write(&handle, data).map_err(BinocError::Io)?;
        Ok(ArtifactDescriptor {
            format: format.clone(),
            subject,
            producer: producer.to_string(),
            handle,
        })
    }

    fn get_artifact(&self, descriptor: &ArtifactDescriptor) -> BinocResult<Option<Vec<u8>>> {
        let path = PathBuf::from(&descriptor.handle);
        self.enforce_policy_for_read_path(&path)?;
        match std::fs::read(&path) {
            Ok(data) => Ok(Some(data)),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(e) => Err(BinocError::Io(e)),
        }
    }

    fn data_root(&self) -> BinocResult<PathBuf> {
        Ok(self.data_root.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn publish_and_get_artifact_round_trip() {
        let da = LocalDataAccess::new();
        let fmt = ArtifactFormat::new("binoc", "tabular", 1);
        let desc = da
            .publish_artifact(&fmt, ArtifactSubject::Left, "binoc.csv", b"hello world")
            .unwrap();
        assert_eq!(desc.format, fmt);
        assert_eq!(desc.subject, ArtifactSubject::Left);
        assert_eq!(desc.producer, "binoc.csv");
        let loaded = da.get_artifact(&desc).unwrap();
        assert_eq!(loaded, Some(b"hello world".to_vec()));
    }

    #[test]
    fn get_artifact_missing_returns_none() {
        let da = LocalDataAccess::new();
        let desc = ArtifactDescriptor {
            format: ArtifactFormat::new("nonexistent", "thing", 1),
            subject: ArtifactSubject::Pair,
            producer: "test".into(),
            handle: "/tmp/does-not-exist-binoc-test".into(),
        };
        assert_eq!(da.get_artifact(&desc).unwrap(), None);
    }

    #[test]
    fn cross_instance_artifact_visibility() {
        let da = LocalDataAccess::new();
        let fmt = ArtifactFormat::new("binoc", "tabular", 1);
        let desc = da
            .publish_artifact(&fmt, ArtifactSubject::Right, "binoc.csv", b"shared-value")
            .unwrap();
        let data_root = da.data_root().unwrap();

        let plugin_da = LocalDataAccess::with_data_root(data_root);
        let loaded = plugin_da.get_artifact(&desc).unwrap();
        assert_eq!(loaded, Some(b"shared-value".to_vec()));
    }

    #[test]
    fn for_plugin_shares_artifacts() {
        let da = LocalDataAccess::new();
        let data_root = da.data_root().unwrap();
        let ws = da.workspace().unwrap();

        let plugin_da = LocalDataAccess::for_plugin(data_root, ws);
        let fmt = ArtifactFormat::new("myplugin", "schema", 1);
        let desc = plugin_da
            .publish_artifact(&fmt, ArtifactSubject::Pair, "myplugin", b"plugin-data")
            .unwrap();

        let loaded = da.get_artifact(&desc).unwrap();
        assert_eq!(loaded, Some(b"plugin-data".to_vec()));
    }

    #[test]
    fn data_root_returns_valid_path() {
        let da = LocalDataAccess::new();
        let root = da.data_root().unwrap();
        assert!(root.exists());
    }

    #[test]
    fn restricted_rejects_register_outside_snapshots() {
        let tmp_a = tempfile::tempdir().unwrap();
        let tmp_b = tempfile::tempdir().unwrap();
        let outside = tempfile::tempdir().unwrap();
        std::fs::write(outside.path().join("x.txt"), b"x").unwrap();

        let da = LocalDataAccess::new_for_diff(tmp_a.path(), tmp_b.path()).unwrap();
        let p = outside.path().join("x.txt");
        let err = da.register_local(&p, "x.txt").unwrap_err();
        assert!(matches!(err, BinocError::PathPolicy(_)));
    }

    #[test]
    fn restricted_allows_register_under_snapshot() {
        let tmp_a = tempfile::tempdir().unwrap();
        let tmp_b = tempfile::tempdir().unwrap();
        let f = tmp_a.path().join("f.txt");
        std::fs::write(&f, b"ok").unwrap();

        let da = LocalDataAccess::new_for_diff(tmp_a.path(), tmp_b.path()).unwrap();
        da.register_local(&f, "f.txt").unwrap();
    }
}