Debian Codegraph @

apt-pkg-c/util.h

raw symbols SCIP
1 #pragma once
2 #include <apt-pkg/algorithms.h>
3 #include <apt-pkg/cachefile.h>
4 #include <apt-pkg/install-progress.h>
5 #include <apt-pkg/pkgsystem.h>
6 #include <apt-pkg/version.h>
7 #include <cstdint>
8 #include <sstream>
9 #include "rust/cxx.h"
10
11 #include "types.h"
12
13 /// Internal Helper Functions.
14 /// Do not expose these on the Rust side - only for use on the C++ side.
15 ///
16 /// Handle any apt errors and return result to rust.
17 inline void handle_errors() {
18 // !_error->empty() will cause a Result when there are only warnings
19 // Instead use PendingErr()
20 // Actual handling of the errors is done in rust
21 if (_error->PendingError()) { throw std::runtime_error("convert to AptErrors"); }
22 }
23
24 /// Handle the situation where a string is null and return a result to rust
25 inline const char* handle_str(const char* str) {
26 if (!str || !strcmp(str, "")) { throw std::runtime_error("&str doesn't exist"); }
27 return str;
28 }
29
30 /// Check if a string exists and return a Result to rust
31 inline String handle_string(std::string string) {
32 if (string.empty()) { throw std::runtime_error("String doesn't exist"); }
33 return string;
34 }
35
36 //////////////////////////////////
37 /// End Internal Helper Functions.
38 //////////////////////////////////
39
40 /// Compare two package version strings.
41 inline i32 cmp_versions(str ver1, str ver2) {
42 if (!_system) { pkgInitSystem(*_config, _system); }
43
44 const char* end1 = ver1.begin() + strlen(ver1.begin());
45 const char* end2 = ver2.begin() + strlen(ver2.begin());
46
47 return _system->VS->DoCmpVersion(ver1.begin(), end1, ver2.begin(), end2);
48 }
49
50 /// Return an APT-styled progress bar (`[#### ]`).
51 inline String get_apt_progress_string(f32 percent, u32 output_width) {
52 return APT::Progress::PackageManagerFancy::GetTextProgressStr(percent, output_width);
53 }
54
55 /// Lock the APT lockfile.
56 inline void apt_lock() {
57 _system->Lock();
58 handle_errors();
59 }
60
61 /// Unlock the APT lockfile.
62 inline void apt_unlock() {
63 // This can only throw an error that says "Not Locked"
64 // By setting NoErrors true, this will return false instead
65 // This is largely irrelevant and will be a void function
66 _system->UnLock(true);
67 }
68
69 /// Lock the Dpkg lockfile.
70 inline void apt_lock_inner() {
71 _system->LockInner();
72 handle_errors();
73 }
74
75 /// Unlock the Dpkg lockfile.
76 inline void apt_unlock_inner() {
77 // UnlockInner can not throw an error and always returns true.
78 _system->UnLockInner();
79 }
80
81 /// Check if the lockfile is locked.
82 inline bool apt_is_locked() { return _system->IsLocked(); }