fidl_fuchsia_pkg_ext/cache/
storage.rs

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
// Copyright 2023 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use super::{OpenBlobError, TruncateBlobError, WriteBlobError};
use anyhow::Context as _;
use zx_status::Status;
use {fidl_fuchsia_fxfs as ffxfs, fidl_fuchsia_io as fio, fidl_fuchsia_pkg as fpkg};

pub(super) fn into_blob_writer_and_closer(
    fidl: fpkg::BlobWriter,
) -> Result<(Box<dyn Writer>, Box<dyn Closer>), OpenBlobError> {
    use fpkg::BlobWriter::*;
    match fidl {
        File(file) => {
            let proxy = file.into_proxy();
            Ok((Box::new(Clone::clone(&proxy)), Box::new(proxy)))
        }
        Writer(writer) => {
            // fuchsia.fxfs/BlobCreator allows concurrent creation attempts, so we don't need to
            // cancel an ongoing attempt before trying again.
            Ok((Box::new(FxBlob::new(writer.into_proxy())), Box::new(())))
        }
    }
}

#[async_trait::async_trait]
pub(super) trait Closer: Send + Sync + std::fmt::Debug {
    /// Close the blob to enable immediate retry of create and write.
    async fn close(&mut self);

    /// Attempt to close the blob. Function may return before blob is closed if closing requires
    /// async.
    fn best_effort_close(&mut self);
}

#[async_trait::async_trait]
impl Closer for fio::FileProxy {
    async fn close(&mut self) {
        let _: Result<Result<(), i32>, fidl::Error> = fio::FileProxy::close(self).await;
    }

    fn best_effort_close(&mut self) {
        let _: fidl::client::QueryResponseFut<Result<(), i32>> = fio::FileProxy::close(self);
    }
}

// fuchsia.fxfs/BlobCreator allows concurrent creation attempts, so we don't need to cancel an
// ongoing attempt before trying again.
#[async_trait::async_trait]
impl Closer for () {
    async fn close(&mut self) {}

    fn best_effort_close(&mut self) {}
}

#[async_trait::async_trait]
pub(super) trait Writer: Send + std::fmt::Debug {
    /// Set the size of the blob.
    /// If the blob is size zero, the returned Future should not complete until the blob
    /// is readable.
    async fn truncate(&mut self, size: u64) -> Result<(), TruncateBlobError>;
    /// Write `bytes` to the blob.
    /// The Future returned by the `write` call that writes the final bytes should
    /// not complete until the blob is readable.
    async fn write(
        &mut self,
        bytes: &[u8],
        after_write: &(dyn Fn(u64) + Send + Sync),
        after_write_ack: &(dyn Fn() + Send + Sync),
    ) -> Result<(), WriteBlobError>;
}

#[async_trait::async_trait]
impl Writer for fio::FileProxy {
    async fn truncate(&mut self, size: u64) -> Result<(), TruncateBlobError> {
        self.resize(size).await?.map_err(|i| match Status::from_raw(i) {
            Status::NO_SPACE => TruncateBlobError::NoSpace,
            other => TruncateBlobError::UnexpectedResponse(other),
        })
    }

    async fn write(
        &mut self,
        mut bytes: &[u8],
        after_write: &(dyn Fn(u64) + Send + Sync),
        after_write_ack: &(dyn Fn() + Send + Sync),
    ) -> Result<(), WriteBlobError> {
        while !bytes.is_empty() {
            let limit = bytes.len().min(fio::MAX_BUF as usize);

            let result_fut = fio::FileProxy::write(self, &bytes[..limit]);
            after_write(bytes.len() as u64);

            let result = result_fut.await;
            after_write_ack();

            let written = result?.map_err(|i| match Status::from_raw(i) {
                Status::IO_DATA_INTEGRITY => WriteBlobError::Corrupt,
                Status::NO_SPACE => WriteBlobError::NoSpace,
                other => WriteBlobError::UnexpectedResponse(other),
            })? as usize;

            if written > bytes.len() {
                return Err(WriteBlobError::Overwrite);
            }
            bytes = &bytes[written..];
        }

        Ok(())
    }
}

#[derive(Debug)]
enum FxBlob {
    NeedsTruncate(ffxfs::BlobWriterProxy),
    NeedsBytes(blob_writer::BlobWriter),
    Invalid,
}

impl FxBlob {
    fn new(proxy: ffxfs::BlobWriterProxy) -> Self {
        Self::NeedsTruncate(proxy)
    }

    fn state_str(&self) -> &'static str {
        match self {
            Self::NeedsTruncate(_) => "needs truncate",
            Self::NeedsBytes(_) => "needs bytes",
            Self::Invalid => "invalid",
        }
    }
}

