Checked the size of partial files #33705
[ntk/apt.git] / apt-pkg / acquire.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire.h,v 1.20 1999/03/27 03:02:39 jgg Exp $
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
42 #include <sys/time.h>
43 #include <unistd.h>
44
45 class pkgAcquireStatus;
46 class pkgAcquire
47 {
48 public:
49
50 class Item;
51 class Queue;
52 class Worker;
53 struct MethodConfig;
54 struct ItemDesc;
55 friend Item;
56 friend Queue;
57
58 protected:
59
60 // List of items to fetch
61 vector<Item *> Items;
62
63 // List of active queues and fetched method configuration parameters
64 Queue *Queues;
65 Worker *Workers;
66 MethodConfig *Configs;
67 pkgAcquireStatus *Log;
68 unsigned long ToFetch;
69
70 // Configurable parameters for the schedular
71 enum {QueueHost,QueueAccess} QueueMode;
72 bool Debug;
73 bool Running;
74
75 void Add(Item *Item);
76 void Remove(Item *Item);
77 void Add(Worker *Work);
78 void Remove(Worker *Work);
79
80 void Enqueue(ItemDesc &Item);
81 void Dequeue(Item *Item);
82 string QueueName(string URI,MethodConfig const *&Config);
83
84 // FDSET managers for derived classes
85 void SetFds(int &Fd,fd_set *RSet,fd_set *WSet);
86 void RunFds(fd_set *RSet,fd_set *WSet);
87
88 // A queue calls this when it dequeues an item
89 void Bump();
90
91 public:
92
93 MethodConfig *GetConfig(string Access);
94 bool Run();
95
96 // Simple iteration mechanism
97 inline Worker *WorkersBegin() {return Workers;};
98 Worker *WorkerStep(Worker *I);
99 inline Item **ItemsBegin() {return Items.begin();};
100 inline Item **ItemsEnd() {return Items.end();};
101
102 // Iterate over queued Item URIs
103 class UriIterator;
104 UriIterator UriBegin();
105 UriIterator UriEnd();
106
107 // Cleans out the download dir
108 bool Clean(string Dir);
109
110 // Returns the size of the total download set
111 unsigned long TotalNeeded();
112 unsigned long FetchNeeded();
113 unsigned long PartialPresent();
114
115 pkgAcquire(pkgAcquireStatus *Log = 0);
116 ~pkgAcquire();
117 };
118
119 // Description of an Item+URI
120 struct pkgAcquire::ItemDesc
121 {
122 string URI;
123 string Description;
124 string ShortDesc;
125 Item *Owner;
126 };
127
128 // List of possible items queued for download.
129 class pkgAcquire::Queue
130 {
131 friend pkgAcquire;
132 friend pkgAcquire::UriIterator;
133 Queue *Next;
134
135 protected:
136
137 // Queued item
138 struct QItem : pkgAcquire::ItemDesc
139 {
140 QItem *Next;
141 pkgAcquire::Worker *Worker;
142
143 void operator =(pkgAcquire::ItemDesc const &I)
144 {
145 URI = I.URI;
146 Description = I.Description;
147 ShortDesc = I.ShortDesc;
148 Owner = I.Owner;
149 };
150 };
151
152 // Name of the queue
153 string Name;
154
155 // Items queued into this queue
156 QItem *Items;
157 pkgAcquire::Worker *Workers;
158 pkgAcquire *Owner;
159 signed long PipeDepth;
160 unsigned long MaxPipeDepth;
161
162 public:
163
164 // Put an item into this queue
165 void Enqueue(ItemDesc &Item);
166 bool Dequeue(Item *Owner);
167
168 // Find a Queued item
169 QItem *FindItem(string URI,pkgAcquire::Worker *Owner);
170 bool ItemStart(QItem *Itm,unsigned long Size);
171 bool ItemDone(QItem *Itm);
172
173 bool Startup();
174 bool Shutdown();
175 bool Cycle();
176 void Bump();
177
178 Queue(string Name,pkgAcquire *Owner);
179 ~Queue();
180 };
181
182 class pkgAcquire::UriIterator
183 {
184 pkgAcquire::Queue *CurQ;
185 pkgAcquire::Queue::QItem *CurItem;
186
187 public:
188
189 // Advance to the next item
190 inline void operator ++() {operator ++();};
191 void operator ++(int)
192 {
193 CurItem = CurItem->Next;
194 while (CurItem == 0 && CurQ != 0)
195 {
196 CurItem = CurQ->Items;
197 CurQ = CurQ->Next;
198 }
199 };
200
201 // Accessors
202 inline pkgAcquire::ItemDesc const *operator ->() const {return CurItem;};
203 inline bool operator !=(UriIterator const &rhs) const {return rhs.CurQ != CurQ || rhs.CurItem != CurItem;};
204 inline bool operator ==(UriIterator const &rhs) const {return rhs.CurQ == CurQ && rhs.CurItem == CurItem;};
205
206 UriIterator(pkgAcquire::Queue *Q) : CurQ(Q), CurItem(0)
207 {
208 while (CurItem == 0 && CurQ != 0)
209 {
210 CurItem = CurQ->Items;
211 CurQ = CurQ->Next;
212 }
213 }
214 };
215
216 // Configuration information from each method
217 struct pkgAcquire::MethodConfig
218 {
219 MethodConfig *Next;
220
221 string Access;
222
223 string Version;
224 bool SingleInstance;
225 bool Pipeline;
226 bool SendConfig;
227 bool LocalOnly;
228
229 MethodConfig();
230 };
231
232 class pkgAcquireStatus
233 {
234 protected:
235
236 struct timeval Time;
237 struct timeval StartTime;
238 unsigned long LastBytes;
239 double CurrentCPS;
240 unsigned long CurrentBytes;
241 unsigned long TotalBytes;
242 unsigned long FetchedBytes;
243 unsigned long ElapsedTime;
244 unsigned long TotalItems;
245 unsigned long CurrentItems;
246
247 public:
248
249 bool Update;
250
251 // Called by items when they have finished a real download
252 virtual void Fetched(unsigned long Size,unsigned long ResumePoint);
253
254 // Called to change media
255 virtual bool MediaChange(string Media,string Drive) = 0;
256
257 // Each of these is called by the workers when an event occures
258 virtual void IMSHit(pkgAcquire::ItemDesc &Itm) {};
259 virtual void Fetch(pkgAcquire::ItemDesc &Itm) {};
260 virtual void Done(pkgAcquire::ItemDesc &Itm) {};
261 virtual void Fail(pkgAcquire::ItemDesc &Itm) {};
262 virtual void Pulse(pkgAcquire *Owner);
263 virtual void Start();
264 virtual void Stop();
265
266 pkgAcquireStatus();
267 virtual ~pkgAcquireStatus() {};
268 };
269
270 #endif