]>
Commit | Line | Data |
---|---|---|
0118833a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b0db36b1 | 3 | // $Id: acquire.cc,v 1.29 1999/03/16 00:43:55 jgg Exp $ |
0118833a AL |
4 | /* ###################################################################### |
5 | ||
6 | Acquire - File Acquiration | |
7 | ||
0a8a80e5 AL |
8 | The core element for the schedual system is the concept of a named |
9 | queue. Each queue is unique and each queue has a name derived from the | |
10 | URI. The degree of paralization can be controled by how the queue | |
11 | name is derived from the URI. | |
12 | ||
0118833a AL |
13 | ##################################################################### */ |
14 | /*}}}*/ | |
15 | // Include Files /*{{{*/ | |
16 | #ifdef __GNUG__ | |
17 | #pragma implementation "apt-pkg/acquire.h" | |
18 | #endif | |
19 | #include <apt-pkg/acquire.h> | |
20 | #include <apt-pkg/acquire-item.h> | |
21 | #include <apt-pkg/acquire-worker.h> | |
0a8a80e5 AL |
22 | #include <apt-pkg/configuration.h> |
23 | #include <apt-pkg/error.h> | |
cdcc6d34 | 24 | #include <apt-pkg/strutl.h> |
8267fe24 | 25 | |
7a7fa5f0 | 26 | #include <dirent.h> |
8267fe24 | 27 | #include <sys/time.h> |
0118833a AL |
28 | /*}}}*/ |
29 | ||
30 | // Acquire::pkgAcquire - Constructor /*{{{*/ | |
31 | // --------------------------------------------------------------------- | |
93bf083d | 32 | /* We grab some runtime state from the configuration space */ |
8267fe24 | 33 | pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log) |
0118833a AL |
34 | { |
35 | Queues = 0; | |
36 | Configs = 0; | |
0a8a80e5 AL |
37 | Workers = 0; |
38 | ToFetch = 0; | |
8b89e57f | 39 | Running = false; |
0a8a80e5 AL |
40 | |
41 | string Mode = _config->Find("Acquire::Queue-Mode","host"); | |
42 | if (strcasecmp(Mode.c_str(),"host") == 0) | |
43 | QueueMode = QueueHost; | |
44 | if (strcasecmp(Mode.c_str(),"access") == 0) | |
45 | QueueMode = QueueAccess; | |
46 | ||
47 | Debug = _config->FindB("Debug::pkgAcquire",false); | |
0118833a AL |
48 | } |
49 | /*}}}*/ | |
50 | // Acquire::~pkgAcquire - Destructor /*{{{*/ | |
51 | // --------------------------------------------------------------------- | |
93bf083d | 52 | /* Free our memory, clean up the queues (destroy the workers) */ |
0118833a AL |
53 | pkgAcquire::~pkgAcquire() |
54 | { | |
55 | while (Items.size() != 0) | |
56 | delete Items[0]; | |
3b5421b4 AL |
57 | |
58 | while (Configs != 0) | |
59 | { | |
60 | MethodConfig *Jnk = Configs; | |
61 | Configs = Configs->Next; | |
62 | delete Jnk; | |
63 | } | |
0a8a80e5 AL |
64 | |
65 | while (Queues != 0) | |
66 | { | |
67 | Queue *Jnk = Queues; | |
68 | Queues = Queues->Next; | |
69 | delete Jnk; | |
70 | } | |
0118833a AL |
71 | } |
72 | /*}}}*/ | |
73 | // Acquire::Add - Add a new item /*{{{*/ | |
74 | // --------------------------------------------------------------------- | |
93bf083d AL |
75 | /* This puts an item on the acquire list. This list is mainly for tracking |
76 | item status */ | |
0118833a AL |
77 | void pkgAcquire::Add(Item *Itm) |
78 | { | |
79 | Items.push_back(Itm); | |
80 | } | |
81 | /*}}}*/ | |
82 | // Acquire::Remove - Remove a item /*{{{*/ | |
83 | // --------------------------------------------------------------------- | |
93bf083d | 84 | /* Remove an item from the acquire list. This is usually not used.. */ |
0118833a AL |
85 | void pkgAcquire::Remove(Item *Itm) |
86 | { | |
87 | for (vector<Item *>::iterator I = Items.begin(); I < Items.end(); I++) | |
88 | { | |
89 | if (*I == Itm) | |
90 | Items.erase(I); | |
8267fe24 | 91 | } |
0118833a AL |
92 | } |
93 | /*}}}*/ | |
0a8a80e5 AL |
94 | // Acquire::Add - Add a worker /*{{{*/ |
95 | // --------------------------------------------------------------------- | |
93bf083d AL |
96 | /* A list of workers is kept so that the select loop can direct their FD |
97 | usage. */ | |
0a8a80e5 AL |
98 | void pkgAcquire::Add(Worker *Work) |
99 | { | |
100 | Work->NextAcquire = Workers; | |
101 | Workers = Work; | |
102 | } | |
103 | /*}}}*/ | |
104 | // Acquire::Remove - Remove a worker /*{{{*/ | |
105 | // --------------------------------------------------------------------- | |
93bf083d AL |
106 | /* A worker has died. This can not be done while the select loop is running |
107 | as it would require that RunFds could handling a changing list state and | |
108 | it cant.. */ | |
0a8a80e5 AL |
109 | void pkgAcquire::Remove(Worker *Work) |
110 | { | |
93bf083d AL |
111 | if (Running == true) |
112 | abort(); | |
113 | ||
0a8a80e5 AL |
114 | Worker **I = &Workers; |
115 | for (; *I != 0;) | |
116 | { | |
117 | if (*I == Work) | |
118 | *I = (*I)->NextAcquire; | |
119 | else | |
120 | I = &(*I)->NextAcquire; | |
121 | } | |
122 | } | |
123 | /*}}}*/ | |
0118833a AL |
124 | // Acquire::Enqueue - Queue an URI for fetching /*{{{*/ |
125 | // --------------------------------------------------------------------- | |
93bf083d AL |
126 | /* This is the entry point for an item. An item calls this function when |
127 | it is construction which creates a queue (based on the current queue | |
128 | mode) and puts the item in that queue. If the system is running then | |
129 | the queue might be started. */ | |
8267fe24 | 130 | void pkgAcquire::Enqueue(ItemDesc &Item) |
0118833a | 131 | { |
0a8a80e5 | 132 | // Determine which queue to put the item in |
e331f6ed AL |
133 | const MethodConfig *Config; |
134 | string Name = QueueName(Item.URI,Config); | |
0a8a80e5 AL |
135 | if (Name.empty() == true) |
136 | return; | |
137 | ||
138 | // Find the queue structure | |
139 | Queue *I = Queues; | |
140 | for (; I != 0 && I->Name != Name; I = I->Next); | |
141 | if (I == 0) | |
142 | { | |
143 | I = new Queue(Name,this); | |
144 | I->Next = Queues; | |
145 | Queues = I; | |
93bf083d AL |
146 | |
147 | if (Running == true) | |
148 | I->Startup(); | |
0a8a80e5 | 149 | } |
bfd22fc0 | 150 | |
e331f6ed AL |
151 | // See if this is a local only URI |
152 | if (Config->LocalOnly == true && Item.Owner->Complete == false) | |
153 | Item.Owner->Local = true; | |
8267fe24 | 154 | Item.Owner->Status = Item::StatIdle; |
0a8a80e5 AL |
155 | |
156 | // Queue it into the named queue | |
8267fe24 | 157 | I->Enqueue(Item); |
0a8a80e5 | 158 | ToFetch++; |
93bf083d | 159 | |
0a8a80e5 AL |
160 | // Some trace stuff |
161 | if (Debug == true) | |
162 | { | |
8267fe24 AL |
163 | clog << "Fetching " << Item.URI << endl; |
164 | clog << " to " << Item.Owner->DestFile << endl; | |
e331f6ed | 165 | clog << " Queue is: " << Name << endl; |
0a8a80e5 | 166 | } |
3b5421b4 AL |
167 | } |
168 | /*}}}*/ | |
0a8a80e5 | 169 | // Acquire::Dequeue - Remove an item from all queues /*{{{*/ |
3b5421b4 | 170 | // --------------------------------------------------------------------- |
93bf083d AL |
171 | /* This is called when an item is finished being fetched. It removes it |
172 | from all the queues */ | |
0a8a80e5 AL |
173 | void pkgAcquire::Dequeue(Item *Itm) |
174 | { | |
175 | Queue *I = Queues; | |
bfd22fc0 | 176 | bool Res = false; |
0a8a80e5 | 177 | for (; I != 0; I = I->Next) |
bfd22fc0 | 178 | Res |= I->Dequeue(Itm); |
93bf083d AL |
179 | |
180 | if (Debug == true) | |
181 | clog << "Dequeuing " << Itm->DestFile << endl; | |
bfd22fc0 AL |
182 | if (Res == true) |
183 | ToFetch--; | |
0a8a80e5 AL |
184 | } |
185 | /*}}}*/ | |
186 | // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/ | |
187 | // --------------------------------------------------------------------- | |
188 | /* The string returned depends on the configuration settings and the | |
189 | method parameters. Given something like http://foo.org/bar it can | |
190 | return http://foo.org or http */ | |
e331f6ed | 191 | string pkgAcquire::QueueName(string Uri,MethodConfig const *&Config) |
3b5421b4 | 192 | { |
93bf083d AL |
193 | URI U(Uri); |
194 | ||
e331f6ed | 195 | Config = GetConfig(U.Access); |
0a8a80e5 AL |
196 | if (Config == 0) |
197 | return string(); | |
198 | ||
199 | /* Single-Instance methods get exactly one queue per URI. This is | |
200 | also used for the Access queue method */ | |
201 | if (Config->SingleInstance == true || QueueMode == QueueAccess) | |
b98f2859 | 202 | return U.Access; |
93bf083d AL |
203 | |
204 | return U.Access + ':' + U.Host; | |
0118833a AL |
205 | } |
206 | /*}}}*/ | |
3b5421b4 AL |
207 | // Acquire::GetConfig - Fetch the configuration information /*{{{*/ |
208 | // --------------------------------------------------------------------- | |
209 | /* This locates the configuration structure for an access method. If | |
210 | a config structure cannot be found a Worker will be created to | |
211 | retrieve it */ | |
0a8a80e5 | 212 | pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access) |
3b5421b4 AL |
213 | { |
214 | // Search for an existing config | |
215 | MethodConfig *Conf; | |
216 | for (Conf = Configs; Conf != 0; Conf = Conf->Next) | |
217 | if (Conf->Access == Access) | |
218 | return Conf; | |
219 | ||
220 | // Create the new config class | |
221 | Conf = new MethodConfig; | |
222 | Conf->Access = Access; | |
223 | Conf->Next = Configs; | |
224 | Configs = Conf; | |
0118833a | 225 | |
3b5421b4 AL |
226 | // Create the worker to fetch the configuration |
227 | Worker Work(Conf); | |
228 | if (Work.Start() == false) | |
229 | return 0; | |
230 | ||
231 | return Conf; | |
232 | } | |
233 | /*}}}*/ | |
0a8a80e5 AL |
234 | // Acquire::SetFds - Deal with readable FDs /*{{{*/ |
235 | // --------------------------------------------------------------------- | |
236 | /* Collect FDs that have activity monitors into the fd sets */ | |
237 | void pkgAcquire::SetFds(int &Fd,fd_set *RSet,fd_set *WSet) | |
238 | { | |
239 | for (Worker *I = Workers; I != 0; I = I->NextAcquire) | |
240 | { | |
241 | if (I->InReady == true && I->InFd >= 0) | |
242 | { | |
243 | if (Fd < I->InFd) | |
244 | Fd = I->InFd; | |
245 | FD_SET(I->InFd,RSet); | |
246 | } | |
247 | if (I->OutReady == true && I->OutFd >= 0) | |
248 | { | |
249 | if (Fd < I->OutFd) | |
250 | Fd = I->OutFd; | |
251 | FD_SET(I->OutFd,WSet); | |
252 | } | |
253 | } | |
254 | } | |
255 | /*}}}*/ | |
256 | // Acquire::RunFds - Deal with active FDs /*{{{*/ | |
257 | // --------------------------------------------------------------------- | |
93bf083d AL |
258 | /* Dispatch active FDs over to the proper workers. It is very important |
259 | that a worker never be erased while this is running! The queue class | |
260 | should never erase a worker except during shutdown processing. */ | |
0a8a80e5 AL |
261 | void pkgAcquire::RunFds(fd_set *RSet,fd_set *WSet) |
262 | { | |
263 | for (Worker *I = Workers; I != 0; I = I->NextAcquire) | |
264 | { | |
265 | if (I->InFd >= 0 && FD_ISSET(I->InFd,RSet) != 0) | |
266 | I->InFdReady(); | |
267 | if (I->OutFd >= 0 && FD_ISSET(I->OutFd,WSet) != 0) | |
268 | I->OutFdReady(); | |
269 | } | |
270 | } | |
271 | /*}}}*/ | |
272 | // Acquire::Run - Run the fetch sequence /*{{{*/ | |
273 | // --------------------------------------------------------------------- | |
274 | /* This runs the queues. It manages a select loop for all of the | |
275 | Worker tasks. The workers interact with the queues and items to | |
276 | manage the actual fetch. */ | |
277 | bool pkgAcquire::Run() | |
278 | { | |
8b89e57f AL |
279 | Running = true; |
280 | ||
0a8a80e5 AL |
281 | for (Queue *I = Queues; I != 0; I = I->Next) |
282 | I->Startup(); | |
283 | ||
b98f2859 AL |
284 | if (Log != 0) |
285 | Log->Start(); | |
286 | ||
0a8a80e5 | 287 | // Run till all things have been acquired |
8267fe24 AL |
288 | struct timeval tv; |
289 | tv.tv_sec = 0; | |
290 | tv.tv_usec = 500000; | |
0a8a80e5 AL |
291 | while (ToFetch > 0) |
292 | { | |
293 | fd_set RFds; | |
294 | fd_set WFds; | |
295 | int Highest = 0; | |
296 | FD_ZERO(&RFds); | |
297 | FD_ZERO(&WFds); | |
298 | SetFds(Highest,&RFds,&WFds); | |
299 | ||
b0db36b1 AL |
300 | int Res; |
301 | do | |
302 | { | |
303 | Res = select(Highest+1,&RFds,&WFds,0,&tv); | |
304 | } | |
305 | while (Res < 0 && errno == EINTR); | |
306 | ||
8267fe24 | 307 | if (Res < 0) |
8b89e57f | 308 | { |
8267fe24 AL |
309 | _error->Errno("select","Select has failed"); |
310 | break; | |
8b89e57f | 311 | } |
93bf083d | 312 | |
0a8a80e5 | 313 | RunFds(&RFds,&WFds); |
93bf083d AL |
314 | if (_error->PendingError() == true) |
315 | break; | |
8267fe24 AL |
316 | |
317 | // Timeout, notify the log class | |
318 | if (Res == 0 || (Log != 0 && Log->Update == true)) | |
319 | { | |
320 | tv.tv_usec = 500000; | |
321 | for (Worker *I = Workers; I != 0; I = I->NextAcquire) | |
322 | I->Pulse(); | |
323 | if (Log != 0) | |
324 | Log->Pulse(this); | |
325 | } | |
0a8a80e5 | 326 | } |
be4401bf | 327 | |
b98f2859 AL |
328 | if (Log != 0) |
329 | Log->Stop(); | |
330 | ||
be4401bf AL |
331 | // Shut down the acquire bits |
332 | Running = false; | |
0a8a80e5 AL |
333 | for (Queue *I = Queues; I != 0; I = I->Next) |
334 | I->Shutdown(); | |
335 | ||
0919e3f9 | 336 | return !_error->PendingError(); |
93bf083d AL |
337 | } |
338 | /*}}}*/ | |
be4401bf | 339 | // Acquire::Bump - Called when an item is dequeued /*{{{*/ |
93bf083d AL |
340 | // --------------------------------------------------------------------- |
341 | /* This routine bumps idle queues in hopes that they will be able to fetch | |
342 | the dequeued item */ | |
343 | void pkgAcquire::Bump() | |
344 | { | |
be4401bf AL |
345 | for (Queue *I = Queues; I != 0; I = I->Next) |
346 | I->Bump(); | |
0a8a80e5 AL |
347 | } |
348 | /*}}}*/ | |
8267fe24 AL |
349 | // Acquire::WorkerStep - Step to the next worker /*{{{*/ |
350 | // --------------------------------------------------------------------- | |
351 | /* Not inlined to advoid including acquire-worker.h */ | |
352 | pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I) | |
353 | { | |
354 | return I->NextAcquire; | |
355 | }; | |
356 | /*}}}*/ | |
a6568219 | 357 | // Acquire::Clean - Cleans a directory /*{{{*/ |
7a7fa5f0 AL |
358 | // --------------------------------------------------------------------- |
359 | /* This is a bit simplistic, it looks at every file in the dir and sees | |
360 | if it is part of the download set. */ | |
361 | bool pkgAcquire::Clean(string Dir) | |
362 | { | |
363 | DIR *D = opendir(Dir.c_str()); | |
364 | if (D == 0) | |
365 | return _error->Errno("opendir","Unable to read %s",Dir.c_str()); | |
366 | ||
367 | string StartDir = SafeGetCWD(); | |
368 | if (chdir(Dir.c_str()) != 0) | |
369 | { | |
370 | closedir(D); | |
371 | return _error->Errno("chdir","Unable to change to ",Dir.c_str()); | |
372 | } | |
373 | ||
374 | for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D)) | |
375 | { | |
376 | // Skip some files.. | |
377 | if (strcmp(Dir->d_name,"lock") == 0 || | |
378 | strcmp(Dir->d_name,"partial") == 0 || | |
379 | strcmp(Dir->d_name,".") == 0 || | |
380 | strcmp(Dir->d_name,"..") == 0) | |
381 | continue; | |
382 | ||
383 | // Look in the get list | |
384 | vector<Item *>::iterator I = Items.begin(); | |
385 | for (; I != Items.end(); I++) | |
386 | if (flNotDir((*I)->DestFile) == Dir->d_name) | |
387 | break; | |
388 | ||
389 | // Nothing found, nuke it | |
390 | if (I == Items.end()) | |
391 | unlink(Dir->d_name); | |
392 | }; | |
393 | ||
394 | chdir(StartDir.c_str()); | |
395 | closedir(D); | |
396 | return true; | |
397 | } | |
398 | /*}}}*/ | |
a6568219 AL |
399 | // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/ |
400 | // --------------------------------------------------------------------- | |
401 | /* This is the total number of bytes needed */ | |
402 | unsigned long pkgAcquire::TotalNeeded() | |
403 | { | |
404 | unsigned long Total = 0; | |
405 | for (pkgAcquire::Item **I = ItemsBegin(); I != ItemsEnd(); I++) | |
406 | Total += (*I)->FileSize; | |
407 | return Total; | |
408 | } | |
409 | /*}}}*/ | |
410 | // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/ | |
411 | // --------------------------------------------------------------------- | |
412 | /* This is the number of bytes that is not local */ | |
413 | unsigned long pkgAcquire::FetchNeeded() | |
414 | { | |
415 | unsigned long Total = 0; | |
416 | for (pkgAcquire::Item **I = ItemsBegin(); I != ItemsEnd(); I++) | |
417 | if ((*I)->Local == false) | |
418 | Total += (*I)->FileSize; | |
419 | return Total; | |
420 | } | |
421 | /*}}}*/ | |
f7a08e33 AL |
422 | // pkgAcquire::UriBegin - Start iterator for the uri list /*{{{*/ |
423 | // --------------------------------------------------------------------- | |
424 | /* */ | |
425 | pkgAcquire::UriIterator pkgAcquire::UriBegin() | |
426 | { | |
427 | return UriIterator(Queues); | |
428 | } | |
429 | /*}}}*/ | |
430 | // pkgAcquire::UriEnd - End iterator for the uri list /*{{{*/ | |
431 | // --------------------------------------------------------------------- | |
432 | /* */ | |
433 | pkgAcquire::UriIterator pkgAcquire::UriEnd() | |
434 | { | |
435 | return UriIterator(0); | |
436 | } | |
437 | /*}}}*/ | |
0a8a80e5 | 438 | |
e331f6ed AL |
439 | // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/ |
440 | // --------------------------------------------------------------------- | |
441 | /* */ | |
442 | pkgAcquire::MethodConfig::MethodConfig() | |
443 | { | |
444 | SingleInstance = false; | |
e331f6ed AL |
445 | Pipeline = false; |
446 | SendConfig = false; | |
447 | LocalOnly = false; | |
448 | Next = 0; | |
449 | } | |
450 | /*}}}*/ | |
451 | ||
0a8a80e5 AL |
452 | // Queue::Queue - Constructor /*{{{*/ |
453 | // --------------------------------------------------------------------- | |
454 | /* */ | |
455 | pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name), | |
456 | Owner(Owner) | |
457 | { | |
458 | Items = 0; | |
459 | Next = 0; | |
460 | Workers = 0; | |
b185acc2 AL |
461 | MaxPipeDepth = 1; |
462 | PipeDepth = 0; | |
0a8a80e5 AL |
463 | } |
464 | /*}}}*/ | |
465 | // Queue::~Queue - Destructor /*{{{*/ | |
466 | // --------------------------------------------------------------------- | |
467 | /* */ | |
468 | pkgAcquire::Queue::~Queue() | |
469 | { | |
470 | Shutdown(); | |
471 | ||
472 | while (Items != 0) | |
473 | { | |
474 | QItem *Jnk = Items; | |
475 | Items = Items->Next; | |
476 | delete Jnk; | |
477 | } | |
478 | } | |
479 | /*}}}*/ | |
480 | // Queue::Enqueue - Queue an item to the queue /*{{{*/ | |
481 | // --------------------------------------------------------------------- | |
482 | /* */ | |
8267fe24 | 483 | void pkgAcquire::Queue::Enqueue(ItemDesc &Item) |
0a8a80e5 | 484 | { |
7a1b1f8b AL |
485 | QItem **I = &Items; |
486 | for (; *I != 0; I = &(*I)->Next); | |
487 | ||
0a8a80e5 | 488 | // Create a new item |
7a1b1f8b AL |
489 | QItem *Itm = new QItem; |
490 | *Itm = Item; | |
491 | Itm->Next = 0; | |
492 | *I = Itm; | |
0a8a80e5 | 493 | |
8267fe24 | 494 | Item.Owner->QueueCounter++; |
93bf083d AL |
495 | if (Items->Next == 0) |
496 | Cycle(); | |
0a8a80e5 AL |
497 | } |
498 | /*}}}*/ | |
c88edf1d | 499 | // Queue::Dequeue - Remove an item from the queue /*{{{*/ |
0a8a80e5 | 500 | // --------------------------------------------------------------------- |
b185acc2 | 501 | /* We return true if we hit something */ |
bfd22fc0 | 502 | bool pkgAcquire::Queue::Dequeue(Item *Owner) |
0a8a80e5 | 503 | { |
b185acc2 AL |
504 | if (Owner->Status == pkgAcquire::Item::StatFetching) |
505 | return _error->Error("Tried to dequeue a fetching object"); | |
506 | ||
bfd22fc0 AL |
507 | bool Res = false; |
508 | ||
0a8a80e5 AL |
509 | QItem **I = &Items; |
510 | for (; *I != 0;) | |
511 | { | |
512 | if ((*I)->Owner == Owner) | |
513 | { | |
514 | QItem *Jnk= *I; | |
515 | *I = (*I)->Next; | |
516 | Owner->QueueCounter--; | |
517 | delete Jnk; | |
bfd22fc0 | 518 | Res = true; |
0a8a80e5 AL |
519 | } |
520 | else | |
521 | I = &(*I)->Next; | |
522 | } | |
bfd22fc0 AL |
523 | |
524 | return Res; | |
0a8a80e5 AL |
525 | } |
526 | /*}}}*/ | |
527 | // Queue::Startup - Start the worker processes /*{{{*/ | |
528 | // --------------------------------------------------------------------- | |
529 | /* */ | |
530 | bool pkgAcquire::Queue::Startup() | |
531 | { | |
532 | Shutdown(); | |
533 | ||
93bf083d AL |
534 | URI U(Name); |
535 | pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access); | |
0a8a80e5 AL |
536 | if (Cnf == 0) |
537 | return false; | |
538 | ||
8267fe24 | 539 | Workers = new Worker(this,Cnf,Owner->Log); |
0a8a80e5 AL |
540 | Owner->Add(Workers); |
541 | if (Workers->Start() == false) | |
542 | return false; | |
0a8a80e5 | 543 | |
5cb5d8dc AL |
544 | /* When pipelining we commit 10 items. This needs to change when we |
545 | added other source retry to have cycle maintain a pipeline depth | |
546 | on its own. */ | |
547 | if (Cnf->Pipeline == true) | |
b185acc2 AL |
548 | MaxPipeDepth = 10; |
549 | else | |
550 | MaxPipeDepth = 1; | |
5cb5d8dc | 551 | |
93bf083d | 552 | return Cycle(); |
0a8a80e5 AL |
553 | } |
554 | /*}}}*/ | |
555 | // Queue::Shutdown - Shutdown the worker processes /*{{{*/ | |
556 | // --------------------------------------------------------------------- | |
557 | /* */ | |
558 | bool pkgAcquire::Queue::Shutdown() | |
559 | { | |
560 | // Delete all of the workers | |
561 | while (Workers != 0) | |
562 | { | |
563 | pkgAcquire::Worker *Jnk = Workers; | |
564 | Workers = Workers->NextQueue; | |
565 | Owner->Remove(Jnk); | |
566 | delete Jnk; | |
567 | } | |
568 | ||
569 | return true; | |
3b5421b4 AL |
570 | } |
571 | /*}}}*/ | |
7d8afa39 | 572 | // Queue::FindItem - Find a URI in the item list /*{{{*/ |
c88edf1d AL |
573 | // --------------------------------------------------------------------- |
574 | /* */ | |
575 | pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner) | |
576 | { | |
577 | for (QItem *I = Items; I != 0; I = I->Next) | |
578 | if (I->URI == URI && I->Worker == Owner) | |
579 | return I; | |
580 | return 0; | |
581 | } | |
582 | /*}}}*/ | |
583 | // Queue::ItemDone - Item has been completed /*{{{*/ | |
584 | // --------------------------------------------------------------------- | |
585 | /* The worker signals this which causes the item to be removed from the | |
93bf083d AL |
586 | queue. If this is the last queue instance then it is removed from the |
587 | main queue too.*/ | |
c88edf1d AL |
588 | bool pkgAcquire::Queue::ItemDone(QItem *Itm) |
589 | { | |
b185acc2 | 590 | PipeDepth--; |
db890fdb AL |
591 | if (Itm->Owner->Status == pkgAcquire::Item::StatFetching) |
592 | Itm->Owner->Status = pkgAcquire::Item::StatDone; | |
593 | ||
93bf083d AL |
594 | if (Itm->Owner->QueueCounter <= 1) |
595 | Owner->Dequeue(Itm->Owner); | |
596 | else | |
597 | { | |
598 | Dequeue(Itm->Owner); | |
599 | Owner->Bump(); | |
600 | } | |
c88edf1d | 601 | |
93bf083d AL |
602 | return Cycle(); |
603 | } | |
604 | /*}}}*/ | |
605 | // Queue::Cycle - Queue new items into the method /*{{{*/ | |
606 | // --------------------------------------------------------------------- | |
b185acc2 AL |
607 | /* This locates a new idle item and sends it to the worker. If pipelining |
608 | is enabled then it keeps the pipe full. */ | |
93bf083d AL |
609 | bool pkgAcquire::Queue::Cycle() |
610 | { | |
611 | if (Items == 0 || Workers == 0) | |
c88edf1d AL |
612 | return true; |
613 | ||
e7432370 AL |
614 | if (PipeDepth < 0) |
615 | return _error->Error("Pipedepth failure"); | |
616 | ||
93bf083d AL |
617 | // Look for a queable item |
618 | QItem *I = Items; | |
e7432370 | 619 | while (PipeDepth < (signed)MaxPipeDepth) |
b185acc2 AL |
620 | { |
621 | for (; I != 0; I = I->Next) | |
622 | if (I->Owner->Status == pkgAcquire::Item::StatIdle) | |
623 | break; | |
624 | ||
625 | // Nothing to do, queue is idle. | |
626 | if (I == 0) | |
627 | return true; | |
628 | ||
629 | I->Worker = Workers; | |
630 | I->Owner->Status = pkgAcquire::Item::StatFetching; | |
e7432370 | 631 | PipeDepth++; |
b185acc2 AL |
632 | if (Workers->QueueItem(I) == false) |
633 | return false; | |
634 | } | |
93bf083d | 635 | |
b185acc2 | 636 | return true; |
c88edf1d AL |
637 | } |
638 | /*}}}*/ | |
be4401bf AL |
639 | // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/ |
640 | // --------------------------------------------------------------------- | |
b185acc2 | 641 | /* This is called when an item in multiple queues is dequeued */ |
be4401bf AL |
642 | void pkgAcquire::Queue::Bump() |
643 | { | |
b185acc2 | 644 | Cycle(); |
be4401bf AL |
645 | } |
646 | /*}}}*/ | |
b98f2859 AL |
647 | |
648 | // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/ | |
649 | // --------------------------------------------------------------------- | |
650 | /* */ | |
651 | pkgAcquireStatus::pkgAcquireStatus() | |
652 | { | |
653 | Start(); | |
654 | } | |
655 | /*}}}*/ | |
656 | // AcquireStatus::Pulse - Called periodically /*{{{*/ | |
657 | // --------------------------------------------------------------------- | |
658 | /* This computes some internal state variables for the derived classes to | |
659 | use. It generates the current downloaded bytes and total bytes to download | |
660 | as well as the current CPS estimate. */ | |
661 | void pkgAcquireStatus::Pulse(pkgAcquire *Owner) | |
662 | { | |
663 | TotalBytes = 0; | |
664 | CurrentBytes = 0; | |
d568ed2d AL |
665 | TotalItems = 0; |
666 | CurrentItems = 0; | |
b98f2859 AL |
667 | |
668 | // Compute the total number of bytes to fetch | |
669 | unsigned int Unknown = 0; | |
670 | unsigned int Count = 0; | |
671 | for (pkgAcquire::Item **I = Owner->ItemsBegin(); I != Owner->ItemsEnd(); | |
672 | I++, Count++) | |
673 | { | |
d568ed2d AL |
674 | TotalItems++; |
675 | if ((*I)->Status == pkgAcquire::Item::StatDone) | |
676 | CurrentItems++; | |
677 | ||
a6568219 AL |
678 | // Totally ignore local items |
679 | if ((*I)->Local == true) | |
680 | continue; | |
681 | ||
b98f2859 AL |
682 | TotalBytes += (*I)->FileSize; |
683 | if ((*I)->Complete == true) | |
684 | CurrentBytes += (*I)->FileSize; | |
685 | if ((*I)->FileSize == 0 && (*I)->Complete == false) | |
686 | Unknown++; | |
687 | } | |
688 | ||
689 | // Compute the current completion | |
690 | for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0; | |
691 | I = Owner->WorkerStep(I)) | |
692 | if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false) | |
693 | CurrentBytes += I->CurrentSize; | |
694 | ||
695 | // Normalize the figures and account for unknown size downloads | |
696 | if (TotalBytes <= 0) | |
697 | TotalBytes = 1; | |
698 | if (Unknown == Count) | |
699 | TotalBytes = Unknown; | |
700 | else | |
701 | TotalBytes += TotalBytes/(Count - Unknown)*Unknown; | |
702 | ||
703 | // Compute the CPS | |
704 | struct timeval NewTime; | |
705 | gettimeofday(&NewTime,0); | |
706 | if (NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec || | |
707 | NewTime.tv_sec - Time.tv_sec > 6) | |
708 | { | |
f17ac097 AL |
709 | double Delta = NewTime.tv_sec - Time.tv_sec + |
710 | (NewTime.tv_usec - Time.tv_usec)/1000000.0; | |
b98f2859 | 711 | |
b98f2859 | 712 | // Compute the CPS value |
f17ac097 | 713 | if (Delta < 0.01) |
e331f6ed AL |
714 | CurrentCPS = 0; |
715 | else | |
f17ac097 | 716 | CurrentCPS = (CurrentBytes - LastBytes)/Delta; |
b98f2859 | 717 | LastBytes = CurrentBytes; |
6d5dd02a | 718 | ElapsedTime = (unsigned long)Delta; |
b98f2859 AL |
719 | Time = NewTime; |
720 | } | |
721 | } | |
722 | /*}}}*/ | |
723 | // AcquireStatus::Start - Called when the download is started /*{{{*/ | |
724 | // --------------------------------------------------------------------- | |
725 | /* We just reset the counters */ | |
726 | void pkgAcquireStatus::Start() | |
727 | { | |
728 | gettimeofday(&Time,0); | |
729 | gettimeofday(&StartTime,0); | |
730 | LastBytes = 0; | |
731 | CurrentCPS = 0; | |
732 | CurrentBytes = 0; | |
733 | TotalBytes = 0; | |
734 | FetchedBytes = 0; | |
735 | ElapsedTime = 0; | |
d568ed2d AL |
736 | TotalItems = 0; |
737 | CurrentItems = 0; | |
b98f2859 AL |
738 | } |
739 | /*}}}*/ | |
a6568219 | 740 | // AcquireStatus::Stop - Finished downloading /*{{{*/ |
b98f2859 AL |
741 | // --------------------------------------------------------------------- |
742 | /* This accurately computes the elapsed time and the total overall CPS. */ | |
743 | void pkgAcquireStatus::Stop() | |
744 | { | |
745 | // Compute the CPS and elapsed time | |
746 | struct timeval NewTime; | |
747 | gettimeofday(&NewTime,0); | |
748 | ||
749 | // Compute the delta time with full accuracy | |
750 | long usdiff = NewTime.tv_usec - StartTime.tv_usec; | |
751 | long sdiff = NewTime.tv_sec - StartTime.tv_sec; | |
752 | ||
753 | // Borrow | |
754 | if (usdiff < 0) | |
755 | { | |
756 | usdiff += 1000000; | |
757 | sdiff--; | |
758 | } | |
e331f6ed | 759 | |
b98f2859 | 760 | // Compute the CPS value |
e331f6ed AL |
761 | if (sdiff == 0 && usdiff == 0) |
762 | CurrentCPS = 0; | |
763 | else | |
764 | CurrentCPS = FetchedBytes/(sdiff + usdiff/1000000.0); | |
b98f2859 AL |
765 | LastBytes = CurrentBytes; |
766 | ElapsedTime = sdiff; | |
767 | } | |
768 | /*}}}*/ | |
769 | // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/ | |
770 | // --------------------------------------------------------------------- | |
771 | /* This is used to get accurate final transfer rate reporting. */ | |
772 | void pkgAcquireStatus::Fetched(unsigned long Size,unsigned long Resume) | |
93274b8d | 773 | { |
b98f2859 AL |
774 | FetchedBytes += Size - Resume; |
775 | } | |
776 | /*}}}*/ |