surjection-core 1.2.0

Core library of Surjection: MCP servers, skills and secret references for AI coding clients
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
//! File system facade. Core code takes `&dyn Fs` so tests can confine every
//! access to a temp root (`surjection_testkit::RootedFs`).

use std::borrow::Cow;
use std::fs::File;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use crate::access::WriteProbe;
use crate::paths::extended;

pub trait Fs: Send + Sync {
    /// The directory this view is confined to, or `None` for the whole file
    /// system. Ancestor walks (such as sync-root detection) stop here.
    fn root(&self) -> Option<&Path>;

    fn exists(&self, path: &Path) -> bool;

    fn is_dir(&self, path: &Path) -> bool;

    fn read_to_string(&self, path: &Path) -> io::Result<String>;

    /// The bytes of a file.
    fn read(&self, path: &Path) -> io::Result<Vec<u8>>;

    /// The size of a file in bytes. The default reads the file; real file
    /// systems answer from its metadata.
    fn file_size(&self, path: &Path) -> io::Result<u64> {
        self.read(path).map(|bytes| bytes.len() as u64)
    }

    fn write(&self, path: &Path, contents: &[u8]) -> io::Result<()>;

    /// Creates `path`, which must not exist, writes `contents` and flushes
    /// them to disk. With `like`, the new file gets the permissions of that
    /// existing file: its mode on Unix, its attributes and DACL on Windows.
    /// Used for the temp file of an atomic replace (SPEC 8.4).
    fn write_new(&self, path: &Path, contents: &[u8], like: Option<&Path>) -> io::Result<()>;

    /// Moves `from` over `to`, replacing it atomically. On Unix the
    /// directory is flushed afterwards.
    fn replace(&self, from: &Path, to: &Path) -> io::Result<()>;

    /// Writes `contents` to `path` (created or truncated) so that only the
    /// current user can read or write it: mode 0600 on Unix, a protected
    /// DACL granting only the file's owner on Windows. Flushes to disk.
    fn write_private(&self, path: &Path, contents: &[u8]) -> io::Result<()>;

    fn create_dir_all(&self, path: &Path) -> io::Result<()>;

    /// Creates `path` and its missing parents; `path` itself is restricted
    /// to the current user (mode 0700 on Unix, a protected DACL granting
    /// only the owner, inherited by new children, on Windows).
    fn create_private_dir_all(&self, path: &Path) -> io::Result<()>;

    fn remove_file(&self, path: &Path) -> io::Result<()>;

    /// Whether `path` itself is a regular file: links are not followed, so
    /// a symbolic link, a junction or another reparse point is not one.
    fn is_regular_file(&self, path: &Path) -> bool;

    /// Overwrites every byte of the existing regular file `path` with zero,
    /// in place: the file is opened for writing without being created or
    /// truncated, as many zero bytes as it holds are written from offset 0,
    /// and the file is flushed to disk (`sync_all`). Its length stays the
    /// same. Fails when `path` is missing or not a regular file.
    fn overwrite_with_zeros(&self, path: &Path) -> io::Result<()>;

    fn remove_dir_all(&self, path: &Path) -> io::Result<()>;

    /// Paths of the entries of a directory, sorted.
    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>>;

    /// Opens `path` for use as an advisory lock file, creating it if needed.
    /// The contents are never read or written.
    fn open_lock_file(&self, path: &Path) -> io::Result<File>;

    /// `path` with symlinks and junctions resolved (`std::fs::canonicalize`;
    /// on Windows a `\\?\` path). Views that cannot resolve links return
    /// `path` as it is.
    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
        Ok(path.to_path_buf())
    }

    /// Notes that what the caller works out depends on the content of
    /// `path`, which it reads some other way (git2 reading a repository's
    /// index). Nothing here; a recording view takes note (the plan cache,
    /// [`crate::apply::cache`]).
    fn note_dependency(&self, _path: &Path) {}

    /// A probe of whether this account can write a path (SPEC 8.2
    /// `read_only_target`), to run on a worker thread; it never writes.
    /// `None`: this view cannot probe, so every target counts as
    /// [`WriteAccess::Unknown`](crate::access::WriteAccess::Unknown).
    fn write_probe(&self) -> Option<WriteProbe> {
        None
    }
}

/// The real file system, unconfined.
#[derive(Debug, Clone, Copy, Default)]
pub struct RealFs;

/// `path` as [`RealFs`] hands it to the OS: on Windows, in its `\\?\` form
/// ([`extended`]) once it is long enough to reach `MAX_PATH`, so names
/// built from whole paths (backup copies) work past it (SPEC 16.1). std
/// does this for its own calls; the Windows security calls need it too.
/// Paths given back to callers stay as they were given.
pub(crate) fn long(path: &Path) -> Cow<'_, Path> {
    // std's threshold: `MAX_PATH` less room for an 8.3 file name.
    const LIMIT: usize = 248;
    if cfg!(windows) && path.as_os_str().len() >= LIMIT {
        extended(path)
    } else {
        Cow::Borrowed(path)
    }
}

