objstore_fs 0.1.0-alpha.1

Filesystem backend for objstore
Documentation
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
mod provider;

pub use self::provider::FsProvider;

use std::{
    collections::HashSet,
    path::{Path, PathBuf},
    sync::Arc,
};

use anyhow::{Context, bail};
use bytes::Bytes;
use futures::{StreamExt as _, TryStreamExt as _};
use time::OffsetDateTime;
use tokio::io::{AsyncReadExt, AsyncWriteExt as _};

use objstore::{
    Copy, DataSource, DownloadUrlArgs, KeyPage, ListArgs, ObjStore, ObjectMeta, ObjectMetaPage,
    Put, ValueStream,
};
use sha2::Digest;
use url::Url;

#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct FsObjStoreConfig {
    path: PathBuf,
}

impl FsObjStoreConfig {
    pub fn new(path: PathBuf) -> Self {
        Self { path }
    }
}

#[derive(Clone, Debug)]
pub struct FsObjStore {
    state: Arc<State>,
}

#[derive(Debug)]
struct State {
    safe_uri: Url,
    root: PathBuf,
}

impl FsObjStore {
    /// The kind of this object store (see [`ObjStore::kind`]).
    pub const KIND: &'static str = "objstore.fs";

    pub fn new(config: FsObjStoreConfig) -> Result<Self, anyhow::Error> {
        let root = config.path.clone();
        std::fs::create_dir_all(&root).with_context(|| {
            format!("Could not create fs kvstore directory '{}'", root.display())
        })?;

        let safe_uri = Url::parse(&format!("file://{}", root.display()))
            .context("Failed to build safe-uri")?;

        Ok(Self {
            state: Arc::new(State { safe_uri, root }),
        })
    }

    fn key_path(&self, key: &str) -> PathBuf {
        self.state.root.join(key)
    }
}

fn meta_from_fs_meta(key: String, fs_meta: std::fs::Metadata) -> ObjectMeta {
    let mut meta = ObjectMeta::new(key);
    meta.size = Some(fs_meta.len());
    meta.created_at = fs_meta.created().ok().map(OffsetDateTime::from);
    meta.updated_at = fs_meta.modified().ok().map(OffsetDateTime::from);

    meta
}

async fn list_dir_rec(
    path: &Path,
    cursor: Option<&str>,
    limit: usize,
    prefix_filter: Option<&str>,
    current_path: &str,
    items: &mut Vec<ObjectMeta>,
    directories: &mut Option<Vec<String>>,
) -> Result<Option<()>, anyhow::Error> {
    let f = async {
        let mut iter = match tokio::fs::read_dir(path).await {
            Ok(iter) => iter,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(err) => return Err(err.into()),
        };

        while let Some(entry) = iter.next_entry().await? {
            let meta = entry.metadata().await?;
            let key = entry.file_name().to_string_lossy().to_string();

            if let Some(prefix) = &prefix_filter
                && !key.starts_with(prefix)
            {
                continue;
            }

            if !meta.is_file() {
                if meta.is_dir() {
                    if let Some(directories) = directories {
                        directories.push(key.clone());
                    }

                    let cpath = if current_path.is_empty() {
                        key
                    } else {
                        format!("{current_path}/{key}")
                    };
                    list_dir_rec(
                        &entry.path(),
                        cursor,
                        limit,
                        None,
                        &cpath,
                        items,
                        directories,
                    )
                    .await?;
                    continue;
                } else {
                    continue;
                }
            }

            if let Some(cursor) = cursor
                && (key.as_str() <= cursor || key.as_str() == cursor)
            {
                continue;
            }

            let full_key = if current_path.is_empty() {
                key
            } else {
                format!("{current_path}/{key}")
            };
            items.push(meta_from_fs_meta(full_key, meta));

            if items.len() >= limit {
                break;
            }
        }

        Ok(Some(()))
    };

    Box::pin(f).await
}

