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
use std::io::{self, Read, Write};
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use async_io::Async;
use futures_io::{AsyncRead, AsyncWrite};
use ssh2::{File, FileStat, OpenFlags, OpenType, RenameFlags, Sftp};

use crate::util::poll_once;

pub struct AsyncSftp<S> {
    inner: Sftp,
    async_io: Arc<Async<S>>,
}

impl<S> AsyncSftp<S> {
    pub(crate) fn from_parts(inner: Sftp, async_io: Arc<Async<S>>) -> Self {
        Self { inner, async_io }
    }
}

impl<S> AsyncSftp<S> {
    pub async fn open_mode(
        &self,
        filename: &Path,
        flags: OpenFlags,
        mode: i32,
        open_type: OpenType,
    ) -> io::Result<AsyncFile<S>> {
        let inner = &self.inner;

        let ret = self
            .async_io
            .write_with(|_| {
                inner
                    .open_mode(filename, flags, mode, open_type)
                    .map_err(|err| err.into())
            })
            .await;

        ret.and_then(|file| Ok(AsyncFile::from_parts(file, self.async_io.clone())))
    }

    pub async fn open(&self, filename: &Path) -> io::Result<AsyncFile<S>> {
        let inner = &self.inner;

        let ret = self
            .async_io
            .write_with(|_| inner.open(filename).map_err(|err| err.into()))
            .await;

        ret.and_then(|file| Ok(AsyncFile::from_parts(file, self.async_io.clone())))
    }

    pub async fn create(&self, filename: &Path) -> io::Result<AsyncFile<S>> {
        let inner = &self.inner;

        let ret = self
            .async_io
            .write_with(|_| inner.create(filename).map_err(|err| err.into()))
            .await;

        ret.and_then(|file| Ok(AsyncFile::from_parts(file, self.async_io.clone())))
    }

    pub async fn opendir(&self, dirname: &Path) -> io::Result<AsyncFile<S>> {
        let inner = &self.inner;

        let ret = self
            .async_io
            .write_with(|_| inner.opendir(dirname).map_err(|err| err.into()))
            .await;

        ret.and_then(|file| Ok(AsyncFile::from_parts(file, self.async_io.clone())))
    }

    pub async fn readdir(&self, dirname: &Path) -> io::Result<Vec<(PathBuf, FileStat)>> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.readdir(dirname).map_err(|err| err.into()))
            .await
    }

    pub async fn mkdir(&self, filename: &Path, mode: i32) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.mkdir(filename, mode).map_err(|err| err.into()))
            .await
    }

    pub async fn rmdir(&self, filename: &Path) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.rmdir(filename).map_err(|err| err.into()))
            .await
    }

    pub async fn stat(&self, filename: &Path) -> io::Result<FileStat> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.stat(filename).map_err(|err| err.into()))
            .await
    }

    pub async fn lstat(&self, filename: &Path) -> io::Result<FileStat> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.lstat(filename).map_err(|err| err.into()))
            .await
    }

    pub async fn setstat(&self, filename: &Path, stat: FileStat) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| {
                inner
                    .setstat(filename, stat.clone())
                    .map_err(|err| err.into())
            })
            .await
    }

    pub async fn symlink(&self, path: &Path, target: &Path) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.symlink(path, target).map_err(|err| err.into()))
            .await
    }

    pub async fn readlink(&self, path: &Path) -> io::Result<PathBuf> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.readlink(path).map_err(|err| err.into()))
            .await
    }

    pub async fn realpath(&self, path: &Path) -> io::Result<PathBuf> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.realpath(path).map_err(|err| err.into()))
            .await
    }

    pub async fn rename(
        &self,
        src: &Path,
        dst: &Path,
        flags: Option<RenameFlags>,
    ) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.rename(src, dst, flags).map_err(|err| err.into()))
            .await
    }

    pub async fn unlink(&self, file: &Path) -> io::Result<()> {
        let inner = &self.inner;

        self.async_io
            .write_with(|_| inner.unlink(file).map_err(|err| err.into()))
            .await
    }
}

//
//
//
pub struct AsyncFile<S> {
    inner: File,
    async_io: Arc<Async<S>>,
}

impl<S> AsyncFile<S> {
    pub(crate) fn from_parts(inner: File, async_io: Arc<Async<S>>) -> Self {
        Self { inner, async_io }
    }
}

impl<S> AsyncRead for AsyncFile<S> {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        let this = self.get_mut();

        let inner = &mut this.inner;

        poll_once(cx, this.async_io.read_with(|_| inner.read(buf)))
    }
}

impl<S> AsyncWrite for AsyncFile<S> {
    fn poll_write(self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
        let this = self.get_mut();

        let inner = &mut this.inner;

        poll_once(cx, this.async_io.write_with(|_| inner.write(buf)))
    }

    fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
        let this = self.get_mut();

        let inner = &mut this.inner;

        poll_once(cx, this.async_io.write_with(|_| inner.flush()))
    }

    fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
        let this = self.get_mut();

        let _ = &mut this.inner;

        // TODO
        poll_once(cx, this.async_io.write_with(|_| Ok(())))
    }
}