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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
use super::{AppendOnlyResumableRecorderMedium, ReadOnlyResumableRecorderMedium, ResumableRecorder, SourceKey};
use digest::Digest;
use sha1::Sha1;
use std::{
    env::temp_dir,
    fmt::{self, Debug},
    fs::{remove_file, DirBuilder, OpenOptions},
    io::Result as IoResult,
    marker::PhantomData,
    path::PathBuf,
};

#[cfg(feature = "async")]
use {
    super::{AppendOnlyAsyncResumableRecorderMedium, ReadOnlyAsyncResumableRecorderMedium},
    async_std::fs::{remove_file as async_remove_file, DirBuilder as AsyncDirBuilder, OpenOptions as AsyncOpenOptions},
    futures::future::BoxFuture,
};

/// 文件系统断点恢复记录器
///
/// 基于文件系统提供断点恢复记录功能
pub struct FileSystemResumableRecorder<O = Sha1> {
    path: PathBuf,
    _unused: PhantomData<O>,
}

const DEFAULT_DIRECTORY_NAME: &str = ".qiniu-rust-sdk";

impl<O> FileSystemResumableRecorder<O> {
    /// 创建文件系统断点恢复记录器,传入一个目录路径用于储存断点记录
    #[inline]
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            path: path.into(),
            _unused: Default::default(),
        }
    }

    fn create_directory(&self) -> IoResult<()> {
        DirBuilder::new().recursive(true).create(&self.path)
    }

    #[cfg(feature = "async")]
    async fn async_create_directory(&self) -> IoResult<()> {
        AsyncDirBuilder::new().recursive(true).create(&self.path).await
    }
}

impl<O: Digest> ResumableRecorder for FileSystemResumableRecorder<O> {
    type HashAlgorithm = O;

    #[inline]
    fn open_for_read(
        &self,
        source_key: &SourceKey<Self::HashAlgorithm>,
    ) -> IoResult<Box<dyn ReadOnlyResumableRecorderMedium>> {
        self.create_directory()?;
        let medium = OpenOptions::new().read(true).open(self.path_of(source_key))?;
        Ok(Box::new(medium))
    }

    fn open_for_append(
        &self,
        source_key: &SourceKey<Self::HashAlgorithm>,
    ) -> IoResult<Box<dyn AppendOnlyResumableRecorderMedium>> {
        self.create_directory()?;
        let medium = OpenOptions::new().append(true).open(self.path_of(source_key))?;
        Ok(Box::new(medium))
    }

    fn open_for_create_new(
        &self,
        source_key: &SourceKey<Self::HashAlgorithm>,
    ) -> IoResult<Box<dyn AppendOnlyResumableRecorderMedium>> {
        self.create_directory()?;
        let medium = OpenOptions::new()
            .create(true)
            .truncate(true)
            .write(true)
            .open(self.path_of(source_key))?;
        Ok(Box::new(medium))
    }

    fn delete(&self, source_key: &SourceKey<Self::HashAlgorithm>) -> IoResult<()> {
        remove_file(self.path_of(source_key))
    }

    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn open_for_async_read<'a>(
        &'a self,
        source_key: &'a SourceKey<Self::HashAlgorithm>,
    ) -> BoxFuture<'a, IoResult<Box<dyn ReadOnlyAsyncResumableRecorderMedium>>> {
        Box::pin(async move {
            self.async_create_directory().await?;
            let medium = AsyncOpenOptions::new()
                .read(true)
                .open(self.path_of(source_key))
                .await?;
            Ok(Box::new(medium) as Box<dyn ReadOnlyAsyncResumableRecorderMedium>)
        })
    }

    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn open_for_async_append<'a>(
        &'a self,
        source_key: &'a SourceKey<Self::HashAlgorithm>,
    ) -> BoxFuture<'a, IoResult<Box<dyn AppendOnlyAsyncResumableRecorderMedium>>> {
        Box::pin(async move {
            self.async_create_directory().await?;
            let medium = AsyncOpenOptions::new()
                .append(true)
                .open(self.path_of(source_key))
                .await?;
            Ok(Box::new(medium) as Box<dyn AppendOnlyAsyncResumableRecorderMedium>)
        })
    }

    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn open_for_async_create_new<'a>(
        &'a self,
        source_key: &'a SourceKey<Self::HashAlgorithm>,
    ) -> BoxFuture<'a, IoResult<Box<dyn AppendOnlyAsyncResumableRecorderMedium>>> {
        Box::pin(async move {
            self.async_create_directory().await?;
            let medium = AsyncOpenOptions::new()
                .create(true)
                .truncate(true)
                .write(true)
                .open(self.path_of(source_key))
                .await?;
            Ok(Box::new(medium) as Box<dyn AppendOnlyAsyncResumableRecorderMedium>)
        })
    }

    #[cfg(feature = "async")]
    #[cfg_attr(feature = "docs", doc(cfg(feature = "async")))]
    fn async_delete<'a>(&'a self, source_key: &'a SourceKey<Self::HashAlgorithm>) -> BoxFuture<'a, IoResult<()>> {
        Box::pin(async move { async_remove_file(self.path_of(source_key)).await })
    }
}