async fn list_dir(
    path: &Path,
    cursor: Option<&str>,
    limit: usize,
    prefix_filter: Option<&str>,
    current_path: &str,
    flat: bool,
) -> Result<(Vec<ObjectMeta>, Option<Vec<String>>), anyhow::Error> {
    let mut items = Vec::new();
    let mut directories = if flat { None } else { Some(Vec::new()) };
    list_dir_rec(
        path,
        cursor,
        limit,
        prefix_filter,
        current_path,
        &mut items,
        &mut directories,
    )
    .await?;

    let mut keys = HashSet::new();

    items.retain(|item| {
        if keys.insert(item.key().to_owned()) {
            true
        } else {
            // If the key is already in the set, we skip it.
            false
        }
    });

    Ok((items, directories))
}

#[async_trait::async_trait]
impl ObjStore for FsObjStore {
    fn kind(&self) -> &str {
        Self::KIND
    }

    fn safe_uri(&self) -> &Url {
        &self.state.safe_uri
    }

    async fn healthcheck(&self) -> Result<(), anyhow::Error> {
        Ok(())
    }

    async fn meta(&self, key: &str) -> Result<Option<ObjectMeta>, anyhow::Error> {
        let path = self.key_path(key);
        match tokio::fs::metadata(&path).await {
            Ok(meta) => Ok(Some(meta_from_fs_meta(key.to_string(), meta))),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(err) => Err(err.into()),
        }
    }

    async fn get(&self, key: &str) -> Result<Option<Bytes>, anyhow::Error> {
        let path = self.key_path(key);
        let data = match tokio::fs::read(&path).await {
            Ok(data) => Some(data.into()),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => None,
            Err(err) => return Err(err.into()),
        };
        Ok(data)
    }

    async fn get_stream(&self, key: &str) -> Result<Option<ValueStream>, anyhow::Error> {
        let path = self.key_path(key);
        match tokio::fs::File::open(&path).await {
            Ok(file) => {
                let stream = tokio_util::io::ReaderStream::new(file)
                    .map_ok(Bytes::from)
                    .map_err(anyhow::Error::from)
                    .boxed();
                Ok(Some(stream))
            }
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(err) => Err(err.into()),
        }
    }

    async fn get_with_meta(&self, key: &str) -> Result<Option<(Bytes, ObjectMeta)>, anyhow::Error> {
        let mut f = match tokio::fs::File::open(self.key_path(key)).await {
            Ok(f) => f,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(err) => return Err(err.into()),
        };
        let fs_meta = match f.metadata().await {
            Ok(meta) => meta,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(err) => return Err(err.into()),
        };
        let mut buf = Vec::with_capacity(fs_meta.len() as usize);
        f.read_to_end(&mut buf).await?;

        let meta = meta_from_fs_meta(key.to_string(), fs_meta);
        Ok(Some((buf.into(), meta)))
    }

    async fn get_stream_with_meta(
        &self,
        key: &str,
    ) -> Result<Option<(ObjectMeta, ValueStream)>, anyhow::Error> {
        let path = self.key_path(key);
        let f = match tokio::fs::File::open(&path).await {
            Ok(f) => f,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(err) => return Err(err.into()),
        };
        let fs_meta = match f.metadata().await {
            Ok(meta) => meta,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(None),
            Err(err) => return Err(err.into()),
        };
        let stream = tokio_util::io::ReaderStream::new(f)
            .map_ok(Bytes::from)
            .map_err(anyhow::Error::from)
            .boxed();

        let meta = meta_from_fs_meta(key.to_string(), fs_meta);
        Ok(Some((meta, stream)))
    }

    async fn generate_download_url(
        &self,
        _args: DownloadUrlArgs,
    ) -> Result<Option<url::Url>, anyhow::Error> {
        Ok(None)
    }

    async fn send_put(&self, put: Put) -> Result<ObjectMeta, anyhow::Error> {
        let path = self.key_path(&put.key);
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }

