Skip to main content

jetstream_9p/
lib.rs

1#![doc(
2    html_logo_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
3)]
4#![doc(
5    html_favicon_url = "https://raw.githubusercontent.com/sevki/jetstream/main/logo/JetStream.png"
6)]
7#![cfg_attr(docsrs, feature(doc_cfg))]
8// Copyright (c) 2024, Sevki <s@sevki.io>
9// Copyright 2018 The ChromiumOS Authors
10// Use of this source code is governed by a BSD-style license that can be
11// found in the LICENSE file.
12
13pub mod messages;
14pub mod ninep_2000_l;
15pub mod server;
16use jetstream_libc as libc;
17
18use std::io;
19
20pub use self::messages::*;
21
22pub fn error_to_rmessage(err: &io::Error) -> Rlerror {
23    let errno = if let Some(errno) = err.raw_os_error() {
24        errno
25    } else {
26        // Make a best-effort guess based on the kind.
27        match err.kind() {
28            io::ErrorKind::NotFound => libc::ENOENT,
29            io::ErrorKind::PermissionDenied => libc::EPERM,
30            io::ErrorKind::ConnectionRefused => libc::ECONNREFUSED,
31            io::ErrorKind::ConnectionReset => libc::ECONNRESET,
32            io::ErrorKind::ConnectionAborted => libc::ECONNABORTED,
33            io::ErrorKind::NotConnected => libc::ENOTCONN,
34            io::ErrorKind::AddrInUse => libc::EADDRINUSE,
35            io::ErrorKind::AddrNotAvailable => libc::EADDRNOTAVAIL,
36            io::ErrorKind::BrokenPipe => libc::EPIPE,
37            io::ErrorKind::AlreadyExists => libc::EEXIST,
38            io::ErrorKind::WouldBlock => libc::EWOULDBLOCK,
39            io::ErrorKind::InvalidInput => libc::EINVAL,
40            io::ErrorKind::InvalidData => libc::EINVAL,
41            io::ErrorKind::TimedOut => libc::ETIMEDOUT,
42            io::ErrorKind::WriteZero => libc::EIO,
43            io::ErrorKind::Interrupted => libc::EINTR,
44            io::ErrorKind::Other => libc::EIO,
45            io::ErrorKind::UnexpectedEof => libc::EIO,
46            _ => libc::EIO,
47        }
48    };
49
50    Rlerror {
51        ecode: errno as u32,
52    }
53}
54
55// Tlopen and Tlcreate flags.  Taken from "include/net/9p/9p.h" in the linux tree.
56pub const P9_RDONLY: u32 = 0o00000000;
57pub const P9_WRONLY: u32 = 0o00000001;
58pub const P9_RDWR: u32 = 0o00000002;
59pub const P9_NOACCESS: u32 = 0o00000003;
60pub const P9_CREATE: u32 = 0o00000100;
61pub const P9_EXCL: u32 = 0o00000200;
62pub const P9_NOCTTY: u32 = 0o00000400;
63pub const P9_TRUNC: u32 = 0o00001000;
64pub const P9_APPEND: u32 = 0o00002000;
65pub const P9_NONBLOCK: u32 = 0o00004000;
66pub const P9_DSYNC: u32 = 0o00010000;
67pub const P9_FASYNC: u32 = 0o00020000;
68pub const P9_DIRECT: u32 = 0o00040000;
69pub const P9_LARGEFILE: u32 = 0o00100000;
70pub const P9_DIRECTORY: u32 = 0o00200000;
71pub const P9_NOFOLLOW: u32 = 0o00400000;
72pub const P9_NOATIME: u32 = 0o01000000;
73pub const _P9_CLOEXEC: u32 = 0o02000000;
74pub const P9_SYNC: u32 = 0o04000000;
75
76// Mapping from 9P flags to libc flags.
77pub const MAPPED_FLAGS: [(u32, i32); 16] = [
78    (P9_WRONLY, libc::O_WRONLY),
79    (P9_RDWR, libc::O_RDWR),
80    (P9_CREATE, libc::O_CREAT),
81    (P9_EXCL, libc::O_EXCL),
82    (P9_NOCTTY, libc::O_NOCTTY),
83    (P9_TRUNC, libc::O_TRUNC),
84    (P9_APPEND, libc::O_APPEND),
85    (P9_NONBLOCK, libc::O_NONBLOCK),
86    (P9_DSYNC, libc::O_DSYNC),
87    (P9_FASYNC, 0), // Unsupported
88    (P9_DIRECT, libc::O_DIRECT),
89    (P9_LARGEFILE, libc::O_LARGEFILE),
90    (P9_DIRECTORY, libc::O_DIRECTORY),
91    (P9_NOFOLLOW, libc::O_NOFOLLOW),
92    (P9_NOATIME, libc::O_NOATIME),
93    (P9_SYNC, libc::O_SYNC),
94];
95
96// 9P Qid types.  Taken from "include/net/9p/9p.h" in the linux tree.
97pub const P9_QTDIR: u8 = 0x80;
98pub const _P9_QTAPPEND: u8 = 0x40;
99pub const _P9_QTEXCL: u8 = 0x20;
100pub const _P9_QTMOUNT: u8 = 0x10;
101pub const _P9_QTAUTH: u8 = 0x08;
102pub const _P9_QTTMP: u8 = 0x04;
103pub const P9_QTSYMLINK: u8 = 0x02;
104pub const _P9_QTLINK: u8 = 0x01;
105pub const P9_QTFILE: u8 = 0x00;
106
107// Bitmask values for the getattr request.
108pub const _P9_GETATTR_MODE: u64 = 0x00000001;
109pub const _P9_GETATTR_NLINK: u64 = 0x00000002;
110pub const _P9_GETATTR_UID: u64 = 0x00000004;
111pub const _P9_GETATTR_GID: u64 = 0x00000008;
112pub const _P9_GETATTR_RDEV: u64 = 0x00000010;
113pub const _P9_GETATTR_ATIME: u64 = 0x00000020;
114pub const _P9_GETATTR_MTIME: u64 = 0x00000040;
115pub const _P9_GETATTR_CTIME: u64 = 0x00000080;
116pub const _P9_GETATTR_INO: u64 = 0x00000100;
117pub const _P9_GETATTR_SIZE: u64 = 0x00000200;
118pub const _P9_GETATTR_BLOCKS: u64 = 0x00000400;
119
120pub const _P9_GETATTR_BTIME: u64 = 0x00000800;
121pub const _P9_GETATTR_GEN: u64 = 0x00001000;
122pub const _P9_GETATTR_DATA_VERSION: u64 = 0x00002000;
123
124pub const P9_GETATTR_BASIC: u64 = 0x000007ff; // Mask for fields up to BLOCKS
125pub const _P9_GETATTR_ALL: u64 = 0x00003fff; // Mask for All fields above
126
127// Bitmask values for the setattr request.
128pub const P9_SETATTR_MODE: u32 = 0x00000001;
129pub const P9_SETATTR_UID: u32 = 0x00000002;
130pub const P9_SETATTR_GID: u32 = 0x00000004;
131pub const P9_SETATTR_SIZE: u32 = 0x00000008;
132pub const P9_SETATTR_ATIME: u32 = 0x00000010;
133pub const P9_SETATTR_MTIME: u32 = 0x00000020;
134pub const P9_SETATTR_CTIME: u32 = 0x00000040;
135pub const P9_SETATTR_ATIME_SET: u32 = 0x00000080;
136pub const P9_SETATTR_MTIME_SET: u32 = 0x00000100;
137
138// 9p lock constants. Taken from "include/net/9p/9p.h" in the linux kernel.
139pub const _P9_LOCK_TYPE_RDLCK: u8 = 0;
140pub const _P9_LOCK_TYPE_WRLCK: u8 = 1;
141pub const P9_LOCK_TYPE_UNLCK: u8 = 2;
142pub const _P9_LOCK_FLAGS_BLOCK: u8 = 1;
143pub const _P9_LOCK_FLAGS_RECLAIM: u8 = 2;
144pub const P9_LOCK_SUCCESS: u8 = 0;
145pub const _P9_LOCK_BLOCKED: u8 = 1;
146pub const _P9_LOCK_ERROR: u8 = 2;
147pub const _P9_LOCK_GRACE: u8 = 3;
148
149// Minimum and maximum message size that we'll expect from the client.
150pub const MIN_MESSAGE_SIZE: u32 = 256;
151pub const MAX_MESSAGE_SIZE: u32 = 64 * 1024 + 24; // 64 KiB of payload plus some extra for the header
152
153pub const DEFAULT_MSIZE: u32 = 8192;