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
use std::collections::HashMap;
use std::default::Default;
use std::fs::File;
use std::fs::OpenOptions;
use std::io::{prelude::*, BufReader, BufWriter, Result, SeekFrom};
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};

use memmap::MmapOptions;

use crate::{
    compression::Compression,
    header::BoxHeader,
    path::BoxPath,
    record::{DirectoryRecord, FileRecord, LinkRecord, Record},
    ser::Serialize,
};

use super::{
    reader::{read_header, read_trailer},
    BoxMetadata,
};

pub struct BoxFileWriter {
    pub(crate) file: BufWriter<File>,
    pub(crate) path: PathBuf,
    pub(crate) header: BoxHeader,
    pub(crate) meta: BoxMetadata,
}

impl Drop for BoxFileWriter {
    fn drop(&mut self) {
        let _ = self.finish_inner();
    }
}

impl BoxFileWriter {
    #[inline(always)]
    fn write_header(&mut self) -> std::io::Result<()> {
        self.file.seek(SeekFrom::Start(0))?;
        self.header.write(&mut self.file)
    }

    #[inline(always)]
    fn finish_inner(&mut self) -> std::io::Result<u64> {
        let pos = self.next_write_addr().get();
        self.header.trailer = NonZeroU64::new(pos);
        self.write_header()?;
        self.file.seek(SeekFrom::Start(pos))?;
        self.meta.write(&mut self.file)?;

        let new_pos = self.file.seek(SeekFrom::Current(0))?;
        let file = self.file.get_mut();
        file.set_len(new_pos)?;
        Ok(new_pos)
    }

    pub fn finish(mut self) -> std::io::Result<u64> {
        self.finish_inner()
    }

    #[inline(always)]
    fn next_write_addr(&self) -> NonZeroU64 {
        // TODO: this is probably slow as hell
        let offset = self
            .meta
            .inodes
            .iter()
            .rev()
            .find_map(|r| r.as_file())
            .map(|r| r.data.get() + r.length)
            .unwrap_or(std::mem::size_of::<BoxHeader>() as u64);

        let v = match self.header.alignment {
            0 => offset,
            alignment => {
                let diff = offset % alignment;
                if diff == 0 {
                    offset
                } else {
                    offset + (alignment - diff)
                }
            }
        };

        NonZeroU64::new(v).unwrap()
    }

    /// This will open an existing `.box` file for writing, and error if the file is not valid.
    pub fn open<P: AsRef<Path>>(path: P) -> std::io::Result<BoxFileWriter> {
        OpenOptions::new()
            .read(true)
            .write(true)
            .open(path.as_ref())
            .map(|mut file| {
                // Try to load the header so we can easily rewrite it when saving.
                // If header is invalid, we're not even loading a .box file.
                let (header, meta) = {
                    let mut reader = BufReader::new(&mut file);
                    let header = read_header(&mut reader, 0)?;
                    let ptr = header.trailer.ok_or_else(|| {
                        std::io::Error::new(std::io::ErrorKind::Other, "no trailer found")
                    })?;
                    let meta = read_trailer(&mut reader, ptr, path.as_ref(), 0)?;
                    (header, meta)
                };

                let f = BoxFileWriter {
                    file: BufWriter::new(file),
                    path: path.as_ref().to_path_buf().canonicalize()?,
                    header,
                    meta,
                };

                Ok(f)
            })?
    }

    /// This will create a new `.box` file for writing, and error if the file already exists.
    pub fn create<P: AsRef<Path>>(path: P) -> std::io::Result<BoxFileWriter> {
        let mut boxfile = OpenOptions::new()
            .write(true)
            .read(true)
            .create_new(true)
            .open(path.as_ref())
            .and_then(|file| {
                Ok(BoxFileWriter {
                    file: BufWriter::new(file),
                    path: path.as_ref().to_path_buf().canonicalize()?,
                    header: BoxHeader::default(),
                    meta: BoxMetadata::default(),
                })
            })?;

        boxfile.write_header()?;

        Ok(boxfile)
    }

    /// This will create a new `.box` file for reading and writing, and error if the file already exists.
    /// Will insert byte-aligned values based on provided `alignment` value. For best results, consider a power of 2.
    pub fn create_with_alignment<P: AsRef<Path>>(
        path: P,
        alignment: u64,
    ) -> std::io::Result<BoxFileWriter> {
        let mut boxfile = OpenOptions::new()
            .write(true)
            .read(true)
            .create_new(true)
            .open(path.as_ref())
            .and_then(|file| {
                Ok(BoxFileWriter {
                    file: BufWriter::new(file),
                    path: path.as_ref().to_path_buf().canonicalize()?,
                    header: BoxHeader::with_alignment(alignment),
                    meta: BoxMetadata::default(),
                })
            })?;

        boxfile.write_header()?;

        Ok(boxfile)
    }

    pub fn path(&self) -> &Path {
        &self.path
    }

    pub fn alignment(&self) -> u64 {
        self.header.alignment
    }

    pub fn version(&self) -> u32 {
        self.header.version
    }

    /// Will return the metadata for the `.box` if it has been provided.
    pub fn metadata(&self) -> &BoxMetadata {
        &self.meta
    }

