rust_apt/acquire.rs
1//! File Acquiration
2//!
3//! The following was taken from libapt-pkg documentation.
4//!
5//! This module contains the Acquire system. It is responsible for bringing
6//! files into the local pathname space. It deals with URIs for files and
7//! URI handlers responsible for downloading or finding the URIs.
8//!
9//! Each file to download is represented by an Acquire::Item class subclassed
10//! into a specialization. The Item class can add itself to several URI
11//! acquire queues each prioritized by the download scheduler. When the
12//! system is run the proper URI handlers are spawned and the acquire
13//! queues are fed into the handlers by the schedular until the queues are
14//! empty. This allows for an Item to be downloaded from an alternate source
15//! if the first try turns out to fail. It also allows concurrent downloading
16//! of multiple items from multiple sources as well as dynamic balancing
17//! of load between the sources.
18//!
19//! Scheduling of downloads is done on a first ask first get basis. This
20//! preserves the order of the download as much as possible. And means the
21//! fastest source will tend to process the largest number of files.
22//!
23//! Internal methods and queues for performing gzip decompression,
24//! md5sum hashing and file copying are provided to allow items to apply
25//! a number of transformations to the data files they are working with.
26
27#[cxx::bridge]
28pub(crate) mod raw {
29 #[repr(u32)]
30 enum ItemState {
31 StatIdle,
32 StatFetching,
33 StatDone,
34 StatError,
35 StatAuthError,
36 StatTransientNetworkError,
37 }
38
39 unsafe extern "C++" {
40 include!("rust-apt/apt-pkg-c/acquire.h");
41 type AcqTextStatus;
42 type PkgAcquire;
43 type AcqWorker;
44 type ItemDesc;
45 type Item;
46 type ItemState;
47
48 type AcquireProgress<'a> = crate::progress::AcquireProgress<'a>;
49
50 /// A client-supplied unique identifier.
51 ///
52 /// APT progress reporting will store an ID as shown in "Get:42…".
53 pub fn id(self: &Item) -> u32;
54 /// `true`` if entire object has been successfully fetched.
55 pub fn complete(self: &Item) -> bool;
56 /// The size of the object to fetch.
57 pub fn file_size(self: &Item) -> u64;
58 /// Get the URI of the item.
59 pub fn uri(self: &Item) -> String;
60 /// The Destination file path.
61 pub fn dest_file(self: &Item) -> String;
62 /// The current status of this item.
63 pub fn status(self: &Item) -> ItemState;
64 /// Contains a textual description of the error encountered
65 /// if ItemState is StatError or StatAuthError.
66 pub fn error_text(self: &Item) -> String;
67 /// Contains the name of the subprocess that is operating on this item.
68 ///
69 /// For instance, "store", "gzip", "rred", "gpgv".
70 pub fn active_subprocess(self: &Item) -> String;
71 /// The acquire process with which this item is associated.
72 pub fn owner(self: &Item) -> UniquePtr<PkgAcquire>;
73
74 /// URI from which to download this item.
75 pub fn uri(self: &ItemDesc) -> String;
76 /// Description of this item.
77 pub fn description(self: &ItemDesc) -> String;
78 /// Shorter description of this item.
79 pub fn short_desc(self: &ItemDesc) -> String;
80 /// Underlying item which is to be downloaded.
81 pub fn owner(self: &ItemDesc) -> UniquePtr<Item>;
82
83 /// # Safety
84 ///
85 /// Before you do anything with AcqTextStatus you must set the callback.
86 ///
87 /// The returned UniquePtr cannot outlive the cache.
88 unsafe fn acquire_status() -> UniquePtr<AcqTextStatus>;
89 /// # Safety
90 ///
91 /// Setting the Callback requires a raw mutable pointer to
92 /// AcquireProgress.
93 ///
94 /// AcquireProgress must not be moved in memory or will segfault when
95 /// AcqTextStatus calls it.
96 unsafe fn set_callback(self: Pin<&mut AcqTextStatus>, progress: *mut AcquireProgress);
97
98 /// The number of bytes fetched as of the most recent call
99 /// to pkgAcquireStatus::Pulse, including local items.
100 pub fn current_cps(self: &AcqTextStatus) -> u64;
101 /// The amount of time that has elapsed since the download started.
102 pub fn elapsed_time(self: &AcqTextStatus) -> u64;
103 /// The total number of bytes accounted for by items that were
104 /// successfully fetched.
105 pub fn fetched_bytes(self: &AcqTextStatus) -> u64;
106 /// The number of bytes fetched as of the most recent call to
107 /// pkgAcquireStatus::Pulse, including local items.
108 pub fn current_bytes(self: &AcqTextStatus) -> u64;
109 /// The total number of bytes that need to be fetched.
110 ///
111 /// This member is inaccurate, as new items might be enqueued while the
112 /// download is in progress!
113 pub fn total_bytes(self: &AcqTextStatus) -> u64;
114 /// The estimated percentage of the download (0-100)
115 pub fn percent(self: &AcqTextStatus) -> f64;
116
117 /// The most recent status string received from the subprocess.
118 pub fn status(self: &AcqWorker) -> String;
119 /// The queue entry that is currently being downloaded.
120 pub fn item(self: &AcqWorker) -> Result<UniquePtr<ItemDesc>>;
121 /// How many bytes of the file have been downloaded.
122 ///
123 /// Zero if the current progress of the file cannot be determined.
124 pub fn current_size(self: &AcqWorker) -> u64;
125 /// The total number of bytes to be downloaded.
126 ///
127 /// Zero if the total size of the final is unknown.
128 pub fn total_size(self: &AcqWorker) -> u64;
129
130 // TODO: This should probably be unsafe, but not sure at the moment how to
131 // handle it I guess we would need to wrap PkgAcquire and AcqWorker so they can
132 // have proper lifetimes?
133
134 /// CxxVector of active workers
135 pub fn workers(self: &PkgAcquire) -> UniquePtr<CxxVector<AcqWorker>>;
136
137 /// Get the ItemDesc that contain the source list URIs
138 ///
139 /// # Safety
140 ///
141 /// You must not let these out of scope of PkgAcquire. SIGABRT.
142 unsafe fn uris(self: &PkgAcquire) -> UniquePtr<CxxVector<ItemDesc>>;
143
144 // It isn't clear that create_acquire should be unsafe.
145 // It doesn't segfault if you drop the Cache.
146 // But it does return a UniquePtr so I assume it is unsafe.
147
148 /// Create PkgAcquire.
149 ///
150 /// # Safety
151 ///
152 /// The returned UniquePtr cannot outlive the cache.
153 unsafe fn create_acquire() -> UniquePtr<PkgAcquire>;
154 }
155}