Resolve cluster mappings via metadata then read allocated clusters with pread64.
(&self, address: u64, buf: &mut [u8])
| 82 | /// Resolve cluster mappings via metadata then read allocated clusters |
| 83 | /// with pread64. |
| 84 | fn read_clusters(&self, address: u64, buf: &mut [u8]) -> io::Result<()> { |
| 85 | let total_len = buf.len(); |
| 86 | let has_backing = self.backing_file.is_some(); |
| 87 | |
| 88 | let mappings = self |
| 89 | .metadata |
| 90 | .map_clusters_for_read(address, total_len, has_backing)?; |
| 91 | |
| 92 | let mut buf_offset = 0usize; |
| 93 | for mapping in mappings { |
| 94 | match mapping { |
| 95 | ClusterReadMapping::Zero { length } => { |
| 96 | buf[buf_offset..buf_offset + length as usize].fill(0); |
| 97 | buf_offset += length as usize; |
| 98 | } |
| 99 | ClusterReadMapping::Allocated { |
| 100 | offset: host_offset, |
| 101 | length, |
| 102 | } => { |
| 103 | pread_exact( |
| 104 | self.data_fd.as_raw_fd(), |
| 105 | &mut buf[buf_offset..buf_offset + length as usize], |
| 106 | host_offset, |
| 107 | )?; |
| 108 | buf_offset += length as usize; |
| 109 | } |
| 110 | ClusterReadMapping::Compressed { |
| 111 | host_offset, |
| 112 | compressed_size, |
| 113 | cluster_offset, |
| 114 | length, |
| 115 | } => { |
| 116 | let compressed = |
| 117 | pread_alloc(self.data_fd.as_raw_fd(), host_offset, compressed_size)?; |
| 118 | let decompressed = decompress_cluster( |
| 119 | &compressed, |
| 120 | self.cluster_size as usize, |
| 121 | &*self.decoder, |
| 122 | )?; |
| 123 | buf[buf_offset..buf_offset + length] |
| 124 | .copy_from_slice(&decompressed[cluster_offset..cluster_offset + length]); |
| 125 | buf_offset += length; |
| 126 | } |
| 127 | ClusterReadMapping::Backing { |
| 128 | offset: backing_offset, |
| 129 | length, |
| 130 | } => { |
| 131 | self.backing_file.as_ref().unwrap().read_at( |
| 132 | backing_offset, |
| 133 | &mut buf[buf_offset..buf_offset + length as usize], |
| 134 | )?; |
| 135 | buf_offset += length as usize; |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | Ok(()) |
| 140 | } |
| 141 | } |
no test coverage detected