zerofs-client 0.2.0

Async, path-based client library for ZeroFS over 9P
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
use ninep_proto::{Rstatfs, Stat};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// Options for [`crate::Client::connect_with`]. Defaults are representable in
/// UniFFI records. Rust resolves `None` identity fields during connect.
///
/// The API has no per-operation timeout. Cancellation reclaims temporary
/// operation resources but does not revoke a dispatched mutation.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ConnectOptions {
    /// Numeric uid asserted at attach (`None` = process euid natively, 0 in a
    /// browser); the server enforces permissions as this user.
    pub uid: Option<u32>,
    /// Group assigned to files/directories created through this client
    /// (`None` = process egid natively, 0 in a browser).
    pub gid: Option<u32>,
    /// Username string sent at attach (`None` = `$USER`, else the uid rendered
    /// as text); informational; `uid` is authoritative.
    pub uname: Option<String>,
    /// Attach name (export selector); empty selects the default export.
    pub aname: String,
    /// Requested 9P message size; the negotiated value appears in [`Capabilities`].
    pub msize: u32,
    /// Bound on the initial connect+attach; expiry surfaces as
    /// [`crate::ZeroFsError::ConnectFailed`]. `None` = wait indefinitely.
    pub connect_timeout_ms: Option<u32>,
}

impl Default for ConnectOptions {
    fn default() -> Self {
        Self {
            uid: None,
            gid: None,
            uname: None,
            aname: String::new(),
            msize: 1024 * 1024,
            connect_timeout_ms: Some(30_000),
        }
    }
}

/// Negotiated session properties, fixed for the logical session lifetime.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Capabilities {
    /// Negotiated 9P message size in bytes.
    pub msize: u32,
    /// Largest single-message read payload (larger reads chunk transparently).
    pub max_read_chunk: u32,
    /// Largest single-message write payload (larger writes chunk transparently).
    pub max_write_chunk: u32,
}

/// FFI-compatible open options. Defaults are false and mode 420 (`0o644`).
/// Append is exposed as [`crate::Client::append`].
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct OpenOptions {
    /// Open for reading.
    pub read: bool,
    /// Open for writing.
    pub write: bool,
    /// Open-or-create. Not a single wire op: composed as open, falling back to
    /// exclusive create, retrying on race.
    pub create: bool,
    /// Fail with `AlreadyExists` unless this call creates the file. This is
    /// the atomic primitive (server creates are natively exclusive).
    pub create_new: bool,
    /// Truncate to zero length on open. Existing-file truncation is a separate,
    /// non-atomic setattr request.
    pub truncate: bool,
    /// Permission bits when the open creates the file. Default 0o644.
    pub mode: u32,
}

impl Default for OpenOptions {
    fn default() -> Self {
        Self {
            read: false,
            write: false,
            create: false,
            create_new: false,
            truncate: false,
            mode: 0o644,
        }
    }
}

impl OpenOptions {
    /// `read` only.
    pub fn read_only() -> Self {
        Self {
            read: true,
            ..Self::default()
        }
    }

    /// `write` only.
    pub fn write_only() -> Self {
        Self {
            write: true,
            ..Self::default()
        }
    }

    /// `read` + `write`.
    pub fn read_write() -> Self {
        Self {
            read: true,
            write: true,
            ..Self::default()
        }
    }

    /// Builder-style toggle for `create`.
    pub fn create(mut self, yes: bool) -> Self {
        self.create = yes;
        self
    }

    /// Builder-style toggle for `create_new`.
    pub fn create_new(mut self, yes: bool) -> Self {
        self.create_new = yes;
        self
    }

    /// Builder-style toggle for `truncate`.
    pub fn truncate(mut self, yes: bool) -> Self {
        self.truncate = yes;
        self
    }

    /// Builder-style setter for the creation mode.
    pub fn mode(mut self, mode: u32) -> Self {
        self.mode = mode;
        self
    }
}

/// File type derived from the mode/dirent type.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FileType {
    /// Regular file.
    File,
    /// Directory.
    Dir,
    /// Symbolic link.
    Symlink,
    /// Named pipe (FIFO).
    Fifo,
    /// Unix-domain socket node.
    Socket,
    /// Character device.
    CharDevice,
    /// Block device.
    BlockDevice,
    /// Unrecognized type.
    Unknown,
}

