apt 3.0.3
commandline package manager
acquire.h
Go to the documentation of this file.
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3/* ######################################################################
4
5 Acquire - File Acquiration
6
7 This module contains the Acquire system. It is responsible for bringing
8 files into the local pathname space. It deals with URIs for files and
9 URI handlers responsible for downloading or finding the URIs.
10
11 Each file to download is represented by an Acquire::Item class subclassed
12 into a specialization. The Item class can add itself to several URI
13 acquire queues each prioritized by the download scheduler. When the
14 system is run the proper URI handlers are spawned and the acquire
15 queues are fed into the handlers by the schedular until the queues are
16 empty. This allows for an Item to be downloaded from an alternate source
17 if the first try turns out to fail. It also allows concurrent downloading
18 of multiple items from multiple sources as well as dynamic balancing
19 of load between the sources.
20
21 Scheduling of downloads is done on a first ask first get basis. This
22 preserves the order of the download as much as possible. And means the
23 fastest source will tend to process the largest number of files.
24
25 Internal methods and queues for performing gzip decompression,
26 md5sum hashing and file copying are provided to allow items to apply
27 a number of transformations to the data files they are working with.
28
29 ##################################################################### */
30 /*}}}*/
31
/*}}}*/
57
65#ifndef PKGLIB_ACQUIRE_H
66#define PKGLIB_ACQUIRE_H
67
68#include <apt-pkg/hashes.h>
69#include <apt-pkg/macros.h>
70#include <apt-pkg/weakptr.h>
71
72#include <chrono>
73#include <string>
74#include <vector>
75
76#include <cstddef>
77#include <sys/select.h>
78#include <sys/time.h>
79
80
81
83class metaIndex;
84
93class APT_PUBLIC pkgAcquire
94{
95 private:
97 using clock = std::chrono::steady_clock;
99 using time_point = std::chrono::time_point<clock>;
101 int LockFD;
103 void * const d;
104
105 public:
106
107 class Item;
108 class Queue;
109 class Worker;
110 struct MethodConfig;
111 struct ItemDesc;
112 friend class Item;
113 friend class pkgAcqMetaBase;
114 friend class Queue;
115
116 typedef std::vector<Item *>::iterator ItemIterator;
117 typedef std::vector<Item *>::const_iterator ItemCIterator;
118
119 protected:
120
126 std::vector<Item *> Items;
127
133 Queue *Queues;
134
140 Worker *Workers;
141
152 MethodConfig *Configs;
153
155 pkgAcquireStatus *Log;
156
158 unsigned long ToFetch;
159
160 // Configurable parameters for the scheduler
161
171 QueueAccess} QueueMode;
172
174 bool const Debug;
177
179 void Add(Item *Item);
180
182 void Remove(Item *Item);
183
185 void Add(Worker *Work);
186
188 void Remove(Worker *Work);
189
196 void Enqueue(ItemDesc &Item);
197
199 void Dequeue(Item *Item);
200
211 std::string QueueName(std::string URI,MethodConfig const *&Config);
212
227 virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
228
241 virtual bool RunFds(fd_set *RSet,fd_set *WSet);
242
249 void Bump();
250
251 public:
252
259 MethodConfig *GetConfig(std::string Access);
260
265
268
272 Cancelled};
273
285 RunResult Run(int PulseInterval=500000);
286
290 void Shutdown();
291
296 inline Worker *WorkersBegin() {return Workers;};
297
303 Worker *WorkerStep(Worker *I) APT_PURE;
304
306 inline ItemIterator ItemsBegin() {return Items.begin();};
307 inline ItemCIterator ItemsBegin() const {return Items.begin();};
308
310 inline ItemIterator ItemsEnd() {return Items.end();};
311 inline ItemCIterator ItemsEnd() const {return Items.end();};
312
313 // Iterate over queued Item URIs
314 class UriIterator;
320 UriIterator UriBegin();
322 UriIterator UriEnd();
323
332 bool Clean(std::string Dir);
333
340 bool CleanLists(std::string const &Dir);
341
345 unsigned long long TotalNeeded();
346
350 unsigned long long FetchNeeded();
351
355 unsigned long long PartialPresent();
356
357 void SetLog(pkgAcquireStatus *Progress) { Log = Progress; }
358
366 bool GetLock(std::string const &Lock);
367
369 explicit pkgAcquire(pkgAcquireStatus *Log);
370 pkgAcquire();
371
377 virtual ~pkgAcquire();
378
379 APT_HIDDEN static std::string URIEncode(std::string const &part);
380
381 private:
382 APT_HIDDEN void Initialize();
383};
384
390struct APT_PUBLIC pkgAcquire::ItemDesc : public WeakPointable
391{
393 std::string URI;
395 std::string Description;
397 std::string ShortDesc;
399 Item *Owner;
400};
401 /*}}}*/
406class APT_PUBLIC pkgAcquire::Queue
407{
408 friend class pkgAcquire;
409 friend class pkgAcquire::UriIterator;
410 friend class pkgAcquire::Worker;
411
413 void * const d;
414
416 Queue *Next;
417
418 protected:
419
421 struct QItem : public ItemDesc
422 {
427
429 std::vector<Item*> Owners;
430
434 unsigned long long CurrentSize = 0;
435
439 unsigned long long TotalSize = 0;
440
444 unsigned long long ResumePoint = 0;
445
446 typedef std::vector<Item*>::const_iterator owner_iterator;
447
451 void operator =(pkgAcquire::ItemDesc const &I)
452 {
453 URI = I.URI;
454 Description = I.Description;
455 ShortDesc = I.ShortDesc;
456 Owners.clear();
457 Owners.push_back(I.Owner);
458 Owner = I.Owner;
459 };
460
463
465 unsigned long long GetMaximumSize() const;
466
469
471 std::string Custom600Headers() const;
473 int APT_HIDDEN GetPriority() const;
475 time_point APT_HIDDEN GetFetchAfter() const;
476 };
477
479 std::string Name;
480
486
497
500
504 signed long PipeDepth;
505
509 unsigned long MaxPipeDepth;
510
511 public:
512
518 bool Enqueue(ItemDesc &Item);
519
524 bool Dequeue(Item *Owner);
525
534 QItem *FindItem(std::string URI,pkgAcquire::Worker *Owner) APT_PURE;
535
540 bool ItemStart(QItem *Itm,unsigned long long Size);
541
552 bool ItemDone(QItem *Itm);
553
561 bool Startup();
562
572 bool Shutdown(bool Final);
573
578 bool Cycle();
579
590 void Bump();
591
597 Queue(std::string const &Name,pkgAcquire * const Owner);
598
602 virtual ~Queue();
603};
604 /*}}}*/
606class APT_PUBLIC pkgAcquire::UriIterator
607{
609 void * const d;
610
612 pkgAcquire::Queue *CurQ;
614 pkgAcquire::Queue::QItem *CurItem;
615
616 public:
617
618 inline void operator ++() {operator ++(0);};
619
620 void operator ++(int)
621 {
622 CurItem = CurItem->Next;
623 while (CurItem == 0 && CurQ != 0)
624 {
625 CurItem = CurQ->Items;
626 CurQ = CurQ->Next;
627 }
628 };
629
630 inline pkgAcquire::Queue::QItem const *operator ->() const {return CurItem;};
631 inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
632 inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
633
638 explicit UriIterator(pkgAcquire::Queue *Q);
639 virtual ~UriIterator();
640};
641 /*}}}*/
643struct APT_PUBLIC pkgAcquire::MethodConfig
644{
645 class Private;
647 Private *const d;
648
653 MethodConfig *Next;
654
656 std::string Access;
657
659 std::string Version;
660
665
668
674
679
687
690
697
698 APT_HIDDEN bool GetAuxRequests() const;
699 APT_HIDDEN void SetAuxRequests(bool const value);
700 APT_HIDDEN bool GetSendURIEncoded() const;
701 APT_HIDDEN void SetSendURIEncoded(bool const value);
702
703 virtual ~MethodConfig();
704};
705 /*}}}*/
710class APT_PUBLIC pkgAcquireStatus
711{
713 void * const d;
714
715 protected:
716
718 struct timeval Time;
719
721 struct timeval StartTime;
722
726 unsigned long long LastBytes;
727
731 unsigned long long CurrentCPS;
732
736 unsigned long long CurrentBytes;
737
743 unsigned long long TotalBytes;
744
748 unsigned long long FetchedBytes;
749
753 unsigned long long ElapsedTime;
754
760 unsigned long TotalItems;
761
763 unsigned long CurrentItems;
764
767 double Percent;
768
769 public:
770
774 bool Update;
775
783
790 virtual void Fetched(unsigned long long Size,unsigned long long ResumePoint);
791
809 virtual bool MediaChange(std::string Media,std::string Drive) = 0;
810
812 {
813 std::string Type;
814 std::string From;
815 std::string To;
816 std::string Message;
818 };
836 virtual bool ReleaseInfoChanges(metaIndex const * const LastRelease, metaIndex const * const CurrentRelease, std::vector<ReleaseInfoChange> &&Changes);
837 APT_HIDDEN static bool ReleaseInfoChangesAsGlobalErrors(std::vector<ReleaseInfoChange> &&Changes);
838
844 virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
845
847 virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
848
850 virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
851
855 virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
856
867 virtual bool Pulse(pkgAcquire *Owner);
868
870 virtual void Start();
871
873 virtual void Stop();
874
877 virtual ~pkgAcquireStatus();
878};
879 /*}}}*/
882#endif
Definition hashes.h:79
Definition strutl.h:220
Definition weakptr.h:34
Definition metaindex.h:21
the manager of a transaction
Definition acquire-item.h:408
A monitor object for downloads controlled by the pkgAcquire class. {{{.
Definition acquire.h:711
virtual void IMSHit(pkgAcquire::ItemDesc &)
Invoked when an item is confirmed to be up-to-date.
Definition acquire.h:844
unsigned long long ElapsedTime
The amount of time that has elapsed since the download started.
Definition acquire.h:753
double Percent
The estimated percentage of the download (0-100)
Definition acquire.h:767
virtual void Fail(pkgAcquire::ItemDesc &)
Invoked when the process of fetching an item encounters a fatal error.
Definition acquire.h:855
virtual void Done(pkgAcquire::ItemDesc &)
Invoked when an item is successfully and completely fetched.
Definition acquire.h:850
virtual bool MediaChange(std::string Media, std::string Drive)=0
Invoked when the user should be prompted to change the inserted removable media.
virtual void Fetch(pkgAcquire::ItemDesc &)
Invoked when some of an item's data is fetched.
Definition acquire.h:847
unsigned long long FetchedBytes
The total number of bytes accounted for by items that were successfully fetched.
Definition acquire.h:748
bool Update
If true, the download scheduler should call Pulse() at the next available opportunity.
Definition acquire.h:774
unsigned long long CurrentBytes
The number of bytes fetched as of the most recent call to pkgAcquireStatus::Pulse,...
Definition acquire.h:736
bool MorePulses
If true, extra Pulse() invocations will be performed.
Definition acquire.h:782
unsigned long CurrentItems
The number of items that have been successfully downloaded.
Definition acquire.h:763
unsigned long long TotalBytes
The total number of bytes that need to be fetched.
Definition acquire.h:743
unsigned long long CurrentCPS
The current rate of download as of the most recent call to pkgAcquireStatus::Pulse,...
Definition acquire.h:731
unsigned long long LastBytes
The number of bytes fetched as of the previous call to pkgAcquireStatus::Pulse, including local items...
Definition acquire.h:726
unsigned long TotalItems
The total number of items that need to be fetched.
Definition acquire.h:760
Represents the process by which a pkgAcquire object should retrieve a file or a collection of files.
Definition acquire-item.h:59
bool Shutdown(bool Final)
Shut down the worker process associated with this queue.
ItemIterator ItemsEnd()
Get the end iterator of the list of items.
Definition acquire.h:310
bool Pipeline
If true, this method supports pipelined downloading.
Definition acquire.h:667
bool NeedsCleanup
If true, the subprocess has to carry out some cleanup actions before shutting down.
Definition acquire.h:686
bool const Debug
If true, debugging information will be dumped to std::clog.
Definition acquire.h:174
MethodConfig()
Set up the default method parameters.
bool Enqueue(ItemDesc &Item)
Insert the given fetch request into this queue.
QItem * Items
The head of the list of items contained in this queue.
Definition acquire.h:485
bool Cycle()
Send idle items to the worker process.
Worker * WorkersBegin()
Get the first Worker object.
Definition acquire.h:296
bool ItemDone(QItem *Itm)
Remove the given item from this queue and set its state to pkgAcquire::Item::StatDone.
std::string Description
description of this item.
Definition acquire.h:395
bool SingleInstance
If true, only one download queue should be created for this method.
Definition acquire.h:664
QItem * FindItem(std::string URI, pkgAcquire::Worker *Owner) APT_PURE
Locate an item in this queue.
bool ItemStart(QItem *Itm, unsigned long long Size)
pkgAcquire * Owner
the download scheduler with which this queue is associated.
Definition acquire.h:499
std::vector< Item * > Items
A list of items to download.
Definition acquire.h:126
bool Removable
If true, this fetch method acquires files from removable media.
Definition acquire.h:689
bool Dequeue(Item *Owner)
Remove all fetch requests for the given item from this queue.
bool LocalOnly
If true, this fetch method does not require network access; all files are to be acquired from the loc...
Definition acquire.h:678
std::string Version
The implementation version of this acquire method.
Definition acquire.h:659
signed long PipeDepth
The number of entries in this queue that are currently being downloaded.
Definition acquire.h:504
std::string ShortDesc
shorter description of this item.
Definition acquire.h:397
Queue(std::string const &Name, pkgAcquire *const Owner)
Create a new Queue.
Item * Owner
underlying item which is to be downloaded.
Definition acquire.h:399
Queue * Queues
The head of the list of active queues.
Definition acquire.h:133
std::string URI
URI from which to download this item.
Definition acquire.h:393
MethodConfig * Configs
The head of the list of acquire method configurations.
Definition acquire.h:152
bool Startup()
Start the worker process associated with this queue.
virtual ~Queue()
unsigned long ToFetch
The number of files which are to be fetched.
Definition acquire.h:158
void Bump()
Check for items that could be enqueued.
Worker(Queue *OwnerQ, MethodConfig *Config, pkgAcquireStatus *Log)
Create a new Worker to download files.
RunResult
Provides information on how a download terminated.
Definition acquire.h:262
@ Failed
Some files failed to download.
Definition acquire.h:267
@ Continue
All files were fetched successfully.
Definition acquire.h:264
UriIterator(pkgAcquire::Queue *Q)
Create a new UriIterator.
std::string Name
The name of this queue.
Definition acquire.h:479
MethodConfig * Next
The next link on the acquire method list.
Definition acquire.h:653
Worker * Workers
The head of the list of active workers.
Definition acquire.h:140
bool Running
If true, a download is currently in progress.
Definition acquire.h:176
pkgAcquire::Worker * Workers
The head of the list of workers associated with this queue.
Definition acquire.h:496
bool SendConfig
If true, the worker process should send the entire APT configuration tree to the fetch subprocess whe...
Definition acquire.h:673
QueueStrategy
Represents the queuing strategy for remote URIs.
Definition acquire.h:163
@ QueueHost
Generate one queue for each protocol/host combination; downloads from multiple hosts can proceed in p...
Definition acquire.h:167
ItemIterator ItemsBegin()
Get the head of the list of items.
Definition acquire.h:306
unsigned long MaxPipeDepth
The maximum number of entries that this queue will attempt to download at once.
Definition acquire.h:509
Definition acquire.h:812
std::string From
Definition acquire.h:814
std::string Type
Definition acquire.h:813
std::string Message
Definition acquire.h:816
bool DefaultAction
Definition acquire.h:817
std::string To
Definition acquire.h:815
A single item placed in this queue.
Definition acquire.h:422
int APT_HIDDEN GetPriority() const
unsigned long long GetMaximumSize() const
HashStringList GetExpectedHashes() const
void SyncDestinationFiles() const
get partial files in order
time_point APT_HIDDEN GetFetchAfter() const
QItem * Next
The next item in the queue.
Definition acquire.h:424
pkgAcquire::Worker * Worker
The worker associated with this item, if any.
Definition acquire.h:426
std::vector< Item * > Owners
The underlying items interested in the download.
Definition acquire.h:429
std::string Custom600Headers() const