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;
15use jetstream_libc as libc;
16
17use std::io;
18
19pub use self::messages::*;
20
21pub fn error_to_rmessage(err: &io::Error) -> Rlerror {
22    let errno = if let Some(errno) = err.raw_os_error() {
23        errno
24    } else {
25        // Make a best-effort guess based on the kind.
26        match err.kind() {
27            io::ErrorKind::NotFound => libc::ENOENT,
28            io::ErrorKind::PermissionDenied => libc::EPERM,
29            io::ErrorKind::ConnectionRefused => libc::ECONNREFUSED,
30            io::ErrorKind::ConnectionReset => libc::ECONNRESET,
31            io::ErrorKind::ConnectionAborted => libc::ECONNABORTED,
32            io::ErrorKind::NotConnected => libc::ENOTCONN,
33            io::ErrorKind::AddrInUse => libc::EADDRINUSE,
34            io::ErrorKind::AddrNotAvailable => libc::EADDRNOTAVAIL,
35            io::ErrorKind::BrokenPipe => libc::EPIPE,
36            io::ErrorKind::AlreadyExists => libc::EEXIST,
37            io::ErrorKind::WouldBlock => libc::EWOULDBLOCK,
38            io::ErrorKind::InvalidInput => libc::EINVAL,
39            io::ErrorKind::InvalidData => libc::EINVAL,
40            io::ErrorKind::TimedOut => libc::ETIMEDOUT,
41            io::ErrorKind::WriteZero => libc::EIO,
42            io::ErrorKind::Interrupted => libc::EINTR,
43            io::ErrorKind::Other => libc::EIO,
44            io::ErrorKind::UnexpectedEof => libc::EIO,
45            _ => libc::EIO,
46        }
47    };
48
49    Rlerror {
50        ecode: errno as u32,
51    }
52}
53
54// Tlopen and Tlcreate flags.  Taken from "include/net/9p/9p.h" in the linux tree.
55pub const P9_RDONLY: u32 = 0o00000000;
56pub const P9_WRONLY: u32 = 0o00000001;
57pub const P9_RDWR: u32 = 0o00000002;
58pub const P9_NOACCESS: u32 = 0o00000003;
59pub const P9_CREATE: u32 = 0o00000100;
60pub const P9_EXCL: u32 = 0o00000200;
61pub const P9_NOCTTY: u32 = 0o00000400;
62pub const P9_TRUNC: u32 = 0o00001000;
63pub const P9_APPEND: u32 = 0o00002000;
64pub const P9_NONBLOCK: u32 = 0o00004000;
65pub const P9_DSYNC: u32 = 0o00010000;
66pub const P9_FASYNC: u32 = 0o00020000;
67pub const P9_DIRECT: u32 = 0o00040000;
68pub const P9_LARGEFILE: u32 = 0o00100000;
69pub const P9_DIRECTORY: u32 = 0o00200000;
70pub const P9_NOFOLLOW: u32 = 0o00400000;
71pub const P9_NOATIME: u32 = 0o01000000;
72pub const _P9_CLOEXEC: u32 = 0o02000000;
73pub const P9_SYNC: u32 = 0o04000000;
74
75// Mapping from 9P flags to libc flags.
76pub const MAPPED_FLAGS: [(u32, i32); 16] = [
77    (P9_WRONLY, libc::O_WRONLY),
78    (P9_RDWR, libc::O_RDWR),
79    (P9_CREATE, libc::O_CREAT),
80    (P9_EXCL, libc::O_EXCL),
81    (P9_NOCTTY, libc::O_NOCTTY),
82    (P9_TRUNC, libc::O_TRUNC),
83    (P9_APPEND, libc::O_APPEND),
84    (P9_NONBLOCK, libc::O_NONBLOCK),
85    (P9_DSYNC, libc::O_DSYNC),
86    (P9_FASYNC, 0), // Unsupported
87    (P9_DIRECT, libc::O_DIRECT),
88    (P9_LARGEFILE, libc::O_LARGEFILE),
89    (P9_DIRECTORY, libc::O_DIRECTORY),
90    (P9_NOFOLLOW, libc::O_NOFOLLOW),
91    (P9_NOATIME, libc::O_NOATIME),
92    (P9_SYNC, libc::O_SYNC),
93];
94
95// 9P Qid types.  Taken from "include/net/9p/9p.h" in the linux tree.
96pub const P9_QTDIR: u8 = 0x80;
97pub const _P9_QTAPPEND: u8 = 0x40;
98pub const _P9_QTEXCL: u8 = 0x20;
99pub const _P9_QTMOUNT: u8 = 0x10;
100pub const _P9_QTAUTH: u8 = 0x08;
101pub const _P9_QTTMP: u8 = 0x04;
102pub const P9_QTSYMLINK: u8 = 0x02;
103pub const _P9_QTLINK: u8 = 0x01;
104pub const P9_QTFILE: u8 = 0x00;
105
106// Bitmask values for the getattr request.
107pub const _P9_GETATTR_MODE: u64 = 0x00000001;
108pub const _P9_GETATTR_NLINK: u64 = 0x00000002;
109pub const _P9_GETATTR_UID: u64 = 0x00000004;
110pub const _P9_GETATTR_GID: u64 = 0x00000008;
111pub const _P9_GETATTR_RDEV: u64 = 0x00000010;
112pub const _P9_GETATTR_ATIME: u64 = 0x00000020;
113pub const _P9_GETATTR_MTIME: u64 = 0x00000040;
114pub const _P9_GETATTR_CTIME: u64 = 0x00000080;
115pub const _P9_GETATTR_INO: u64 = 0x00000100;
116pub const _P9_GETATTR_SIZE: u64 = 0x00000200;
117pub const _P9_GETATTR_BLOCKS: u64 = 0x00000400;
118
119pub const _P9_GETATTR_BTIME: u64 = 0x00000800;
120pub const _P9_GETATTR_GEN: u64 = 0x00001000;
121pub const _P9_GETATTR_DATA_VERSION: u64 = 0x00002000;
122
123pub const P9_GETATTR_BASIC: u64 = 0x000007ff; // Mask for fields up to BLOCKS
124pub const _P9_GETATTR_ALL: u64 = 0x00003fff; // Mask for All fields above
125
126// Bitmask values for the setattr request.
127pub const P9_SETATTR_MODE: u32 = 0x00000001;
128pub const P9_SETATTR_UID: u32 = 0x00000002;
129pub const P9_SETATTR_GID: u32 = 0x00000004;
130pub const P9_SETATTR_SIZE: u32 = 0x00000008;
131pub const P9_SETATTR_ATIME: u32 = 0x00000010;
132pub const P9_SETATTR_MTIME: u32 = 0x00000020;
133pub const P9_SETATTR_CTIME: u32 = 0x00000040;
134pub const P9_SETATTR_ATIME_SET: u32 = 0x00000080;
135pub const P9_SETATTR_MTIME_SET: u32 = 0x00000100;
136
137// 9p lock constants. Taken from "include/net/9p/9p.h" in the linux kernel.
138pub const _P9_LOCK_TYPE_RDLCK: u8 = 0;
139pub const _P9_LOCK_TYPE_WRLCK: u8 = 1;
140pub const P9_LOCK_TYPE_UNLCK: u8 = 2;
141pub const _P9_LOCK_FLAGS_BLOCK: u8 = 1;
142pub const _P9_LOCK_FLAGS_RECLAIM: u8 = 2;
143pub const P9_LOCK_SUCCESS: u8 = 0;
144pub const _P9_LOCK_BLOCKED: u8 = 1;
145pub const _P9_LOCK_ERROR: u8 = 2;
146pub const _P9_LOCK_GRACE: u8 = 3;
147
148// Minimum and maximum message size that we'll expect from the client.
149pub const MIN_MESSAGE_SIZE: u32 = 256;
150pub const MAX_MESSAGE_SIZE: u32 = 64 * 1024 + 24; // 64 KiB of payload plus some extra for the header
151
152pub enum Version {
153    V9P2000 = 0,
154    V9P2000U = 1,
155    V9P2000L = 2,
156    V9P2000Lu = 3,
157    V9P2024q9p = 4,
158}
159
160impl From<&str> for Version {
161    fn from(version: &str) -> Self {
162        match version {
163            "9P2000" => Version::V9P2000,
164            "9P2000.u" => Version::V9P2000U,
165            "9P2000.L" => Version::V9P2000L,
166            "9P2000.Lu" => Version::V9P2000Lu,
167            "9P2024.q9p" => Version::V9P2024q9p,
168            _ => panic!("Invalid 9P version: {}", version),
169        }
170    }
171}
172
173impl From<String> for Version {
174    fn from(version: String) -> Self {
175        match version.as_str() {
176            "9P2000" => Version::V9P2000,
177            "9P2000.u" => Version::V9P2000U,
178            "9P2000.L" => Version::V9P2000L,
179            "9P2000.Lu" => Version::V9P2000Lu,
180            "9P2024.q9p" => Version::V9P2024q9p,
181            _ => panic!("Invalid 9P version: {}", version),
182        }
183    }
184}
185
186impl From<Version> for String {
187    fn from(val: Version) -> Self {
188        match val {
189            Version::V9P2000 => "9P2000".to_string(),
190            Version::V9P2000U => "9P2000.u".to_string(),
191            Version::V9P2000L => "9P2000.L".to_string(),
192            Version::V9P2000Lu => "9P2000.Lu".to_string(),
193            Version::V9P2024q9p => "9P2024.q9p".to_string(),
194        }
195    }
196}
197
198impl From<Version> for &str {
199    fn from(val: Version) -> Self {
200        match val {
201            Version::V9P2000 => "9P2000",
202            Version::V9P2000U => "9P2000.u",
203            Version::V9P2000L => "9P2000.L",
204            Version::V9P2000Lu => "9P2000.Lu",
205            Version::V9P2024q9p => "9P2024.q9p",
206        }
207    }
208}
209
210pub const DEFAULT_MSIZE: u32 = 8192;