impl Fs for RealFs {
    fn root(&self) -> Option<&Path> {
        None
    }

    fn exists(&self, path: &Path) -> bool {
        long(path).exists()
    }

    fn is_dir(&self, path: &Path) -> bool {
        long(path).is_dir()
    }

    fn read_to_string(&self, path: &Path) -> io::Result<String> {
        std::fs::read_to_string(long(path))
    }

    fn read(&self, path: &Path) -> io::Result<Vec<u8>> {
        std::fs::read(long(path))
    }

    fn file_size(&self, path: &Path) -> io::Result<u64> {
        std::fs::metadata(long(path)).map(|meta| meta.len())
    }

    fn write(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
        std::fs::write(long(path), contents)
    }

    fn write_new(&self, path: &Path, contents: &[u8], like: Option<&Path>) -> io::Result<()> {
        let path = long(path);
        let like = like.map(long);
        let mut options = File::options();
        options.write(true).create_new(true);
        if let Some(like) = &like {
            platform::copy_open_options(&mut options, like)?;
        }
        let mut file = options.open(&path)?;
        let written = (|| {
            if let Some(like) = &like {
                platform::copy_permissions(like, &path, &file)?;
            }
            file.write_all(contents)?;
            file.sync_all()
        })();
        if written.is_err() {
            drop(file);
            let _ = std::fs::remove_file(&path);
        }
        written
    }

    fn replace(&self, from: &Path, to: &Path) -> io::Result<()> {
        let to = long(to);
        std::fs::rename(long(from), &to)?;
        platform::sync_parent(&to)
    }

    fn write_private(&self, path: &Path, contents: &[u8]) -> io::Result<()> {
        let path = long(path);
        let mut options = File::options();
        options.write(true).create(true).truncate(true);
        platform::private_open_options(&mut options);
        let mut file = options.open(&path)?;
        // Restrict before any content lands, also for an existing file.
        platform::make_private(&path, &file)?;
        file.write_all(contents)?;
        file.sync_all()
    }

    fn create_dir_all(&self, path: &Path) -> io::Result<()> {
        std::fs::create_dir_all(long(path))
    }

    fn create_private_dir_all(&self, path: &Path) -> io::Result<()> {
        let path = long(path);
        std::fs::create_dir_all(&path)?;
        platform::make_private_dir(&path)
    }

    fn remove_file(&self, path: &Path) -> io::Result<()> {
        std::fs::remove_file(long(path))
    }

    fn is_regular_file(&self, path: &Path) -> bool {
        std::fs::symlink_metadata(long(path)).is_ok_and(|meta| meta.file_type().is_file())
    }

    fn overwrite_with_zeros(&self, path: &Path) -> io::Result<()> {
        let path = long(path);
        if !std::fs::symlink_metadata(&path)?.file_type().is_file() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "not a regular file",
            ));
        }
        // No `create`, no `truncate`: the zeros land on the file's own
        // blocks (as far as the file system allows), never on new ones.
        let mut file = File::options().write(true).open(&path)?;
        let mut left = file.metadata()?.len();
        let zeros = [0u8; 64 * 1024];
        while left > 0 {
            let chunk = usize::try_from(left).map_or(zeros.len(), |left| left.min(zeros.len()));
            file.write_all(&zeros[..chunk])?;
            left -= chunk as u64;
        }
        file.sync_all()
    }

    fn remove_dir_all(&self, path: &Path) -> io::Result<()> {
        std::fs::remove_dir_all(long(path))
    }

    fn read_dir(&self, path: &Path) -> io::Result<Vec<PathBuf>> {
        // Entries are joined to `path` as given, never to its `\\?\` form.
        let mut entries = std::fs::read_dir(long(path))?
            .map(|entry| entry.map(|entry| path.join(entry.file_name())))
            .collect::<io::Result<Vec<_>>>()?;
        entries.sort();
        Ok(entries)
    }

    fn open_lock_file(&self, path: &Path) -> io::Result<File> {
        File::options()
            .read(true)
            .write(true)
            .create(true)
            .truncate(false)
            .open(long(path))
    }

    fn canonicalize(&self, path: &Path) -> io::Result<PathBuf> {
        std::fs::canonicalize(long(path))
    }

    fn write_probe(&self) -> Option<WriteProbe> {
        Some(Arc::new(crate::access::probe))
    }
}

#[cfg(unix)]
mod platform {
    use std::fs::{File, OpenOptions, Permissions};
    use std::io;
    use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
    use std::path::Path;

    pub fn copy_open_options(options: &mut OpenOptions, like: &Path) -> io::Result<()> {
        let mode = std::fs::metadata(like)?.permissions().mode();
        options.mode(mode & 0o7777);
        Ok(())
    }

    /// The umask may have narrowed the mode given at creation.
    pub fn copy_permissions(like: &Path, _path: &Path, file: &File) -> io::Result<()> {
        let mode = std::fs::metadata(like)?.permissions().mode();
        file.set_permissions(Permissions::from_mode(mode & 0o7777))
    }

