Debian Codegraph @

src/opts.rs

raw
1 //! Bindings to libgit2's git_libgit2_opts function.
2
3 use std::ffi::CString;
4 use std::ptr;
5
6 use crate::string_array::StringArray;
7 use crate::util::Binding;
8 use crate::{raw, Buf, ConfigLevel, Error, IntoCString, ObjectType};
9
10 /// Set the search path for a level of config data. The search path applied to
11 /// shared attributes and ignore files, too.
12 ///
13 /// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
14 /// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
15 ///
16 /// `path` lists directories delimited by `GIT_PATH_LIST_SEPARATOR`.
17 /// Use magic path `$PATH` to include the old value of the path
18 /// (if you want to prepend or append, for instance).
19 ///
20 /// This function is unsafe as it mutates the global state but cannot guarantee
21 /// thread-safety. It needs to be externally synchronized with calls to access
22 /// the global state.
23 pub unsafe fn set_search_path<P>(level: ConfigLevel, path: P) -> Result<(), Error>
24 where
25 P: IntoCString,
26 {
27 crate::init();
28 try_call!(raw::git_libgit2_opts(
29 raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
30 level as libc::c_int,
31 path.into_c_string()?.as_ptr()
32 ));
33 Ok(())
34 }
35
36 /// Reset the search path for a given level of config data to the default
37 /// (generally based on environment variables).
38 ///
39 /// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
40 /// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
41 ///
42 /// This function is unsafe as it mutates the global state but cannot guarantee
43 /// thread-safety. It needs to be externally synchronized with calls to access
44 /// the global state.
45 pub unsafe fn reset_search_path(level: ConfigLevel) -> Result<(), Error> {
46 crate::init();
47 try_call!(raw::git_libgit2_opts(
48 raw::GIT_OPT_SET_SEARCH_PATH as libc::c_int,
49 level as libc::c_int,
50 core::ptr::null::<u8>()
51 ));
52 Ok(())
53 }
54
55 /// Get the search path for a given level of config data.
56 ///
57 /// `level` must be one of [`ConfigLevel::System`], [`ConfigLevel::Global`],
58 /// [`ConfigLevel::XDG`], [`ConfigLevel::ProgramData`].
59 ///
60 /// This function is unsafe as it mutates the global state but cannot guarantee
61 /// thread-safety. It needs to be externally synchronized with calls to access
62 /// the global state.
63 pub unsafe fn get_search_path(level: ConfigLevel) -> Result<CString, Error> {
64 crate::init();
65 let buf = Buf::new();
66 try_call!(raw::git_libgit2_opts(
67 raw::GIT_OPT_GET_SEARCH_PATH as libc::c_int,
68 level as libc::c_int,
69 buf.raw() as *const _
70 ));
71 buf.into_c_string()
72 }
73
74 /// Controls whether or not libgit2 will cache loaded objects. Enabled by
75 /// default, but disabling this can improve performance and memory usage if
76 /// loading a large number of objects that will not be referenced again.
77 /// Disabling this will cause repository objects to clear their caches when next
78 /// accessed.
79 pub fn enable_caching(enabled: bool) {
80 crate::init();
81 let error = unsafe {
82 raw::git_libgit2_opts(
83 raw::GIT_OPT_ENABLE_CACHING as libc::c_int,
84 enabled as libc::c_int,
85 )
86 };
87 // This function cannot actually fail, but the function has an error return
88 // for other options that can.
89 debug_assert!(error >= 0);
90 }
91
92 /// Set the maximum data size for the given type of object to be considered
93 /// eligible for caching in memory. Setting to value to zero means that that
94 /// type of object will not be cached. Defaults to 0 for [`ObjectType::Blob`]
95 /// (i.e. won't cache blobs) and 4k for [`ObjectType::Commit`],
96 /// [`ObjectType::Tree`], and [`ObjectType::Tag`].
97 ///
98 /// `kind` must be one of [`ObjectType::Blob`], [`ObjectType::Commit`],
99 /// [`ObjectType::Tree`], and [`ObjectType::Tag`].
100 ///
101 /// # Safety
102 /// This function is modifying a C global without synchronization, so it is not
103 /// thread safe, and should only be called before any thread is spawned.
104 pub unsafe fn set_cache_object_limit(kind: ObjectType, size: libc::size_t) -> Result<(), Error> {
105 crate::init();
106 try_call!(raw::git_libgit2_opts(
107 raw::GIT_OPT_SET_CACHE_OBJECT_LIMIT as libc::c_int,
108 kind as libc::c_int,
109 size
110 ));
111 Ok(())
112 }
113
114 /// Set the maximum total data size that will be cached in memory across all
115 /// repositories before libgit2 starts evicting objects from the cache. This
116 /// is a soft limit, in that the library might briefly exceed it, but will start
117 /// aggressively evicting objects from cache when that happens. The default
118 /// cache size is 256MB.
119 ///
120 /// # Safety
121 /// This function is modifying a C global without synchronization, so it is not
122 /// thread safe, and should only be called before any thread is spawned.
123 pub unsafe fn set_cache_max_size(size: libc::ssize_t) -> Result<(), Error> {
124 crate::init();
125 try_call!(raw::git_libgit2_opts(
126 raw::GIT_OPT_SET_CACHE_MAX_SIZE as libc::c_int,
127 size
128 ));
129 Ok(())
130 }
131
132 /// Get the current bytes in cache and the maximum that would be allowed in the cache.
133 ///
134 /// # Safety
135 /// This function is reading a C global without synchronization, so it is not
136 /// thread safe, and should only be called before any thread is spawned.
137 pub unsafe fn get_cached_memory() -> Result<(libc::ssize_t, libc::ssize_t), Error> {
138 crate::init();
139 let mut current = 0;
140 let mut allowed = 0;
141 try_call!(raw::git_libgit2_opts(
142 raw::GIT_OPT_GET_CACHED_MEMORY as libc::c_int,
143 &mut current,
144 &mut allowed
145 ));
146 Ok((current, allowed))
147 }
148
149 /// Controls whether or not libgit2 will verify when writing an object that all
150 /// objects it references are valid. Enabled by default, but disabling this can
151 /// significantly improve performance, at the cost of potentially allowing the
152 /// creation of objects that reference invalid objects (due to programming
153 /// error or repository corruption).
154 pub fn strict_object_creation(enabled: bool) {
155 crate::init();
156 let error = unsafe {
157 raw::git_libgit2_opts(
158 raw::GIT_OPT_ENABLE_STRICT_OBJECT_CREATION as libc::c_int,
159 enabled as libc::c_int,
160 )
161 };
162 // This function cannot actually fail, but the function has an error return
163 // for other options that can.
164 debug_assert!(error >= 0);
165 }
166
167 /// Controls whether or not libgit2 will verify that objects loaded have the
168 /// expected hash. Enabled by default, but disabling this can significantly
169 /// improve performance, at the cost of relying on repository integrity
170 /// without checking it.
171 pub fn strict_hash_verification(enabled: bool) {
172 crate::init();
173 let error = unsafe {
174 raw::git_libgit2_opts(
175 raw::GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION as libc::c_int,
176 enabled as libc::c_int,
177 )
178 };
179 // This function cannot actually fail, but the function has an error return
180 // for other options that can.
181 debug_assert!(error >= 0);
182 }
183
184 /// Returns the list of git extensions that are supported. This is the list of
185 /// built-in extensions supported by libgit2 and custom extensions that have
186 /// been added with [`set_extensions`]. Extensions that have been negated will
187 /// not be returned.
188 ///
189 /// # Safety
190 ///
191 /// libgit2 stores user extensions in a static variable.
192 /// This function is effectively reading a `static mut` and should be treated as such
193 pub unsafe fn get_extensions() -> Result<StringArray, Error> {
194 crate::init();
195
196 let mut extensions = raw::git_strarray {
197 strings: ptr::null_mut(),
198 count: 0,
199 };
200
201 try_call!(raw::git_libgit2_opts(
202 raw::GIT_OPT_GET_EXTENSIONS as libc::c_int,
203 &mut extensions
204 ));
205
206 Ok(StringArray::from_raw(extensions))
207 }
208
209 /// Set that the given git extensions are supported by the caller. Extensions
210 /// supported by libgit2 may be negated by prefixing them with a `!`.
211 /// For example: setting extensions to `[ "!noop", "newext" ]` indicates that
212 /// the caller does not want to support repositories with the `noop` extension
213 /// but does want to support repositories with the `newext` extension.
214 ///
215 /// # Safety
216 ///
217 /// libgit2 stores user extensions in a static variable.
218 /// This function is effectively modifying a `static mut` and should be treated as such
219 pub unsafe fn set_extensions<E>(extensions: &[E]) -> Result<(), Error>
220 where
221 for<'x> &'x E: IntoCString,
222 {
223 crate::init();
224
225 let extensions = extensions
226 .iter()
227 .map(|e| e.into_c_string())
228 .collect::<Result<Vec<_>, _>>()?;
229
230 let extension_ptrs = extensions.iter().map(|e| e.as_ptr()).collect::<Vec<_>>();
231
232 try_call!(raw::git_libgit2_opts(
233 raw::GIT_OPT_SET_EXTENSIONS as libc::c_int,
234 extension_ptrs.as_ptr(),
235 extension_ptrs.len() as libc::size_t
236 ));
237
238 Ok(())
239 }
240
241 /// Set whether or not to verify ownership before performing a repository.
242 /// Enabled by default, but disabling this can lead to code execution vulnerabilities.
243 pub unsafe fn set_verify_owner_validation(enabled: bool) -> Result<(), Error> {
244 crate::init();
245 let error = raw::git_libgit2_opts(
246 raw::GIT_OPT_SET_OWNER_VALIDATION as libc::c_int,
247 enabled as libc::c_int,
248 );
249 // This function cannot actually fail, but the function has an error return
250 // for other options that can.
251 debug_assert!(error >= 0);
252 Ok(())
253 }
254
255 /// Set the SSL certificate-authority location to `file`. `file` is the location
256 /// of a file containing several certificates concatenated together.
257 pub unsafe fn set_ssl_cert_file<P>(file: P) -> Result<(), Error>
258 where
259 P: IntoCString,
260 {
261 crate::init();
262
263 unsafe {
264 try_call!(raw::git_libgit2_opts(
265 raw::GIT_OPT_SET_SSL_CERT_LOCATIONS as libc::c_int,
266 file.into_c_string()?.as_ptr(),
267 core::ptr::null::<libc::c_char>()
268 ));
269 }
270
271 Ok(())
272 }
273
274 /// Set the SSL certificate-authority location to `path`. `path` is the location
275 /// of a directory holding several certificates, one per file.
276 pub unsafe fn set_ssl_cert_dir<P>(path: P) -> Result<(), Error>
277 where
278 P: IntoCString,
279 {
280 crate::init();
281
282 unsafe {
283 try_call!(raw::git_libgit2_opts(
284 raw::GIT_OPT_SET_SSL_CERT_LOCATIONS as libc::c_int,
285 core::ptr::null::<libc::c_char>(),
286 path.into_c_string()?.as_ptr()
287 ));
288 }
289
290 Ok(())
291 }
292
293 /// Get the maximum mmap window size
294 ///
295 /// # Safety
296 /// This function is reading a C global without synchronization, so it is not
297 /// thread safe, and should only be called before any thread is spawned.
298 pub unsafe fn get_mwindow_size() -> Result<libc::size_t, Error> {
299 crate::init();
300
301 let mut size = 0;
302
303 try_call!(raw::git_libgit2_opts(
304 raw::GIT_OPT_GET_MWINDOW_SIZE as libc::c_int,
305 &mut size
306 ));
307
308 Ok(size)
309 }
310
311 /// Set the maximum mmap window size
312 ///
313 /// # Safety
314 /// This function is modifying a C global without synchronization, so it is not
315 /// thread safe, and should only be called before any thread is spawned.
316 pub unsafe fn set_mwindow_size(size: libc::size_t) -> Result<(), Error> {
317 crate::init();
318
319 try_call!(raw::git_libgit2_opts(
320 raw::GIT_OPT_SET_MWINDOW_SIZE as libc::c_int,
321 size
322 ));
323
324 Ok(())
325 }
326
327 /// Get the maximum memory that will be mapped in total by the library
328 ///
329 /// # Safety
330 /// This function is reading a C global without synchronization, so it is not
331 /// thread safe, and should only be called before any thread is spawned.
332 pub unsafe fn get_mwindow_mapped_limit() -> Result<libc::size_t, Error> {
333 crate::init();
334
335 let mut limit = 0;
336
337 try_call!(raw::git_libgit2_opts(
338 raw::GIT_OPT_GET_MWINDOW_MAPPED_LIMIT as libc::c_int,
339 &mut limit
340 ));
341
342 Ok(limit)
343 }
344
345 /// Set the maximum amount of memory that can be mapped at any time
346 /// by the library.
347 ///
348 /// # Safety
349 /// This function is modifying a C global without synchronization, so it is not
350 /// thread safe, and should only be called before any thread is spawned.
351 pub unsafe fn set_mwindow_mapped_limit(limit: libc::size_t) -> Result<(), Error> {
352 crate::init();
353
354 try_call!(raw::git_libgit2_opts(
355 raw::GIT_OPT_SET_MWINDOW_MAPPED_LIMIT as libc::c_int,
356 limit
357 ));
358
359 Ok(())
360 }
361
362 /// Get the maximum number of files that will be mapped at any time by the
363 /// library.
364 ///
365 /// # Safety
366 /// This function is reading a C global without synchronization, so it is not
367 /// thread safe, and should only be called before any thread is spawned.
368 pub unsafe fn get_mwindow_file_limit() -> Result<libc::size_t, Error> {
369 crate::init();
370
371 let mut limit = 0;
372
373 try_call!(raw::git_libgit2_opts(
374 raw::GIT_OPT_GET_MWINDOW_FILE_LIMIT as libc::c_int,
375 &mut limit
376 ));
377
378 Ok(limit)
379 }
380
381 /// Set the maximum number of files that can be mapped at any time
382 /// by the library. The default (0) is unlimited.
383 ///
384 /// # Safety
385 /// This function is modifying a C global without synchronization, so it is not
386 /// thread safe, and should only be called before any thread is spawned.
387 pub unsafe fn set_mwindow_file_limit(limit: libc::size_t) -> Result<(), Error> {
388 crate::init();
389
390 try_call!(raw::git_libgit2_opts(
391 raw::GIT_OPT_SET_MWINDOW_FILE_LIMIT as libc::c_int,
392 limit
393 ));
394
395 Ok(())
396 }
397
398 /// Get server connect timeout in milliseconds
399 ///
400 /// # Safety
401 /// This function is modifying a C global without synchronization, so it is not
402 /// thread safe, and should only be called before any thread is spawned.
403 pub unsafe fn get_server_connect_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
404 crate::init();
405
406 let mut server_connect_timeout = 0;
407
408 try_call!(raw::git_libgit2_opts(
409 raw::GIT_OPT_GET_SERVER_CONNECT_TIMEOUT as libc::c_int,
410 &mut server_connect_timeout
411 ));
412
413 Ok(server_connect_timeout)
414 }
415
416 /// Set server connect timeout in milliseconds
417 ///
418 /// # Safety
419 /// This function is modifying a C global without synchronization, so it is not
420 /// thread safe, and should only be called before any thread is spawned.
421 pub unsafe fn set_server_connect_timeout_in_milliseconds(
422 timeout: libc::c_int,
423 ) -> Result<(), Error> {
424 crate::init();
425
426 let error = raw::git_libgit2_opts(
427 raw::GIT_OPT_SET_SERVER_CONNECT_TIMEOUT as libc::c_int,
428 timeout,
429 );
430 // This function cannot actually fail, but the function has an error return
431 // for other options that can.
432 debug_assert!(error >= 0);
433
434 Ok(())
435 }
436
437 /// Get server timeout in milliseconds
438 ///
439 /// # Safety
440 /// This function is modifying a C global without synchronization, so it is not
441 /// thread safe, and should only be called before any thread is spawned.
442 pub unsafe fn get_server_timeout_in_milliseconds() -> Result<libc::c_int, Error> {
443 crate::init();
444
445 let mut server_timeout = 0;
446
447 try_call!(raw::git_libgit2_opts(
448 raw::GIT_OPT_GET_SERVER_TIMEOUT as libc::c_int,
449 &mut server_timeout
450 ));
451
452 Ok(server_timeout)
453 }
454
455 /// Set server timeout in milliseconds
456 ///
457 /// # Safety
458 /// This function is modifying a C global without synchronization, so it is not
459 /// thread safe, and should only be called before any thread is spawned.
460 pub unsafe fn set_server_timeout_in_milliseconds(timeout: libc::c_int) -> Result<(), Error> {
461 crate::init();
462
463 let error = raw::git_libgit2_opts(
464 raw::GIT_OPT_SET_SERVER_TIMEOUT as libc::c_int,
465 timeout as libc::c_int,
466 );
467 // This function cannot actually fail, but the function has an error return
468 // for other options that can.
469 debug_assert!(error >= 0);
470
471 Ok(())
472 }
473
474 #[cfg(test)]
475 mod test {
476 use super::*;
477
478 #[test]
479 fn smoke() {
480 strict_hash_verification(false);
481 }
482
483 #[test]
484 fn mwindow_size() {
485 unsafe {
486 assert!(set_mwindow_size(1024).is_ok());
487 assert!(get_mwindow_size().unwrap() == 1024);
488 }
489 }
490
491 #[test]
492 fn mwindow_mapped_limit() {
493 unsafe {
494 assert!(set_mwindow_mapped_limit(1024).is_ok());
495 assert!(get_mwindow_mapped_limit().unwrap() == 1024);
496 }
497 }
498
499 #[test]
500 fn mwindow_file_limit() {
501 unsafe {
502 assert!(set_mwindow_file_limit(1024).is_ok());
503 assert!(get_mwindow_file_limit().unwrap() == 1024);
504 }
505 }
506
507 #[test]
508 fn server_connect_timeout() {
509 unsafe {
510 assert!(set_server_connect_timeout_in_milliseconds(5000).is_ok());
511 assert!(get_server_connect_timeout_in_milliseconds().unwrap() == 5000);
512 }
513 }
514
515 #[test]
516 fn server_timeout() {
517 unsafe {
518 assert!(set_server_timeout_in_milliseconds(10_000).is_ok());
519 assert!(get_server_timeout_in_milliseconds().unwrap() == 10_000);
520 }
521 }
522
523 #[test]
524 fn cache_size() {
525 unsafe {
526 assert!(set_cache_max_size(20 * 1024 * 1024).is_ok());
527 assert!(get_cached_memory().is_ok_and(|m| m.1 == 20 * 1024 * 1024));
528 }
529 }
530 }