##// END OF EJS Templates
rust-dirstate: handle invalid length of p1/p2 parameters...
Yuya Nishihara -
r43068:79561843 default
parent child Browse files
Show More
@@ -1,510 +1,515 b''
1 // dirstate_map.rs
1 // dirstate_map.rs
2 //
2 //
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
3 // Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
4 //
4 //
5 // This software may be used and distributed according to the terms of the
5 // This software may be used and distributed according to the terms of the
6 // GNU General Public License version 2 or any later version.
6 // GNU General Public License version 2 or any later version.
7
7
8 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
8 //! Bindings for the `hg::dirstate::dirstate_map` file provided by the
9 //! `hg-core` package.
9 //! `hg-core` package.
10
10
11 use std::cell::RefCell;
11 use std::cell::RefCell;
12 use std::convert::TryInto;
12 use std::convert::TryInto;
13 use std::time::Duration;
13 use std::time::Duration;
14
14
15 use cpython::{
15 use cpython::{
16 exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject,
16 exc, ObjectProtocol, PyBool, PyBytes, PyClone, PyDict, PyErr, PyObject,
17 PyResult, PyTuple, Python, PythonObject, ToPyObject,
17 PyResult, PyTuple, Python, PythonObject, ToPyObject,
18 };
18 };
19 use libc::c_char;
19 use libc::c_char;
20
20
21 use crate::{
21 use crate::{
22 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
22 dirstate::copymap::{CopyMap, CopyMapItemsIterator, CopyMapKeysIterator},
23 dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs},
23 dirstate::{decapsule_make_dirstate_tuple, dirs_multiset::Dirs},
24 ref_sharing::PySharedState,
24 ref_sharing::PySharedState,
25 };
25 };
26 use hg::{
26 use hg::{
27 DirsIterable, DirsMultiset, DirstateEntry,
27 DirsIterable, DirsMultiset, DirstateEntry, DirstateMap as RustDirstateMap,
28 DirstateMap as RustDirstateMap, DirstateParents, DirstateParseError,
28 DirstateParents, DirstateParseError, EntryState, PARENT_SIZE,
29 EntryState,
30 };
29 };
31
30
32 // TODO
31 // TODO
33 // This object needs to share references to multiple members of its Rust
32 // This object needs to share references to multiple members of its Rust
34 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
33 // inner struct, namely `copy_map`, `dirs` and `all_dirs`.
35 // Right now `CopyMap` is done, but it needs to have an explicit reference
34 // Right now `CopyMap` is done, but it needs to have an explicit reference
36 // to `RustDirstateMap` which itself needs to have an encapsulation for
35 // to `RustDirstateMap` which itself needs to have an encapsulation for
37 // every method in `CopyMap` (copymapcopy, etc.).
36 // every method in `CopyMap` (copymapcopy, etc.).
38 // This is ugly and hard to maintain.
37 // This is ugly and hard to maintain.
39 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
38 // The same logic applies to `dirs` and `all_dirs`, however the `Dirs`
40 // `py_class!` is already implemented and does not mention
39 // `py_class!` is already implemented and does not mention
41 // `RustDirstateMap`, rightfully so.
40 // `RustDirstateMap`, rightfully so.
42 // All attributes also have to have a separate refcount data attribute for
41 // All attributes also have to have a separate refcount data attribute for
43 // leaks, with all methods that go along for reference sharing.
42 // leaks, with all methods that go along for reference sharing.
44 py_class!(pub class DirstateMap |py| {
43 py_class!(pub class DirstateMap |py| {
45 data inner: RefCell<RustDirstateMap>;
44 data inner: RefCell<RustDirstateMap>;
46 data py_shared_state: PySharedState;
45 data py_shared_state: PySharedState;
47
46
48 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
47 def __new__(_cls, _root: PyObject) -> PyResult<Self> {
49 let inner = RustDirstateMap::default();
48 let inner = RustDirstateMap::default();
50 Self::create_instance(
49 Self::create_instance(
51 py,
50 py,
52 RefCell::new(inner),
51 RefCell::new(inner),
53 PySharedState::default()
52 PySharedState::default()
54 )
53 )
55 }
54 }
56
55
57 def clear(&self) -> PyResult<PyObject> {
56 def clear(&self) -> PyResult<PyObject> {
58 self.borrow_mut(py)?.clear();
57 self.borrow_mut(py)?.clear();
59 Ok(py.None())
58 Ok(py.None())
60 }
59 }
61
60
62 def get(
61 def get(
63 &self,
62 &self,
64 key: PyObject,
63 key: PyObject,
65 default: Option<PyObject> = None
64 default: Option<PyObject> = None
66 ) -> PyResult<Option<PyObject>> {
65 ) -> PyResult<Option<PyObject>> {
67 let key = key.extract::<PyBytes>(py)?;
66 let key = key.extract::<PyBytes>(py)?;
68 match self.inner(py).borrow().get(key.data(py)) {
67 match self.inner(py).borrow().get(key.data(py)) {
69 Some(entry) => {
68 Some(entry) => {
70 // Explicitly go through u8 first, then cast to
69 // Explicitly go through u8 first, then cast to
71 // platform-specific `c_char`.
70 // platform-specific `c_char`.
72 let state: u8 = entry.state.into();
71 let state: u8 = entry.state.into();
73 Ok(Some(decapsule_make_dirstate_tuple(py)?(
72 Ok(Some(decapsule_make_dirstate_tuple(py)?(
74 state as c_char,
73 state as c_char,
75 entry.mode,
74 entry.mode,
76 entry.size,
75 entry.size,
77 entry.mtime,
76 entry.mtime,
78 )))
77 )))
79 },
78 },
80 None => Ok(default)
79 None => Ok(default)
81 }
80 }
82 }
81 }
83
82
84 def addfile(
83 def addfile(
85 &self,
84 &self,
86 f: PyObject,
85 f: PyObject,
87 oldstate: PyObject,
86 oldstate: PyObject,
88 state: PyObject,
87 state: PyObject,
89 mode: PyObject,
88 mode: PyObject,
90 size: PyObject,
89 size: PyObject,
91 mtime: PyObject
90 mtime: PyObject
92 ) -> PyResult<PyObject> {
91 ) -> PyResult<PyObject> {
93 self.borrow_mut(py)?.add_file(
92 self.borrow_mut(py)?.add_file(
94 f.extract::<PyBytes>(py)?.data(py),
93 f.extract::<PyBytes>(py)?.data(py),
95 oldstate.extract::<PyBytes>(py)?.data(py)[0]
94 oldstate.extract::<PyBytes>(py)?.data(py)[0]
96 .try_into()
95 .try_into()
97 .map_err(|e: DirstateParseError| {
96 .map_err(|e: DirstateParseError| {
98 PyErr::new::<exc::ValueError, _>(py, e.to_string())
97 PyErr::new::<exc::ValueError, _>(py, e.to_string())
99 })?,
98 })?,
100 DirstateEntry {
99 DirstateEntry {
101 state: state.extract::<PyBytes>(py)?.data(py)[0]
100 state: state.extract::<PyBytes>(py)?.data(py)[0]
102 .try_into()
101 .try_into()
103 .map_err(|e: DirstateParseError| {
102 .map_err(|e: DirstateParseError| {
104 PyErr::new::<exc::ValueError, _>(py, e.to_string())
103 PyErr::new::<exc::ValueError, _>(py, e.to_string())
105 })?,
104 })?,
106 mode: mode.extract(py)?,
105 mode: mode.extract(py)?,
107 size: size.extract(py)?,
106 size: size.extract(py)?,
108 mtime: mtime.extract(py)?,
107 mtime: mtime.extract(py)?,
109 },
108 },
110 );
109 );
111 Ok(py.None())
110 Ok(py.None())
112 }
111 }
113
112
114 def removefile(
113 def removefile(
115 &self,
114 &self,
116 f: PyObject,
115 f: PyObject,
117 oldstate: PyObject,
116 oldstate: PyObject,
118 size: PyObject
117 size: PyObject
119 ) -> PyResult<PyObject> {
118 ) -> PyResult<PyObject> {
120 self.borrow_mut(py)?
119 self.borrow_mut(py)?
121 .remove_file(
120 .remove_file(
122 f.extract::<PyBytes>(py)?.data(py),
121 f.extract::<PyBytes>(py)?.data(py),
123 oldstate.extract::<PyBytes>(py)?.data(py)[0]
122 oldstate.extract::<PyBytes>(py)?.data(py)[0]
124 .try_into()
123 .try_into()
125 .map_err(|e: DirstateParseError| {
124 .map_err(|e: DirstateParseError| {
126 PyErr::new::<exc::ValueError, _>(py, e.to_string())
125 PyErr::new::<exc::ValueError, _>(py, e.to_string())
127 })?,
126 })?,
128 size.extract(py)?,
127 size.extract(py)?,
129 )
128 )
130 .or_else(|_| {
129 .or_else(|_| {
131 Err(PyErr::new::<exc::OSError, _>(
130 Err(PyErr::new::<exc::OSError, _>(
132 py,
131 py,
133 "Dirstate error".to_string(),
132 "Dirstate error".to_string(),
134 ))
133 ))
135 })?;
134 })?;
136 Ok(py.None())
135 Ok(py.None())
137 }
136 }
138
137
139 def dropfile(
138 def dropfile(
140 &self,
139 &self,
141 f: PyObject,
140 f: PyObject,
142 oldstate: PyObject
141 oldstate: PyObject
143 ) -> PyResult<PyBool> {
142 ) -> PyResult<PyBool> {
144 self.borrow_mut(py)?
143 self.borrow_mut(py)?
145 .drop_file(
144 .drop_file(
146 f.extract::<PyBytes>(py)?.data(py),
145 f.extract::<PyBytes>(py)?.data(py),
147 oldstate.extract::<PyBytes>(py)?.data(py)[0]
146 oldstate.extract::<PyBytes>(py)?.data(py)[0]
148 .try_into()
147 .try_into()
149 .map_err(|e: DirstateParseError| {
148 .map_err(|e: DirstateParseError| {
150 PyErr::new::<exc::ValueError, _>(py, e.to_string())
149 PyErr::new::<exc::ValueError, _>(py, e.to_string())
151 })?,
150 })?,
152 )
151 )
153 .and_then(|b| Ok(b.to_py_object(py)))
152 .and_then(|b| Ok(b.to_py_object(py)))
154 .or_else(|_| {
153 .or_else(|_| {
155 Err(PyErr::new::<exc::OSError, _>(
154 Err(PyErr::new::<exc::OSError, _>(
156 py,
155 py,
157 "Dirstate error".to_string(),
156 "Dirstate error".to_string(),
158 ))
157 ))
159 })
158 })
160 }
159 }
161
160
162 def clearambiguoustimes(
161 def clearambiguoustimes(
163 &self,
162 &self,
164 files: PyObject,
163 files: PyObject,
165 now: PyObject
164 now: PyObject
166 ) -> PyResult<PyObject> {
165 ) -> PyResult<PyObject> {
167 let files: PyResult<Vec<Vec<u8>>> = files
166 let files: PyResult<Vec<Vec<u8>>> = files
168 .iter(py)?
167 .iter(py)?
169 .map(|filename| {
168 .map(|filename| {
170 Ok(filename?.extract::<PyBytes>(py)?.data(py).to_owned())
169 Ok(filename?.extract::<PyBytes>(py)?.data(py).to_owned())
171 })
170 })
172 .collect();
171 .collect();
173 self.inner(py)
172 self.inner(py)
174 .borrow_mut()
173 .borrow_mut()
175 .clear_ambiguous_times(files?, now.extract(py)?);
174 .clear_ambiguous_times(files?, now.extract(py)?);
176 Ok(py.None())
175 Ok(py.None())
177 }
176 }
178
177
179 // TODO share the reference
178 // TODO share the reference
180 def nonnormalentries(&self) -> PyResult<PyObject> {
179 def nonnormalentries(&self) -> PyResult<PyObject> {
181 let (non_normal, other_parent) =
180 let (non_normal, other_parent) =
182 self.inner(py).borrow().non_normal_other_parent_entries();
181 self.inner(py).borrow().non_normal_other_parent_entries();
183
182
184 let locals = PyDict::new(py);
183 let locals = PyDict::new(py);
185 locals.set_item(
184 locals.set_item(
186 py,
185 py,
187 "non_normal",
186 "non_normal",
188 non_normal
187 non_normal
189 .iter()
188 .iter()
190 .map(|v| PyBytes::new(py, &v))
189 .map(|v| PyBytes::new(py, &v))
191 .collect::<Vec<PyBytes>>()
190 .collect::<Vec<PyBytes>>()
192 .to_py_object(py),
191 .to_py_object(py),
193 )?;
192 )?;
194 locals.set_item(
193 locals.set_item(
195 py,
194 py,
196 "other_parent",
195 "other_parent",
197 other_parent
196 other_parent
198 .iter()
197 .iter()
199 .map(|v| PyBytes::new(py, &v))
198 .map(|v| PyBytes::new(py, &v))
200 .collect::<Vec<PyBytes>>()
199 .collect::<Vec<PyBytes>>()
201 .to_py_object(py),
200 .to_py_object(py),
202 )?;
201 )?;
203
202
204 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
203 py.eval("set(non_normal), set(other_parent)", None, Some(&locals))
205 }
204 }
206
205
207 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
206 def hastrackeddir(&self, d: PyObject) -> PyResult<PyBool> {
208 let d = d.extract::<PyBytes>(py)?;
207 let d = d.extract::<PyBytes>(py)?;
209 Ok(self
208 Ok(self
210 .inner(py)
209 .inner(py)
211 .borrow_mut()
210 .borrow_mut()
212 .has_tracked_dir(d.data(py))
211 .has_tracked_dir(d.data(py))
213 .to_py_object(py))
212 .to_py_object(py))
214 }
213 }
215
214
216 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
215 def hasdir(&self, d: PyObject) -> PyResult<PyBool> {
217 let d = d.extract::<PyBytes>(py)?;
216 let d = d.extract::<PyBytes>(py)?;
218 Ok(self
217 Ok(self
219 .inner(py)
218 .inner(py)
220 .borrow_mut()
219 .borrow_mut()
221 .has_dir(d.data(py))
220 .has_dir(d.data(py))
222 .to_py_object(py))
221 .to_py_object(py))
223 }
222 }
224
223
225 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
224 def parents(&self, st: PyObject) -> PyResult<PyTuple> {
226 self.inner(py)
225 self.inner(py)
227 .borrow_mut()
226 .borrow_mut()
228 .parents(st.extract::<PyBytes>(py)?.data(py))
227 .parents(st.extract::<PyBytes>(py)?.data(py))
229 .and_then(|d| {
228 .and_then(|d| {
230 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
229 Ok((PyBytes::new(py, &d.p1), PyBytes::new(py, &d.p2))
231 .to_py_object(py))
230 .to_py_object(py))
232 })
231 })
233 .or_else(|_| {
232 .or_else(|_| {
234 Err(PyErr::new::<exc::OSError, _>(
233 Err(PyErr::new::<exc::OSError, _>(
235 py,
234 py,
236 "Dirstate error".to_string(),
235 "Dirstate error".to_string(),
237 ))
236 ))
238 })
237 })
239 }
238 }
240
239
241 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
240 def setparents(&self, p1: PyObject, p2: PyObject) -> PyResult<PyObject> {
242 // TODO: don't panic; raise Python exception instead.
241 let p1 = extract_node_id(py, &p1)?;
243 let p1 = p1.extract::<PyBytes>(py)?.data(py).try_into().unwrap();
242 let p2 = extract_node_id(py, &p2)?;
244 let p2 = p2.extract::<PyBytes>(py)?.data(py).try_into().unwrap();
245
243
246 self.inner(py)
244 self.inner(py)
247 .borrow_mut()
245 .borrow_mut()
248 .set_parents(DirstateParents { p1, p2 });
246 .set_parents(DirstateParents { p1, p2 });
249 Ok(py.None())
247 Ok(py.None())
250 }
248 }
251
249
252 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
250 def read(&self, st: PyObject) -> PyResult<Option<PyObject>> {
253 match self
251 match self
254 .inner(py)
252 .inner(py)
255 .borrow_mut()
253 .borrow_mut()
256 .read(st.extract::<PyBytes>(py)?.data(py))
254 .read(st.extract::<PyBytes>(py)?.data(py))
257 {
255 {
258 Ok(Some(parents)) => Ok(Some(
256 Ok(Some(parents)) => Ok(Some(
259 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
257 (PyBytes::new(py, &parents.p1), PyBytes::new(py, &parents.p2))
260 .to_py_object(py)
258 .to_py_object(py)
261 .into_object(),
259 .into_object(),
262 )),
260 )),
263 Ok(None) => Ok(Some(py.None())),
261 Ok(None) => Ok(Some(py.None())),
264 Err(_) => Err(PyErr::new::<exc::OSError, _>(
262 Err(_) => Err(PyErr::new::<exc::OSError, _>(
265 py,
263 py,
266 "Dirstate error".to_string(),
264 "Dirstate error".to_string(),
267 )),
265 )),
268 }
266 }
269 }
267 }
270 def write(
268 def write(
271 &self,
269 &self,
272 p1: PyObject,
270 p1: PyObject,
273 p2: PyObject,
271 p2: PyObject,
274 now: PyObject
272 now: PyObject
275 ) -> PyResult<PyBytes> {
273 ) -> PyResult<PyBytes> {
276 let now = Duration::new(now.extract(py)?, 0);
274 let now = Duration::new(now.extract(py)?, 0);
277 let parents = DirstateParents {
275 let parents = DirstateParents {
278 // TODO: don't panic; raise Python exception instead.
276 p1: extract_node_id(py, &p1)?,
279 p1: p1.extract::<PyBytes>(py)?.data(py).try_into().unwrap(),
277 p2: extract_node_id(py, &p2)?,
280 p2: p2.extract::<PyBytes>(py)?.data(py).try_into().unwrap(),
281 };
278 };
282
279
283 match self.borrow_mut(py)?.pack(parents, now) {
280 match self.borrow_mut(py)?.pack(parents, now) {
284 Ok(packed) => Ok(PyBytes::new(py, &packed)),
281 Ok(packed) => Ok(PyBytes::new(py, &packed)),
285 Err(_) => Err(PyErr::new::<exc::OSError, _>(
282 Err(_) => Err(PyErr::new::<exc::OSError, _>(
286 py,
283 py,
287 "Dirstate error".to_string(),
284 "Dirstate error".to_string(),
288 )),
285 )),
289 }
286 }
290 }
287 }
291
288
292 def filefoldmapasdict(&self) -> PyResult<PyDict> {
289 def filefoldmapasdict(&self) -> PyResult<PyDict> {
293 let dict = PyDict::new(py);
290 let dict = PyDict::new(py);
294 for (key, value) in
291 for (key, value) in
295 self.borrow_mut(py)?.build_file_fold_map().iter()
292 self.borrow_mut(py)?.build_file_fold_map().iter()
296 {
293 {
297 dict.set_item(py, key, value)?;
294 dict.set_item(py, key, value)?;
298 }
295 }
299 Ok(dict)
296 Ok(dict)
300 }
297 }
301
298
302 def __len__(&self) -> PyResult<usize> {
299 def __len__(&self) -> PyResult<usize> {
303 Ok(self.inner(py).borrow().len())
300 Ok(self.inner(py).borrow().len())
304 }
301 }
305
302
306 def __contains__(&self, key: PyObject) -> PyResult<bool> {
303 def __contains__(&self, key: PyObject) -> PyResult<bool> {
307 let key = key.extract::<PyBytes>(py)?;
304 let key = key.extract::<PyBytes>(py)?;
308 Ok(self.inner(py).borrow().contains_key(key.data(py)))
305 Ok(self.inner(py).borrow().contains_key(key.data(py)))
309 }
306 }
310
307
311 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
308 def __getitem__(&self, key: PyObject) -> PyResult<PyObject> {
312 let key = key.extract::<PyBytes>(py)?;
309 let key = key.extract::<PyBytes>(py)?;
313 let key = key.data(py);
310 let key = key.data(py);
314 match self.inner(py).borrow().get(key) {
311 match self.inner(py).borrow().get(key) {
315 Some(entry) => {
312 Some(entry) => {
316 // Explicitly go through u8 first, then cast to
313 // Explicitly go through u8 first, then cast to
317 // platform-specific `c_char`.
314 // platform-specific `c_char`.
318 let state: u8 = entry.state.into();
315 let state: u8 = entry.state.into();
319 Ok(decapsule_make_dirstate_tuple(py)?(
316 Ok(decapsule_make_dirstate_tuple(py)?(
320 state as c_char,
317 state as c_char,
321 entry.mode,
318 entry.mode,
322 entry.size,
319 entry.size,
323 entry.mtime,
320 entry.mtime,
324 ))
321 ))
325 },
322 },
326 None => Err(PyErr::new::<exc::KeyError, _>(
323 None => Err(PyErr::new::<exc::KeyError, _>(
327 py,
324 py,
328 String::from_utf8_lossy(key),
325 String::from_utf8_lossy(key),
329 )),
326 )),
330 }
327 }
331 }
328 }
332
329
333 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
330 def keys(&self) -> PyResult<DirstateMapKeysIterator> {
334 DirstateMapKeysIterator::from_inner(
331 DirstateMapKeysIterator::from_inner(
335 py,
332 py,
336 Some(DirstateMapLeakedRef::new(py, &self)),
333 Some(DirstateMapLeakedRef::new(py, &self)),
337 Box::new(self.leak_immutable(py)?.iter()),
334 Box::new(self.leak_immutable(py)?.iter()),
338 )
335 )
339 }
336 }
340
337
341 def items(&self) -> PyResult<DirstateMapItemsIterator> {
338 def items(&self) -> PyResult<DirstateMapItemsIterator> {
342 DirstateMapItemsIterator::from_inner(
339 DirstateMapItemsIterator::from_inner(
343 py,
340 py,
344 Some(DirstateMapLeakedRef::new(py, &self)),
341 Some(DirstateMapLeakedRef::new(py, &self)),
345 Box::new(self.leak_immutable(py)?.iter()),
342 Box::new(self.leak_immutable(py)?.iter()),
346 )
343 )
347 }
344 }
348
345
349 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
346 def __iter__(&self) -> PyResult<DirstateMapKeysIterator> {
350 DirstateMapKeysIterator::from_inner(
347 DirstateMapKeysIterator::from_inner(
351 py,
348 py,
352 Some(DirstateMapLeakedRef::new(py, &self)),
349 Some(DirstateMapLeakedRef::new(py, &self)),
353 Box::new(self.leak_immutable(py)?.iter()),
350 Box::new(self.leak_immutable(py)?.iter()),
354 )
351 )
355 }
352 }
356
353
357 def getdirs(&self) -> PyResult<Dirs> {
354 def getdirs(&self) -> PyResult<Dirs> {
358 // TODO don't copy, share the reference
355 // TODO don't copy, share the reference
359 self.inner(py).borrow_mut().set_dirs();
356 self.inner(py).borrow_mut().set_dirs();
360 Dirs::from_inner(
357 Dirs::from_inner(
361 py,
358 py,
362 DirsMultiset::new(
359 DirsMultiset::new(
363 DirsIterable::Dirstate(&self.inner(py).borrow()),
360 DirsIterable::Dirstate(&self.inner(py).borrow()),
364 Some(EntryState::Removed),
361 Some(EntryState::Removed),
365 ),
362 ),
366 )
363 )
367 }
364 }
368 def getalldirs(&self) -> PyResult<Dirs> {
365 def getalldirs(&self) -> PyResult<Dirs> {
369 // TODO don't copy, share the reference
366 // TODO don't copy, share the reference
370 self.inner(py).borrow_mut().set_all_dirs();
367 self.inner(py).borrow_mut().set_all_dirs();
371 Dirs::from_inner(
368 Dirs::from_inner(
372 py,
369 py,
373 DirsMultiset::new(
370 DirsMultiset::new(
374 DirsIterable::Dirstate(&self.inner(py).borrow()),
371 DirsIterable::Dirstate(&self.inner(py).borrow()),
375 None,
372 None,
376 ),
373 ),
377 )
374 )
378 }
375 }
379
376
380 // TODO all copymap* methods, see docstring above
377 // TODO all copymap* methods, see docstring above
381 def copymapcopy(&self) -> PyResult<PyDict> {
378 def copymapcopy(&self) -> PyResult<PyDict> {
382 let dict = PyDict::new(py);
379 let dict = PyDict::new(py);
383 for (key, value) in self.inner(py).borrow().copy_map.iter() {
380 for (key, value) in self.inner(py).borrow().copy_map.iter() {
384 dict.set_item(py, PyBytes::new(py, key), PyBytes::new(py, value))?;
381 dict.set_item(py, PyBytes::new(py, key), PyBytes::new(py, value))?;
385 }
382 }
386 Ok(dict)
383 Ok(dict)
387 }
384 }
388
385
389 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
386 def copymapgetitem(&self, key: PyObject) -> PyResult<PyBytes> {
390 let key = key.extract::<PyBytes>(py)?;
387 let key = key.extract::<PyBytes>(py)?;
391 match self.inner(py).borrow().copy_map.get(key.data(py)) {
388 match self.inner(py).borrow().copy_map.get(key.data(py)) {
392 Some(copy) => Ok(PyBytes::new(py, copy)),
389 Some(copy) => Ok(PyBytes::new(py, copy)),
393 None => Err(PyErr::new::<exc::KeyError, _>(
390 None => Err(PyErr::new::<exc::KeyError, _>(
394 py,
391 py,
395 String::from_utf8_lossy(key.data(py)),
392 String::from_utf8_lossy(key.data(py)),
396 )),
393 )),
397 }
394 }
398 }
395 }
399 def copymap(&self) -> PyResult<CopyMap> {
396 def copymap(&self) -> PyResult<CopyMap> {
400 CopyMap::from_inner(py, self.clone_ref(py))
397 CopyMap::from_inner(py, self.clone_ref(py))
401 }
398 }
402
399
403 def copymaplen(&self) -> PyResult<usize> {
400 def copymaplen(&self) -> PyResult<usize> {
404 Ok(self.inner(py).borrow().copy_map.len())
401 Ok(self.inner(py).borrow().copy_map.len())
405 }
402 }
406 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
403 def copymapcontains(&self, key: PyObject) -> PyResult<bool> {
407 let key = key.extract::<PyBytes>(py)?;
404 let key = key.extract::<PyBytes>(py)?;
408 Ok(self.inner(py).borrow().copy_map.contains_key(key.data(py)))
405 Ok(self.inner(py).borrow().copy_map.contains_key(key.data(py)))
409 }
406 }
410 def copymapget(
407 def copymapget(
411 &self,
408 &self,
412 key: PyObject,
409 key: PyObject,
413 default: Option<PyObject>
410 default: Option<PyObject>
414 ) -> PyResult<Option<PyObject>> {
411 ) -> PyResult<Option<PyObject>> {
415 let key = key.extract::<PyBytes>(py)?;
412 let key = key.extract::<PyBytes>(py)?;
416 match self.inner(py).borrow().copy_map.get(key.data(py)) {
413 match self.inner(py).borrow().copy_map.get(key.data(py)) {
417 Some(copy) => Ok(Some(PyBytes::new(py, copy).into_object())),
414 Some(copy) => Ok(Some(PyBytes::new(py, copy).into_object())),
418 None => Ok(default),
415 None => Ok(default),
419 }
416 }
420 }
417 }
421 def copymapsetitem(
418 def copymapsetitem(
422 &self,
419 &self,
423 key: PyObject,
420 key: PyObject,
424 value: PyObject
421 value: PyObject
425 ) -> PyResult<PyObject> {
422 ) -> PyResult<PyObject> {
426 let key = key.extract::<PyBytes>(py)?;
423 let key = key.extract::<PyBytes>(py)?;
427 let value = value.extract::<PyBytes>(py)?;
424 let value = value.extract::<PyBytes>(py)?;
428 self.inner(py)
425 self.inner(py)
429 .borrow_mut()
426 .borrow_mut()
430 .copy_map
427 .copy_map
431 .insert(key.data(py).to_vec(), value.data(py).to_vec());
428 .insert(key.data(py).to_vec(), value.data(py).to_vec());
432 Ok(py.None())
429 Ok(py.None())
433 }
430 }
434 def copymappop(
431 def copymappop(
435 &self,
432 &self,
436 key: PyObject,
433 key: PyObject,
437 default: Option<PyObject>
434 default: Option<PyObject>
438 ) -> PyResult<Option<PyObject>> {
435 ) -> PyResult<Option<PyObject>> {
439 let key = key.extract::<PyBytes>(py)?;
436 let key = key.extract::<PyBytes>(py)?;
440 match self.inner(py).borrow_mut().copy_map.remove(key.data(py)) {
437 match self.inner(py).borrow_mut().copy_map.remove(key.data(py)) {
441 Some(_) => Ok(None),
438 Some(_) => Ok(None),
442 None => Ok(default),
439 None => Ok(default),
443 }
440 }
444 }
441 }
445
442
446 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
443 def copymapiter(&self) -> PyResult<CopyMapKeysIterator> {
447 CopyMapKeysIterator::from_inner(
444 CopyMapKeysIterator::from_inner(
448 py,
445 py,
449 Some(DirstateMapLeakedRef::new(py, &self)),
446 Some(DirstateMapLeakedRef::new(py, &self)),
450 Box::new(self.leak_immutable(py)?.copy_map.iter()),
447 Box::new(self.leak_immutable(py)?.copy_map.iter()),
451 )
448 )
452 }
449 }
453
450
454 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
451 def copymapitemsiter(&self) -> PyResult<CopyMapItemsIterator> {
455 CopyMapItemsIterator::from_inner(
452 CopyMapItemsIterator::from_inner(
456 py,
453 py,
457 Some(DirstateMapLeakedRef::new(py, &self)),
454 Some(DirstateMapLeakedRef::new(py, &self)),
458 Box::new(self.leak_immutable(py)?.copy_map.iter()),
455 Box::new(self.leak_immutable(py)?.copy_map.iter()),
459 )
456 )
460 }
457 }
461
458
462 });
459 });
463
460
464 impl DirstateMap {
461 impl DirstateMap {
465 fn translate_key(
462 fn translate_key(
466 py: Python,
463 py: Python,
467 res: (&Vec<u8>, &DirstateEntry),
464 res: (&Vec<u8>, &DirstateEntry),
468 ) -> PyResult<Option<PyBytes>> {
465 ) -> PyResult<Option<PyBytes>> {
469 Ok(Some(PyBytes::new(py, res.0)))
466 Ok(Some(PyBytes::new(py, res.0)))
470 }
467 }
471 fn translate_key_value(
468 fn translate_key_value(
472 py: Python,
469 py: Python,
473 res: (&Vec<u8>, &DirstateEntry),
470 res: (&Vec<u8>, &DirstateEntry),
474 ) -> PyResult<Option<(PyBytes, PyObject)>> {
471 ) -> PyResult<Option<(PyBytes, PyObject)>> {
475 let (f, entry) = res;
472 let (f, entry) = res;
476
473
477 // Explicitly go through u8 first, then cast to
474 // Explicitly go through u8 first, then cast to
478 // platform-specific `c_char`.
475 // platform-specific `c_char`.
479 let state: u8 = entry.state.into();
476 let state: u8 = entry.state.into();
480 Ok(Some((
477 Ok(Some((
481 PyBytes::new(py, f),
478 PyBytes::new(py, f),
482 decapsule_make_dirstate_tuple(py)?(
479 decapsule_make_dirstate_tuple(py)?(
483 state as c_char,
480 state as c_char,
484 entry.mode,
481 entry.mode,
485 entry.size,
482 entry.size,
486 entry.mtime,
483 entry.mtime,
487 ),
484 ),
488 )))
485 )))
489 }
486 }
490 }
487 }
491
488
492 py_shared_ref!(DirstateMap, RustDirstateMap, inner, DirstateMapLeakedRef,);
489 py_shared_ref!(DirstateMap, RustDirstateMap, inner, DirstateMapLeakedRef,);
493
490
494 py_shared_mapping_iterator!(
491 py_shared_mapping_iterator!(
495 DirstateMapKeysIterator,
492 DirstateMapKeysIterator,
496 DirstateMapLeakedRef,
493 DirstateMapLeakedRef,
497 Vec<u8>,
494 Vec<u8>,
498 DirstateEntry,
495 DirstateEntry,
499 DirstateMap::translate_key,
496 DirstateMap::translate_key,
500 Option<PyBytes>
497 Option<PyBytes>
501 );
498 );
502
499
503 py_shared_mapping_iterator!(
500 py_shared_mapping_iterator!(
504 DirstateMapItemsIterator,
501 DirstateMapItemsIterator,
505 DirstateMapLeakedRef,
502 DirstateMapLeakedRef,
506 Vec<u8>,
503 Vec<u8>,
507 DirstateEntry,
504 DirstateEntry,
508 DirstateMap::translate_key_value,
505 DirstateMap::translate_key_value,
509 Option<(PyBytes, PyObject)>
506 Option<(PyBytes, PyObject)>
510 );
507 );
508
509 fn extract_node_id(py: Python, obj: &PyObject) -> PyResult<[u8; PARENT_SIZE]> {
510 let bytes = obj.extract::<PyBytes>(py)?;
511 match bytes.data(py).try_into() {
512 Ok(s) => Ok(s),
513 Err(e) => Err(PyErr::new::<exc::ValueError, _>(py, e.to_string())),
514 }
515 }
General Comments 0
You need to be logged in to leave comments. Login now