openssh_sftp_protocol/
open_options.rs1#![forbid(unsafe_code)]
2
3use super::{constants, file_attrs::FileAttrs, request::OpenFileRequest};
4
5use std::{borrow::Cow, path::Path};
6
7#[derive(Debug, Copy, Clone)]
8pub struct OpenOptions {
9 read: bool,
10 write: bool,
11 append: bool,
12}
13
14impl OpenOptions {
15 pub const fn new() -> Self {
16 Self {
17 read: false,
18 write: false,
19 append: false,
20 }
21 }
22
23 pub const fn read(mut self, read: bool) -> Self {
24 self.read = read;
25 self
26 }
27
28 pub const fn get_read(self) -> bool {
29 self.read
30 }
31
32 pub const fn write(mut self, write: bool) -> Self {
33 self.write = write;
34 self
35 }
36
37 pub const fn get_write(self) -> bool {
38 self.write || self.append
39 }
40
41 pub const fn append(mut self, append: bool) -> Self {
42 self.append = append;
43 self
44 }
45
46 pub const fn get_append(self) -> bool {
47 self.append
48 }
49
50 pub const fn open(self, filename: Cow<'_, Path>) -> OpenFileRequest<'_> {
51 let mut flags: u32 = 0;
52
53 if self.read {
54 flags |= constants::SSH_FXF_READ;
55 }
56
57 if self.write || self.append {
58 flags |= constants::SSH_FXF_WRITE;
59 }
60
61 if self.append {
62 flags |= constants::SSH_FXF_APPEND;
63 }
64
65 OpenFileRequest {
66 filename,
67 flags,
68 attrs: FileAttrs::new(),
69 }
70 }
71
72 pub const fn create(
73 self,
74 filename: Cow<'_, Path>,
75 flags: CreateFlags,
76 attrs: FileAttrs,
77 ) -> OpenFileRequest<'_> {
78 let mut openfile = self.open(filename);
79 openfile.flags |= constants::SSH_FXF_CREAT | flags as u32;
80 openfile.attrs = attrs;
81 openfile
82 }
83}
84
85#[derive(Debug, Copy, Clone)]
86#[repr(u32)]
87pub enum CreateFlags {
88 None = 0,
89
90 Trunc = constants::SSH_FXF_TRUNC,
93
94 Excl = constants::SSH_FXF_EXCL,
96}