File: util.h

package info (click to toggle)
rust-rust-apt 0.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 620 kB
  • sloc: cpp: 703; makefile: 8
file content (82 lines) | stat: -rw-r--r-- 2,464 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <apt-pkg/algorithms.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/install-progress.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/version.h>
#include <cstdint>
#include <sstream>
#include "rust/cxx.h"

#include "types.h"

/// Internal Helper Functions.
/// Do not expose these on the Rust side - only for use on the C++ side.
///
/// Handle any apt errors and return result to rust.
inline void handle_errors() {
	// !_error->empty() will cause a Result when there are only warnings
	// Instead use PendingErr()
	// Actual handling of the errors is done in rust
	if (_error->PendingError()) { throw std::runtime_error("convert to AptErrors"); }
}

/// Handle the situation where a string is null and return a result to rust
inline const char* handle_str(const char* str) {
	if (!str || !strcmp(str, "")) { throw std::runtime_error("&str doesn't exist"); }
	return str;
}

/// Check if a string exists and return a Result to rust
inline String handle_string(std::string string) {
	if (string.empty()) { throw std::runtime_error("String doesn't exist"); }
	return string;
}

//////////////////////////////////
/// End Internal Helper Functions.
//////////////////////////////////

/// Compare two package version strings.
inline i32 cmp_versions(str ver1, str ver2) {
	if (!_system) { pkgInitSystem(*_config, _system); }

	const char* end1 = ver1.begin() + strlen(ver1.begin());
	const char* end2 = ver2.begin() + strlen(ver2.begin());

	return _system->VS->DoCmpVersion(ver1.begin(), end1, ver2.begin(), end2);
}

/// Return an APT-styled progress bar (`[####  ]`).
inline String get_apt_progress_string(f32 percent, u32 output_width) {
	return APT::Progress::PackageManagerFancy::GetTextProgressStr(percent, output_width);
}

/// Lock the APT lockfile.
inline void apt_lock() {
	_system->Lock();
	handle_errors();
}

/// Unlock the APT lockfile.
inline void apt_unlock() {
	// This can only throw an error that says "Not Locked"
	// By setting NoErrors true, this will return false instead
	// This is largely irrelevant and will be a void function
	_system->UnLock(true);
}

/// Lock the Dpkg lockfile.
inline void apt_lock_inner() {
	_system->LockInner();
	handle_errors();
}

/// Unlock the Dpkg lockfile.
inline void apt_unlock_inner() {
	// UnlockInner can not throw an error and always returns true.
	_system->UnLockInner();
}

/// Check if the lockfile is locked.
inline bool apt_is_locked() { return _system->IsLocked(); }