##// END OF EJS Templates
rust-clippy: ignore clippy's recommendation for "useless" cast...
Raphaël Gomès -
r52012:d58e754f default
parent child Browse files
Show More
@@ -1,865 +1,869 b''
1 //! The "version 2" disk representation of the dirstate
1 //! The "version 2" disk representation of the dirstate
2 //!
2 //!
3 //! See `mercurial/helptext/internals/dirstate-v2.txt`
3 //! See `mercurial/helptext/internals/dirstate-v2.txt`
4
4
5 use crate::dirstate::{DirstateV2Data, TruncatedTimestamp};
5 use crate::dirstate::{DirstateV2Data, TruncatedTimestamp};
6 use crate::dirstate_tree::dirstate_map::DirstateVersion;
6 use crate::dirstate_tree::dirstate_map::DirstateVersion;
7 use crate::dirstate_tree::dirstate_map::{
7 use crate::dirstate_tree::dirstate_map::{
8 self, DirstateMap, DirstateMapWriteMode, NodeRef,
8 self, DirstateMap, DirstateMapWriteMode, NodeRef,
9 };
9 };
10 use crate::dirstate_tree::path_with_basename::WithBasename;
10 use crate::dirstate_tree::path_with_basename::WithBasename;
11 use crate::errors::HgError;
11 use crate::errors::HgError;
12 use crate::utils::hg_path::HgPath;
12 use crate::utils::hg_path::HgPath;
13 use crate::DirstateEntry;
13 use crate::DirstateEntry;
14 use crate::DirstateError;
14 use crate::DirstateError;
15 use crate::DirstateParents;
15 use crate::DirstateParents;
16 use bitflags::bitflags;
16 use bitflags::bitflags;
17 use bytes_cast::unaligned::{U16Be, U32Be};
17 use bytes_cast::unaligned::{U16Be, U32Be};
18 use bytes_cast::BytesCast;
18 use bytes_cast::BytesCast;
19 use format_bytes::format_bytes;
19 use format_bytes::format_bytes;
20 use rand::Rng;
20 use rand::Rng;
21 use std::borrow::Cow;
21 use std::borrow::Cow;
22 use std::fmt::Write;
22 use std::fmt::Write;
23
23
24 /// Added at the start of `.hg/dirstate` when the "v2" format is used.
24 /// Added at the start of `.hg/dirstate` when the "v2" format is used.
25 /// This a redundant sanity check more than an actual "magic number" since
25 /// This a redundant sanity check more than an actual "magic number" since
26 /// `.hg/requires` already governs which format should be used.
26 /// `.hg/requires` already governs which format should be used.
27 pub const V2_FORMAT_MARKER: &[u8; 12] = b"dirstate-v2\n";
27 pub const V2_FORMAT_MARKER: &[u8; 12] = b"dirstate-v2\n";
28
28
29 /// Keep space for 256-bit hashes
29 /// Keep space for 256-bit hashes
30 const STORED_NODE_ID_BYTES: usize = 32;
30 const STORED_NODE_ID_BYTES: usize = 32;
31
31
32 /// … even though only 160 bits are used for now, with SHA-1
32 /// … even though only 160 bits are used for now, with SHA-1
33 const USED_NODE_ID_BYTES: usize = 20;
33 const USED_NODE_ID_BYTES: usize = 20;
34
34
35 pub(super) const IGNORE_PATTERNS_HASH_LEN: usize = 20;
35 pub(super) const IGNORE_PATTERNS_HASH_LEN: usize = 20;
36 pub(super) type IgnorePatternsHash = [u8; IGNORE_PATTERNS_HASH_LEN];
36 pub(super) type IgnorePatternsHash = [u8; IGNORE_PATTERNS_HASH_LEN];
37
37
38 /// Must match constants of the same names in `mercurial/dirstateutils/v2.py`
38 /// Must match constants of the same names in `mercurial/dirstateutils/v2.py`
39 const TREE_METADATA_SIZE: usize = 44;
39 const TREE_METADATA_SIZE: usize = 44;
40 const NODE_SIZE: usize = 44;
40 const NODE_SIZE: usize = 44;
41
41
42 /// Make sure that size-affecting changes are made knowingly
42 /// Make sure that size-affecting changes are made knowingly
43 #[allow(unused)]
43 #[allow(unused)]
44 fn static_assert_size_of() {
44 fn static_assert_size_of() {
45 let _ = std::mem::transmute::<TreeMetadata, [u8; TREE_METADATA_SIZE]>;
45 let _ = std::mem::transmute::<TreeMetadata, [u8; TREE_METADATA_SIZE]>;
46 let _ = std::mem::transmute::<DocketHeader, [u8; TREE_METADATA_SIZE + 81]>;
46 let _ = std::mem::transmute::<DocketHeader, [u8; TREE_METADATA_SIZE + 81]>;
47 let _ = std::mem::transmute::<Node, [u8; NODE_SIZE]>;
47 let _ = std::mem::transmute::<Node, [u8; NODE_SIZE]>;
48 }
48 }
49
49
50 // Must match `HEADER` in `mercurial/dirstateutils/docket.py`
50 // Must match `HEADER` in `mercurial/dirstateutils/docket.py`
51 #[derive(BytesCast)]
51 #[derive(BytesCast)]
52 #[repr(C)]
52 #[repr(C)]
53 struct DocketHeader {
53 struct DocketHeader {
54 marker: [u8; V2_FORMAT_MARKER.len()],
54 marker: [u8; V2_FORMAT_MARKER.len()],
55 parent_1: [u8; STORED_NODE_ID_BYTES],
55 parent_1: [u8; STORED_NODE_ID_BYTES],
56 parent_2: [u8; STORED_NODE_ID_BYTES],
56 parent_2: [u8; STORED_NODE_ID_BYTES],
57
57
58 metadata: TreeMetadata,
58 metadata: TreeMetadata,
59
59
60 /// Counted in bytes
60 /// Counted in bytes
61 data_size: Size,
61 data_size: Size,
62
62
63 uuid_size: u8,
63 uuid_size: u8,
64 }
64 }
65
65
66 pub struct Docket<'on_disk> {
66 pub struct Docket<'on_disk> {
67 header: &'on_disk DocketHeader,
67 header: &'on_disk DocketHeader,
68 pub uuid: &'on_disk [u8],
68 pub uuid: &'on_disk [u8],
69 }
69 }
70
70
71 /// Fields are documented in the *Tree metadata in the docket file*
71 /// Fields are documented in the *Tree metadata in the docket file*
72 /// section of `mercurial/helptext/internals/dirstate-v2.txt`
72 /// section of `mercurial/helptext/internals/dirstate-v2.txt`
73 #[derive(BytesCast)]
73 #[derive(BytesCast)]
74 #[repr(C)]
74 #[repr(C)]
75 pub struct TreeMetadata {
75 pub struct TreeMetadata {
76 root_nodes: ChildNodes,
76 root_nodes: ChildNodes,
77 nodes_with_entry_count: Size,
77 nodes_with_entry_count: Size,
78 nodes_with_copy_source_count: Size,
78 nodes_with_copy_source_count: Size,
79 unreachable_bytes: Size,
79 unreachable_bytes: Size,
80 unused: [u8; 4],
80 unused: [u8; 4],
81
81
82 /// See *Optional hash of ignore patterns* section of
82 /// See *Optional hash of ignore patterns* section of
83 /// `mercurial/helptext/internals/dirstate-v2.txt`
83 /// `mercurial/helptext/internals/dirstate-v2.txt`
84 ignore_patterns_hash: IgnorePatternsHash,
84 ignore_patterns_hash: IgnorePatternsHash,
85 }
85 }
86
86
87 /// Fields are documented in the *The data file format*
87 /// Fields are documented in the *The data file format*
88 /// section of `mercurial/helptext/internals/dirstate-v2.txt`
88 /// section of `mercurial/helptext/internals/dirstate-v2.txt`
89 #[derive(BytesCast, Debug)]
89 #[derive(BytesCast, Debug)]
90 #[repr(C)]
90 #[repr(C)]
91 pub(super) struct Node {
91 pub(super) struct Node {
92 full_path: PathSlice,
92 full_path: PathSlice,
93
93
94 /// In bytes from `self.full_path.start`
94 /// In bytes from `self.full_path.start`
95 base_name_start: PathSize,
95 base_name_start: PathSize,
96
96
97 copy_source: OptPathSlice,
97 copy_source: OptPathSlice,
98 children: ChildNodes,
98 children: ChildNodes,
99 pub(super) descendants_with_entry_count: Size,
99 pub(super) descendants_with_entry_count: Size,
100 pub(super) tracked_descendants_count: Size,
100 pub(super) tracked_descendants_count: Size,
101 flags: U16Be,
101 flags: U16Be,
102 size: U32Be,
102 size: U32Be,
103 mtime: PackedTruncatedTimestamp,
103 mtime: PackedTruncatedTimestamp,
104 }
104 }
105
105
106 bitflags! {
106 bitflags! {
107 #[repr(C)]
107 #[repr(C)]
108 struct Flags: u16 {
108 struct Flags: u16 {
109 const WDIR_TRACKED = 1 << 0;
109 const WDIR_TRACKED = 1 << 0;
110 const P1_TRACKED = 1 << 1;
110 const P1_TRACKED = 1 << 1;
111 const P2_INFO = 1 << 2;
111 const P2_INFO = 1 << 2;
112 const MODE_EXEC_PERM = 1 << 3;
112 const MODE_EXEC_PERM = 1 << 3;
113 const MODE_IS_SYMLINK = 1 << 4;
113 const MODE_IS_SYMLINK = 1 << 4;
114 const HAS_FALLBACK_EXEC = 1 << 5;
114 const HAS_FALLBACK_EXEC = 1 << 5;
115 const FALLBACK_EXEC = 1 << 6;
115 const FALLBACK_EXEC = 1 << 6;
116 const HAS_FALLBACK_SYMLINK = 1 << 7;
116 const HAS_FALLBACK_SYMLINK = 1 << 7;
117 const FALLBACK_SYMLINK = 1 << 8;
117 const FALLBACK_SYMLINK = 1 << 8;
118 const EXPECTED_STATE_IS_MODIFIED = 1 << 9;
118 const EXPECTED_STATE_IS_MODIFIED = 1 << 9;
119 const HAS_MODE_AND_SIZE = 1 <<10;
119 const HAS_MODE_AND_SIZE = 1 <<10;
120 const HAS_MTIME = 1 <<11;
120 const HAS_MTIME = 1 <<11;
121 const MTIME_SECOND_AMBIGUOUS = 1 << 12;
121 const MTIME_SECOND_AMBIGUOUS = 1 << 12;
122 const DIRECTORY = 1 <<13;
122 const DIRECTORY = 1 <<13;
123 const ALL_UNKNOWN_RECORDED = 1 <<14;
123 const ALL_UNKNOWN_RECORDED = 1 <<14;
124 const ALL_IGNORED_RECORDED = 1 <<15;
124 const ALL_IGNORED_RECORDED = 1 <<15;
125 }
125 }
126 }
126 }
127
127
128 /// Duration since the Unix epoch
128 /// Duration since the Unix epoch
129 #[derive(BytesCast, Copy, Clone, Debug)]
129 #[derive(BytesCast, Copy, Clone, Debug)]
130 #[repr(C)]
130 #[repr(C)]
131 struct PackedTruncatedTimestamp {
131 struct PackedTruncatedTimestamp {
132 truncated_seconds: U32Be,
132 truncated_seconds: U32Be,
133 nanoseconds: U32Be,
133 nanoseconds: U32Be,
134 }
134 }
135
135
136 /// Counted in bytes from the start of the file
136 /// Counted in bytes from the start of the file
137 ///
137 ///
138 /// NOTE: not supporting `.hg/dirstate` files larger than 4 GiB.
138 /// NOTE: not supporting `.hg/dirstate` files larger than 4 GiB.
139 type Offset = U32Be;
139 type Offset = U32Be;
140
140
141 /// Counted in number of items
141 /// Counted in number of items
142 ///
142 ///
143 /// NOTE: we choose not to support counting more than 4 billion nodes anywhere.
143 /// NOTE: we choose not to support counting more than 4 billion nodes anywhere.
144 type Size = U32Be;
144 type Size = U32Be;
145
145
146 /// Counted in bytes
146 /// Counted in bytes
147 ///
147 ///
148 /// NOTE: we choose not to support file names/paths longer than 64 KiB.
148 /// NOTE: we choose not to support file names/paths longer than 64 KiB.
149 type PathSize = U16Be;
149 type PathSize = U16Be;
150
150
151 /// A contiguous sequence of `len` times `Node`, representing the child nodes
151 /// A contiguous sequence of `len` times `Node`, representing the child nodes
152 /// of either some other node or of the repository root.
152 /// of either some other node or of the repository root.
153 ///
153 ///
154 /// Always sorted by ascending `full_path`, to allow binary search.
154 /// Always sorted by ascending `full_path`, to allow binary search.
155 /// Since nodes with the same parent nodes also have the same parent path,
155 /// Since nodes with the same parent nodes also have the same parent path,
156 /// only the `base_name`s need to be compared during binary search.
156 /// only the `base_name`s need to be compared during binary search.
157 #[derive(BytesCast, Copy, Clone, Debug)]
157 #[derive(BytesCast, Copy, Clone, Debug)]
158 #[repr(C)]
158 #[repr(C)]
159 struct ChildNodes {
159 struct ChildNodes {
160 start: Offset,
160 start: Offset,
161 len: Size,
161 len: Size,
162 }
162 }
163
163
164 /// A `HgPath` of `len` bytes
164 /// A `HgPath` of `len` bytes
165 #[derive(BytesCast, Copy, Clone, Debug)]
165 #[derive(BytesCast, Copy, Clone, Debug)]
166 #[repr(C)]
166 #[repr(C)]
167 struct PathSlice {
167 struct PathSlice {
168 start: Offset,
168 start: Offset,
169 len: PathSize,
169 len: PathSize,
170 }
170 }
171
171
172 /// Either nothing if `start == 0`, or a `HgPath` of `len` bytes
172 /// Either nothing if `start == 0`, or a `HgPath` of `len` bytes
173 type OptPathSlice = PathSlice;
173 type OptPathSlice = PathSlice;
174
174
175 /// Unexpected file format found in `.hg/dirstate` with the "v2" format.
175 /// Unexpected file format found in `.hg/dirstate` with the "v2" format.
176 ///
176 ///
177 /// This should only happen if Mercurial is buggy or a repository is corrupted.
177 /// This should only happen if Mercurial is buggy or a repository is corrupted.
178 #[derive(Debug)]
178 #[derive(Debug)]
179 pub struct DirstateV2ParseError {
179 pub struct DirstateV2ParseError {
180 message: String,
180 message: String,
181 }
181 }
182
182
183 impl DirstateV2ParseError {
183 impl DirstateV2ParseError {
184 pub fn new<S: Into<String>>(message: S) -> Self {
184 pub fn new<S: Into<String>>(message: S) -> Self {
185 Self {
185 Self {
186 message: message.into(),
186 message: message.into(),
187 }
187 }
188 }
188 }
189 }
189 }
190
190
191 impl From<DirstateV2ParseError> for HgError {
191 impl From<DirstateV2ParseError> for HgError {
192 fn from(e: DirstateV2ParseError) -> Self {
192 fn from(e: DirstateV2ParseError) -> Self {
193 HgError::corrupted(format!("dirstate-v2 parse error: {}", e.message))
193 HgError::corrupted(format!("dirstate-v2 parse error: {}", e.message))
194 }
194 }
195 }
195 }
196
196
197 impl From<DirstateV2ParseError> for crate::DirstateError {
197 impl From<DirstateV2ParseError> for crate::DirstateError {
198 fn from(error: DirstateV2ParseError) -> Self {
198 fn from(error: DirstateV2ParseError) -> Self {
199 HgError::from(error).into()
199 HgError::from(error).into()
200 }
200 }
201 }
201 }
202
202
203 impl TreeMetadata {
203 impl TreeMetadata {
204 pub fn as_bytes(&self) -> &[u8] {
204 pub fn as_bytes(&self) -> &[u8] {
205 BytesCast::as_bytes(self)
205 BytesCast::as_bytes(self)
206 }
206 }
207 }
207 }
208
208
209 impl<'on_disk> Docket<'on_disk> {
209 impl<'on_disk> Docket<'on_disk> {
210 /// Generate the identifier for a new data file
210 /// Generate the identifier for a new data file
211 ///
211 ///
212 /// TODO: support the `HGTEST_UUIDFILE` environment variable.
212 /// TODO: support the `HGTEST_UUIDFILE` environment variable.
213 /// See `mercurial/revlogutils/docket.py`
213 /// See `mercurial/revlogutils/docket.py`
214 pub fn new_uid() -> String {
214 pub fn new_uid() -> String {
215 const ID_LENGTH: usize = 8;
215 const ID_LENGTH: usize = 8;
216 let mut id = String::with_capacity(ID_LENGTH);
216 let mut id = String::with_capacity(ID_LENGTH);
217 let mut rng = rand::thread_rng();
217 let mut rng = rand::thread_rng();
218 for _ in 0..ID_LENGTH {
218 for _ in 0..ID_LENGTH {
219 // One random hexadecimal digit.
219 // One random hexadecimal digit.
220 // `unwrap` never panics because `impl Write for String`
220 // `unwrap` never panics because `impl Write for String`
221 // never returns an error.
221 // never returns an error.
222 write!(&mut id, "{:x}", rng.gen_range(0..16)).unwrap();
222 write!(&mut id, "{:x}", rng.gen_range(0..16)).unwrap();
223 }
223 }
224 id
224 id
225 }
225 }
226
226
227 pub fn serialize(
227 pub fn serialize(
228 parents: DirstateParents,
228 parents: DirstateParents,
229 tree_metadata: TreeMetadata,
229 tree_metadata: TreeMetadata,
230 data_size: u64,
230 data_size: u64,
231 uuid: &[u8],
231 uuid: &[u8],
232 ) -> Result<Vec<u8>, std::num::TryFromIntError> {
232 ) -> Result<Vec<u8>, std::num::TryFromIntError> {
233 let header = DocketHeader {
233 let header = DocketHeader {
234 marker: *V2_FORMAT_MARKER,
234 marker: *V2_FORMAT_MARKER,
235 parent_1: parents.p1.pad_to_256_bits(),
235 parent_1: parents.p1.pad_to_256_bits(),
236 parent_2: parents.p2.pad_to_256_bits(),
236 parent_2: parents.p2.pad_to_256_bits(),
237 metadata: tree_metadata,
237 metadata: tree_metadata,
238 data_size: u32::try_from(data_size)?.into(),
238 data_size: u32::try_from(data_size)?.into(),
239 uuid_size: uuid.len().try_into()?,
239 uuid_size: uuid.len().try_into()?,
240 };
240 };
241 let header = header.as_bytes();
241 let header = header.as_bytes();
242 let mut docket = Vec::with_capacity(header.len() + uuid.len());
242 let mut docket = Vec::with_capacity(header.len() + uuid.len());
243 docket.extend_from_slice(header);
243 docket.extend_from_slice(header);
244 docket.extend_from_slice(uuid);
244 docket.extend_from_slice(uuid);
245 Ok(docket)
245 Ok(docket)
246 }
246 }
247
247
248 pub fn parents(&self) -> DirstateParents {
248 pub fn parents(&self) -> DirstateParents {
249 use crate::Node;
249 use crate::Node;
250 let p1 = Node::try_from(&self.header.parent_1[..USED_NODE_ID_BYTES])
250 let p1 = Node::try_from(&self.header.parent_1[..USED_NODE_ID_BYTES])
251 .unwrap();
251 .unwrap();
252 let p2 = Node::try_from(&self.header.parent_2[..USED_NODE_ID_BYTES])
252 let p2 = Node::try_from(&self.header.parent_2[..USED_NODE_ID_BYTES])
253 .unwrap();
253 .unwrap();
254 DirstateParents { p1, p2 }
254 DirstateParents { p1, p2 }
255 }
255 }
256
256
257 pub fn tree_metadata(&self) -> &[u8] {
257 pub fn tree_metadata(&self) -> &[u8] {
258 self.header.metadata.as_bytes()
258 self.header.metadata.as_bytes()
259 }
259 }
260
260
261 pub fn data_size(&self) -> usize {
261 pub fn data_size(&self) -> usize {
262 // This `unwrap` could only panic on a 16-bit CPU
262 // This `unwrap` could only panic on a 16-bit CPU
263 self.header.data_size.get().try_into().unwrap()
263 self.header.data_size.get().try_into().unwrap()
264 }
264 }
265
265
266 pub fn data_filename(&self) -> String {
266 pub fn data_filename(&self) -> String {
267 String::from_utf8(format_bytes!(b"dirstate.{}", self.uuid)).unwrap()
267 String::from_utf8(format_bytes!(b"dirstate.{}", self.uuid)).unwrap()
268 }
268 }
269 }
269 }
270
270
271 pub fn read_docket(
271 pub fn read_docket(
272 on_disk: &[u8],
272 on_disk: &[u8],
273 ) -> Result<Docket<'_>, DirstateV2ParseError> {
273 ) -> Result<Docket<'_>, DirstateV2ParseError> {
274 let (header, uuid) = DocketHeader::from_bytes(on_disk).map_err(|e| {
274 let (header, uuid) = DocketHeader::from_bytes(on_disk).map_err(|e| {
275 DirstateV2ParseError::new(format!("when reading docket, {}", e))
275 DirstateV2ParseError::new(format!("when reading docket, {}", e))
276 })?;
276 })?;
277 let uuid_size = header.uuid_size as usize;
277 let uuid_size = header.uuid_size as usize;
278 if header.marker == *V2_FORMAT_MARKER && uuid.len() == uuid_size {
278 if header.marker == *V2_FORMAT_MARKER && uuid.len() == uuid_size {
279 Ok(Docket { header, uuid })
279 Ok(Docket { header, uuid })
280 } else {
280 } else {
281 Err(DirstateV2ParseError::new(
281 Err(DirstateV2ParseError::new(
282 "invalid format marker or uuid size",
282 "invalid format marker or uuid size",
283 ))
283 ))
284 }
284 }
285 }
285 }
286
286
287 pub(super) fn read<'on_disk>(
287 pub(super) fn read<'on_disk>(
288 on_disk: &'on_disk [u8],
288 on_disk: &'on_disk [u8],
289 metadata: &[u8],
289 metadata: &[u8],
290 uuid: Vec<u8>,
290 uuid: Vec<u8>,
291 identity: Option<u64>,
291 identity: Option<u64>,
292 ) -> Result<DirstateMap<'on_disk>, DirstateV2ParseError> {
292 ) -> Result<DirstateMap<'on_disk>, DirstateV2ParseError> {
293 if on_disk.is_empty() {
293 if on_disk.is_empty() {
294 let mut map = DirstateMap::empty(on_disk);
294 let mut map = DirstateMap::empty(on_disk);
295 map.dirstate_version = DirstateVersion::V2;
295 map.dirstate_version = DirstateVersion::V2;
296 return Ok(map);
296 return Ok(map);
297 }
297 }
298 let (meta, _) = TreeMetadata::from_bytes(metadata).map_err(|e| {
298 let (meta, _) = TreeMetadata::from_bytes(metadata).map_err(|e| {
299 DirstateV2ParseError::new(format!("when parsing tree metadata, {}", e))
299 DirstateV2ParseError::new(format!("when parsing tree metadata, {}", e))
300 })?;
300 })?;
301 let dirstate_map = DirstateMap {
301 let dirstate_map = DirstateMap {
302 on_disk,
302 on_disk,
303 root: dirstate_map::ChildNodes::OnDisk(
303 root: dirstate_map::ChildNodes::OnDisk(
304 read_nodes(on_disk, meta.root_nodes).map_err(|mut e| {
304 read_nodes(on_disk, meta.root_nodes).map_err(|mut e| {
305 e.message = format!("{}, when reading root notes", e.message);
305 e.message = format!("{}, when reading root notes", e.message);
306 e
306 e
307 })?,
307 })?,
308 ),
308 ),
309 nodes_with_entry_count: meta.nodes_with_entry_count.get(),
309 nodes_with_entry_count: meta.nodes_with_entry_count.get(),
310 nodes_with_copy_source_count: meta.nodes_with_copy_source_count.get(),
310 nodes_with_copy_source_count: meta.nodes_with_copy_source_count.get(),
311 ignore_patterns_hash: meta.ignore_patterns_hash,
311 ignore_patterns_hash: meta.ignore_patterns_hash,
312 unreachable_bytes: meta.unreachable_bytes.get(),
312 unreachable_bytes: meta.unreachable_bytes.get(),
313 old_data_size: on_disk.len(),
313 old_data_size: on_disk.len(),
314 old_uuid: Some(uuid),
314 old_uuid: Some(uuid),
315 identity,
315 identity,
316 dirstate_version: DirstateVersion::V2,
316 dirstate_version: DirstateVersion::V2,
317 write_mode: DirstateMapWriteMode::Auto,
317 write_mode: DirstateMapWriteMode::Auto,
318 };
318 };
319 Ok(dirstate_map)
319 Ok(dirstate_map)
320 }
320 }
321
321
322 impl Node {
322 impl Node {
323 pub(super) fn full_path<'on_disk>(
323 pub(super) fn full_path<'on_disk>(
324 &self,
324 &self,
325 on_disk: &'on_disk [u8],
325 on_disk: &'on_disk [u8],
326 ) -> Result<&'on_disk HgPath, DirstateV2ParseError> {
326 ) -> Result<&'on_disk HgPath, DirstateV2ParseError> {
327 read_hg_path(on_disk, self.full_path)
327 read_hg_path(on_disk, self.full_path)
328 }
328 }
329
329
330 pub(super) fn base_name_start(
330 pub(super) fn base_name_start(
331 &self,
331 &self,
332 ) -> Result<usize, DirstateV2ParseError> {
332 ) -> Result<usize, DirstateV2ParseError> {
333 let start = self.base_name_start.get();
333 let start = self.base_name_start.get();
334 if start < self.full_path.len.get() {
334 if start < self.full_path.len.get() {
335 let start = usize::try_from(start)
335 let start = usize::try_from(start)
336 // u32 -> usize, could only panic on a 16-bit CPU
336 // u32 -> usize, could only panic on a 16-bit CPU
337 .expect("dirstate-v2 base_name_start out of bounds");
337 .expect("dirstate-v2 base_name_start out of bounds");
338 Ok(start)
338 Ok(start)
339 } else {
339 } else {
340 Err(DirstateV2ParseError::new("not enough bytes for base name"))
340 Err(DirstateV2ParseError::new("not enough bytes for base name"))
341 }
341 }
342 }
342 }
343
343
344 pub(super) fn base_name<'on_disk>(
344 pub(super) fn base_name<'on_disk>(
345 &self,
345 &self,
346 on_disk: &'on_disk [u8],
346 on_disk: &'on_disk [u8],
347 ) -> Result<&'on_disk HgPath, DirstateV2ParseError> {
347 ) -> Result<&'on_disk HgPath, DirstateV2ParseError> {
348 let full_path = self.full_path(on_disk)?;
348 let full_path = self.full_path(on_disk)?;
349 let base_name_start = self.base_name_start()?;
349 let base_name_start = self.base_name_start()?;
350 Ok(HgPath::new(&full_path.as_bytes()[base_name_start..]))
350 Ok(HgPath::new(&full_path.as_bytes()[base_name_start..]))
351 }
351 }
352
352
353 pub(super) fn path<'on_disk>(
353 pub(super) fn path<'on_disk>(
354 &self,
354 &self,
355 on_disk: &'on_disk [u8],
355 on_disk: &'on_disk [u8],
356 ) -> Result<dirstate_map::NodeKey<'on_disk>, DirstateV2ParseError> {
356 ) -> Result<dirstate_map::NodeKey<'on_disk>, DirstateV2ParseError> {
357 Ok(WithBasename::from_raw_parts(
357 Ok(WithBasename::from_raw_parts(
358 Cow::Borrowed(self.full_path(on_disk)?),
358 Cow::Borrowed(self.full_path(on_disk)?),
359 self.base_name_start()?,
359 self.base_name_start()?,
360 ))
360 ))
361 }
361 }
362
362
363 pub(super) fn has_copy_source(&self) -> bool {
363 pub(super) fn has_copy_source(&self) -> bool {
364 self.copy_source.start.get() != 0
364 self.copy_source.start.get() != 0
365 }
365 }
366
366
367 pub(super) fn copy_source<'on_disk>(
367 pub(super) fn copy_source<'on_disk>(
368 &self,
368 &self,
369 on_disk: &'on_disk [u8],
369 on_disk: &'on_disk [u8],
370 ) -> Result<Option<&'on_disk HgPath>, DirstateV2ParseError> {
370 ) -> Result<Option<&'on_disk HgPath>, DirstateV2ParseError> {
371 Ok(if self.has_copy_source() {
371 Ok(if self.has_copy_source() {
372 Some(read_hg_path(on_disk, self.copy_source)?)
372 Some(read_hg_path(on_disk, self.copy_source)?)
373 } else {
373 } else {
374 None
374 None
375 })
375 })
376 }
376 }
377
377
378 fn flags(&self) -> Flags {
378 fn flags(&self) -> Flags {
379 Flags::from_bits_truncate(self.flags.get())
379 Flags::from_bits_truncate(self.flags.get())
380 }
380 }
381
381
382 fn has_entry(&self) -> bool {
382 fn has_entry(&self) -> bool {
383 self.flags().intersects(
383 self.flags().intersects(
384 Flags::WDIR_TRACKED | Flags::P1_TRACKED | Flags::P2_INFO,
384 Flags::WDIR_TRACKED | Flags::P1_TRACKED | Flags::P2_INFO,
385 )
385 )
386 }
386 }
387
387
388 pub(super) fn node_data(
388 pub(super) fn node_data(
389 &self,
389 &self,
390 ) -> Result<dirstate_map::NodeData, DirstateV2ParseError> {
390 ) -> Result<dirstate_map::NodeData, DirstateV2ParseError> {
391 if self.has_entry() {
391 if self.has_entry() {
392 Ok(dirstate_map::NodeData::Entry(self.assume_entry()?))
392 Ok(dirstate_map::NodeData::Entry(self.assume_entry()?))
393 } else if let Some(mtime) = self.cached_directory_mtime()? {
393 } else if let Some(mtime) = self.cached_directory_mtime()? {
394 Ok(dirstate_map::NodeData::CachedDirectory { mtime })
394 Ok(dirstate_map::NodeData::CachedDirectory { mtime })
395 } else {
395 } else {
396 Ok(dirstate_map::NodeData::None)
396 Ok(dirstate_map::NodeData::None)
397 }
397 }
398 }
398 }
399
399
400 pub(super) fn cached_directory_mtime(
400 pub(super) fn cached_directory_mtime(
401 &self,
401 &self,
402 ) -> Result<Option<TruncatedTimestamp>, DirstateV2ParseError> {
402 ) -> Result<Option<TruncatedTimestamp>, DirstateV2ParseError> {
403 // For now we do not have code to handle the absence of
403 // For now we do not have code to handle the absence of
404 // ALL_UNKNOWN_RECORDED, so we ignore the mtime if the flag is
404 // ALL_UNKNOWN_RECORDED, so we ignore the mtime if the flag is
405 // unset.
405 // unset.
406 if self.flags().contains(Flags::DIRECTORY)
406 if self.flags().contains(Flags::DIRECTORY)
407 && self.flags().contains(Flags::HAS_MTIME)
407 && self.flags().contains(Flags::HAS_MTIME)
408 && self.flags().contains(Flags::ALL_UNKNOWN_RECORDED)
408 && self.flags().contains(Flags::ALL_UNKNOWN_RECORDED)
409 {
409 {
410 Ok(Some(self.mtime()?))
410 Ok(Some(self.mtime()?))
411 } else {
411 } else {
412 Ok(None)
412 Ok(None)
413 }
413 }
414 }
414 }
415
415
416 fn synthesize_unix_mode(&self) -> u32 {
416 fn synthesize_unix_mode(&self) -> u32 {
417 // Some platforms' libc don't have the same type (MacOS uses i32 here)
418 #[allow(clippy::unnecessary_cast)]
417 let file_type = if self.flags().contains(Flags::MODE_IS_SYMLINK) {
419 let file_type = if self.flags().contains(Flags::MODE_IS_SYMLINK) {
418 libc::S_IFLNK as u32
420 libc::S_IFLNK as u32
419 } else {
421 } else {
420 libc::S_IFREG as u32
422 libc::S_IFREG as u32
421 };
423 };
422 let permissions = if self.flags().contains(Flags::MODE_EXEC_PERM) {
424 let permissions = if self.flags().contains(Flags::MODE_EXEC_PERM) {
423 0o755
425 0o755
424 } else {
426 } else {
425 0o644
427 0o644
426 };
428 };
427 file_type | permissions
429 file_type | permissions
428 }
430 }
429
431
430 fn mtime(&self) -> Result<TruncatedTimestamp, DirstateV2ParseError> {
432 fn mtime(&self) -> Result<TruncatedTimestamp, DirstateV2ParseError> {
431 let mut m: TruncatedTimestamp = self.mtime.try_into()?;
433 let mut m: TruncatedTimestamp = self.mtime.try_into()?;
432 if self.flags().contains(Flags::MTIME_SECOND_AMBIGUOUS) {
434 if self.flags().contains(Flags::MTIME_SECOND_AMBIGUOUS) {
433 m.second_ambiguous = true;
435 m.second_ambiguous = true;
434 }
436 }
435 Ok(m)
437 Ok(m)
436 }
438 }
437
439
438 fn assume_entry(&self) -> Result<DirstateEntry, DirstateV2ParseError> {
440 fn assume_entry(&self) -> Result<DirstateEntry, DirstateV2ParseError> {
439 // TODO: convert through raw bits instead?
441 // TODO: convert through raw bits instead?
440 let wc_tracked = self.flags().contains(Flags::WDIR_TRACKED);
442 let wc_tracked = self.flags().contains(Flags::WDIR_TRACKED);
441 let p1_tracked = self.flags().contains(Flags::P1_TRACKED);
443 let p1_tracked = self.flags().contains(Flags::P1_TRACKED);
442 let p2_info = self.flags().contains(Flags::P2_INFO);
444 let p2_info = self.flags().contains(Flags::P2_INFO);
443 let mode_size = if self.flags().contains(Flags::HAS_MODE_AND_SIZE)
445 let mode_size = if self.flags().contains(Flags::HAS_MODE_AND_SIZE)
444 && !self.flags().contains(Flags::EXPECTED_STATE_IS_MODIFIED)
446 && !self.flags().contains(Flags::EXPECTED_STATE_IS_MODIFIED)
445 {
447 {
446 Some((self.synthesize_unix_mode(), self.size.into()))
448 Some((self.synthesize_unix_mode(), self.size.into()))
447 } else {
449 } else {
448 None
450 None
449 };
451 };
450 let mtime = if self.flags().contains(Flags::HAS_MTIME)
452 let mtime = if self.flags().contains(Flags::HAS_MTIME)
451 && !self.flags().contains(Flags::DIRECTORY)
453 && !self.flags().contains(Flags::DIRECTORY)
452 && !self.flags().contains(Flags::EXPECTED_STATE_IS_MODIFIED)
454 && !self.flags().contains(Flags::EXPECTED_STATE_IS_MODIFIED)
453 {
455 {
454 Some(self.mtime()?)
456 Some(self.mtime()?)
455 } else {
457 } else {
456 None
458 None
457 };
459 };
458 let fallback_exec = if self.flags().contains(Flags::HAS_FALLBACK_EXEC)
460 let fallback_exec = if self.flags().contains(Flags::HAS_FALLBACK_EXEC)
459 {
461 {
460 Some(self.flags().contains(Flags::FALLBACK_EXEC))
462 Some(self.flags().contains(Flags::FALLBACK_EXEC))
461 } else {
463 } else {
462 None
464 None
463 };
465 };
464 let fallback_symlink =
466 let fallback_symlink =
465 if self.flags().contains(Flags::HAS_FALLBACK_SYMLINK) {
467 if self.flags().contains(Flags::HAS_FALLBACK_SYMLINK) {
466 Some(self.flags().contains(Flags::FALLBACK_SYMLINK))
468 Some(self.flags().contains(Flags::FALLBACK_SYMLINK))
467 } else {
469 } else {
468 None
470 None
469 };
471 };
470 Ok(DirstateEntry::from_v2_data(DirstateV2Data {
472 Ok(DirstateEntry::from_v2_data(DirstateV2Data {
471 wc_tracked,
473 wc_tracked,
472 p1_tracked,
474 p1_tracked,
473 p2_info,
475 p2_info,
474 mode_size,
476 mode_size,
475 mtime,
477 mtime,
476 fallback_exec,
478 fallback_exec,
477 fallback_symlink,
479 fallback_symlink,
478 }))
480 }))
479 }
481 }
480
482
481 pub(super) fn entry(
483 pub(super) fn entry(
482 &self,
484 &self,
483 ) -> Result<Option<DirstateEntry>, DirstateV2ParseError> {
485 ) -> Result<Option<DirstateEntry>, DirstateV2ParseError> {
484 if self.has_entry() {
486 if self.has_entry() {
485 Ok(Some(self.assume_entry()?))
487 Ok(Some(self.assume_entry()?))
486 } else {
488 } else {
487 Ok(None)
489 Ok(None)
488 }
490 }
489 }
491 }
490
492
491 pub(super) fn children<'on_disk>(
493 pub(super) fn children<'on_disk>(
492 &self,
494 &self,
493 on_disk: &'on_disk [u8],
495 on_disk: &'on_disk [u8],
494 ) -> Result<&'on_disk [Node], DirstateV2ParseError> {
496 ) -> Result<&'on_disk [Node], DirstateV2ParseError> {
495 read_nodes(on_disk, self.children)
497 read_nodes(on_disk, self.children)
496 }
498 }
497
499
498 pub(super) fn to_in_memory_node<'on_disk>(
500 pub(super) fn to_in_memory_node<'on_disk>(
499 &self,
501 &self,
500 on_disk: &'on_disk [u8],
502 on_disk: &'on_disk [u8],
501 ) -> Result<dirstate_map::Node<'on_disk>, DirstateV2ParseError> {
503 ) -> Result<dirstate_map::Node<'on_disk>, DirstateV2ParseError> {
502 Ok(dirstate_map::Node {
504 Ok(dirstate_map::Node {
503 children: dirstate_map::ChildNodes::OnDisk(
505 children: dirstate_map::ChildNodes::OnDisk(
504 self.children(on_disk)?,
506 self.children(on_disk)?,
505 ),
507 ),
506 copy_source: self.copy_source(on_disk)?.map(Cow::Borrowed),
508 copy_source: self.copy_source(on_disk)?.map(Cow::Borrowed),
507 data: self.node_data()?,
509 data: self.node_data()?,
508 descendants_with_entry_count: self
510 descendants_with_entry_count: self
509 .descendants_with_entry_count
511 .descendants_with_entry_count
510 .get(),
512 .get(),
511 tracked_descendants_count: self.tracked_descendants_count.get(),
513 tracked_descendants_count: self.tracked_descendants_count.get(),
512 })
514 })
513 }
515 }
514
516
515 fn from_dirstate_entry(
517 fn from_dirstate_entry(
516 entry: &DirstateEntry,
518 entry: &DirstateEntry,
517 ) -> (Flags, U32Be, PackedTruncatedTimestamp) {
519 ) -> (Flags, U32Be, PackedTruncatedTimestamp) {
518 let DirstateV2Data {
520 let DirstateV2Data {
519 wc_tracked,
521 wc_tracked,
520 p1_tracked,
522 p1_tracked,
521 p2_info,
523 p2_info,
522 mode_size: mode_size_opt,
524 mode_size: mode_size_opt,
523 mtime: mtime_opt,
525 mtime: mtime_opt,
524 fallback_exec,
526 fallback_exec,
525 fallback_symlink,
527 fallback_symlink,
526 } = entry.v2_data();
528 } = entry.v2_data();
527 // TODO: convert through raw flag bits instead?
529 // TODO: convert through raw flag bits instead?
528 let mut flags = Flags::empty();
530 let mut flags = Flags::empty();
529 flags.set(Flags::WDIR_TRACKED, wc_tracked);
531 flags.set(Flags::WDIR_TRACKED, wc_tracked);
530 flags.set(Flags::P1_TRACKED, p1_tracked);
532 flags.set(Flags::P1_TRACKED, p1_tracked);
531 flags.set(Flags::P2_INFO, p2_info);
533 flags.set(Flags::P2_INFO, p2_info);
534 // Some platforms' libc don't have the same type (MacOS uses i32 here)
535 #[allow(clippy::unnecessary_cast)]
532 let size = if let Some((m, s)) = mode_size_opt {
536 let size = if let Some((m, s)) = mode_size_opt {
533 let exec_perm = m & (libc::S_IXUSR as u32) != 0;
537 let exec_perm = m & (libc::S_IXUSR as u32) != 0;
534 let is_symlink = m & (libc::S_IFMT as u32) == libc::S_IFLNK as u32;
538 let is_symlink = m & (libc::S_IFMT as u32) == libc::S_IFLNK as u32;
535 flags.set(Flags::MODE_EXEC_PERM, exec_perm);
539 flags.set(Flags::MODE_EXEC_PERM, exec_perm);
536 flags.set(Flags::MODE_IS_SYMLINK, is_symlink);
540 flags.set(Flags::MODE_IS_SYMLINK, is_symlink);
537 flags.insert(Flags::HAS_MODE_AND_SIZE);
541 flags.insert(Flags::HAS_MODE_AND_SIZE);
538 s.into()
542 s.into()
539 } else {
543 } else {
540 0.into()
544 0.into()
541 };
545 };
542 let mtime = if let Some(m) = mtime_opt {
546 let mtime = if let Some(m) = mtime_opt {
543 flags.insert(Flags::HAS_MTIME);
547 flags.insert(Flags::HAS_MTIME);
544 if m.second_ambiguous {
548 if m.second_ambiguous {
545 flags.insert(Flags::MTIME_SECOND_AMBIGUOUS);
549 flags.insert(Flags::MTIME_SECOND_AMBIGUOUS);
546 };
550 };
547 m.into()
551 m.into()
548 } else {
552 } else {
549 PackedTruncatedTimestamp::null()
553 PackedTruncatedTimestamp::null()
550 };
554 };
551 if let Some(f_exec) = fallback_exec {
555 if let Some(f_exec) = fallback_exec {
552 flags.insert(Flags::HAS_FALLBACK_EXEC);
556 flags.insert(Flags::HAS_FALLBACK_EXEC);
553 if f_exec {
557 if f_exec {
554 flags.insert(Flags::FALLBACK_EXEC);
558 flags.insert(Flags::FALLBACK_EXEC);
555 }
559 }
556 }
560 }
557 if let Some(f_symlink) = fallback_symlink {
561 if let Some(f_symlink) = fallback_symlink {
558 flags.insert(Flags::HAS_FALLBACK_SYMLINK);
562 flags.insert(Flags::HAS_FALLBACK_SYMLINK);
559 if f_symlink {
563 if f_symlink {
560 flags.insert(Flags::FALLBACK_SYMLINK);
564 flags.insert(Flags::FALLBACK_SYMLINK);
561 }
565 }
562 }
566 }
563 (flags, size, mtime)
567 (flags, size, mtime)
564 }
568 }
565 }
569 }
566
570
567 fn read_hg_path(
571 fn read_hg_path(
568 on_disk: &[u8],
572 on_disk: &[u8],
569 slice: PathSlice,
573 slice: PathSlice,
570 ) -> Result<&HgPath, DirstateV2ParseError> {
574 ) -> Result<&HgPath, DirstateV2ParseError> {
571 read_slice(on_disk, slice.start, slice.len.get()).map(HgPath::new)
575 read_slice(on_disk, slice.start, slice.len.get()).map(HgPath::new)
572 }
576 }
573
577
574 fn read_nodes(
578 fn read_nodes(
575 on_disk: &[u8],
579 on_disk: &[u8],
576 slice: ChildNodes,
580 slice: ChildNodes,
577 ) -> Result<&[Node], DirstateV2ParseError> {
581 ) -> Result<&[Node], DirstateV2ParseError> {
578 read_slice(on_disk, slice.start, slice.len.get())
582 read_slice(on_disk, slice.start, slice.len.get())
579 }
583 }
580
584
581 fn read_slice<T, Len>(
585 fn read_slice<T, Len>(
582 on_disk: &[u8],
586 on_disk: &[u8],
583 start: Offset,
587 start: Offset,
584 len: Len,
588 len: Len,
585 ) -> Result<&[T], DirstateV2ParseError>
589 ) -> Result<&[T], DirstateV2ParseError>
586 where
590 where
587 T: BytesCast,
591 T: BytesCast,
588 Len: TryInto<usize>,
592 Len: TryInto<usize>,
589 {
593 {
590 // Either `usize::MAX` would result in "out of bounds" error since a single
594 // Either `usize::MAX` would result in "out of bounds" error since a single
591 // `&[u8]` cannot occupy the entire addess space.
595 // `&[u8]` cannot occupy the entire addess space.
592 let start = start.get().try_into().unwrap_or(std::usize::MAX);
596 let start = start.get().try_into().unwrap_or(std::usize::MAX);
593 let len = len.try_into().unwrap_or(std::usize::MAX);
597 let len = len.try_into().unwrap_or(std::usize::MAX);
594 let bytes = match on_disk.get(start..) {
598 let bytes = match on_disk.get(start..) {
595 Some(bytes) => bytes,
599 Some(bytes) => bytes,
596 None => {
600 None => {
597 return Err(DirstateV2ParseError::new(
601 return Err(DirstateV2ParseError::new(
598 "not enough bytes from disk",
602 "not enough bytes from disk",
599 ))
603 ))
600 }
604 }
601 };
605 };
602 T::slice_from_bytes(bytes, len)
606 T::slice_from_bytes(bytes, len)
603 .map_err(|e| {
607 .map_err(|e| {
604 DirstateV2ParseError::new(format!("when reading a slice, {}", e))
608 DirstateV2ParseError::new(format!("when reading a slice, {}", e))
605 })
609 })
606 .map(|(slice, _rest)| slice)
610 .map(|(slice, _rest)| slice)
607 }
611 }
608
612
609 /// Returns new data and metadata, together with whether that data should be
613 /// Returns new data and metadata, together with whether that data should be
610 /// appended to the existing data file whose content is at
614 /// appended to the existing data file whose content is at
611 /// `dirstate_map.on_disk` (true), instead of written to a new data file
615 /// `dirstate_map.on_disk` (true), instead of written to a new data file
612 /// (false), and the previous size of data on disk.
616 /// (false), and the previous size of data on disk.
613 pub(super) fn write(
617 pub(super) fn write(
614 dirstate_map: &DirstateMap,
618 dirstate_map: &DirstateMap,
615 write_mode: DirstateMapWriteMode,
619 write_mode: DirstateMapWriteMode,
616 ) -> Result<(Vec<u8>, TreeMetadata, bool, usize), DirstateError> {
620 ) -> Result<(Vec<u8>, TreeMetadata, bool, usize), DirstateError> {
617 let append = match write_mode {
621 let append = match write_mode {
618 DirstateMapWriteMode::Auto => dirstate_map.write_should_append(),
622 DirstateMapWriteMode::Auto => dirstate_map.write_should_append(),
619 DirstateMapWriteMode::ForceNewDataFile => false,
623 DirstateMapWriteMode::ForceNewDataFile => false,
620 DirstateMapWriteMode::ForceAppend => true,
624 DirstateMapWriteMode::ForceAppend => true,
621 };
625 };
622 if append {
626 if append {
623 log::trace!("appending to the dirstate data file");
627 log::trace!("appending to the dirstate data file");
624 } else {
628 } else {
625 log::trace!("creating new dirstate data file");
629 log::trace!("creating new dirstate data file");
626 }
630 }
627
631
628 // This ignores the space for paths, and for nodes without an entry.
632 // This ignores the space for paths, and for nodes without an entry.
629 // TODO: better estimate? Skip the `Vec` and write to a file directly?
633 // TODO: better estimate? Skip the `Vec` and write to a file directly?
630 let size_guess = std::mem::size_of::<Node>()
634 let size_guess = std::mem::size_of::<Node>()
631 * dirstate_map.nodes_with_entry_count as usize;
635 * dirstate_map.nodes_with_entry_count as usize;
632
636
633 let mut writer = Writer {
637 let mut writer = Writer {
634 dirstate_map,
638 dirstate_map,
635 append,
639 append,
636 out: Vec::with_capacity(size_guess),
640 out: Vec::with_capacity(size_guess),
637 };
641 };
638
642
639 let root_nodes = writer.write_nodes(dirstate_map.root.as_ref())?;
643 let root_nodes = writer.write_nodes(dirstate_map.root.as_ref())?;
640
644
641 let unreachable_bytes = if append {
645 let unreachable_bytes = if append {
642 dirstate_map.unreachable_bytes
646 dirstate_map.unreachable_bytes
643 } else {
647 } else {
644 0
648 0
645 };
649 };
646 let meta = TreeMetadata {
650 let meta = TreeMetadata {
647 root_nodes,
651 root_nodes,
648 nodes_with_entry_count: dirstate_map.nodes_with_entry_count.into(),
652 nodes_with_entry_count: dirstate_map.nodes_with_entry_count.into(),
649 nodes_with_copy_source_count: dirstate_map
653 nodes_with_copy_source_count: dirstate_map
650 .nodes_with_copy_source_count
654 .nodes_with_copy_source_count
651 .into(),
655 .into(),
652 unreachable_bytes: unreachable_bytes.into(),
656 unreachable_bytes: unreachable_bytes.into(),
653 unused: [0; 4],
657 unused: [0; 4],
654 ignore_patterns_hash: dirstate_map.ignore_patterns_hash,
658 ignore_patterns_hash: dirstate_map.ignore_patterns_hash,
655 };
659 };
656 Ok((writer.out, meta, append, dirstate_map.old_data_size))
660 Ok((writer.out, meta, append, dirstate_map.old_data_size))
657 }
661 }
658
662
659 struct Writer<'dmap, 'on_disk> {
663 struct Writer<'dmap, 'on_disk> {
660 dirstate_map: &'dmap DirstateMap<'on_disk>,
664 dirstate_map: &'dmap DirstateMap<'on_disk>,
661 append: bool,
665 append: bool,
662 out: Vec<u8>,
666 out: Vec<u8>,
663 }
667 }
664
668
665 impl Writer<'_, '_> {
669 impl Writer<'_, '_> {
666 fn write_nodes(
670 fn write_nodes(
667 &mut self,
671 &mut self,
668 nodes: dirstate_map::ChildNodesRef,
672 nodes: dirstate_map::ChildNodesRef,
669 ) -> Result<ChildNodes, DirstateError> {
673 ) -> Result<ChildNodes, DirstateError> {
670 // Reuse already-written nodes if possible
674 // Reuse already-written nodes if possible
671 if self.append {
675 if self.append {
672 if let dirstate_map::ChildNodesRef::OnDisk(nodes_slice) = nodes {
676 if let dirstate_map::ChildNodesRef::OnDisk(nodes_slice) = nodes {
673 let start = self.on_disk_offset_of(nodes_slice).expect(
677 let start = self.on_disk_offset_of(nodes_slice).expect(
674 "dirstate-v2 OnDisk nodes not found within on_disk",
678 "dirstate-v2 OnDisk nodes not found within on_disk",
675 );
679 );
676 let len = child_nodes_len_from_usize(nodes_slice.len());
680 let len = child_nodes_len_from_usize(nodes_slice.len());
677 return Ok(ChildNodes { start, len });
681 return Ok(ChildNodes { start, len });
678 }
682 }
679 }
683 }
680
684
681 // `dirstate_map::ChildNodes::InMemory` contains a `HashMap` which has
685 // `dirstate_map::ChildNodes::InMemory` contains a `HashMap` which has
682 // undefined iteration order. Sort to enable binary search in the
686 // undefined iteration order. Sort to enable binary search in the
683 // written file.
687 // written file.
684 let nodes = nodes.sorted();
688 let nodes = nodes.sorted();
685 let nodes_len = nodes.len();
689 let nodes_len = nodes.len();
686
690
687 // First accumulate serialized nodes in a `Vec`
691 // First accumulate serialized nodes in a `Vec`
688 let mut on_disk_nodes = Vec::with_capacity(nodes_len);
692 let mut on_disk_nodes = Vec::with_capacity(nodes_len);
689 for node in nodes {
693 for node in nodes {
690 let children =
694 let children =
691 self.write_nodes(node.children(self.dirstate_map.on_disk)?)?;
695 self.write_nodes(node.children(self.dirstate_map.on_disk)?)?;
692 let full_path = node.full_path(self.dirstate_map.on_disk)?;
696 let full_path = node.full_path(self.dirstate_map.on_disk)?;
693 let full_path = self.write_path(full_path.as_bytes());
697 let full_path = self.write_path(full_path.as_bytes());
694 let copy_source = if let Some(source) =
698 let copy_source = if let Some(source) =
695 node.copy_source(self.dirstate_map.on_disk)?
699 node.copy_source(self.dirstate_map.on_disk)?
696 {
700 {
697 self.write_path(source.as_bytes())
701 self.write_path(source.as_bytes())
698 } else {
702 } else {
699 PathSlice {
703 PathSlice {
700 start: 0.into(),
704 start: 0.into(),
701 len: 0.into(),
705 len: 0.into(),
702 }
706 }
703 };
707 };
704 on_disk_nodes.push(match node {
708 on_disk_nodes.push(match node {
705 NodeRef::InMemory(path, node) => {
709 NodeRef::InMemory(path, node) => {
706 let (flags, size, mtime) = match &node.data {
710 let (flags, size, mtime) = match &node.data {
707 dirstate_map::NodeData::Entry(entry) => {
711 dirstate_map::NodeData::Entry(entry) => {
708 Node::from_dirstate_entry(entry)
712 Node::from_dirstate_entry(entry)
709 }
713 }
710 dirstate_map::NodeData::CachedDirectory { mtime } => {
714 dirstate_map::NodeData::CachedDirectory { mtime } => {
711 // we currently never set a mtime if unknown file
715 // we currently never set a mtime if unknown file
712 // are present.
716 // are present.
713 // So if we have a mtime for a directory, we know
717 // So if we have a mtime for a directory, we know
714 // they are no unknown
718 // they are no unknown
715 // files and we
719 // files and we
716 // blindly set ALL_UNKNOWN_RECORDED.
720 // blindly set ALL_UNKNOWN_RECORDED.
717 //
721 //
718 // We never set ALL_IGNORED_RECORDED since we
722 // We never set ALL_IGNORED_RECORDED since we
719 // don't track that case
723 // don't track that case
720 // currently.
724 // currently.
721 let mut flags = Flags::DIRECTORY
725 let mut flags = Flags::DIRECTORY
722 | Flags::HAS_MTIME
726 | Flags::HAS_MTIME
723 | Flags::ALL_UNKNOWN_RECORDED;
727 | Flags::ALL_UNKNOWN_RECORDED;
724 if mtime.second_ambiguous {
728 if mtime.second_ambiguous {
725 flags.insert(Flags::MTIME_SECOND_AMBIGUOUS)
729 flags.insert(Flags::MTIME_SECOND_AMBIGUOUS)
726 }
730 }
727 (flags, 0.into(), (*mtime).into())
731 (flags, 0.into(), (*mtime).into())
728 }
732 }
729 dirstate_map::NodeData::None => (
733 dirstate_map::NodeData::None => (
730 Flags::DIRECTORY,
734 Flags::DIRECTORY,
731 0.into(),
735 0.into(),
732 PackedTruncatedTimestamp::null(),
736 PackedTruncatedTimestamp::null(),
733 ),
737 ),
734 };
738 };
735 Node {
739 Node {
736 children,
740 children,
737 copy_source,
741 copy_source,
738 full_path,
742 full_path,
739 base_name_start: u16::try_from(path.base_name_start())
743 base_name_start: u16::try_from(path.base_name_start())
740 // Could only panic for paths over 64 KiB
744 // Could only panic for paths over 64 KiB
741 .expect("dirstate-v2 path length overflow")
745 .expect("dirstate-v2 path length overflow")
742 .into(),
746 .into(),
743 descendants_with_entry_count: node
747 descendants_with_entry_count: node
744 .descendants_with_entry_count
748 .descendants_with_entry_count
745 .into(),
749 .into(),
746 tracked_descendants_count: node
750 tracked_descendants_count: node
747 .tracked_descendants_count
751 .tracked_descendants_count
748 .into(),
752 .into(),
749 flags: flags.bits().into(),
753 flags: flags.bits().into(),
750 size,
754 size,
751 mtime,
755 mtime,
752 }
756 }
753 }
757 }
754 NodeRef::OnDisk(node) => Node {
758 NodeRef::OnDisk(node) => Node {
755 children,
759 children,
756 copy_source,
760 copy_source,
757 full_path,
761 full_path,
758 ..*node
762 ..*node
759 },
763 },
760 })
764 })
761 }
765 }
762 // … so we can write them contiguously, after writing everything else
766 // … so we can write them contiguously, after writing everything else
763 // they refer to.
767 // they refer to.
764 let start = self.current_offset();
768 let start = self.current_offset();
765 let len = child_nodes_len_from_usize(nodes_len);
769 let len = child_nodes_len_from_usize(nodes_len);
766 self.out.extend(on_disk_nodes.as_bytes());
770 self.out.extend(on_disk_nodes.as_bytes());
767 Ok(ChildNodes { start, len })
771 Ok(ChildNodes { start, len })
768 }
772 }
769
773
770 /// If the given slice of items is within `on_disk`, returns its offset
774 /// If the given slice of items is within `on_disk`, returns its offset
771 /// from the start of `on_disk`.
775 /// from the start of `on_disk`.
772 fn on_disk_offset_of<T>(&self, slice: &[T]) -> Option<Offset>
776 fn on_disk_offset_of<T>(&self, slice: &[T]) -> Option<Offset>
773 where
777 where
774 T: BytesCast,
778 T: BytesCast,
775 {
779 {
776 fn address_range(slice: &[u8]) -> std::ops::RangeInclusive<usize> {
780 fn address_range(slice: &[u8]) -> std::ops::RangeInclusive<usize> {
777 let start = slice.as_ptr() as usize;
781 let start = slice.as_ptr() as usize;
778 let end = start + slice.len();
782 let end = start + slice.len();
779 start..=end
783 start..=end
780 }
784 }
781 let slice_addresses = address_range(slice.as_bytes());
785 let slice_addresses = address_range(slice.as_bytes());
782 let on_disk_addresses = address_range(self.dirstate_map.on_disk);
786 let on_disk_addresses = address_range(self.dirstate_map.on_disk);
783 if on_disk_addresses.contains(slice_addresses.start())
787 if on_disk_addresses.contains(slice_addresses.start())
784 && on_disk_addresses.contains(slice_addresses.end())
788 && on_disk_addresses.contains(slice_addresses.end())
785 {
789 {
786 let offset = slice_addresses.start() - on_disk_addresses.start();
790 let offset = slice_addresses.start() - on_disk_addresses.start();
787 Some(offset_from_usize(offset))
791 Some(offset_from_usize(offset))
788 } else {
792 } else {
789 None
793 None
790 }
794 }
791 }
795 }
792
796
793 fn current_offset(&mut self) -> Offset {
797 fn current_offset(&mut self) -> Offset {
794 let mut offset = self.out.len();
798 let mut offset = self.out.len();
795 if self.append {
799 if self.append {
796 offset += self.dirstate_map.on_disk.len()
800 offset += self.dirstate_map.on_disk.len()
797 }
801 }
798 offset_from_usize(offset)
802 offset_from_usize(offset)
799 }
803 }
800
804
801 fn write_path(&mut self, slice: &[u8]) -> PathSlice {
805 fn write_path(&mut self, slice: &[u8]) -> PathSlice {
802 let len = path_len_from_usize(slice.len());
806 let len = path_len_from_usize(slice.len());
803 // Reuse an already-written path if possible
807 // Reuse an already-written path if possible
804 if self.append {
808 if self.append {
805 if let Some(start) = self.on_disk_offset_of(slice) {
809 if let Some(start) = self.on_disk_offset_of(slice) {
806 return PathSlice { start, len };
810 return PathSlice { start, len };
807 }
811 }
808 }
812 }
809 let start = self.current_offset();
813 let start = self.current_offset();
810 self.out.extend(slice.as_bytes());
814 self.out.extend(slice.as_bytes());
811 PathSlice { start, len }
815 PathSlice { start, len }
812 }
816 }
813 }
817 }
814
818
815 fn offset_from_usize(x: usize) -> Offset {
819 fn offset_from_usize(x: usize) -> Offset {
816 u32::try_from(x)
820 u32::try_from(x)
817 // Could only panic for a dirstate file larger than 4 GiB
821 // Could only panic for a dirstate file larger than 4 GiB
818 .expect("dirstate-v2 offset overflow")
822 .expect("dirstate-v2 offset overflow")
819 .into()
823 .into()
820 }
824 }
821
825
822 fn child_nodes_len_from_usize(x: usize) -> Size {
826 fn child_nodes_len_from_usize(x: usize) -> Size {
823 u32::try_from(x)
827 u32::try_from(x)
824 // Could only panic with over 4 billion nodes
828 // Could only panic with over 4 billion nodes
825 .expect("dirstate-v2 slice length overflow")
829 .expect("dirstate-v2 slice length overflow")
826 .into()
830 .into()
827 }
831 }
828
832
829 fn path_len_from_usize(x: usize) -> PathSize {
833 fn path_len_from_usize(x: usize) -> PathSize {
830 u16::try_from(x)
834 u16::try_from(x)
831 // Could only panic for paths over 64 KiB
835 // Could only panic for paths over 64 KiB
832 .expect("dirstate-v2 path length overflow")
836 .expect("dirstate-v2 path length overflow")
833 .into()
837 .into()
834 }
838 }
835
839
836 impl From<TruncatedTimestamp> for PackedTruncatedTimestamp {
840 impl From<TruncatedTimestamp> for PackedTruncatedTimestamp {
837 fn from(timestamp: TruncatedTimestamp) -> Self {
841 fn from(timestamp: TruncatedTimestamp) -> Self {
838 Self {
842 Self {
839 truncated_seconds: timestamp.truncated_seconds().into(),
843 truncated_seconds: timestamp.truncated_seconds().into(),
840 nanoseconds: timestamp.nanoseconds().into(),
844 nanoseconds: timestamp.nanoseconds().into(),
841 }
845 }
842 }
846 }
843 }
847 }
844
848
845 impl TryFrom<PackedTruncatedTimestamp> for TruncatedTimestamp {
849 impl TryFrom<PackedTruncatedTimestamp> for TruncatedTimestamp {
846 type Error = DirstateV2ParseError;
850 type Error = DirstateV2ParseError;
847
851
848 fn try_from(
852 fn try_from(
849 timestamp: PackedTruncatedTimestamp,
853 timestamp: PackedTruncatedTimestamp,
850 ) -> Result<Self, Self::Error> {
854 ) -> Result<Self, Self::Error> {
851 Self::from_already_truncated(
855 Self::from_already_truncated(
852 timestamp.truncated_seconds.get(),
856 timestamp.truncated_seconds.get(),
853 timestamp.nanoseconds.get(),
857 timestamp.nanoseconds.get(),
854 false,
858 false,
855 )
859 )
856 }
860 }
857 }
861 }
858 impl PackedTruncatedTimestamp {
862 impl PackedTruncatedTimestamp {
859 fn null() -> Self {
863 fn null() -> Self {
860 Self {
864 Self {
861 truncated_seconds: 0.into(),
865 truncated_seconds: 0.into(),
862 nanoseconds: 0.into(),
866 nanoseconds: 0.into(),
863 }
867 }
864 }
868 }
865 }
869 }
General Comments 0
You need to be logged in to leave comments. Login now