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
// MIT License
//
// Copyright (c) 2022-2024 Robin Doer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.

//! Nuts backend implementation where the blocks of the container are stored
//! in a file hierarchy.
//!
//! # Introduction
//!
//! The _nuts-directory_ crate implements a [nuts] backend where the blocks of
//! the container are stored in a file hierarchy. Each block is identified by
//! an [id](Id), which is basically a 16 byte random number.
//!
//! When storing a block to disks the path to the file is derived from the id:
//!
//! 1. The id is converted into a hex string.
//! 2. The path then would be:
//!    `<first two chars>/<next two chars>/<remaining chars>`
//!
//! The header of the container is stored in the file
//! `00/00/0000000000000000000000000000`.
//!
//! # Create a new backend instance
//!
//! The [`CreateOptions`] type is used to create a new backend instance, which
//! is passed to the [`Container::create`] method. You need at least a
//! directory where the backend put its blocks. See the [`CreateOptions`]
//! documentation for further options.
//!
//! # Open an existing backend
//!
//! The [`OpenOptions`] type is used to open a backend instance, which is
//! passed to the [`Container::open`] method. You need the directory where the
//! backend put its blocks.
//!
//! [nuts]: https://crates.io/crates/nuts-container
//! [`Container::create`]: https://docs.rs/nuts-container/latest/nuts_container/container/struct.Container.html#method.create
//! [`Container::open`]: https://docs.rs/nuts-container/latest/nuts_container/container/struct.Container.html#method.open

mod error;
mod id;
mod info;
mod options;

use log::{error, warn};
use nuts_backend::{Backend, ReceiveHeader, HEADER_MAX_SIZE};
use std::io::{self, ErrorKind, Read, Write};
use std::path::Path;
use std::{cmp, fs};

pub use error::Error;
pub use id::Id;
pub use info::Info;
pub use options::{CreateOptions, OpenOptions, Settings};

use crate::error::Result;

fn read_block(path: &Path, id: &Id, bsize: u32, buf: &mut [u8]) -> Result<usize> {
    let path = id.to_pathbuf(path);
    let mut fh = fs::OpenOptions::new().read(true).open(path)?;

    let len = cmp::min(buf.len(), bsize as usize);
    let target = &mut buf[..len];

    fh.read_exact(target)?;

    Ok(len)
}

fn write_block(
    path: &Path,
    id: &Id,
    aquire: bool,
    header: bool,
    bsize: u32,
    buf: &[u8],
) -> Result<usize> {
    let path = id.to_pathbuf(path);

    if let Some(dir) = path.parent() {
        fs::create_dir_all(dir)?;
    }

    if aquire {
        // A block is aquired. Allow only to create non-existing files.
        if path.exists() {
            return Err(io::Error::new(
                ErrorKind::Other,
                format!("cannot aquire {}, already stored in {}", id, path.display()),
            )
            .into());
        }
    } else {
        // * The header block can be created even if it does not exist.
        // * Any other block must be aquired before, thus open should fail if the
        //   file does not exist.
        if !header && !path.is_file() {
            return Err(io::Error::new(
                ErrorKind::Other,
                format!("cannot open {}, no related file {}", id, path.display()),
            )
            .into());
        }
    }

    let tmp_path = path.with_extension("tmp");

    let mut fh = fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&tmp_path)?;

    let len = cmp::min(buf.len(), bsize as usize);
    let pad_len = bsize as usize - len;

    fh.write_all(&buf[..len])?;
    fh.write_all(&vec![0; pad_len])?;
    fh.flush()?;

    fs::rename(tmp_path, path)?;

    Ok(len)
}

fn read_header(path: &Path, buf: &mut [u8]) -> Result<()> {
    read_block(path, &Id::min(), HEADER_MAX_SIZE as u32, buf).map(|_| ())
}

fn write_header(path: &Path, bsize: u32, buf: &[u8]) -> Result<()> {
    write_block(path, &Id::min(), false, true, bsize, buf).map(|_| ())
}

#[derive(Debug)]
pub struct DirectoryBackend<P: AsRef<Path>> {
    bsize: u32,
    path: P,
}

impl<P: AsRef<Path>> ReceiveHeader<Self> for DirectoryBackend<P> {
    fn get_header_bytes(&mut self, bytes: &mut [u8; HEADER_MAX_SIZE]) -> Result<()> {
        read_header(self.path.as_ref(), bytes)
    }
}

impl<P: AsRef<Path>> Backend for DirectoryBackend<P> {
    type Settings = Settings;
    type Err = Error;
    type Id = Id;
    type Info = Info;

    fn info(&self) -> Result<Info> {
        Ok(Info { bsize: self.bsize })
    }

    fn block_size(&self) -> u32 {
        self.bsize
    }

    fn aquire(&mut self, buf: &[u8]) -> Result<Self::Id> {
        const MAX: u8 = 3;

        for n in 0..MAX {
            let id = Id::generate();

            match write_block(self.path.as_ref(), &id, true, false, self.bsize, buf) {
                Ok(_) => return Ok(id),
                Err(Error::Io(err)) => {
                    if err.kind() == ErrorKind::AlreadyExists {
                        warn!("Id {} already exists try again ({}/{})", id, n + 1, MAX);
                    } else {
                        return Err(err.into());
                    }
                }
                Err(err) => return Err(err),
            };
        }

        Err(Error::UniqueId)
    }

    fn release(&mut self, id: Self::Id) -> Result<()> {
        let path = id.to_pathbuf(self.path.as_ref());

        Ok(fs::remove_file(&path)?)
    }

    fn read(&mut self, id: &Id, buf: &mut [u8]) -> Result<usize> {
        read_block(self.path.as_ref(), id, self.bsize, buf)
    }

    fn write(&mut self, id: &Id, buf: &[u8]) -> Result<usize> {
        write_block(self.path.as_ref(), id, false, false, self.bsize, buf)
    }

    fn write_header(&mut self, buf: &[u8; HEADER_MAX_SIZE]) -> Result<()> {
        write_header(self.path.as_ref(), self.bsize, buf)
    }

    fn delete(self) {
        if let Err(err) = fs::remove_dir_all(self.path) {
            error!("failed to delete backend instance: {}", err);
        }
    }
}