• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::fs::File;
6 use std::io::Read;
7 use std::io::Seek;
8 use std::io::SeekFrom;
9 
10 use base::read_overlapped_blocking;
11 use cros_async::Executor;
12 
13 use crate::Error;
14 use crate::Result;
15 use crate::SingleFileDisk;
16 
17 impl SingleFileDisk {
new(disk: File, ex: &Executor) -> Result<Self>18     pub fn new(disk: File, ex: &Executor) -> Result<Self> {
19         ex.async_overlapped_from(disk)
20             .map_err(Error::CreateSingleFileDisk)
21             .map(|inner| SingleFileDisk { inner })
22     }
23 }
24 
25 /// On Windows, if the file is sparse, we set the option. On Linux this is not needed.
apply_raw_disk_file_options(raw_image: &File, is_sparse_file: bool) -> Result<()>26 pub fn apply_raw_disk_file_options(raw_image: &File, is_sparse_file: bool) -> Result<()> {
27     if is_sparse_file {
28         base::set_sparse_file(raw_image).map_err(Error::SetSparseFailure)?;
29     }
30     Ok(())
31 }
32 
read_from_disk( mut file: &File, offset: u64, buf: &mut [u8], overlapped_mode: bool, ) -> Result<()>33 pub fn read_from_disk(
34     mut file: &File,
35     offset: u64,
36     buf: &mut [u8],
37     overlapped_mode: bool,
38 ) -> Result<()> {
39     file.seek(SeekFrom::Start(offset))
40         .map_err(Error::SeekingFile)?;
41     if overlapped_mode {
42         read_overlapped_blocking(file, offset, buf)
43             .map(|_| ())
44             .map_err(Error::ReadingHeader)
45     } else {
46         file.read_exact(buf).map_err(Error::ReadingHeader)
47     }
48 }
49