fuchsia_archive/
async_read.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::{
6    DirectoryEntry, Error, Index, IndexEntry, DIRECTORY_ENTRY_LEN, DIR_CHUNK_TYPE,
7    DIR_NAMES_CHUNK_TYPE, INDEX_ENTRY_LEN, INDEX_LEN, MAGIC_INDEX_VALUE,
8};
9use fuchsia_fs::file::{AsyncGetSize, AsyncGetSizeExt, AsyncReadAt, AsyncReadAtExt};
10use std::convert::TryInto as _;
11use zerocopy::IntoBytes as _;
12
13/// A struct to open and read a FAR-formatted archive asynchronously.
14#[derive(Debug)]
15pub struct AsyncReader<T>
16where
17    T: AsyncReadAt + AsyncGetSize + Unpin,
18{
19    source: T,
20    directory_entries: Box<[DirectoryEntry]>,
21    path_data: Box<[u8]>,
22}
23
24impl<T> AsyncReader<T>
25where
26    T: AsyncReadAt + AsyncGetSize + Unpin,
27{
28    /// Create a new AsyncReader for the provided source.
29    pub async fn new(mut source: T) -> Result<Self, Error> {
30        let index = Self::read_index_header(&mut source).await?;
31        let (dir_index, dir_name_index, end_of_last_non_content_chunk) =
32            Self::read_index_entries(&mut source, &index).await?;
33        let stream_len = source.get_size().await.map_err(Error::GetSize)?;
34
35        // Read directory entries
36        if dir_index.length.get() % DIRECTORY_ENTRY_LEN != 0 {
37            return Err(Error::InvalidDirectoryChunkLen(dir_index.length.get()));
38        }
39        let mut directory_entries =
40            vec![
41                DirectoryEntry::default();
42                (dir_index.length.get() / DIRECTORY_ENTRY_LEN)
43                    .try_into()
44                    .map_err(|_| { Error::InvalidDirectoryChunkLen(dir_index.length.get()) })?
45            ];
46        source
47            .read_at_exact(dir_index.offset.get(), directory_entries.as_mut_bytes())
48            .await
49            .map_err(Error::Read)?;
50        let directory_entries = directory_entries.into_boxed_slice();
51
52        // Read path data
53        if dir_name_index.length.get() % 8 != 0 || dir_name_index.length.get() > stream_len {
54            return Err(Error::InvalidDirectoryNamesChunkLen(dir_name_index.length.get()));
55        }
56        let path_data_length = dir_name_index
57            .length
58            .get()
59            .try_into()
60            .map_err(|_| Error::InvalidDirectoryNamesChunkLen(dir_name_index.length.get()))?;
61        let mut path_data = vec![0; path_data_length];
62        source
63            .read_at_exact(dir_name_index.offset.get(), &mut path_data)
64            .await
65            .map_err(Error::Read)?;
66        let path_data = path_data.into_boxed_slice();
67
68        let () = crate::validate_directory_entries_and_paths(
69            &directory_entries,
70            &path_data,
71            stream_len,
72            end_of_last_non_content_chunk,
73        )?;
74
75        Ok(Self { source, directory_entries, path_data })
76    }
77
78    async fn read_index_header(source: &mut T) -> Result<Index, Error> {
79        let mut index = Index::default();
80        source.read_at_exact(0, index.as_mut_bytes()).await.map_err(Error::Read)?;
81        if index.magic != MAGIC_INDEX_VALUE {
82            Err(Error::InvalidMagic(index.magic))
83        } else if index.length.get() % INDEX_ENTRY_LEN != 0
84            || INDEX_LEN.checked_add(index.length.get()).is_none()
85        {
86            Err(Error::InvalidIndexEntriesLen(index.length.get()))
87        } else {
88            Ok(index)
89        }
90    }
91
92    // Returns (directory_index, directory_names_index, end_of_last_chunk).
93    async fn read_index_entries(
94        source: &mut T,
95        index: &Index,
96    ) -> Result<(IndexEntry, IndexEntry, u64), Error> {
97        let mut dir_index: Option<IndexEntry> = None;
98        let mut dir_name_index: Option<IndexEntry> = None;
99        let mut previous_entry: Option<IndexEntry> = None;
100        for i in 0..index.length.get() / INDEX_ENTRY_LEN {
101            let mut entry = IndexEntry::default();
102            let entry_offset = INDEX_LEN + INDEX_ENTRY_LEN * i;
103            source.read_at_exact(entry_offset, entry.as_mut_bytes()).await.map_err(Error::Read)?;
104
105            let expected_offset = if let Some(previous_entry) = previous_entry {
106                if previous_entry.chunk_type >= entry.chunk_type {
107                    return Err(Error::IndexEntriesOutOfOrder {
108                        prev: previous_entry.chunk_type,
109                        next: entry.chunk_type,
110                    });
111                }
112                previous_entry.offset.get() + previous_entry.length.get()
113            } else {
114                INDEX_LEN + index.length.get()
115            };
116            if entry.offset.get() != expected_offset {
117                return Err(Error::InvalidChunkOffset {
118                    chunk_type: entry.chunk_type,
119                    expected: expected_offset,
120                    actual: entry.offset.get(),
121                });
122            }
123            if entry.offset.get().checked_add(entry.length.get()).is_none() {
124                return Err(Error::InvalidChunkLength {
125                    chunk_type: entry.chunk_type,
126                    offset: entry.offset.get(),
127                    length: entry.length.get(),
128                });
129            }
130
131            match entry.chunk_type {
132                DIR_CHUNK_TYPE => {
133                    dir_index = Some(entry);
134                }
135                DIR_NAMES_CHUNK_TYPE => {
136                    dir_name_index = Some(entry);
137                }
138                // FAR spec does not forbid unknown chunk types
139                _ => {}
140            }
141            previous_entry = Some(entry);
142        }
143        let end_of_last_chunk = if let Some(previous_entry) = previous_entry {
144            previous_entry.offset.get() + previous_entry.length.get()
145        } else {
146            INDEX_LEN
147        };
148        Ok((
149            dir_index.ok_or(Error::MissingDirectoryChunkIndexEntry)?,
150            dir_name_index.ok_or(Error::MissingDirectoryNamesChunkIndexEntry)?,
151            end_of_last_chunk,
152        ))
153    }
154
155    /// Return a list of the items in the archive
156    pub fn list(&self) -> impl ExactSizeIterator<Item = crate::Entry<'_>> {
157        crate::list(&self.directory_entries, &self.path_data)
158    }
159
160    /// Read the entire contents of the entry with the specified path.
161    /// O(log(# directory entries))
162    pub async fn read_file(&mut self, path: &[u8]) -> Result<Vec<u8>, Error> {
163        let entry = crate::find_directory_entry(&self.directory_entries, &self.path_data, path)?;
164        let mut data = vec![
165            0;
166            usize::try_from(entry.data_length.get()).map_err(|_| {
167                Error::ContentChunkDoesNotFitInMemory {
168                    name: path.into(),
169                    chunk_size: entry.data_length.get(),
170                }
171            })?
172        ];
173        let () = self
174            .source
175            .read_at_exact(entry.data_offset.get(), &mut data)
176            .await
177            .map_err(Error::Read)?;
178        Ok(data)
179    }
180
181    /// Get the size in bytes of the entry with the specified path.
182    /// O(log(# directory entries))
183    pub fn get_size(&mut self, path: &[u8]) -> Result<u64, Error> {
184        Ok(crate::find_directory_entry(&self.directory_entries, &self.path_data, path)?
185            .data_length
186            .get())
187    }
188}
189
190#[cfg(test)]
191mod tests {
192    use super::*;
193    use crate::tests::example_archive;
194    use assert_matches::assert_matches;
195    use fuchsia_async as fasync;
196    use fuchsia_fs::file::Adapter;
197    use futures::io::Cursor;
198
199    #[fasync::run_singlethreaded(test)]
200    async fn list() {
201        let example = example_archive();
202        let reader = AsyncReader::new(Adapter::new(Cursor::new(&example))).await.unwrap();
203        itertools::assert_equal(
204            reader.list(),
205            [
206                crate::Entry { path: b"a", offset: 4096, length: 2 },
207                crate::Entry { path: b"b", offset: 8192, length: 2 },
208                crate::Entry { path: b"dir/c", offset: 12288, length: 6 },
209            ],
210        );
211    }
212
213    #[fasync::run_singlethreaded(test)]
214    async fn read_file() {
215        let example = example_archive();
216        let mut reader = AsyncReader::new(Adapter::new(Cursor::new(&example))).await.unwrap();
217        for one_name in ["a", "b", "dir/c"].iter().map(|s| s.as_bytes()) {
218            let content = reader.read_file(one_name).await.unwrap();
219            let content_str = std::str::from_utf8(&content).unwrap();
220            let expected = format!("{}\n", std::str::from_utf8(one_name).unwrap());
221            assert_eq!(content_str, &expected);
222        }
223    }
224
225    #[fasync::run_singlethreaded(test)]
226    async fn get_size() {
227        let example = example_archive();
228        let mut reader = AsyncReader::new(Adapter::new(Cursor::new(&example))).await.unwrap();
229        for one_name in ["a", "b", "dir/c"].iter().map(|s| s.as_bytes()) {
230            let returned_size = reader.get_size(one_name).unwrap();
231            let expected_size = one_name.len() + 1;
232            assert_eq!(returned_size, u64::try_from(expected_size).unwrap());
233        }
234    }
235
236    #[fasync::run_singlethreaded(test)]
237    async fn accessors_error_on_missing_path() {
238        let example = example_archive();
239        let mut reader = AsyncReader::new(Adapter::new(Cursor::new(&example))).await.unwrap();
240        assert_matches!(
241            reader.read_file(b"missing-path").await,
242            Err(Error::PathNotPresent(path)) if path == b"missing-path"
243        );
244        assert_matches!(
245            reader.get_size(b"missing-path"),
246            Err(Error::PathNotPresent(path)) if path == b"missing-path"
247        );
248    }
249}