    #[inline(always)]
    fn iter(&self) -> super::meta::Records {
        super::meta::Records::new(self.metadata(), &*self.metadata().root, None)
    }

    #[inline(always)]
    fn insert_inner<F>(&mut self, path: BoxPath, create_record: F) -> std::io::Result<()>
    where
        F: FnOnce(&mut Self, &BoxPath) -> std::io::Result<Record>,
    {
        log::debug!("insert_inner path: {:?}", path);
        match path.parent() {
            Some(parent) => {
                log::debug!("insert_inner parent: {:?}", parent);

                match self.meta.inode(&parent) {
                    None => {
                        let err = std::io::Error::new(
                            std::io::ErrorKind::Other,
                            format!("No inode found for path: {:?}", parent),
                        );
                        Err(err)
                    }
                    Some(parent) => {
                        let record = create_record(self, &path)?;
                        log::debug!("Inserting record into parent {:?}: {:?}", &parent, &record);
                        let new_inode = self.meta.insert_record(record);
                        log::debug!("Inserted with inode: {:?}", &new_inode);
                        let parent = self
                            .meta
                            .record_mut(parent)
                            .unwrap()
                            .as_directory_mut()
                            .unwrap();
                        parent.inodes.push(new_inode);
                        Ok(())
                    }
                }
            }
            None => {
                let record = create_record(self, &path)?;
                log::debug!("Inserting record into root: {:?}", &record);
                let new_inode = self.meta.insert_record(record);
                self.meta.root.push(new_inode);
                Ok(())
            }
        }
    }

    pub fn mkdir(&mut self, path: BoxPath, attrs: HashMap<String, Vec<u8>>) -> std::io::Result<()> {
        log::debug!("mkdir: {}", path);

        self.insert_inner(path, move |this, path| {
            let attrs = attrs
                .into_iter()
                .map(|(k, v)| {
                    let k = this.meta.attr_key_or_create(&k);
                    (k, v)
                })
                .collect::<HashMap<_, _>>();

            let dir_record = DirectoryRecord {
                name: path.filename(),
                inodes: vec![],
                attrs,
            };

            Ok(dir_record.upcast())
        })
    }

    pub fn link(
        &mut self,
        path: BoxPath,
        target: BoxPath,
        attrs: HashMap<String, Vec<u8>>,
    ) -> std::io::Result<()> {
        self.insert_inner(path, move |this, path| {
            let attrs = attrs
                .into_iter()
                .map(|(k, v)| {
                    let k = this.meta.attr_key_or_create(&k);
                    (k, v)
                })
                .collect::<HashMap<_, _>>();

            let link_record = LinkRecord {
                name: path.filename(),
                target,
                attrs,
            };

            Ok(link_record.upcast())
        })
    }

    pub fn insert<R: Read>(
        &mut self,
        compression: Compression,
        path: BoxPath,
        value: &mut R,
        attrs: HashMap<String, Vec<u8>>,
    ) -> std::io::Result<&FileRecord> {
        self.insert_inner(path, move |this, path| {
            let next_addr = this.next_write_addr();
            let byte_count = this.write_data::<R>(compression, next_addr.get(), value)?;
            let attrs = attrs
                .into_iter()
                .map(|(k, v)| {
                    let k = this.meta.attr_key_or_create(&k);
                    (k, v)
                })
                .collect::<HashMap<_, _>>();

            let record = FileRecord {
                compression,
                length: byte_count.write,
                decompressed_length: byte_count.read,
                name: path.filename(),
                data: next_addr,
                attrs,
            };

            Ok(record.upcast())
        })?;

        Ok(&self.meta.inodes.last().unwrap().as_file().unwrap())
    }

    /// # Safety
    ///
    /// Use of memory maps is unsafe as modifications to the file could affect the operation
    /// of the application. Ensure that the Box being operated on is not mutated while a memory
    /// map is in use.
    pub unsafe fn data(&self, record: &FileRecord) -> std::io::Result<memmap::Mmap> {
        self.read_data(record)
    }

    #[inline(always)]
    fn write_data<R: Read>(
        &mut self,
        compression: Compression,
        pos: u64,
        reader: &mut R,
    ) -> std::io::Result<comde::com::ByteCount> {
        self.file.seek(SeekFrom::Start(pos))?;
        compression.compress(&mut self.file, reader)
    }

    pub fn set_attr<S: AsRef<str>>(
        &mut self,
        path: &BoxPath,
        key: S,
        value: Vec<u8>,
    ) -> Result<()> {
        let inode = match self.iter().find(|r| &r.path == path) {
            Some(v) => v.inode,
            None => todo!(),
        };

        let key = self.meta.attr_key_or_create(key.as_ref());
        let record = self.meta.record_mut(inode).unwrap();
        record.attrs_mut().insert(key, value);

        Ok(())
    }

    pub fn set_file_attr<S: AsRef<str>>(&mut self, key: S, value: Vec<u8>) -> Result<()> {
        let key = self.meta.attr_key_or_create(key.as_ref());

        self.meta.attrs.insert(key, value);

        Ok(())
    }

    #[inline(always)]
    unsafe fn read_data(&self, header: &FileRecord) -> std::io::Result<memmap::Mmap> {
        MmapOptions::new()
            .offset(header.data.get())
            .len(header.length as usize)
            .map(&self.file.get_ref())
    }
}