impl<O: Digest> FileSystemResumableRecorder<O> {
    fn path_of(&self, source_key: &SourceKey<O>) -> PathBuf {
        self.path.join(&hex::encode(source_key.as_slice()))
    }
}

impl<O> Default for FileSystemResumableRecorder<O> {
    #[inline]
    fn default() -> Self {
        Self::new(temp_dir().join(DEFAULT_DIRECTORY_NAME))
    }
}

impl<O> Debug for FileSystemResumableRecorder<O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("FileSystemResumableRecorder")
            .field("path", &self.path)
            .finish()
    }
}

impl<O> Clone for FileSystemResumableRecorder<O> {
    #[inline]
    fn clone(&self) -> Self {
        Self {
            path: self.path.clone(),
            _unused: self._unused,
        }
    }
}

#[allow(unsafe_code)]
unsafe impl<O> Send for FileSystemResumableRecorder<O> {}

#[allow(unsafe_code)]
unsafe impl<O> Sync for FileSystemResumableRecorder<O> {}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Result;
    use rand::{thread_rng, RngCore};
    use std::io::{Read, Write};
    use tempfile::tempdir;

    #[test]
    fn test_file_system_resumable_recorder() -> Result<()> {
        let dir = tempdir()?;
        let source_key = SourceKey::new([0u8; 20]);
        let recorder = FileSystemResumableRecorder::<Sha1>::new(dir.path());
        let mut rander = thread_rng();
        let mut buf = vec![0u8; 1 << 20];
        rander.fill_bytes(&mut buf);

        recorder.delete(&source_key).ok();
        recorder.open_for_append(&source_key).unwrap_err();
        {
            let mut medium = recorder.open_for_create_new(&source_key)?;
            medium.write_all(&buf)?;
        }
        {
            let mut medium = recorder.open_for_append(&source_key)?;
            medium.write_all(&buf)?;
        }
        {
            let mut medium = recorder.open_for_read(&source_key)?;
            let mut buf1 = Vec::new();
            let mut buf2 = Vec::new();
            (&mut medium).take(1 << 20).read_to_end(&mut buf1)?;
            (&mut medium).take(1 << 20).read_to_end(&mut buf2)?;
            assert_eq!(buf1.len(), buf.len());
            assert_eq!(buf2.len(), buf.len());
            assert!(buf1 == buf);
            assert!(buf2 == buf);
        }

        Ok(())
    }

    #[cfg(feature = "async")]
    #[async_std::test]
    async fn test_async_file_system_resumable_recorder() -> Result<()> {
        use futures::{AsyncReadExt, AsyncWriteExt};

        let dir = tempdir()?;
        let source_key = SourceKey::new([0u8; 20]);
        let recorder = FileSystemResumableRecorder::<Sha1>::new(dir.path());
        let mut rander = thread_rng();
        let mut buf = vec![0u8; 1 << 20];
        rander.fill_bytes(&mut buf);

        recorder.async_delete(&source_key).await.ok();
        recorder.open_for_async_append(&source_key).await.unwrap_err();
        {
            let mut medium = recorder.open_for_async_create_new(&source_key).await?;
            medium.write_all(&buf).await?;
            medium.flush().await?;
        }
        {
            let mut medium = recorder.open_for_async_append(&source_key).await?;
            medium.write_all(&buf).await?;
            medium.flush().await?;
            let mut medium = recorder.open_for_async_read(&source_key).await?;
            let mut buf1 = Vec::new();
            let mut buf2 = Vec::new();
            (&mut medium).take(1 << 20).read_to_end(&mut buf1).await?;
            (&mut medium).take(1 << 20).read_to_end(&mut buf2).await?;
            assert_eq!(buf1.len(), buf.len());
            assert_eq!(buf2.len(), buf.len());
            assert!(buf1 == buf);
            assert!(buf2 == buf);
        }
        {}

        Ok(())
    }
}