openssh_sftp_protocol/
open_options.rs

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
#![forbid(unsafe_code)]

use super::{constants, file_attrs::FileAttrs, request::OpenFileRequest};

use std::{borrow::Cow, path::Path};

#[derive(Debug, Copy, Clone)]
pub struct OpenOptions {
    read: bool,
    write: bool,
    append: bool,
}

impl OpenOptions {
    pub const fn new() -> Self {
        Self {
            read: false,
            write: false,
            append: false,
        }
    }

    pub const fn read(mut self, read: bool) -> Self {
        self.read = read;
        self
    }

    pub const fn get_read(self) -> bool {
        self.read
    }

    pub const fn write(mut self, write: bool) -> Self {
        self.write = write;
        self
    }

    pub const fn get_write(self) -> bool {
        self.write || self.append
    }

    pub const fn append(mut self, append: bool) -> Self {
        self.append = append;
        self
    }

    pub const fn get_append(self) -> bool {
        self.append
    }

    pub const fn open(self, filename: Cow<'_, Path>) -> OpenFileRequest<'_> {
        let mut flags: u32 = 0;

        if self.read {
            flags |= constants::SSH_FXF_READ;
        }

        if self.write || self.append {
            flags |= constants::SSH_FXF_WRITE;
        }

        if self.append {
            flags |= constants::SSH_FXF_APPEND;
        }

        OpenFileRequest {
            filename,
            flags,
            attrs: FileAttrs::new(),
        }
    }

    pub const fn create(
        self,
        filename: Cow<'_, Path>,
        flags: CreateFlags,
        attrs: FileAttrs,
    ) -> OpenFileRequest<'_> {
        let mut openfile = self.open(filename);
        openfile.flags |= constants::SSH_FXF_CREAT | flags as u32;
        openfile.attrs = attrs;
        openfile
    }
}

#[derive(Debug, Copy, Clone)]
#[repr(u32)]
pub enum CreateFlags {
    None = 0,

    /// Forces an existing file with the same name to be truncated to zero
    /// length when creating a file.
    Trunc = constants::SSH_FXF_TRUNC,

    /// Causes the request to fail if the named file already exists.
    Excl = constants::SSH_FXF_EXCL,
}