Debian Codegraph @

rust/hg-pyo3/src/dirstate/dirstate_map.rs

raw
1 // dirstate_map.rs
2 //
3 // Copyright 2019 Raphaël Gomès <[email protected]>
4 // 2025 Georges Racinet <[email protected]>
5 //
6 // This software may be used and distributed according to the terms of the
7 // GNU General Public License version 2 or any later version.
8 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
9 //! `hg-core` package.
10
11 use std::sync::RwLockReadGuard;
12 use std::sync::RwLockWriteGuard;
13
14 use hg::dirstate::dirstate_map::DirstateEntryReset;
15 use hg::dirstate::dirstate_map::DirstateIdentity as CoreDirstateIdentity;
16 use hg::dirstate::dirstate_map::DirstateMapWriteMode;
17 use hg::dirstate::entry::DirstateEntry;
18 use hg::dirstate::entry::ParentFileData;
19 use hg::dirstate::entry::TruncatedTimestamp;
20 use hg::dirstate::on_disk::DirstateV2ParseError;
21 use hg::dirstate::owning::OwningDirstateMap;
22 use hg::dirstate::StateMapIter;
23 use hg::utils::files::normalize_case;
24 use hg::utils::hg_path::HgPath;
25 use hg::DirstateParents;
26 use pyo3::exceptions::PyKeyError;
27 use pyo3::exceptions::PyOSError;
28 use pyo3::prelude::*;
29 use pyo3::types::PyBytes;
30 use pyo3::types::PyBytesMethods;
31 use pyo3::types::PyDict;
32 use pyo3::types::PyDictMethods;
33 use pyo3::types::PyList;
34 use pyo3::types::PyTuple;
35 use pyo3_sharedref::py_shared_iterator;
36 use pyo3_sharedref::PyShareable;
37
38 use super::copy_map::CopyMap;
39 use super::item::DirstateItem;
40 use crate::exceptions::dirstate_error;
41 use crate::exceptions::dirstate_v2_error;
42 use crate::exceptions::map_try_lock_error;
43 use crate::exceptions::to_string_value_error;
44 use crate::node::node_from_py_bytes;
45 use crate::node::PyNode;
46 use crate::path::PyHgPathBuf;
47 use crate::path::PyHgPathDirstateV2Result;
48 use crate::path::PyHgPathRef;
49 use crate::utils::PyBytesDeref;
50
51 /// Type alias to satisfy Clippy in `DirstateMap::reset_state)`
52 ///
53 /// It is *not* the same as [`super::item::UncheckedTruncatedTimeStamp`] and
54 /// this is worth reviewing.
55 type UncheckedTruncatedTimeStamp = Option<(i64, u32, bool)>;
56
57 #[pyclass(mapping)]
58 pub struct DirstateMap {
59 pub(super) inner: PyShareable<OwningDirstateMap>,
60 }
61
62 #[pymethods]
63 impl DirstateMap {
64 #[staticmethod]
65 #[pyo3(signature = (on_disk, identity))]
66 /// Returns a `(dirstate_map, parents)` tuple
67 ///
68 /// The Python call site is using the positional argument style, hence
69 /// despite the fact that `identity` can be `None`, we specify the
70 /// matching signature.
71 fn new_v1(
72 py: Python,
73 on_disk: Py<PyBytes>,
74 identity: Option<&Bound<'_, DirstateIdentity>>,
75 ) -> PyResult<Py<PyTuple>> {
76 let on_disk = PyBytesDeref::new(py, on_disk);
77 let (map, parents) = OwningDirstateMap::new_v1(
78 on_disk,
79 identity.map(|i| i.borrow().inner),
80 )
81 .map_err(dirstate_error)?;
82 let map = Self { inner: map.into() };
83 let parents = (PyNode(parents.p1), PyNode(parents.p2));
84 Ok((map, parents).into_pyobject(py)?.into())
85 }
86
87 #[staticmethod]
88 #[pyo3(signature = (on_disk, data_size, tree_metadata, uuid, identity))]
89 fn new_v2(
90 py: Python,
91 on_disk: Py<PyBytes>,
92 data_size: usize,
93 tree_metadata: &Bound<'_, PyBytes>,
94 uuid: &Bound<'_, PyBytes>,
95 identity: Option<&Bound<'_, DirstateIdentity>>,
96 ) -> PyResult<Self> {
97 Ok(Self {
98 inner: OwningDirstateMap::new_v2(
99 PyBytesDeref::new(py, on_disk),
100 data_size,
101 tree_metadata.as_bytes(),
102 uuid.as_bytes().to_owned(),
103 identity.map(|i| i.borrow().inner),
104 )
105 .map_err(dirstate_error)?
106 .into(),
107 })
108 }
109
110 #[staticmethod]
111 fn new_empty() -> PyResult<Self> {
112 Ok(Self { inner: OwningDirstateMap::new_empty(vec![], None).into() })
113 }
114
115 fn clear(slf: &Bound<'_, Self>) -> PyResult<()> {
116 Self::with_inner_write(slf, |_self_ref, mut inner| {
117 inner.clear();
118 Ok(())
119 })
120 }
121
122 #[pyo3(signature = (key, default=None))]
123 fn get(
124 slf: &Bound<'_, Self>,
125 key: &Bound<'_, PyBytes>,
126 default: Option<Py<PyAny>>,
127 ) -> PyResult<Option<Py<PyAny>>> {
128 let path = HgPath::new(key.as_bytes());
129
130 Self::with_inner_read(slf, |_self_ref, inner| {
131 match inner.get(path).map_err(dirstate_v2_error)? {
132 Some(entry) => Ok(Some(
133 DirstateItem::new_as_py(slf.py(), entry)?.into_any(),
134 )),
135 None => Ok(default),
136 }
137 })
138 }
139
140 fn set_tracked(
141 slf: &Bound<'_, Self>,
142 f: &Bound<'_, PyBytes>,
143 ) -> PyResult<bool> {
144 Self::with_inner_write(slf, |_self_ref, mut inner| {
145 inner
146 .set_tracked(HgPath::new(f.as_bytes()))
147 .map_err(dirstate_v2_error)
148 })
149 }
150
151 fn set_untracked(
152 slf: &Bound<'_, Self>,
153 f: &Bound<'_, PyBytes>,
154 ) -> PyResult<bool> {
155 Self::with_inner_write(slf, |_self_ref, mut inner| {
156 // here it would be more straightforward to use dirstate_v2_error,
157 // but that raises ValueError instead of OSError
158 inner
159 .set_untracked(HgPath::new(f.as_bytes()))
160 .map_err(|_| PyOSError::new_err("Dirstate error"))
161 })
162 }
163
164 fn set_clean(
165 slf: &Bound<'_, Self>,
166 f: &Bound<'_, PyBytes>,
167 mode: u32,
168 size: u32,
169 mtime: (i64, u32, bool),
170 ) -> PyResult<()> {
171 let (mtime_s, mtime_ns, second_ambiguous) = mtime;
172 let timestamp = TruncatedTimestamp::new_truncate(
173 mtime_s,
174 mtime_ns,
175 second_ambiguous,
176 );
177
178 Self::with_inner_write(slf, |_self_ref, mut inner| {
179 inner
180 .set_clean(HgPath::new(f.as_bytes()), mode, size, timestamp)
181 .map_err(dirstate_error)
182 })
183 }
184
185 fn set_possibly_dirty(
186 slf: &Bound<'_, Self>,
187 f: &Bound<'_, PyBytes>,
188 ) -> PyResult<()> {
189 Self::with_inner_write(slf, |_self_ref, mut inner| {
190 inner
191 .set_possibly_dirty(HgPath::new(f.as_bytes()))
192 .map_err(dirstate_error)
193 })
194 }
195
196 #[pyo3(signature = (f,
197 wc_tracked=false,
198 p1_tracked=false,
199 p2_info=false,
200 has_meaningful_mtime=true,
201 parentfiledata=None))]
202 fn reset_state(
203 slf: &Bound<'_, Self>,
204 f: &Bound<'_, PyBytes>,
205 wc_tracked: bool,
206 p1_tracked: bool,
207 p2_info: bool,
208 has_meaningful_mtime: bool,
209 parentfiledata: Option<(u32, u32, UncheckedTruncatedTimeStamp)>,
210 ) -> PyResult<()> {
211 let mut has_meaningful_mtime = has_meaningful_mtime;
212 let parent_file_data = match parentfiledata {
213 None => {
214 has_meaningful_mtime = false;
215 None
216 }
217 Some(data) => {
218 let (mode, size, mtime_info) = data;
219 let mtime = if let Some(mtime_info) = mtime_info {
220 let (mtime_s, mtime_ns, second_ambiguous) = mtime_info;
221 let timestamp = TruncatedTimestamp::new_truncate(
222 mtime_s,
223 mtime_ns,
224 second_ambiguous,
225 );
226 Some(timestamp)
227 } else {
228 has_meaningful_mtime = false;
229 None
230 };
231 Some(ParentFileData { mode_size: Some((mode, size)), mtime })
232 }
233 };
234
235 let reset = DirstateEntryReset {
236 filename: HgPath::new(f.as_bytes()),
237 wc_tracked,
238 p1_tracked,
239 p2_info,
240 has_meaningful_mtime,
241 parent_file_data_opt: parent_file_data,
242 from_empty: false,
243 };
244
245 Self::with_inner_write(slf, |_self_ref, mut inner| {
246 inner.reset_state(reset).map_err(dirstate_error)?;
247 Ok(())
248 })
249 }
250
251 fn hastrackeddir(
252 slf: &Bound<'_, Self>,
253 d: &Bound<'_, PyBytes>,
254 ) -> PyResult<bool> {
255 Self::with_inner_write(slf, |_self_ref, mut inner| {
256 inner
257 .has_tracked_dir(HgPath::new(d.as_bytes()))
258 .map_err(to_string_value_error)
259 })
260 }
261
262 fn hasdir(slf: &Bound<'_, Self>, d: &Bound<'_, PyBytes>) -> PyResult<bool> {
263 Self::with_inner_write(slf, |_self_ref, mut inner| {
264 inner
265 .has_dir(HgPath::new(d.as_bytes()))
266 .map_err(to_string_value_error)
267 })
268 }
269
270 /// Returns suitable data for writing on disk in v1 format
271 ///
272 /// Despite the name, this is not a mutation of the object.
273 fn write_v1(
274 slf: &Bound<'_, Self>,
275 py: Python,
276 p1: &Bound<'_, PyBytes>,
277 p2: &Bound<'_, PyBytes>,
278 ) -> PyResult<Py<PyBytes>> {
279 Self::with_inner_read(slf, |_self_ref, inner| {
280 let parents = DirstateParents {
281 p1: node_from_py_bytes(p1)?,
282 p2: node_from_py_bytes(p2)?,
283 };
284 let packed = inner.pack_v1(parents).map_err(dirstate_error)?;
285 // TODO optim, see `write_v2()`
286 Ok(PyBytes::new(py, &packed).unbind())
287 })
288 }
289
290 /// Returns suitable new data for writing on disk in v2 format
291 ///
292 /// Despite the name, this is not a mutation of the object.
293 ///
294 /// The new data together with whether that data should be appended to
295 /// the existing data file whose content is at `self.on_disk` (True),
296 /// instead of written to a new data file (False).
297 fn write_v2(
298 slf: &Bound<'_, Self>,
299 py: Python,
300 write_mode: usize,
301 ) -> PyResult<Py<PyTuple>> {
302 Self::with_inner_read(slf, |_self_ref, inner| {
303 let rust_write_mode = match write_mode {
304 0 => DirstateMapWriteMode::Auto,
305 1 => DirstateMapWriteMode::ForceNewDataFile,
306 2 => DirstateMapWriteMode::ForceAppend,
307 _ => DirstateMapWriteMode::Auto, // XXX should we error out?
308 };
309 let (packed, tree_metadata, append, _old_data_size) =
310 inner.pack_v2(rust_write_mode).map_err(dirstate_error)?;
311 // TODO optim. In theory we should be able to avoid these copies,
312 // since we have full ownership of `packed` and `tree_metadata`.
313 // But the copy is done by CPython itself, in
314 // `PyBytes_FromStringAndSize()`. Perhaps something better can
315 // be done with `PyBytes_FromObject` (buffer protocol).
316 let packed = PyBytes::new(py, &packed).unbind();
317 let tree_metadata =
318 PyBytes::new(py, tree_metadata.as_bytes()).unbind();
319 Ok((packed, tree_metadata, append).into_pyobject(py)?.into())
320 })
321 }
322
323 fn filefoldmapasdict(
324 slf: &Bound<'_, Self>,
325 py: Python,
326 ) -> PyResult<Py<PyDict>> {
327 let dict = PyDict::new(py);
328 Self::with_inner_read(slf, |_self_ref, inner| {
329 for item in inner.iter() {
330 let (path, entry) = item.map_err(dirstate_v2_error)?;
331 if !entry.removed() {
332 let key = normalize_case(path);
333 dict.set_item(PyHgPathBuf(key), PyHgPathRef(path))?;
334 }
335 }
336 Ok(())
337 })?;
338 Ok(dict.unbind())
339 }
340
341 fn __len__(slf: &Bound<'_, Self>) -> PyResult<usize> {
342 Self::with_inner_read(slf, |_self_ref, inner| Ok(inner.len()))
343 }
344
345 fn __contains__(
346 slf: &Bound<'_, Self>,
347 // TODO we should accept PyAny and return false if wrong type
348 // review similar "protocol" methods (see example in dirs_multiset)
349 key: &Bound<'_, PyBytes>,
350 ) -> PyResult<bool> {
351 Self::with_inner_read(slf, |_self_ref, inner| {
352 inner
353 .contains_key(HgPath::new(key.as_bytes()))
354 .map_err(dirstate_v2_error)
355 })
356 }
357
358 fn __getitem__(
359 slf: &Bound<'_, Self>,
360 key: &Bound<'_, PyBytes>,
361 ) -> PyResult<Py<DirstateItem>> {
362 let key_bytes = key.as_bytes();
363 let path = HgPath::new(key_bytes);
364 Self::with_inner_read(slf, |_self_ref, inner| {
365 match inner.get(path).map_err(dirstate_v2_error)? {
366 Some(entry) => DirstateItem::new_as_py(slf.py(), entry),
367 None => Err(PyKeyError::new_err(
368 String::from_utf8_lossy(key_bytes).to_string(),
369 )),
370 }
371 })
372 }
373
374 fn keys(slf: &Bound<'_, Self>) -> PyResult<DirstateMapKeysIterator> {
375 DirstateMapKeysIterator::new(slf)
376 }
377
378 fn items(slf: &Bound<'_, Self>) -> PyResult<DirstateMapItemsIterator> {
379 DirstateMapItemsIterator::new(slf)
380 }
381
382 fn __iter__(slf: &Bound<'_, Self>) -> PyResult<DirstateMapKeysIterator> {
383 Self::keys(slf)
384 }
385
386 fn copymap(slf: &Bound<'_, Self>) -> PyResult<Py<CopyMap>> {
387 CopyMap::new(slf).and_then(|cm| Py::new(slf.py(), cm))
388 }
389
390 fn tracked_dirs(slf: &Bound<'_, Self>, py: Python) -> PyResult<Py<PyList>> {
391 // core iterator is not exact sized, we cannot use `PyList::new`
392 let dirs = PyList::empty(py);
393 Self::with_inner_write(slf, |_self_ref, mut inner| {
394 for path in inner.iter_tracked_dirs().map_err(dirstate_error)? {
395 dirs.append(PyHgPathDirstateV2Result(path))?;
396 }
397 Ok(())
398 })?;
399 Ok(dirs.unbind())
400 }
401
402 fn setparents_fixup(
403 slf: &Bound<'_, Self>,
404 py: Python,
405 ) -> PyResult<Py<PyDict>> {
406 let dict = PyDict::new(py);
407 let copies = Self::with_inner_write(slf, |_self_ref, mut inner| {
408 inner.setparents_fixup().map_err(dirstate_v2_error)
409 })?;
410
411 // it might be interesting to try and use the `IntoPyDict` trait,
412 // but it does about the same thing
413 // but that would require performing the inner `as_bytes()` as well
414 for (key, value) in copies {
415 dict.set_item(PyHgPathBuf(key), PyHgPathBuf(value))?;
416 }
417 Ok(dict.unbind())
418 }
419
420 fn debug_iter(
421 slf: &Bound<'_, Self>,
422 py: Python,
423 all: bool,
424 ) -> PyResult<Py<PyAny>> {
425 Self::with_inner_read(slf, |_self_ref, inner| {
426 // the iterator returned by `debug_iter()` does not
427 // implement ExactSizeIterator, which is needed by
428 // `PyList::new()`, so we need to collect. Probably not a
429 // performance issue, as this is a debug method.
430 let as_vec: PyResult<Vec<_>> = inner
431 .debug_iter(all)
432 .map(|item| {
433 let (path, (state, mode, size, mtime)) =
434 item.map_err(dirstate_v2_error)?;
435 Ok((PyHgPathRef(path), state, mode, size, mtime))
436 })
437 .collect();
438 // `IntoPyObject` on `Vec` and `&[T]` gives `PyList` or `PyBytes`
439 Ok(as_vec?.into_pyobject(py)?.unbind())
440 })
441 }
442 }
443
444 py_shared_iterator!(
445 DirstateMapKeysIterator,
446 PyBytes,
447 DirstateMap,
448 inner,
449 StateMapIter<'static>,
450 |dsm| dsm.iter(),
451 DirstateMap::keys_next_result
452 );
453
454 py_shared_iterator!(
455 DirstateMapItemsIterator,
456 PyTuple,
457 DirstateMap,
458 inner,
459 StateMapIter<'static>,
460 |dsm| dsm.iter(),
461 DirstateMap::items_next_result
462 );
463
464 impl DirstateMap {
465 fn keys_next_result(
466 py: Python,
467 res: Result<(&HgPath, DirstateEntry), DirstateV2ParseError>,
468 ) -> PyResult<Option<Py<PyBytes>>> {
469 let key = res.map_err(dirstate_v2_error)?.0;
470 Ok(Some(PyHgPathRef(key).into_pyobject(py)?.unbind()))
471 }
472
473 fn items_next_result(
474 py: Python,
475 res: Result<(&HgPath, DirstateEntry), DirstateV2ParseError>,
476 ) -> PyResult<Option<Py<PyTuple>>> {
477 let (key, entry) = res.map_err(dirstate_v2_error)?;
478 let py_entry = DirstateItem::new_as_py(py, entry)?;
479 Ok(Some((PyHgPathRef(key), py_entry).into_pyobject(py)?.into()))
480 }
481
482 pub(super) fn with_inner_read<'py, T>(
483 slf: &Bound<'py, Self>,
484 f: impl FnOnce(
485 &PyRef<'py, Self>,
486 RwLockReadGuard<OwningDirstateMap>,
487 ) -> PyResult<T>,
488 ) -> PyResult<T> {
489 let self_ref = slf.borrow();
490 // Safety: the owner is the right one. We will anyway
491 // not actually `share` it.
492 let shareable_ref = unsafe { self_ref.inner.borrow_with_owner(slf) };
493 let guard = shareable_ref.try_read().map_err(map_try_lock_error)?;
494 f(&self_ref, guard)
495 }
496
497 pub fn with_inner_write<'py, T>(
498 slf: &Bound<'py, Self>,
499 f: impl FnOnce(
500 &PyRef<'py, Self>,
501 RwLockWriteGuard<OwningDirstateMap>,
502 ) -> PyResult<T>,
503 ) -> PyResult<T> {
504 let self_ref = slf.borrow();
505 // Safety: the owner is the right one. We will anyway
506 // not actually `share` it.
507 let shareable_ref = unsafe { self_ref.inner.borrow_with_owner(slf) };
508 let guard = shareable_ref.try_write().map_err(map_try_lock_error)?;
509 f(&self_ref, guard)
510 }
511 }
512
513 #[pyclass]
514 pub struct DirstateIdentity {
515 #[allow(dead_code)]
516 inner: CoreDirstateIdentity,
517 }
518
519 #[pymethods]
520 impl DirstateIdentity {
521 #[new]
522 #[allow(clippy::too_many_arguments)]
523 fn new(
524 mode: u32,
525 dev: u64,
526 ino: u64,
527 nlink: u64,
528 uid: u32,
529 gid: u32,
530 size: u64,
531 mtime: i64,
532 mtime_nsec: i64,
533 ctime: i64,
534 ctime_nsec: i64,
535 ) -> PyResult<Self> {
536 Ok(Self {
537 inner: CoreDirstateIdentity {
538 mode,
539 dev,
540 ino,
541 nlink,
542 uid,
543 gid,
544 size,
545 mtime,
546 mtime_nsec,
547 ctime,
548 ctime_nsec,
549 },
550 })
551 }
552 }