nuts_directory/
options.rs

1// MIT License
2//
3// Copyright (c) 2022-2024 Robin Doer
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to
7// deal in the Software without restriction, including without limitation the
8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9// sell copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in
13// all copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21// IN THE SOFTWARE.
22
23#[cfg(test)]
24mod tests;
25
26use nuts_backend::{Binary, Create, Open, ReceiveHeader, HEADER_MAX_SIZE};
27use std::convert::TryInto;
28use std::path::Path;
29
30use crate::error::{Error, Result};
31use crate::id::Id;
32use crate::{read_header, write_header, DirectoryBackend};
33
34const BLOCK_MIN_SIZE: u32 = 512;
35
36/// [Options](nuts_backend::Create) needed to create the backend.
37///
38/// You must pass the path, where the directory tree should be stored, to
39/// [`CreateOptions::for_path()`], if creating a `CreateOptions` instance.
40///
41/// Furthermore the following options can be specified:
42///
43/// * [`CreateOptions::with_bsize()`]: Specifies the block size of the backend.
44///   This is the number of bytes, which can  be stored in an individual block.
45///   The minimum block size is 512 bytes. The default is `512`.
46#[derive(Clone, Debug)]
47pub struct CreateOptions<P: AsRef<Path>> {
48    path: P,
49    bsize: u32,
50}
51
52impl<P: AsRef<Path>> CreateOptions<P> {
53    /// Creates a new `CreateOptions` instance.
54    ///
55    /// You must pass the `path`, where the directory tree should be stored, to
56    /// the function.
57    ///
58    /// For further options default values are applied.
59    pub fn for_path(path: P) -> Self {
60        CreateOptions {
61            path,
62            bsize: BLOCK_MIN_SIZE,
63        }
64    }
65
66    /// Assigns a new block size to the options.
67    ///
68    /// This is the number of bytes, which can  be stored in an individual
69    /// block.
70    pub fn with_bsize(mut self, bsize: u32) -> Self {
71        self.bsize = bsize;
72        self
73    }
74
75    fn validate(&self) -> Result<()> {
76        if self.bsize >= BLOCK_MIN_SIZE {
77            Ok(())
78        } else {
79            Err(Error::InvalidBlockSize(self.bsize))
80        }
81    }
82}
83
84impl<P: AsRef<Path>> Create<DirectoryBackend<P>> for CreateOptions<P> {
85    fn settings(&self) -> Settings {
86        Settings { bsize: self.bsize }
87    }
88
89    fn build(self, header: [u8; HEADER_MAX_SIZE], overwrite: bool) -> Result<DirectoryBackend<P>> {
90        self.validate()?;
91
92        if !overwrite {
93            let header_path = Id::min().to_pathbuf(self.path.as_ref());
94
95            if header_path.exists() {
96                return Err(Error::Exists);
97            }
98        }
99
100        write_header(self.path.as_ref(), self.bsize, &header)?;
101
102        Ok(DirectoryBackend {
103            bsize: self.bsize,
104            path: self.path,
105        })
106    }
107}
108
109/// [Options](nuts_backend::Open) needed to open the backend.
110///
111/// You must pass the path, where the directory tree is stored, to
112/// [`OpenOptions::for_path()`], if creating a `OpenOptions` instance.
113pub struct OpenOptions<P: AsRef<Path>> {
114    path: P,
115}
116
117impl<P: AsRef<Path>> OpenOptions<P> {
118    /// Creates a new `OpenOptions` instance.
119    ///
120    /// You must pass the `path`, where the directory tree should is stored, to
121    /// the function.
122    pub fn for_path(path: P) -> OpenOptions<P> {
123        OpenOptions { path }
124    }
125}
126
127impl<P: AsRef<Path>> ReceiveHeader<DirectoryBackend<P>> for OpenOptions<P> {
128    fn get_header_bytes(&mut self, bytes: &mut [u8; HEADER_MAX_SIZE]) -> Result<()> {
129        read_header(self.path.as_ref(), bytes)
130    }
131}
132
133impl<P: AsRef<Path>> Open<DirectoryBackend<P>> for OpenOptions<P> {
134    fn build(self, settings: Settings) -> Result<DirectoryBackend<P>> {
135        Ok(DirectoryBackend {
136            bsize: settings.bsize,
137            path: self.path,
138        })
139    }
140}
141
142/// [Settings](nuts_backend::Backend::Settings) used by the backend.
143#[derive(Clone, Debug)]
144pub struct Settings {
145    bsize: u32,
146}
147
148impl Binary for Settings {
149    fn from_bytes(bytes: &[u8]) -> Option<Settings> {
150        match bytes.try_into() {
151            Ok(bytes) => {
152                let bsize = u32::from_be_bytes(bytes);
153                Some(Settings { bsize })
154            }
155            Err(_) => None,
156        }
157    }
158
159    fn as_bytes(&self) -> Vec<u8> {
160        self.bsize.to_be_bytes().to_vec()
161    }
162}