]> git.saurik.com Git - apt.git/blame - apt-pkg/acquire.h
BSD testing fixes
[apt.git] / apt-pkg / acquire.h
CommitLineData
0118833a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
b2e465d6 3// $Id: acquire.h,v 1.28 2001/02/20 07:03:17 jgg Exp $
0118833a
AL
4/* ######################################################################
5
6 Acquire - File Acquiration
7
8 This module contians the Acquire system. It is responsible for bringing
9 files into the local pathname space. It deals with URIs for files and
10 URI handlers responsible for downloading or finding the URIs.
11
12 Each file to download is represented by an Acquire::Item class subclassed
13 into a specialization. The Item class can add itself to several URI
14 acquire queues each prioritized by the download scheduler. When the
15 system is run the proper URI handlers are spawned and the the acquire
16 queues are fed into the handlers by the schedular until the queues are
17 empty. This allows for an Item to be downloaded from an alternate source
18 if the first try turns out to fail. It also alows concurrent downloading
19 of multiple items from multiple sources as well as dynamic balancing
20 of load between the sources.
21
22 Schedualing of downloads is done on a first ask first get basis. This
23 preserves the order of the download as much as possible. And means the
24 fastest source will tend to process the largest number of files.
25
26 Internal methods and queues for performing gzip decompression,
27 md5sum hashing and file copying are provided to allow items to apply
28 a number of transformations to the data files they are working with.
29
30 ##################################################################### */
31 /*}}}*/
32#ifndef PKGLIB_ACQUIRE_H
33#define PKGLIB_ACQUIRE_H
34
35#include <vector>
36#include <string>
37
38#ifdef __GNUG__
39#pragma interface "apt-pkg/acquire.h"
40#endif
41
b98f2859 42#include <sys/time.h>
0a8a80e5
AL
43#include <unistd.h>
44
8267fe24 45class pkgAcquireStatus;
0118833a
AL
46class pkgAcquire
47{
48 public:
49
50 class Item;
51 class Queue;
52 class Worker;
53 struct MethodConfig;
8267fe24 54 struct ItemDesc;
b2e465d6
AL
55 friend class Item;
56 friend class Queue;
0118833a
AL
57
58 protected:
59
0a8a80e5 60 // List of items to fetch
0118833a 61 vector<Item *> Items;
0a8a80e5
AL
62
63 // List of active queues and fetched method configuration parameters
0118833a 64 Queue *Queues;
0a8a80e5 65 Worker *Workers;
0118833a 66 MethodConfig *Configs;
8267fe24 67 pkgAcquireStatus *Log;
0a8a80e5 68 unsigned long ToFetch;
8267fe24 69
0a8a80e5
AL
70 // Configurable parameters for the schedular
71 enum {QueueHost,QueueAccess} QueueMode;
72 bool Debug;
8b89e57f 73 bool Running;
0118833a
AL
74
75 void Add(Item *Item);
76 void Remove(Item *Item);
0a8a80e5
AL
77 void Add(Worker *Work);
78 void Remove(Worker *Work);
79
8267fe24 80 void Enqueue(ItemDesc &Item);
0a8a80e5 81 void Dequeue(Item *Item);
e331f6ed 82 string QueueName(string URI,MethodConfig const *&Config);
0a8a80e5
AL
83
84 // FDSET managers for derived classes
281daf46
AL
85 virtual void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
86 virtual void RunFds(fd_set *RSet,fd_set *WSet);
93bf083d
AL
87
88 // A queue calls this when it dequeues an item
89 void Bump();
0118833a
AL
90
91 public:
3b5421b4 92
0a8a80e5 93 MethodConfig *GetConfig(string Access);
024d1123
AL
94
95 enum RunResult {Continue,Failed,Cancelled};
96
97 RunResult Run();
281daf46
AL
98 void Shutdown();
99
8267fe24
AL
100 // Simple iteration mechanism
101 inline Worker *WorkersBegin() {return Workers;};
102 Worker *WorkerStep(Worker *I);
103 inline Item **ItemsBegin() {return Items.begin();};
104 inline Item **ItemsEnd() {return Items.end();};
f7a08e33
AL
105
106 // Iterate over queued Item URIs
107 class UriIterator;
108 UriIterator UriBegin();
109 UriIterator UriEnd();
110
7a7fa5f0
AL
111 // Cleans out the download dir
112 bool Clean(string Dir);
a6568219
AL
113
114 // Returns the size of the total download set
b2e465d6
AL
115 double TotalNeeded();
116 double FetchNeeded();
117 double PartialPresent();
0118833a 118
8267fe24 119 pkgAcquire(pkgAcquireStatus *Log = 0);
58d63ae6 120 virtual ~pkgAcquire();
0118833a
AL
121};
122
8267fe24
AL
123// Description of an Item+URI
124struct pkgAcquire::ItemDesc
125{
126 string URI;
127 string Description;
128 string ShortDesc;
129 Item *Owner;
130};
131
0118833a
AL
132// List of possible items queued for download.
133class pkgAcquire::Queue
134{
b2e465d6
AL
135 friend class pkgAcquire;
136 friend class pkgAcquire::UriIterator;
137 friend class pkgAcquire::Worker;
0118833a
AL
138 Queue *Next;
139
140 protected:
3b5421b4 141
0a8a80e5 142 // Queued item
8267fe24 143 struct QItem : pkgAcquire::ItemDesc
0a8a80e5 144 {
8267fe24 145 QItem *Next;
c88edf1d 146 pkgAcquire::Worker *Worker;
8267fe24
AL
147
148 void operator =(pkgAcquire::ItemDesc const &I)
149 {
150 URI = I.URI;
151 Description = I.Description;
152 ShortDesc = I.ShortDesc;
153 Owner = I.Owner;
154 };
155 };
0a8a80e5
AL
156
157 // Name of the queue
158 string Name;
159
160 // Items queued into this queue
161 QItem *Items;
162 pkgAcquire::Worker *Workers;
163 pkgAcquire *Owner;
e7432370 164 signed long PipeDepth;
b185acc2 165 unsigned long MaxPipeDepth;
0118833a
AL
166
167 public:
0a8a80e5
AL
168
169 // Put an item into this queue
8267fe24 170 void Enqueue(ItemDesc &Item);
bfd22fc0 171 bool Dequeue(Item *Owner);
0a8a80e5 172
c88edf1d
AL
173 // Find a Queued item
174 QItem *FindItem(string URI,pkgAcquire::Worker *Owner);
8267fe24 175 bool ItemStart(QItem *Itm,unsigned long Size);
c88edf1d
AL
176 bool ItemDone(QItem *Itm);
177
0a8a80e5 178 bool Startup();
8e5fc8f5 179 bool Shutdown(bool Final);
93bf083d 180 bool Cycle();
be4401bf 181 void Bump();
0a8a80e5
AL
182
183 Queue(string Name,pkgAcquire *Owner);
184 ~Queue();
0118833a
AL
185};
186
f7a08e33
AL
187class pkgAcquire::UriIterator
188{
189 pkgAcquire::Queue *CurQ;
190 pkgAcquire::Queue::QItem *CurItem;
191
192 public:
193
194 // Advance to the next item
195 inline void operator ++() {operator ++();};
196 void operator ++(int)
197 {
198 CurItem = CurItem->Next;
199 while (CurItem == 0 && CurQ != 0)
200 {
201 CurItem = CurQ->Items;
202 CurQ = CurQ->Next;
203 }
204 };
205
206 // Accessors
207 inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
208 inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
209 inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
210
211 UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
212 {
213 while (CurItem == 0 && CurQ != 0)
214 {
215 CurItem = CurQ->Items;
216 CurQ = CurQ->Next;
217 }
218 }
219};
220
0118833a
AL
221// Configuration information from each method
222struct pkgAcquire::MethodConfig
223{
3b5421b4
AL
224 MethodConfig *Next;
225
0118833a
AL
226 string Access;
227
228 string Version;
229 bool SingleInstance;
0a8a80e5
AL
230 bool Pipeline;
231 bool SendConfig;
e331f6ed 232 bool LocalOnly;
8e5fc8f5 233 bool NeedsCleanup;
459681d3 234 bool Removable;
8e5fc8f5 235
0118833a 236 MethodConfig();
0118833a
AL
237};
238
8267fe24
AL
239class pkgAcquireStatus
240{
b98f2859
AL
241 protected:
242
243 struct timeval Time;
244 struct timeval StartTime;
b2e465d6 245 double LastBytes;
b98f2859 246 double CurrentCPS;
b2e465d6
AL
247 double CurrentBytes;
248 double TotalBytes;
249 double FetchedBytes;
b98f2859 250 unsigned long ElapsedTime;
d568ed2d
AL
251 unsigned long TotalItems;
252 unsigned long CurrentItems;
b98f2859 253
8267fe24
AL
254 public:
255
256 bool Update;
c5ccf175
AL
257 bool MorePulses;
258
b98f2859
AL
259 // Called by items when they have finished a real download
260 virtual void Fetched(unsigned long Size,unsigned long ResumePoint);
261
542ec555
AL
262 // Called to change media
263 virtual bool MediaChange(string Media,string Drive) = 0;
264
8267fe24 265 // Each of these is called by the workers when an event occures
727f18af
AL
266 virtual void IMSHit(pkgAcquire::ItemDesc &/*Itm*/) {};
267 virtual void Fetch(pkgAcquire::ItemDesc &/*Itm*/) {};
268 virtual void Done(pkgAcquire::ItemDesc &/*Itm*/) {};
269 virtual void Fail(pkgAcquire::ItemDesc &/*Itm*/) {};
024d1123 270 virtual bool Pulse(pkgAcquire *Owner); // returns false on user cancel
b98f2859
AL
271 virtual void Start();
272 virtual void Stop();
a6568219 273
b98f2859 274 pkgAcquireStatus();
8267fe24
AL
275 virtual ~pkgAcquireStatus() {};
276};
277
0118833a 278#endif