impl FileType {
    pub(crate) fn from_mode(mode: u32) -> Self {
        match mode & crate::linux::S_IFMT {
            x if x == crate::linux::S_IFREG => Self::File,
            x if x == crate::linux::S_IFDIR => Self::Dir,
            x if x == crate::linux::S_IFLNK => Self::Symlink,
            x if x == crate::linux::S_IFIFO => Self::Fifo,
            x if x == crate::linux::S_IFSOCK => Self::Socket,
            x if x == crate::linux::S_IFCHR => Self::CharDevice,
            x if x == crate::linux::S_IFBLK => Self::BlockDevice,
            _ => Self::Unknown,
        }
    }
}

/// Nanosecond UNIX timestamp as explicit fields (predictable across all bindings).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Timestamp {
    /// Whole seconds since the UNIX epoch (negative before it).
    pub secs: i64,
    /// Nanoseconds within the second, `0..1_000_000_000`.
    pub nanos: u32,
}

impl From<SystemTime> for Timestamp {
    fn from(t: SystemTime) -> Self {
        match t.duration_since(UNIX_EPOCH) {
            Ok(d) => Timestamp {
                secs: d.as_secs() as i64,
                nanos: d.subsec_nanos(),
            },
            // Pre-epoch: split so `secs + nanos/1e9` still equals the instant
            // (the inverse of the decoding in `systime`).
            Err(e) => {
                let d = e.duration();
                let (secs, nanos) = (d.as_secs() as i64, d.subsec_nanos());
                if nanos == 0 {
                    Timestamp {
                        secs: -secs,
                        nanos: 0,
                    }
                } else {
                    Timestamp {
                        secs: -secs - 1,
                        nanos: 1_000_000_000 - nanos,
                    }
                }
            }
        }
    }
}

impl From<Timestamp> for SystemTime {
    fn from(t: Timestamp) -> Self {
        systime(t)
    }
}

/// A time to set: the server's current clock, or an explicit instant.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SetTime {
    /// Use the server's current clock.
    Now,
    /// Set to this explicit instant.
    At {
        /// The instant to set.
        time: Timestamp,
    },
}

impl From<SystemTime> for SetTime {
    fn from(t: SystemTime) -> Self {
        SetTime::At { time: t.into() }
    }
}

/// Metadata changes; `None` fields are untouched. All-`None` is a no-op.
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SetAttrs {
    /// New permission bits (low 12 bits used).
    pub mode: Option<u32>,
    /// New owner uid.
    pub uid: Option<u32>,
    /// New owner gid.
    pub gid: Option<u32>,
    /// New length (truncates or extends).
    pub size: Option<u64>,
    /// New access time.
    pub atime: Option<SetTime>,
    /// New modification time.
    pub mtime: Option<SetTime>,
}

/// Kind of special node for `mknod`; a tagged enum so callers never pack
/// `S_IF*` bits or pass meaningless major/minor for fifos and sockets.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NodeKind {
    /// Named pipe (FIFO).
    Fifo,
    /// Unix-domain socket node.
    Socket,
    /// Block device with the given major/minor numbers.
    BlockDevice {
        /// Device major number.
        major: u32,
        /// Device minor number.
        minor: u32,
    },
    /// Character device with the given major/minor numbers.
    CharDevice {
        /// Device major number.
        major: u32,
        /// Device minor number.
        minor: u32,
    },
}

/// POSIX-shaped attributes; plain data record everywhere.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Metadata {
    /// Stable, non-reused inode number.
    pub ino: u64,
    /// File type.
    pub file_type: FileType,
    /// Full st_mode (type bits + permission bits).
    pub mode: u32,
    /// Hard-link count.
    pub nlink: u64,
    /// Owner uid.
    pub uid: u32,
    /// Owner gid.
    pub gid: u32,
    /// Size in bytes.
    pub size: u64,
    /// Preferred I/O block size reported by the server.
    pub block_size: u64,
    /// Number of 512-byte blocks allocated.
    pub blocks: u64,
    /// Device id for char/block nodes; 0 otherwise.
    pub rdev: u64,
    /// Last access time.
    pub atime: Timestamp,
    /// Last modification time.
    pub mtime: Timestamp,
    /// Last status-change time.
    pub ctime: Timestamp,
    /// Reserved creation time. Current servers report the epoch.
    pub btime: Timestamp,
    /// Reserved content-change counter. Current servers report zero; use `mtime`
    /// for change detection.
    pub data_version: u64,
}

