Skip to main content

fxfs/
object_handle.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::object_store::{DirType, PosixAttributes, Timestamp};
6use anyhow::Error;
7use async_trait::async_trait;
8use std::future::Future;
9use std::ops::Deref;
10use std::pin::Pin;
11use storage_device::buffer::{BufferFuture, BufferRef, MutableBufferRef};
12
13// Some places use Default and assume that zero is an invalid object ID, so this cannot be changed
14// easily.
15pub const INVALID_OBJECT_ID: u64 = 0;
16
17/// A handle for a generic object.  For objects with a data payload, use the ReadObjectHandle or
18/// WriteObjectHandle traits.
19pub trait ObjectHandle: Send + Sync + 'static {
20    /// Returns the object identifier for this object which will be unique for the store that the
21    /// object is contained in, but not necessarily unique within the entire system.
22    fn object_id(&self) -> u64;
23
24    /// Returns the filesystem block size, which should be at least as big as the device block size,
25    /// but not necessarily the same.
26    fn block_size(&self) -> u64;
27
28    /// Allocates a buffer for doing I/O (read and write) for the object.
29    fn allocate_buffer(&self, size: usize) -> BufferFuture<'_>;
30
31    /// Sets tracing for this object.
32    fn set_trace(&self, _v: bool) {}
33}
34
35#[derive(Clone, Debug, PartialEq)]
36pub struct ObjectProperties {
37    /// The number of references to this object.
38    pub refs: u64,
39    /// The number of bytes allocated to all extents across all attributes for this object.
40    pub allocated_size: u64,
41    /// The logical content size for the default data attribute of this object, i.e. the size of a
42    /// file.  (Objects with no data attribute have size 0.)
43    pub data_attribute_size: u64,
44    /// The timestamp at which the object was created (i.e. crtime).
45    pub creation_time: Timestamp,
46    /// The timestamp at which the objects's data was last modified (i.e. mtime).
47    pub modification_time: Timestamp,
48    /// The timestamp at which the object was last read (i.e. atime).
49    pub access_time: Timestamp,
50    /// The timestamp at which the object's status was last modified (i.e. ctime).
51    pub change_time: Timestamp,
52    /// The number of sub-directories.
53    pub sub_dirs: u64,
54    /// POSIX attributes: mode, uid, gid, rdev
55    pub posix_attributes: Option<PosixAttributes>,
56    /// The type of directory (encryption, casefolding, etc.)
57    pub dir_type: DirType,
58}
59
60#[async_trait]
61pub trait ReadObjectHandle: ObjectHandle {
62    /// Fills |buf| with up to |buf.len()| bytes read from |offset| on the underlying device.
63    /// |offset| and |buf| must both be block-aligned.
64    async fn read(&self, offset: u64, buf: MutableBufferRef<'_>) -> Result<usize, Error>;
65
66    /// Returns the size of the object.
67    fn get_size(&self) -> u64;
68}
69
70pub trait WriteObjectHandle: ObjectHandle {
71    /// Writes |buf.len())| bytes at |offset| (or the end of the file), returning the object size
72    /// after writing.
73    /// The writes may be cached, in which case a later call to |flush| is necessary to persist the
74    /// writes.
75    fn write_or_append(
76        &self,
77        offset: Option<u64>,
78        buf: BufferRef<'_>,
79    ) -> impl Future<Output = Result<u64, Error>> + Send;
80
81    /// Truncates the object to |size| bytes.
82    /// The truncate may be cached, in which case a later call to |flush| is necessary to persist
83    /// the truncate.
84    fn truncate(&self, size: u64) -> impl Future<Output = Result<(), Error>> + Send;
85
86    /// Flushes all pending data and metadata updates for the object.
87    fn flush(&self) -> impl Future<Output = Result<(), Error>> + Send;
88}
89
90/// This trait is an asynchronous streaming writer.
91pub trait WriteBytes: Sized {
92    fn block_size(&self) -> u64;
93
94    /// Buffers writes to be written to the underlying handle. This may flush bytes immediately
95    /// or when buffers are full.
96    fn write_bytes(&mut self, buf: &[u8]) -> impl Future<Output = Result<(), Error>> + Send;
97
98    /// Called to flush to the handle.  Named to avoid conflict with the flush method above.
99    fn complete(&mut self) -> impl Future<Output = Result<(), Error>> + Send;
100
101    /// Moves the offset forward by `amount`, which will result in zeroes in the output stream, even
102    /// if no other data is appended to it.
103    fn skip(&mut self, amount: u64) -> impl Future<Output = Result<(), Error>> + Send;
104
105    /// Returns the total bytes written to the writer.
106    fn bytes_written(&self) -> u64;
107}
108
109// Implements ReadObjectHandle for things like `Arc<dyn ReadObjectHandle>` and
110// `Box<dyn ReadObjectHandle>`.  The below impl of `ObjectHandle` is also necessary for this.
111impl<T: Deref<Target = dyn ReadObjectHandle> + Send + Sync + 'static> ReadObjectHandle for T {
112    // Manual expansion of `async_trait` to avoid double boxing the `Future`.
113    fn read<'a, 'b, 'c>(
114        &'a self,
115        offset: u64,
116        buf: MutableBufferRef<'b>,
117    ) -> Pin<Box<dyn Future<Output = Result<usize, Error>> + Send + 'c>>
118    where
119        'a: 'c,
120        'b: 'c,
121        Self: 'c,
122    {
123        (**self).read(offset, buf)
124    }
125
126    fn get_size(&self) -> u64 {
127        (**self).get_size()
128    }
129}
130
131impl<T: Deref<Target = dyn ReadObjectHandle> + Send + Sync + 'static> ObjectHandle for T {
132    fn object_id(&self) -> u64 {
133        (**self).object_id()
134    }
135
136    fn block_size(&self) -> u64 {
137        (**self).block_size()
138    }
139
140    fn allocate_buffer(&self, size: usize) -> BufferFuture<'_> {
141        (**self).allocate_buffer(size)
142    }
143
144    fn set_trace(&self, v: bool) {
145        (**self).set_trace(v)
146    }
147}