#[async_trait::async_trait]
impl Writer for FxBlob {
    async fn truncate(&mut self, size: u64) -> Result<(), TruncateBlobError> {
        *self = match std::mem::replace(self, Self::Invalid) {
            Self::NeedsTruncate(proxy) => Self::NeedsBytes(
                blob_writer::BlobWriter::create(proxy, size)
                    .await
                    .context("creating a BlobWriter")
                    .map_err(TruncateBlobError::Other)?,
            ),
            Self::NeedsBytes(_) => {
                return Err(TruncateBlobError::AlreadyTruncated(self.state_str()))
            }
            Self::Invalid => return Err(TruncateBlobError::BadState),
        };
        Ok(())
    }

    async fn write(
        &mut self,
        bytes: &[u8],
        after_write: &(dyn Fn(u64) + Send + Sync),
        after_write_ack: &(dyn Fn() + Send + Sync),
    ) -> Result<(), WriteBlobError> {
        let Self::NeedsBytes(writer) = self else {
            return Err(WriteBlobError::BytesNotNeeded(self.state_str()));
        };
        let fut = writer.write(bytes);
        let () = after_write(bytes.len() as u64);
        let res = fut.await;
        let () = after_write_ack();
        res.context("calling write on BlobWriter").map_err(WriteBlobError::Other)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::stream::TryStreamExt as _;

    #[fuchsia_async::run_singlethreaded(test)]
    async fn file_proxy_chunks_writes() {
        let (mut proxy, mut server) = fidl::endpoints::create_proxy_and_stream::<fio::FileMarker>();
        let bytes = vec![0; fio::MAX_BUF as usize + 1];

        let write_fut = async move {
            <fio::FileProxy as Writer>::write(&mut proxy, &bytes, &|_| (), &|| ()).await.unwrap()
        };
        let server_fut = async move {
            match server.try_next().await.unwrap().unwrap() {
                fio::FileRequest::Write { data, responder } => {
                    // Proxy limited writes to MAX_BUF bytes.
                    assert_eq!(data, vec![0; fio::MAX_BUF as usize]);
                    let () = responder.send(Ok(fio::MAX_BUF)).unwrap();
                }
                req => panic!("unexpected request {req:?}"),
            }
            match server.try_next().await.unwrap().unwrap() {
                fio::FileRequest::Write { data, responder } => {
                    assert_eq!(data, vec![0; 1]);
                    let () = responder.send(Ok(1)).unwrap();
                }
                req => panic!("unexpected request {req:?}"),
            }
            assert!(server.try_next().await.unwrap().is_none());
        };

        let ((), ()) = futures::future::join(write_fut, server_fut).await;
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn file_proxy_handles_short_writes() {
        let (mut proxy, mut server) = fidl::endpoints::create_proxy_and_stream::<fio::FileMarker>();
        let bytes = [0; 10];

        let write_fut = async move {
            <fio::FileProxy as Writer>::write(&mut proxy, &bytes, &|_| (), &|| ()).await.unwrap()
        };
        let server_fut = async move {
            match server.try_next().await.unwrap().unwrap() {
                fio::FileRequest::Write { data, responder } => {
                    assert_eq!(data, [0; 10]);
                    // Ack only 8 of the 10 bytes.
                    let () = responder.send(Ok(8)).unwrap();
                }
                req => panic!("unexpected request {req:?}"),
            }
            match server.try_next().await.unwrap().unwrap() {
                fio::FileRequest::Write { data, responder } => {
                    assert_eq!(data, [0; 2]);
                    let () = responder.send(Ok(2)).unwrap();
                }
                req => panic!("unexpected request {req:?}"),
            }
            assert!(server.try_next().await.unwrap().is_none());
        };

        let ((), ()) = futures::future::join(write_fut, server_fut).await;
    }

    #[fuchsia_async::run_singlethreaded(test)]
    async fn fxblob_writer() {
        let blobfs = blobfs_ramdisk::BlobfsRamdisk::builder().fxblob().start().await.unwrap();
        assert_eq!(blobfs.list_blobs().unwrap(), std::collections::BTreeSet::new());
        let contents = [0u8; 7];
        let hash = fuchsia_merkle::from_slice(&contents).root();
        let compressed = delivery_blob::Type1Blob::generate(
            &contents[..],
            delivery_blob::CompressionMode::Attempt,
        );
        let writer = blobfs
            .blob_creator_proxy()
            .unwrap()
            .unwrap()
            .create(&hash.into(), false)
            .await
            .unwrap()
            .unwrap();

        let (mut writer, _closer) =
            into_blob_writer_and_closer(fpkg::BlobWriter::Writer(writer)).unwrap();
        let () = writer.truncate(compressed.len().try_into().unwrap()).await.unwrap();
        let () = writer.write(&compressed, &|_| (), &|| ()).await.unwrap();

        assert_eq!(blobfs.list_blobs().unwrap(), std::collections::BTreeSet::from([hash]));

        let () = blobfs.stop().await.unwrap();
    }
}