impl Metadata {
    pub(crate) fn from_stat(st: &Stat) -> Self {
        let ts = |secs: u64, nanos: u64| Timestamp {
            secs: secs as i64,
            nanos: nanos as u32,
        };
        Self {
            ino: st.qid.path,
            file_type: FileType::from_mode(st.mode),
            mode: st.mode,
            nlink: st.nlink,
            uid: st.uid,
            gid: st.gid,
            size: st.size,
            block_size: st.blksize,
            blocks: st.blocks,
            rdev: st.rdev,
            atime: ts(st.atime_sec, st.atime_nsec),
            mtime: ts(st.mtime_sec, st.mtime_nsec),
            ctime: ts(st.ctime_sec, st.ctime_nsec),
            btime: ts(st.btime_sec, st.btime_nsec),
            data_version: st.data_version,
        }
    }

    /// True if this is a regular file.
    pub fn is_file(&self) -> bool {
        self.file_type == FileType::File
    }

    /// True if this is a directory.
    pub fn is_dir(&self) -> bool {
        self.file_type == FileType::Dir
    }

    /// True if this is a symbolic link.
    pub fn is_symlink(&self) -> bool {
        self.file_type == FileType::Symlink
    }

    /// Permission bits only.
    pub fn permissions(&self) -> u32 {
        self.mode & 0o7777
    }

    /// `mtime` as a `SystemTime` (Rust-only sugar).
    pub fn modified(&self) -> SystemTime {
        systime(self.mtime)
    }

    /// `atime` as a `SystemTime` (Rust-only sugar).
    pub fn accessed(&self) -> SystemTime {
        systime(self.atime)
    }
}

fn systime(t: Timestamp) -> SystemTime {
    // Defend against out-of-range / un-normalized wire data: clamp sub-second
    // nanos and saturate rather than panic on an unrepresentable instant.
    let nanos = t.nanos.min(999_999_999);
    if t.secs >= 0 {
        UNIX_EPOCH
            .checked_add(Duration::new(t.secs as u64, nanos))
            .unwrap_or(UNIX_EPOCH)
    } else {
        let base = UNIX_EPOCH
            .checked_sub(Duration::new(t.secs.unsigned_abs(), 0))
            .unwrap_or(UNIX_EPOCH);
        base.checked_add(Duration::new(0, nanos)).unwrap_or(base)
    }
}

/// Filesystem usage, from 9P statfs.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StatFs {
    /// Optimal transfer block size.
    pub block_size: u32,
    /// Total data blocks in the filesystem.
    pub blocks: u64,
    /// Free blocks.
    pub blocks_free: u64,
    /// Free blocks available to unprivileged users.
    pub blocks_available: u64,
    /// Total inodes (files).
    pub files: u64,
    /// Free inodes.
    pub files_free: u64,
    /// Filesystem id.
    pub filesystem_id: u64,
    /// Maximum filename length.
    pub max_name_len: u32,
}

impl StatFs {
    pub(crate) fn from_wire(r: &Rstatfs) -> Self {
        Self {
            block_size: r.bsize,
            blocks: r.blocks,
            blocks_free: r.bfree,
            blocks_available: r.bavail,
            files: r.files,
            files_free: r.ffree,
            filesystem_id: r.fsid,
            max_name_len: r.namelen,
        }
    }
}

/// One directory entry; the library filters out `.` and `..`.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct DirEntry {
    /// Name decoded as UTF-8 (lossy: invalid bytes become U+FFFD).
    pub name: String,
    /// Exact on-wire name bytes; authoritative, feed into the `Dir::*_at`
    /// methods verbatim.
    pub name_bytes: Vec<u8>,
    /// True when `name` losslessly round-trips to `name_bytes`.
    pub name_is_utf8: bool,
    /// Entry type, known without a stat.
    pub file_type: FileType,
    /// Stable, non-reused inode number.
    pub ino: u64,
    /// Full metadata returned by the ZeroFS directory-read operation.
    pub metadata: Metadata,
}

impl DirEntry {
    pub(crate) fn from_plus(e: &ninep_proto::DirEntryPlus) -> Self {
        let name_bytes = e.name.data.clone();
        Self {
            name: String::from_utf8_lossy(&name_bytes).into_owned(),
            name_is_utf8: std::str::from_utf8(&name_bytes).is_ok(),
            file_type: FileType::from_mode(e.stat.mode),
            ino: e.qid.path,
            metadata: Metadata::from_stat(&e.stat),
            name_bytes,
        }
    }
}