]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.50 2004/03/17 05:17:11 mdz Exp $
4 /* ######################################################################
6 Acquire - File Acquiration
8 The core element for the schedule 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 controlled by how the queue
11 name is derived from the URI.
13 ##################################################################### */
15 // Include Files /*{{{*/
18 #include <apt-pkg/acquire.h>
19 #include <apt-pkg/acquire-item.h>
20 #include <apt-pkg/acquire-worker.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/fileutl.h>
38 #include <sys/select.h>
47 // Acquire::pkgAcquire - Constructor /*{{{*/
48 // ---------------------------------------------------------------------
49 /* We grab some runtime state from the configuration space */
50 pkgAcquire::pkgAcquire() : LockFD(-1), Queues(0), Workers(0), Configs(0), Log(NULL
), ToFetch(0),
51 Debug(_config
->FindB("Debug::pkgAcquire",false)),
54 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
55 if (strcasecmp(Mode
.c_str(),"host") == 0)
56 QueueMode
= QueueHost
;
57 if (strcasecmp(Mode
.c_str(),"access") == 0)
58 QueueMode
= QueueAccess
;
60 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Progress
) : LockFD(-1), Queues(0), Workers(0),
61 Configs(0), Log(Progress
), ToFetch(0),
62 Debug(_config
->FindB("Debug::pkgAcquire",false)),
65 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
66 if (strcasecmp(Mode
.c_str(),"host") == 0)
67 QueueMode
= QueueHost
;
68 if (strcasecmp(Mode
.c_str(),"access") == 0)
69 QueueMode
= QueueAccess
;
73 // Acquire::Setup - Delayed Constructor /*{{{*/
74 // ---------------------------------------------------------------------
75 /* Do everything needed to be a complete Acquire object and report the
76 success (or failure) back so the user knows that something is wrong… */
77 bool pkgAcquire::Setup(pkgAcquireStatus
*Progress
, string
const &Lock
,
78 bool const createDirectories
)
82 // check for existence and possibly create auxiliary directories
83 if (createDirectories
== true)
85 string
const listDir
= _config
->FindDir("Dir::State::lists");
86 string
const partialListDir
= listDir
+ "partial/";
87 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
88 string
const partialArchivesDir
= archivesDir
+ "partial/";
90 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::State"), partialListDir
) == false &&
91 CreateAPTDirectoryIfNeeded(listDir
, partialListDir
) == false)
92 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
94 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::Cache"), partialArchivesDir
) == false &&
95 CreateAPTDirectoryIfNeeded(archivesDir
, partialArchivesDir
) == false)
96 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
99 if (Lock
.empty() == true || _config
->FindB("Debug::NoLocking", false) == true)
102 // Lock the directory this acquire object will work in
103 LockFD
= GetLock(flCombine(Lock
, "lock"));
105 return _error
->Error(_("Unable to lock directory %s"), Lock
.c_str());
110 // Acquire::~pkgAcquire - Destructor /*{{{*/
111 // ---------------------------------------------------------------------
112 /* Free our memory, clean up the queues (destroy the workers) */
113 pkgAcquire::~pkgAcquire()
122 MethodConfig
*Jnk
= Configs
;
123 Configs
= Configs
->Next
;
128 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
129 // ---------------------------------------------------------------------
131 void pkgAcquire::Shutdown()
133 while (Items
.empty() == false)
135 if (Items
[0]->Status
== Item::StatFetching
)
136 Items
[0]->Status
= Item::StatError
;
143 Queues
= Queues
->Next
;
148 // Acquire::Add - Add a new item /*{{{*/
149 // ---------------------------------------------------------------------
150 /* This puts an item on the acquire list. This list is mainly for tracking
152 void pkgAcquire::Add(Item
*Itm
)
154 Items
.push_back(Itm
);
157 // Acquire::Remove - Remove a item /*{{{*/
158 // ---------------------------------------------------------------------
159 /* Remove an item from the acquire list. This is usually not used.. */
160 void pkgAcquire::Remove(Item
*Itm
)
164 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
176 // Acquire::Add - Add a worker /*{{{*/
177 // ---------------------------------------------------------------------
178 /* A list of workers is kept so that the select loop can direct their FD
180 void pkgAcquire::Add(Worker
*Work
)
182 Work
->NextAcquire
= Workers
;
186 // Acquire::Remove - Remove a worker /*{{{*/
187 // ---------------------------------------------------------------------
188 /* A worker has died. This can not be done while the select loop is running
189 as it would require that RunFds could handling a changing list state and
191 void pkgAcquire::Remove(Worker
*Work
)
196 Worker
**I
= &Workers
;
200 *I
= (*I
)->NextAcquire
;
202 I
= &(*I
)->NextAcquire
;
206 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
207 // ---------------------------------------------------------------------
208 /* This is the entry point for an item. An item calls this function when
209 it is constructed which creates a queue (based on the current queue
210 mode) and puts the item in that queue. If the system is running then
211 the queue might be started. */
212 void pkgAcquire::Enqueue(ItemDesc
&Item
)
214 // Determine which queue to put the item in
215 const MethodConfig
*Config
;
216 string Name
= QueueName(Item
.URI
,Config
);
217 if (Name
.empty() == true)
220 // Find the queue structure
222 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
225 I
= new Queue(Name
,this);
233 // See if this is a local only URI
234 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
235 Item
.Owner
->Local
= true;
236 Item
.Owner
->Status
= Item::StatIdle
;
238 // Queue it into the named queue
245 clog
<< "Fetching " << Item
.URI
<< endl
;
246 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
247 clog
<< " Queue is: " << Name
<< endl
;
251 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
252 // ---------------------------------------------------------------------
253 /* This is called when an item is finished being fetched. It removes it
254 from all the queues */
255 void pkgAcquire::Dequeue(Item
*Itm
)
260 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
262 for (; I
!= 0; I
= I
->Next
)
268 clog
<< "Dequeued from " << I
->Name
<< endl
;
276 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
277 // ---------------------------------------------------------------------
278 /* The string returned depends on the configuration settings and the
279 method parameters. Given something like http://foo.org/bar it can
280 return http://foo.org or http */
281 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
285 Config
= GetConfig(U
.Access
);
289 /* Single-Instance methods get exactly one queue per URI. This is
290 also used for the Access queue method */
291 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
294 string AccessSchema
= U
.Access
+ ':',
295 FullQueueName
= AccessSchema
+ U
.Host
;
296 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
299 for (; I
!= 0; I
= I
->Next
) {
300 // if the queue already exists, re-use it
301 if (I
->Name
== FullQueueName
)
302 return FullQueueName
;
304 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
309 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
312 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
315 return FullQueueName
;
318 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
319 // ---------------------------------------------------------------------
320 /* This locates the configuration structure for an access method. If
321 a config structure cannot be found a Worker will be created to
323 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
325 // Search for an existing config
327 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
328 if (Conf
->Access
== Access
)
331 // Create the new config class
332 Conf
= new MethodConfig
;
333 Conf
->Access
= Access
;
334 Conf
->Next
= Configs
;
337 // Create the worker to fetch the configuration
339 if (Work
.Start() == false)
342 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
343 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
344 Conf
->SingleInstance
= true;
349 // Acquire::SetFds - Deal with readable FDs /*{{{*/
350 // ---------------------------------------------------------------------
351 /* Collect FDs that have activity monitors into the fd sets */
352 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
354 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
356 if (I
->InReady
== true && I
->InFd
>= 0)
360 FD_SET(I
->InFd
,RSet
);
362 if (I
->OutReady
== true && I
->OutFd
>= 0)
366 FD_SET(I
->OutFd
,WSet
);
371 // Acquire::RunFds - Deal with active FDs /*{{{*/
372 // ---------------------------------------------------------------------
373 /* Dispatch active FDs over to the proper workers. It is very important
374 that a worker never be erased while this is running! The queue class
375 should never erase a worker except during shutdown processing. */
376 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
378 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
380 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
382 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
387 // Acquire::Run - Run the fetch sequence /*{{{*/
388 // ---------------------------------------------------------------------
389 /* This runs the queues. It manages a select loop for all of the
390 Worker tasks. The workers interact with the queues and items to
391 manage the actual fetch. */
392 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
396 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
402 bool WasCancelled
= false;
404 // Run till all things have been acquired
407 tv
.tv_usec
= PulseIntervall
;
415 SetFds(Highest
,&RFds
,&WFds
);
420 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
422 while (Res
< 0 && errno
== EINTR
);
426 _error
->Errno("select","Select has failed");
431 if (_error
->PendingError() == true)
434 // Timeout, notify the log class
435 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
437 tv
.tv_usec
= PulseIntervall
;
438 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
440 if (Log
!= 0 && Log
->Pulse(this) == false)
451 // Shut down the acquire bits
453 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
456 // Shut down the items
457 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
460 if (_error
->PendingError())
467 // Acquire::Bump - Called when an item is dequeued /*{{{*/
468 // ---------------------------------------------------------------------
469 /* This routine bumps idle queues in hopes that they will be able to fetch
471 void pkgAcquire::Bump()
473 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
477 // Acquire::WorkerStep - Step to the next worker /*{{{*/
478 // ---------------------------------------------------------------------
479 /* Not inlined to advoid including acquire-worker.h */
480 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
482 return I
->NextAcquire
;
485 // Acquire::Clean - Cleans a directory /*{{{*/
486 // ---------------------------------------------------------------------
487 /* This is a bit simplistic, it looks at every file in the dir and sees
488 if it is part of the download set. */
489 bool pkgAcquire::Clean(string Dir
)
491 // non-existing directories are by definition clean…
492 if (DirectoryExists(Dir
) == false)
496 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
498 DIR *D
= opendir(Dir
.c_str());
500 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
502 string StartDir
= SafeGetCWD();
503 if (chdir(Dir
.c_str()) != 0)
506 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
509 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
512 if (strcmp(Dir
->d_name
,"lock") == 0 ||
513 strcmp(Dir
->d_name
,"partial") == 0 ||
514 strcmp(Dir
->d_name
,".") == 0 ||
515 strcmp(Dir
->d_name
,"..") == 0)
518 // Look in the get list
519 ItemCIterator I
= Items
.begin();
520 for (; I
!= Items
.end(); ++I
)
521 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
524 // Nothing found, nuke it
525 if (I
== Items
.end())
530 if (chdir(StartDir
.c_str()) != 0)
531 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
535 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
536 // ---------------------------------------------------------------------
537 /* This is the total number of bytes needed */
538 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
540 unsigned long long Total
= 0;
541 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
542 Total
+= (*I
)->FileSize
;
546 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
547 // ---------------------------------------------------------------------
548 /* This is the number of bytes that is not local */
549 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
551 unsigned long long Total
= 0;
552 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
553 if ((*I
)->Local
== false)
554 Total
+= (*I
)->FileSize
;
558 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
559 // ---------------------------------------------------------------------
560 /* This is the number of bytes that is not local */
561 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
563 unsigned long long Total
= 0;
564 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
565 if ((*I
)->Local
== false)
566 Total
+= (*I
)->PartialSize
;
570 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
571 // ---------------------------------------------------------------------
573 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
575 return UriIterator(Queues
);
578 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
579 // ---------------------------------------------------------------------
581 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
583 return UriIterator(0);
586 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
587 // ---------------------------------------------------------------------
589 pkgAcquire::MethodConfig::MethodConfig() : d(NULL
), Next(0), SingleInstance(false),
590 Pipeline(false), SendConfig(false), LocalOnly(false), NeedsCleanup(false),
595 // Queue::Queue - Constructor /*{{{*/
596 // ---------------------------------------------------------------------
598 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : d(NULL
), Next(0),
599 Name(Name
), Items(0), Workers(0), Owner(Owner
), PipeDepth(0), MaxPipeDepth(1)
603 // Queue::~Queue - Destructor /*{{{*/
604 // ---------------------------------------------------------------------
606 pkgAcquire::Queue::~Queue()
618 // Queue::Enqueue - Queue an item to the queue /*{{{*/
619 // ---------------------------------------------------------------------
621 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
624 // move to the end of the queue and check for duplicates here
625 for (; *I
!= 0; I
= &(*I
)->Next
)
626 if (Item
.URI
== (*I
)->URI
)
628 Item
.Owner
->Status
= Item::StatDone
;
633 QItem
*Itm
= new QItem
;
638 Item
.Owner
->QueueCounter
++;
639 if (Items
->Next
== 0)
644 // Queue::Dequeue - Remove an item from the queue /*{{{*/
645 // ---------------------------------------------------------------------
646 /* We return true if we hit something */
647 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
649 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
650 return _error
->Error("Tried to dequeue a fetching object");
657 if ((*I
)->Owner
== Owner
)
661 Owner
->QueueCounter
--;
672 // Queue::Startup - Start the worker processes /*{{{*/
673 // ---------------------------------------------------------------------
674 /* It is possible for this to be called with a pre-existing set of
676 bool pkgAcquire::Queue::Startup()
681 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
685 Workers
= new Worker(this,Cnf
,Owner
->Log
);
687 if (Workers
->Start() == false)
690 /* When pipelining we commit 10 items. This needs to change when we
691 added other source retry to have cycle maintain a pipeline depth
693 if (Cnf
->Pipeline
== true)
694 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
702 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
703 // ---------------------------------------------------------------------
704 /* If final is true then all workers are eliminated, otherwise only workers
705 that do not need cleanup are removed */
706 bool pkgAcquire::Queue::Shutdown(bool Final
)
708 // Delete all of the workers
709 pkgAcquire::Worker
**Cur
= &Workers
;
712 pkgAcquire::Worker
*Jnk
= *Cur
;
713 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
715 *Cur
= Jnk
->NextQueue
;
720 Cur
= &(*Cur
)->NextQueue
;
726 // Queue::FindItem - Find a URI in the item list /*{{{*/
727 // ---------------------------------------------------------------------
729 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
731 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
732 if (I
->URI
== URI
&& I
->Worker
== Owner
)
737 // Queue::ItemDone - Item has been completed /*{{{*/
738 // ---------------------------------------------------------------------
739 /* The worker signals this which causes the item to be removed from the
740 queue. If this is the last queue instance then it is removed from the
742 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
745 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
746 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
748 if (Itm
->Owner
->QueueCounter
<= 1)
749 Owner
->Dequeue(Itm
->Owner
);
759 // Queue::Cycle - Queue new items into the method /*{{{*/
760 // ---------------------------------------------------------------------
761 /* This locates a new idle item and sends it to the worker. If pipelining
762 is enabled then it keeps the pipe full. */
763 bool pkgAcquire::Queue::Cycle()
765 if (Items
== 0 || Workers
== 0)
769 return _error
->Error("Pipedepth failure");
771 // Look for a queable item
773 while (PipeDepth
< (signed)MaxPipeDepth
)
775 for (; I
!= 0; I
= I
->Next
)
776 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
779 // Nothing to do, queue is idle.
784 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
786 if (Workers
->QueueItem(I
) == false)
793 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
794 // ---------------------------------------------------------------------
795 /* This is called when an item in multiple queues is dequeued */
796 void pkgAcquire::Queue::Bump()
801 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
802 // ---------------------------------------------------------------------
804 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Percent(0), Update(true), MorePulses(false)
809 // AcquireStatus::Pulse - Called periodically /*{{{*/
810 // ---------------------------------------------------------------------
811 /* This computes some internal state variables for the derived classes to
812 use. It generates the current downloaded bytes and total bytes to download
813 as well as the current CPS estimate. */
814 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
821 // Compute the total number of bytes to fetch
822 unsigned int Unknown
= 0;
823 unsigned int Count
= 0;
824 bool UnfetchedReleaseFiles
= false;
825 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
826 I
!= Owner
->ItemsEnd();
830 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
833 // Totally ignore local items
834 if ((*I
)->Local
== true)
837 // see if the method tells us to expect more
838 TotalItems
+= (*I
)->ExpectedAdditionalItems
;
840 // check if there are unfetched Release files
841 if ((*I
)->Complete
== false && (*I
)->ExpectedAdditionalItems
> 0)
842 UnfetchedReleaseFiles
= true;
844 TotalBytes
+= (*I
)->FileSize
;
845 if ((*I
)->Complete
== true)
846 CurrentBytes
+= (*I
)->FileSize
;
847 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
851 // Compute the current completion
852 unsigned long long ResumeSize
= 0;
853 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
854 I
= Owner
->WorkerStep(I
))
856 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
858 CurrentBytes
+= I
->CurrentSize
;
859 ResumeSize
+= I
->ResumePoint
;
861 // Files with unknown size always have 100% completion
862 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
863 I
->CurrentItem
->Owner
->Complete
== false)
864 TotalBytes
+= I
->CurrentSize
;
868 // Normalize the figures and account for unknown size downloads
871 if (Unknown
== Count
)
872 TotalBytes
= Unknown
;
874 // Wha?! Is not supposed to happen.
875 if (CurrentBytes
> TotalBytes
)
876 CurrentBytes
= TotalBytes
;
879 if (_config
->FindB("Debug::acquire::progress", false) == true)
880 std::clog
<< " Bytes: "
881 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
885 struct timeval NewTime
;
886 gettimeofday(&NewTime
,0);
887 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
888 NewTime
.tv_sec
- Time
.tv_sec
> 6)
890 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
891 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
893 // Compute the CPS value
897 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
898 LastBytes
= CurrentBytes
- ResumeSize
;
899 ElapsedTime
= (unsigned long long)Delta
;
903 // calculate the percentage, if we have too little data assume 1%
904 if (TotalBytes
> 0 && UnfetchedReleaseFiles
)
907 // use both files and bytes because bytes can be unreliable
908 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
909 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
911 int fd
= _config
->FindI("APT::Status-Fd",-1);
914 ostringstream status
;
917 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
918 unsigned long long ETA
= 0;
920 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
922 // only show the ETA if it makes sense
923 if (ETA
> 0 && ETA
< 172800 /* two days */ )
924 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
926 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
928 // build the status str
929 status
<< "dlstatus:" << i
930 << ":" << std::setprecision(3) << Percent
934 std::string
const dlstatus
= status
.str();
935 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
941 // AcquireStatus::Start - Called when the download is started /*{{{*/
942 // ---------------------------------------------------------------------
943 /* We just reset the counters */
944 void pkgAcquireStatus::Start()
946 gettimeofday(&Time
,0);
947 gettimeofday(&StartTime
,0);
958 // AcquireStatus::Stop - Finished downloading /*{{{*/
959 // ---------------------------------------------------------------------
960 /* This accurately computes the elapsed time and the total overall CPS. */
961 void pkgAcquireStatus::Stop()
963 // Compute the CPS and elapsed time
964 struct timeval NewTime
;
965 gettimeofday(&NewTime
,0);
967 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
968 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
970 // Compute the CPS value
974 CurrentCPS
= FetchedBytes
/Delta
;
975 LastBytes
= CurrentBytes
;
976 ElapsedTime
= (unsigned long long)Delta
;
979 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
980 // ---------------------------------------------------------------------
981 /* This is used to get accurate final transfer rate reporting. */
982 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
984 FetchedBytes
+= Size
- Resume
;