]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
Support for memory-only caching
[apt.git] / apt-pkg / acquire.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire.cc,v 1.32 1999/04/07 06:02:56 jgg Exp $
4 /* ######################################################################
5
6 Acquire - File Acquiration
7
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
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>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/strutl.h>
25
26 #include <dirent.h>
27 #include <sys/time.h>
28 #include <errno.h>
29 /*}}}*/
30
31 // Acquire::pkgAcquire - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* We grab some runtime state from the configuration space */
34 pkgAcquire::pkgAcquire(pkgAcquireStatus *Log) : Log(Log)
35 {
36 Queues = 0;
37 Configs = 0;
38 Workers = 0;
39 ToFetch = 0;
40 Running = false;
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);
49 }
50 /*}}}*/
51 // Acquire::~pkgAcquire - Destructor /*{{{*/
52 // ---------------------------------------------------------------------
53 /* Free our memory, clean up the queues (destroy the workers) */
54 pkgAcquire::~pkgAcquire()
55 {
56 while (Items.size() != 0)
57 delete Items[0];
58
59 while (Configs != 0)
60 {
61 MethodConfig *Jnk = Configs;
62 Configs = Configs->Next;
63 delete Jnk;
64 }
65
66 while (Queues != 0)
67 {
68 Queue *Jnk = Queues;
69 Queues = Queues->Next;
70 delete Jnk;
71 }
72 }
73 /*}}}*/
74 // Acquire::Add - Add a new item /*{{{*/
75 // ---------------------------------------------------------------------
76 /* This puts an item on the acquire list. This list is mainly for tracking
77 item status */
78 void pkgAcquire::Add(Item *Itm)
79 {
80 Items.push_back(Itm);
81 }
82 /*}}}*/
83 // Acquire::Remove - Remove a item /*{{{*/
84 // ---------------------------------------------------------------------
85 /* Remove an item from the acquire list. This is usually not used.. */
86 void 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);
92 }
93 }
94 /*}}}*/
95 // Acquire::Add - Add a worker /*{{{*/
96 // ---------------------------------------------------------------------
97 /* A list of workers is kept so that the select loop can direct their FD
98 usage. */
99 void pkgAcquire::Add(Worker *Work)
100 {
101 Work->NextAcquire = Workers;
102 Workers = Work;
103 }
104 /*}}}*/
105 // Acquire::Remove - Remove a worker /*{{{*/
106 // ---------------------------------------------------------------------
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.. */
110 void pkgAcquire::Remove(Worker *Work)
111 {
112 if (Running == true)
113 abort();
114
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 /*}}}*/
125 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
126 // ---------------------------------------------------------------------
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. */
131 void pkgAcquire::Enqueue(ItemDesc &Item)
132 {
133 // Determine which queue to put the item in
134 const MethodConfig *Config;
135 string Name = QueueName(Item.URI,Config);
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;
147
148 if (Running == true)
149 I->Startup();
150 }
151
152 // See if this is a local only URI
153 if (Config->LocalOnly == true && Item.Owner->Complete == false)
154 Item.Owner->Local = true;
155 Item.Owner->Status = Item::StatIdle;
156
157 // Queue it into the named queue
158 I->Enqueue(Item);
159 ToFetch++;
160
161 // Some trace stuff
162 if (Debug == true)
163 {
164 clog << "Fetching " << Item.URI << endl;
165 clog << " to " << Item.Owner->DestFile << endl;
166 clog << " Queue is: " << Name << endl;
167 }
168 }
169 /*}}}*/
170 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This is called when an item is finished being fetched. It removes it
173 from all the queues */
174 void pkgAcquire::Dequeue(Item *Itm)
175 {
176 Queue *I = Queues;
177 bool Res = false;
178 for (; I != 0; I = I->Next)
179 Res |= I->Dequeue(Itm);
180
181 if (Debug == true)
182 clog << "Dequeuing " << Itm->DestFile << endl;
183 if (Res == true)
184 ToFetch--;
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 */
192 string pkgAcquire::QueueName(string Uri,MethodConfig const *&Config)
193 {
194 URI U(Uri);
195
196 Config = GetConfig(U.Access);
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)
203 return U.Access;
204
205 return U.Access + ':' + U.Host;
206 }
207 /*}}}*/
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 */
213 pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
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;
226
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 /*}}}*/
235 // Acquire::SetFds - Deal with readable FDs /*{{{*/
236 // ---------------------------------------------------------------------
237 /* Collect FDs that have activity monitors into the fd sets */
238 void 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 // ---------------------------------------------------------------------
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. */
262 void 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. */
278 bool pkgAcquire::Run()
279 {
280 Running = true;
281
282 for (Queue *I = Queues; I != 0; I = I->Next)
283 I->Startup();
284
285 if (Log != 0)
286 Log->Start();
287
288 // Run till all things have been acquired
289 struct timeval tv;
290 tv.tv_sec = 0;
291 tv.tv_usec = 500000;
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
301 int Res;
302 do
303 {
304 Res = select(Highest+1,&RFds,&WFds,0,&tv);
305 }
306 while (Res < 0 && errno == EINTR);
307
308 if (Res < 0)
309 {
310 _error->Errno("select","Select has failed");
311 break;
312 }
313
314 RunFds(&RFds,&WFds);
315 if (_error->PendingError() == true)
316 break;
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 }
327 }
328
329 if (Log != 0)
330 Log->Stop();
331
332 // Shut down the acquire bits
333 Running = false;
334 for (Queue *I = Queues; I != 0; I = I->Next)
335 I->Shutdown();
336
337 return !_error->PendingError();
338 }
339 /*}}}*/
340 // Acquire::Bump - Called when an item is dequeued /*{{{*/
341 // ---------------------------------------------------------------------
342 /* This routine bumps idle queues in hopes that they will be able to fetch
343 the dequeued item */
344 void pkgAcquire::Bump()
345 {
346 for (Queue *I = Queues; I != 0; I = I->Next)
347 I->Bump();
348 }
349 /*}}}*/
350 // Acquire::WorkerStep - Step to the next worker /*{{{*/
351 // ---------------------------------------------------------------------
352 /* Not inlined to advoid including acquire-worker.h */
353 pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
354 {
355 return I->NextAcquire;
356 };
357 /*}}}*/
358 // Acquire::Clean - Cleans a directory /*{{{*/
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. */
362 bool 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 /*}}}*/
400 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
401 // ---------------------------------------------------------------------
402 /* This is the total number of bytes needed */
403 unsigned 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 */
414 unsigned 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 /*}}}*/
423 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
424 // ---------------------------------------------------------------------
425 /* This is the number of bytes that is not local */
426 unsigned long pkgAcquire::PartialPresent()
427 {
428 unsigned long Total = 0;
429 for (pkgAcquire::Item **I = ItemsBegin(); I != ItemsEnd(); I++)
430 if ((*I)->Local == false)
431 Total += (*I)->PartialSize;
432 return Total;
433 }
434 /*}}}*/
435 // pkgAcquire::UriBegin - Start iterator for the uri list /*{{{*/
436 // ---------------------------------------------------------------------
437 /* */
438 pkgAcquire::UriIterator pkgAcquire::UriBegin()
439 {
440 return UriIterator(Queues);
441 }
442 /*}}}*/
443 // pkgAcquire::UriEnd - End iterator for the uri list /*{{{*/
444 // ---------------------------------------------------------------------
445 /* */
446 pkgAcquire::UriIterator pkgAcquire::UriEnd()
447 {
448 return UriIterator(0);
449 }
450 /*}}}*/
451
452 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
453 // ---------------------------------------------------------------------
454 /* */
455 pkgAcquire::MethodConfig::MethodConfig()
456 {
457 SingleInstance = false;
458 Pipeline = false;
459 SendConfig = false;
460 LocalOnly = false;
461 Next = 0;
462 }
463 /*}}}*/
464
465 // Queue::Queue - Constructor /*{{{*/
466 // ---------------------------------------------------------------------
467 /* */
468 pkgAcquire::Queue::Queue(string Name,pkgAcquire *Owner) : Name(Name),
469 Owner(Owner)
470 {
471 Items = 0;
472 Next = 0;
473 Workers = 0;
474 MaxPipeDepth = 1;
475 PipeDepth = 0;
476 }
477 /*}}}*/
478 // Queue::~Queue - Destructor /*{{{*/
479 // ---------------------------------------------------------------------
480 /* */
481 pkgAcquire::Queue::~Queue()
482 {
483 Shutdown();
484
485 while (Items != 0)
486 {
487 QItem *Jnk = Items;
488 Items = Items->Next;
489 delete Jnk;
490 }
491 }
492 /*}}}*/
493 // Queue::Enqueue - Queue an item to the queue /*{{{*/
494 // ---------------------------------------------------------------------
495 /* */
496 void pkgAcquire::Queue::Enqueue(ItemDesc &Item)
497 {
498 QItem **I = &Items;
499 for (; *I != 0; I = &(*I)->Next);
500
501 // Create a new item
502 QItem *Itm = new QItem;
503 *Itm = Item;
504 Itm->Next = 0;
505 *I = Itm;
506
507 Item.Owner->QueueCounter++;
508 if (Items->Next == 0)
509 Cycle();
510 }
511 /*}}}*/
512 // Queue::Dequeue - Remove an item from the queue /*{{{*/
513 // ---------------------------------------------------------------------
514 /* We return true if we hit something */
515 bool pkgAcquire::Queue::Dequeue(Item *Owner)
516 {
517 if (Owner->Status == pkgAcquire::Item::StatFetching)
518 return _error->Error("Tried to dequeue a fetching object");
519
520 bool Res = false;
521
522 QItem **I = &Items;
523 for (; *I != 0;)
524 {
525 if ((*I)->Owner == Owner)
526 {
527 QItem *Jnk= *I;
528 *I = (*I)->Next;
529 Owner->QueueCounter--;
530 delete Jnk;
531 Res = true;
532 }
533 else
534 I = &(*I)->Next;
535 }
536
537 return Res;
538 }
539 /*}}}*/
540 // Queue::Startup - Start the worker processes /*{{{*/
541 // ---------------------------------------------------------------------
542 /* */
543 bool pkgAcquire::Queue::Startup()
544 {
545 Shutdown();
546
547 URI U(Name);
548 pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
549 if (Cnf == 0)
550 return false;
551
552 Workers = new Worker(this,Cnf,Owner->Log);
553 Owner->Add(Workers);
554 if (Workers->Start() == false)
555 return false;
556
557 /* When pipelining we commit 10 items. This needs to change when we
558 added other source retry to have cycle maintain a pipeline depth
559 on its own. */
560 if (Cnf->Pipeline == true)
561 MaxPipeDepth = 10;
562 else
563 MaxPipeDepth = 1;
564
565 return Cycle();
566 }
567 /*}}}*/
568 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
569 // ---------------------------------------------------------------------
570 /* */
571 bool pkgAcquire::Queue::Shutdown()
572 {
573 // Delete all of the workers
574 while (Workers != 0)
575 {
576 pkgAcquire::Worker *Jnk = Workers;
577 Workers = Workers->NextQueue;
578 Owner->Remove(Jnk);
579 delete Jnk;
580 }
581
582 return true;
583 }
584 /*}}}*/
585 // Queue::FindItem - Find a URI in the item list /*{{{*/
586 // ---------------------------------------------------------------------
587 /* */
588 pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
589 {
590 for (QItem *I = Items; I != 0; I = I->Next)
591 if (I->URI == URI && I->Worker == Owner)
592 return I;
593 return 0;
594 }
595 /*}}}*/
596 // Queue::ItemDone - Item has been completed /*{{{*/
597 // ---------------------------------------------------------------------
598 /* The worker signals this which causes the item to be removed from the
599 queue. If this is the last queue instance then it is removed from the
600 main queue too.*/
601 bool pkgAcquire::Queue::ItemDone(QItem *Itm)
602 {
603 PipeDepth--;
604 if (Itm->Owner->Status == pkgAcquire::Item::StatFetching)
605 Itm->Owner->Status = pkgAcquire::Item::StatDone;
606
607 if (Itm->Owner->QueueCounter <= 1)
608 Owner->Dequeue(Itm->Owner);
609 else
610 {
611 Dequeue(Itm->Owner);
612 Owner->Bump();
613 }
614
615 return Cycle();
616 }
617 /*}}}*/
618 // Queue::Cycle - Queue new items into the method /*{{{*/
619 // ---------------------------------------------------------------------
620 /* This locates a new idle item and sends it to the worker. If pipelining
621 is enabled then it keeps the pipe full. */
622 bool pkgAcquire::Queue::Cycle()
623 {
624 if (Items == 0 || Workers == 0)
625 return true;
626
627 if (PipeDepth < 0)
628 return _error->Error("Pipedepth failure");
629
630 // Look for a queable item
631 QItem *I = Items;
632 while (PipeDepth < (signed)MaxPipeDepth)
633 {
634 for (; I != 0; I = I->Next)
635 if (I->Owner->Status == pkgAcquire::Item::StatIdle)
636 break;
637
638 // Nothing to do, queue is idle.
639 if (I == 0)
640 return true;
641
642 I->Worker = Workers;
643 I->Owner->Status = pkgAcquire::Item::StatFetching;
644 PipeDepth++;
645 if (Workers->QueueItem(I) == false)
646 return false;
647 }
648
649 return true;
650 }
651 /*}}}*/
652 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
653 // ---------------------------------------------------------------------
654 /* This is called when an item in multiple queues is dequeued */
655 void pkgAcquire::Queue::Bump()
656 {
657 Cycle();
658 }
659 /*}}}*/
660
661 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
662 // ---------------------------------------------------------------------
663 /* */
664 pkgAcquireStatus::pkgAcquireStatus()
665 {
666 Start();
667 }
668 /*}}}*/
669 // AcquireStatus::Pulse - Called periodically /*{{{*/
670 // ---------------------------------------------------------------------
671 /* This computes some internal state variables for the derived classes to
672 use. It generates the current downloaded bytes and total bytes to download
673 as well as the current CPS estimate. */
674 void pkgAcquireStatus::Pulse(pkgAcquire *Owner)
675 {
676 TotalBytes = 0;
677 CurrentBytes = 0;
678 TotalItems = 0;
679 CurrentItems = 0;
680
681 // Compute the total number of bytes to fetch
682 unsigned int Unknown = 0;
683 unsigned int Count = 0;
684 for (pkgAcquire::Item **I = Owner->ItemsBegin(); I != Owner->ItemsEnd();
685 I++, Count++)
686 {
687 TotalItems++;
688 if ((*I)->Status == pkgAcquire::Item::StatDone)
689 CurrentItems++;
690
691 // Totally ignore local items
692 if ((*I)->Local == true)
693 continue;
694
695 TotalBytes += (*I)->FileSize;
696 if ((*I)->Complete == true)
697 CurrentBytes += (*I)->FileSize;
698 if ((*I)->FileSize == 0 && (*I)->Complete == false)
699 Unknown++;
700 }
701
702 // Compute the current completion
703 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
704 I = Owner->WorkerStep(I))
705 if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false)
706 CurrentBytes += I->CurrentSize;
707
708 // Normalize the figures and account for unknown size downloads
709 if (TotalBytes <= 0)
710 TotalBytes = 1;
711 if (Unknown == Count)
712 TotalBytes = Unknown;
713
714 // Compute the CPS
715 struct timeval NewTime;
716 gettimeofday(&NewTime,0);
717 if (NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec ||
718 NewTime.tv_sec - Time.tv_sec > 6)
719 {
720 double Delta = NewTime.tv_sec - Time.tv_sec +
721 (NewTime.tv_usec - Time.tv_usec)/1000000.0;
722
723 // Compute the CPS value
724 if (Delta < 0.01)
725 CurrentCPS = 0;
726 else
727 CurrentCPS = (CurrentBytes - LastBytes)/Delta;
728 LastBytes = CurrentBytes;
729 ElapsedTime = (unsigned long)Delta;
730 Time = NewTime;
731 }
732 }
733 /*}}}*/
734 // AcquireStatus::Start - Called when the download is started /*{{{*/
735 // ---------------------------------------------------------------------
736 /* We just reset the counters */
737 void pkgAcquireStatus::Start()
738 {
739 gettimeofday(&Time,0);
740 gettimeofday(&StartTime,0);
741 LastBytes = 0;
742 CurrentCPS = 0;
743 CurrentBytes = 0;
744 TotalBytes = 0;
745 FetchedBytes = 0;
746 ElapsedTime = 0;
747 TotalItems = 0;
748 CurrentItems = 0;
749 }
750 /*}}}*/
751 // AcquireStatus::Stop - Finished downloading /*{{{*/
752 // ---------------------------------------------------------------------
753 /* This accurately computes the elapsed time and the total overall CPS. */
754 void pkgAcquireStatus::Stop()
755 {
756 // Compute the CPS and elapsed time
757 struct timeval NewTime;
758 gettimeofday(&NewTime,0);
759
760 // Compute the delta time with full accuracy
761 long usdiff = NewTime.tv_usec - StartTime.tv_usec;
762 long sdiff = NewTime.tv_sec - StartTime.tv_sec;
763
764 // Borrow
765 if (usdiff < 0)
766 {
767 usdiff += 1000000;
768 sdiff--;
769 }
770
771 // Compute the CPS value
772 if (sdiff == 0 && usdiff == 0)
773 CurrentCPS = 0;
774 else
775 CurrentCPS = FetchedBytes/(sdiff + usdiff/1000000.0);
776 LastBytes = CurrentBytes;
777 ElapsedTime = sdiff;
778 }
779 /*}}}*/
780 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
781 // ---------------------------------------------------------------------
782 /* This is used to get accurate final transfer rate reporting. */
783 void pkgAcquireStatus::Fetched(unsigned long Size,unsigned long Resume)
784 {
785 FetchedBytes += Size - Resume;
786 }
787 /*}}}*/