    pub fn private_open_options(options: &mut OpenOptions) {
        options.mode(0o600);
    }

    pub fn make_private(_path: &Path, file: &File) -> io::Result<()> {
        file.set_permissions(Permissions::from_mode(0o600))
    }

    pub fn make_private_dir(path: &Path) -> io::Result<()> {
        std::fs::set_permissions(path, Permissions::from_mode(0o700))
    }

    /// Flushes the directory entry of a rename.
    pub fn sync_parent(path: &Path) -> io::Result<()> {
        match path.parent().filter(|dir| !dir.as_os_str().is_empty()) {
            Some(dir) => File::open(dir)?.sync_all(),
            None => Ok(()),
        }
    }
}

#[cfg(windows)]
mod platform {
    use std::fs::{File, OpenOptions};
    use std::io;
    use std::os::windows::fs::{MetadataExt, OpenOptionsExt};
    use std::path::Path;

    use windows_permissions::constants::{AceFlags, SeObjectType, SecurityInformation};
    use windows_permissions::{LocalBox, SecurityDescriptor, wrappers};

    /// Attributes a replaced file keeps (as `ReplaceFileW` does). Read-only
    /// is not carried over: the replace itself would fail on it.
    const KEPT_ATTRIBUTES: u32 = 0x2 // hidden
        | 0x4 // system
        | 0x20 // archive
        | 0x2000; // not content indexed

    pub fn copy_open_options(options: &mut OpenOptions, like: &Path) -> io::Result<()> {
        let attributes = std::fs::metadata(like)?.file_attributes() & KEPT_ATTRIBUTES;
        options.attributes(attributes);
        Ok(())
    }

    /// Gives `path` the DACL of `like`. A DACL made only of inherited
    /// entries is already right: the new file sits in the same directory.
    pub fn copy_permissions(like: &Path, path: &Path, _file: &File) -> io::Result<()> {
        let sd = wrappers::GetNamedSecurityInfo(
            like.as_os_str(),
            SeObjectType::SE_FILE_OBJECT,
            SecurityInformation::Dacl,
        )?;
        let sddl = sd.as_sddl()?.to_string_lossy().into_owned();
        let protected = dacl_flags(&sddl).contains('P');
        let Some(dacl) = sd.dacl() else {
            return Ok(());
        };
        let explicit = (0..dacl.len())
            .filter_map(|index| dacl.get_ace(index))
            .any(|ace| !ace.flags().contains(AceFlags::Inherited));
        if !protected && !explicit {
            return Ok(());
        }
        let protection = if protected {
            SecurityInformation::ProtectedDacl
        } else {
            SecurityInformation::UnprotectedDacl
        };
        wrappers::SetNamedSecurityInfo(
            path.as_os_str(),
            SeObjectType::SE_FILE_OBJECT,
            SecurityInformation::Dacl | protection,
            None,
            None,
            Some(dacl),
            None,
        )
    }

    /// The control flags of the DACL in an SDDL string (`D:PAI(...)` gives
    /// `P` plus `AI`), without the auto-inherit flags.
    fn dacl_flags(sddl: &str) -> String {
        let Some(start) = sddl.find("D:") else {
            return String::new();
        };
        let rest = &sddl[start + 2..];
        let end = rest.find('(').unwrap_or(rest.len());
        rest[..end].replace("AI", "").replace("AR", "")
    }

    pub fn private_open_options(_options: &mut OpenOptions) {}

    pub fn make_private(path: &Path, _file: &File) -> io::Result<()> {
        owner_only(path, false)
    }

    pub fn make_private_dir(path: &Path) -> io::Result<()> {
        owner_only(path, true)
    }

    /// A protected DACL with one entry: full control for the owner of
    /// `path` (the current user for anything this process creates).
    fn owner_only(path: &Path, dir: bool) -> io::Result<()> {
        let sd = wrappers::GetNamedSecurityInfo(
            path.as_os_str(),
            SeObjectType::SE_FILE_OBJECT,
            SecurityInformation::Owner,
        )?;
        let owner = sd
            .owner()
            .ok_or_else(|| io::Error::other("the file has no owner"))?;
        let inherit = if dir { "OICI" } else { "" };
        let sddl = format!("D:P(A;{inherit};FA;;;{owner})");
        let private: LocalBox<SecurityDescriptor> = sddl.parse()?;
        let dacl = private
            .dacl()
            .ok_or_else(|| io::Error::other("the private DACL did not parse"))?;
        wrappers::SetNamedSecurityInfo(
            path.as_os_str(),
            SeObjectType::SE_FILE_OBJECT,
            SecurityInformation::Dacl | SecurityInformation::ProtectedDacl,
            None,
            None,
            Some(dacl),
            None,
        )
    }

    /// NTFS makes a completed rename durable without a directory flush.
    pub fn sync_parent(_path: &Path) -> io::Result<()> {
        Ok(())
    }
}