        match put.data {
            DataSource::Data(value) => {
                tokio::fs::write(&path, &value).await?;
            }
            DataSource::Stream(mut stream) => {
                let mut file = tokio::fs::File::create(&path).await?;

                while let Some(chunk) = stream.next().await {
                    file.write_all(&chunk?).await?;
                }

                file.sync_all().await?;
            }
        }

        let fs_meta = tokio::fs::metadata(&path).await?;
        let meta = meta_from_fs_meta(put.key, fs_meta);

        Ok(meta)
    }

    async fn send_copy(&self, copy: Copy) -> Result<ObjectMeta, anyhow::Error> {
        let src_path = self.key_path(&copy.source_key);
        let dst_path = self.key_path(&copy.target_key);
        // If requested, ensure destination does not exist

        // TODO: conditions support

        if let Some(parent) = dst_path.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }
        // Perform file copy
        tokio::fs::copy(&src_path, &dst_path).await?;
        // Build metadata from filesystem and compute hash
        let fs_meta = tokio::fs::metadata(&dst_path).await?;
        let data = tokio::fs::read(&dst_path).await?;
        let mut meta = meta_from_fs_meta(copy.target_key.clone(), fs_meta);
        // Compute sha256 hash of copied data
        let digest = sha2::Sha256::digest(&data);
        meta.hash_sha256 = Some(digest.into());
        Ok(meta)
    }

    async fn delete(&self, key: &str) -> Result<(), anyhow::Error> {
        let path = self.key_path(key);
        tokio::fs::remove_file(&path).await?;
        Ok(())
    }

    async fn list(&self, args: ListArgs) -> Result<ObjectMetaPage, anyhow::Error> {
        let limit = args.limit().unwrap_or(10_000) as usize;

        // Must compute the prefix as a parent directory.

        let (path, key_path, prefix) = if let Some(prefix) = args.prefix() {
            match prefix.rsplit_once('/') {
                Some((main, rest)) => (self.key_path(main), main, Some(rest)),
                None => (self.state.root.clone(), "", Some(prefix)),
            }
        } else {
            (self.state.root.clone(), "", None)
        };

        let flat = if let Some(delim) = args.delimiter() {
            if delim == "/" {
                true
            } else {
                bail!("The fs store only supports '/' as a delimiter");
            }
        } else {
            false
        };

        let (items, directories) =
            list_dir(&path, args.cursor(), limit, prefix, key_path, flat).await?;

        Ok(ObjectMetaPage {
            next_cursor: items.last().map(|item| item.key().to_owned()),
            items,
            prefixes: directories,
        })
    }

    async fn list_keys(&self, args: ListArgs) -> Result<KeyPage, anyhow::Error> {
        let meta_items = self.list(args).await?;
        let items = meta_items.items.into_iter().map(|item| item.key).collect();
        let page = KeyPage {
            items,
            next_cursor: meta_items.next_cursor,
        };
        Ok(page)
    }

    async fn list_all_keys(&self, prefix: &str) -> Result<Vec<String>, anyhow::Error> {
        let args = ListArgs::new().with_prefix(prefix).with_limit(u64::MAX);
        let meta_items = self.list(args).await?;
        let keys = meta_items
            .items
            .into_iter()
            .map(|item| item.key)
            .collect::<Vec<_>>();
        Ok(keys)
    }

    async fn delete_prefix(&self, prefix: &str) -> Result<(), anyhow::Error> {
        let path = self.key_path(prefix);

        // check if dir or file
        let meta = match tokio::fs::metadata(&path).await {
            Ok(meta) => meta,
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(()),
            Err(err) => return Err(err.into()),
        };

        let res = if meta.is_dir() {
            tokio::fs::remove_dir_all(&path).await
        } else {
            tokio::fs::remove_file(&path).await
        };
        match res {
            Ok(_) => Ok(()),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(err) => Err(err.into()),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn test_kv_fs() {
        let dir = tempfile::tempdir().unwrap();
        let config = FsObjStoreConfig::new(dir.path().to_owned());
        let store = FsObjStore::new(config).unwrap();

        objstore_test::test_objstore(&store).await;
    }
}