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