]>
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>
46 // Acquire::pkgAcquire - Constructor /*{{{*/
47 // ---------------------------------------------------------------------
48 /* We grab some runtime state from the configuration space */
49 pkgAcquire::pkgAcquire() : LockFD(-1), Queues(0), Workers(0), Configs(0), Log(NULL
), ToFetch(0),
50 Debug(_config
->FindB("Debug::pkgAcquire",false)),
53 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
54 if (strcasecmp(Mode
.c_str(),"host") == 0)
55 QueueMode
= QueueHost
;
56 if (strcasecmp(Mode
.c_str(),"access") == 0)
57 QueueMode
= QueueAccess
;
59 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Progress
) : LockFD(-1), Queues(0), Workers(0),
60 Configs(0), Log(Progress
), ToFetch(0),
61 Debug(_config
->FindB("Debug::pkgAcquire",false)),
64 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
65 if (strcasecmp(Mode
.c_str(),"host") == 0)
66 QueueMode
= QueueHost
;
67 if (strcasecmp(Mode
.c_str(),"access") == 0)
68 QueueMode
= QueueAccess
;
72 // Acquire::Setup - Delayed Constructor /*{{{*/
73 // ---------------------------------------------------------------------
74 /* Do everything needed to be a complete Acquire object and report the
75 success (or failure) back so the user knows that something is wrong… */
76 bool pkgAcquire::Setup(pkgAcquireStatus
*Progress
, string
const &Lock
,
77 bool const createDirectories
)
81 // check for existence and possibly create auxiliary directories
82 if (createDirectories
== true)
84 string
const listDir
= _config
->FindDir("Dir::State::lists");
85 string
const partialListDir
= listDir
+ "partial/";
86 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
87 string
const partialArchivesDir
= archivesDir
+ "partial/";
89 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::State"), partialListDir
) == false &&
90 CreateAPTDirectoryIfNeeded(listDir
, partialListDir
) == false)
91 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
93 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::Cache"), partialArchivesDir
) == false &&
94 CreateAPTDirectoryIfNeeded(archivesDir
, partialArchivesDir
) == false)
95 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
98 if (Lock
.empty() == true || _config
->FindB("Debug::NoLocking", false) == true)
101 // Lock the directory this acquire object will work in
102 LockFD
= GetLock(flCombine(Lock
, "lock"));
104 return _error
->Error(_("Unable to lock directory %s"), Lock
.c_str());
109 // Acquire::~pkgAcquire - Destructor /*{{{*/
110 // ---------------------------------------------------------------------
111 /* Free our memory, clean up the queues (destroy the workers) */
112 pkgAcquire::~pkgAcquire()
121 MethodConfig
*Jnk
= Configs
;
122 Configs
= Configs
->Next
;
127 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
128 // ---------------------------------------------------------------------
130 void pkgAcquire::Shutdown()
132 while (Items
.empty() == false)
134 if (Items
[0]->Status
== Item::StatFetching
)
135 Items
[0]->Status
= Item::StatError
;
142 Queues
= Queues
->Next
;
147 // Acquire::Add - Add a new item /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This puts an item on the acquire list. This list is mainly for tracking
151 void pkgAcquire::Add(Item
*Itm
)
153 Items
.push_back(Itm
);
156 // Acquire::Remove - Remove a item /*{{{*/
157 // ---------------------------------------------------------------------
158 /* Remove an item from the acquire list. This is usually not used.. */
159 void pkgAcquire::Remove(Item
*Itm
)
163 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
175 // Acquire::Add - Add a worker /*{{{*/
176 // ---------------------------------------------------------------------
177 /* A list of workers is kept so that the select loop can direct their FD
179 void pkgAcquire::Add(Worker
*Work
)
181 Work
->NextAcquire
= Workers
;
185 // Acquire::Remove - Remove a worker /*{{{*/
186 // ---------------------------------------------------------------------
187 /* A worker has died. This can not be done while the select loop is running
188 as it would require that RunFds could handling a changing list state and
190 void pkgAcquire::Remove(Worker
*Work
)
195 Worker
**I
= &Workers
;
199 *I
= (*I
)->NextAcquire
;
201 I
= &(*I
)->NextAcquire
;
205 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
206 // ---------------------------------------------------------------------
207 /* This is the entry point for an item. An item calls this function when
208 it is constructed which creates a queue (based on the current queue
209 mode) and puts the item in that queue. If the system is running then
210 the queue might be started. */
211 void pkgAcquire::Enqueue(ItemDesc
&Item
)
213 // Determine which queue to put the item in
214 const MethodConfig
*Config
;
215 string Name
= QueueName(Item
.URI
,Config
);
216 if (Name
.empty() == true)
219 // Find the queue structure
221 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
224 I
= new Queue(Name
,this);
232 // See if this is a local only URI
233 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
234 Item
.Owner
->Local
= true;
235 Item
.Owner
->Status
= Item::StatIdle
;
237 // Queue it into the named queue
244 clog
<< "Fetching " << Item
.URI
<< endl
;
245 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
246 clog
<< " Queue is: " << Name
<< endl
;
250 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
251 // ---------------------------------------------------------------------
252 /* This is called when an item is finished being fetched. It removes it
253 from all the queues */
254 void pkgAcquire::Dequeue(Item
*Itm
)
259 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
261 for (; I
!= 0; I
= I
->Next
)
267 clog
<< "Dequeued from " << I
->Name
<< endl
;
275 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
276 // ---------------------------------------------------------------------
277 /* The string returned depends on the configuration settings and the
278 method parameters. Given something like http://foo.org/bar it can
279 return http://foo.org or http */
280 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
284 Config
= GetConfig(U
.Access
);
288 /* Single-Instance methods get exactly one queue per URI. This is
289 also used for the Access queue method */
290 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
293 string AccessSchema
= U
.Access
+ ':',
294 FullQueueName
= AccessSchema
+ U
.Host
;
295 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
298 for (; I
!= 0; I
= I
->Next
) {
299 // if the queue already exists, re-use it
300 if (I
->Name
== FullQueueName
)
301 return FullQueueName
;
303 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
308 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
311 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
314 return FullQueueName
;
317 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
318 // ---------------------------------------------------------------------
319 /* This locates the configuration structure for an access method. If
320 a config structure cannot be found a Worker will be created to
322 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
324 // Search for an existing config
326 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
327 if (Conf
->Access
== Access
)
330 // Create the new config class
331 Conf
= new MethodConfig
;
332 Conf
->Access
= Access
;
333 Conf
->Next
= Configs
;
336 // Create the worker to fetch the configuration
338 if (Work
.Start() == false)
341 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
342 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
343 Conf
->SingleInstance
= true;
348 // Acquire::SetFds - Deal with readable FDs /*{{{*/
349 // ---------------------------------------------------------------------
350 /* Collect FDs that have activity monitors into the fd sets */
351 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
353 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
355 if (I
->InReady
== true && I
->InFd
>= 0)
359 FD_SET(I
->InFd
,RSet
);
361 if (I
->OutReady
== true && I
->OutFd
>= 0)
365 FD_SET(I
->OutFd
,WSet
);
370 // Acquire::RunFds - Deal with active FDs /*{{{*/
371 // ---------------------------------------------------------------------
372 /* Dispatch active FDs over to the proper workers. It is very important
373 that a worker never be erased while this is running! The queue class
374 should never erase a worker except during shutdown processing. */
375 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
377 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
379 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
381 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
386 // Acquire::Run - Run the fetch sequence /*{{{*/
387 // ---------------------------------------------------------------------
388 /* This runs the queues. It manages a select loop for all of the
389 Worker tasks. The workers interact with the queues and items to
390 manage the actual fetch. */
391 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
395 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
401 bool WasCancelled
= false;
403 // Run till all things have been acquired
406 tv
.tv_usec
= PulseIntervall
;
414 SetFds(Highest
,&RFds
,&WFds
);
419 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
421 while (Res
< 0 && errno
== EINTR
);
425 _error
->Errno("select","Select has failed");
430 if (_error
->PendingError() == true)
433 // Timeout, notify the log class
434 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
436 tv
.tv_usec
= PulseIntervall
;
437 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
439 if (Log
!= 0 && Log
->Pulse(this) == false)
450 // Shut down the acquire bits
452 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
455 // Shut down the items
456 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
459 if (_error
->PendingError())
466 // Acquire::Bump - Called when an item is dequeued /*{{{*/
467 // ---------------------------------------------------------------------
468 /* This routine bumps idle queues in hopes that they will be able to fetch
470 void pkgAcquire::Bump()
472 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
476 // Acquire::WorkerStep - Step to the next worker /*{{{*/
477 // ---------------------------------------------------------------------
478 /* Not inlined to advoid including acquire-worker.h */
479 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
481 return I
->NextAcquire
;
484 // Acquire::Clean - Cleans a directory /*{{{*/
485 // ---------------------------------------------------------------------
486 /* This is a bit simplistic, it looks at every file in the dir and sees
487 if it is part of the download set. */
488 bool pkgAcquire::Clean(string Dir
)
490 // non-existing directories are by definition clean…
491 if (DirectoryExists(Dir
) == false)
495 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
497 DIR *D
= opendir(Dir
.c_str());
499 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
501 string StartDir
= SafeGetCWD();
502 if (chdir(Dir
.c_str()) != 0)
505 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
508 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
511 if (strcmp(Dir
->d_name
,"lock") == 0 ||
512 strcmp(Dir
->d_name
,"partial") == 0 ||
513 strcmp(Dir
->d_name
,".") == 0 ||
514 strcmp(Dir
->d_name
,"..") == 0)
517 // Look in the get list
518 ItemCIterator I
= Items
.begin();
519 for (; I
!= Items
.end(); ++I
)
520 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
523 // Nothing found, nuke it
524 if (I
== Items
.end())
529 if (chdir(StartDir
.c_str()) != 0)
530 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
534 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
535 // ---------------------------------------------------------------------
536 /* This is the total number of bytes needed */
537 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
539 unsigned long long Total
= 0;
540 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
541 Total
+= (*I
)->FileSize
;
545 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
546 // ---------------------------------------------------------------------
547 /* This is the number of bytes that is not local */
548 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
550 unsigned long long Total
= 0;
551 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
552 if ((*I
)->Local
== false)
553 Total
+= (*I
)->FileSize
;
557 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
558 // ---------------------------------------------------------------------
559 /* This is the number of bytes that is not local */
560 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
562 unsigned long long Total
= 0;
563 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
564 if ((*I
)->Local
== false)
565 Total
+= (*I
)->PartialSize
;
569 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
570 // ---------------------------------------------------------------------
572 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
574 return UriIterator(Queues
);
577 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
578 // ---------------------------------------------------------------------
580 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
582 return UriIterator(0);
585 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
586 // ---------------------------------------------------------------------
588 pkgAcquire::MethodConfig::MethodConfig() : d(NULL
), Next(0), SingleInstance(false),
589 Pipeline(false), SendConfig(false), LocalOnly(false), NeedsCleanup(false),
594 // Queue::Queue - Constructor /*{{{*/
595 // ---------------------------------------------------------------------
597 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : d(NULL
), Next(0),
598 Name(Name
), Items(0), Workers(0), Owner(Owner
), PipeDepth(0), MaxPipeDepth(1)
602 // Queue::~Queue - Destructor /*{{{*/
603 // ---------------------------------------------------------------------
605 pkgAcquire::Queue::~Queue()
617 // Queue::Enqueue - Queue an item to the queue /*{{{*/
618 // ---------------------------------------------------------------------
620 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
623 // move to the end of the queue and check for duplicates here
624 for (; *I
!= 0; I
= &(*I
)->Next
)
625 if (Item
.URI
== (*I
)->URI
)
627 Item
.Owner
->Status
= Item::StatDone
;
632 QItem
*Itm
= new QItem
;
637 Item
.Owner
->QueueCounter
++;
638 if (Items
->Next
== 0)
643 // Queue::Dequeue - Remove an item from the queue /*{{{*/
644 // ---------------------------------------------------------------------
645 /* We return true if we hit something */
646 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
648 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
649 return _error
->Error("Tried to dequeue a fetching object");
656 if ((*I
)->Owner
== Owner
)
660 Owner
->QueueCounter
--;
671 // Queue::Startup - Start the worker processes /*{{{*/
672 // ---------------------------------------------------------------------
673 /* It is possible for this to be called with a pre-existing set of
675 bool pkgAcquire::Queue::Startup()
680 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
684 Workers
= new Worker(this,Cnf
,Owner
->Log
);
686 if (Workers
->Start() == false)
689 /* When pipelining we commit 10 items. This needs to change when we
690 added other source retry to have cycle maintain a pipeline depth
692 if (Cnf
->Pipeline
== true)
693 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
701 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
702 // ---------------------------------------------------------------------
703 /* If final is true then all workers are eliminated, otherwise only workers
704 that do not need cleanup are removed */
705 bool pkgAcquire::Queue::Shutdown(bool Final
)
707 // Delete all of the workers
708 pkgAcquire::Worker
**Cur
= &Workers
;
711 pkgAcquire::Worker
*Jnk
= *Cur
;
712 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
714 *Cur
= Jnk
->NextQueue
;
719 Cur
= &(*Cur
)->NextQueue
;
725 // Queue::FindItem - Find a URI in the item list /*{{{*/
726 // ---------------------------------------------------------------------
728 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
730 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
731 if (I
->URI
== URI
&& I
->Worker
== Owner
)
736 // Queue::ItemDone - Item has been completed /*{{{*/
737 // ---------------------------------------------------------------------
738 /* The worker signals this which causes the item to be removed from the
739 queue. If this is the last queue instance then it is removed from the
741 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
744 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
745 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
747 if (Itm
->Owner
->QueueCounter
<= 1)
748 Owner
->Dequeue(Itm
->Owner
);
758 // Queue::Cycle - Queue new items into the method /*{{{*/
759 // ---------------------------------------------------------------------
760 /* This locates a new idle item and sends it to the worker. If pipelining
761 is enabled then it keeps the pipe full. */
762 bool pkgAcquire::Queue::Cycle()
764 if (Items
== 0 || Workers
== 0)
768 return _error
->Error("Pipedepth failure");
770 // Look for a queable item
772 while (PipeDepth
< (signed)MaxPipeDepth
)
774 for (; I
!= 0; I
= I
->Next
)
775 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
778 // Nothing to do, queue is idle.
783 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
785 if (Workers
->QueueItem(I
) == false)
792 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
793 // ---------------------------------------------------------------------
794 /* This is called when an item in multiple queues is dequeued */
795 void pkgAcquire::Queue::Bump()
800 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
801 // ---------------------------------------------------------------------
803 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Percent(0), Update(true), MorePulses(false)
808 // AcquireStatus::Pulse - Called periodically /*{{{*/
809 // ---------------------------------------------------------------------
810 /* This computes some internal state variables for the derived classes to
811 use. It generates the current downloaded bytes and total bytes to download
812 as well as the current CPS estimate. */
813 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
820 // Compute the total number of bytes to fetch
821 unsigned int Unknown
= 0;
822 unsigned int Count
= 0;
823 bool UnfetchedReleaseFiles
= false;
824 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
825 I
!= Owner
->ItemsEnd();
829 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
832 // Totally ignore local items
833 if ((*I
)->Local
== true)
836 // see if the method tells us to expect more
837 TotalItems
+= (*I
)->ExpectedAdditionalItems
;
839 // check if there are unfetched Release files
840 if ((*I
)->Complete
== false && (*I
)->ExpectedAdditionalItems
> 0)
841 UnfetchedReleaseFiles
= true;
843 TotalBytes
+= (*I
)->FileSize
;
844 if ((*I
)->Complete
== true)
845 CurrentBytes
+= (*I
)->FileSize
;
846 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
850 // Compute the current completion
851 unsigned long long ResumeSize
= 0;
852 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
853 I
= Owner
->WorkerStep(I
))
855 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
857 CurrentBytes
+= I
->CurrentSize
;
858 ResumeSize
+= I
->ResumePoint
;
860 // Files with unknown size always have 100% completion
861 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
862 I
->CurrentItem
->Owner
->Complete
== false)
863 TotalBytes
+= I
->CurrentSize
;
867 // Normalize the figures and account for unknown size downloads
870 if (Unknown
== Count
)
871 TotalBytes
= Unknown
;
873 // Wha?! Is not supposed to happen.
874 if (CurrentBytes
> TotalBytes
)
875 CurrentBytes
= TotalBytes
;
878 if (_config
->FindB("Debug::acquire::progress", false) == true)
879 std::clog
<< " Bytes: "
880 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
884 struct timeval NewTime
;
885 gettimeofday(&NewTime
,0);
886 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
887 NewTime
.tv_sec
- Time
.tv_sec
> 6)
889 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
890 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
892 // Compute the CPS value
896 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
897 LastBytes
= CurrentBytes
- ResumeSize
;
898 ElapsedTime
= (unsigned long long)Delta
;
902 // calculate the percentage, if we have too little data assume 1%
903 if (TotalBytes
> 0 && UnfetchedReleaseFiles
)
906 // use both files and bytes because bytes can be unreliable
907 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
908 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
910 int fd
= _config
->FindI("APT::Status-Fd",-1);
913 ostringstream status
;
916 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
917 unsigned long long ETA
= 0;
919 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
921 // only show the ETA if it makes sense
922 if (ETA
> 0 && ETA
< 172800 /* two days */ )
923 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
925 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
927 // build the status str
928 status
<< "dlstatus:" << i
929 << ":" << std::setprecision(3) << Percent
933 std::string
const dlstatus
= status
.str();
934 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
940 // AcquireStatus::Start - Called when the download is started /*{{{*/
941 // ---------------------------------------------------------------------
942 /* We just reset the counters */
943 void pkgAcquireStatus::Start()
945 gettimeofday(&Time
,0);
946 gettimeofday(&StartTime
,0);
957 // AcquireStatus::Stop - Finished downloading /*{{{*/
958 // ---------------------------------------------------------------------
959 /* This accurately computes the elapsed time and the total overall CPS. */
960 void pkgAcquireStatus::Stop()
962 // Compute the CPS and elapsed time
963 struct timeval NewTime
;
964 gettimeofday(&NewTime
,0);
966 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
967 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
969 // Compute the CPS value
973 CurrentCPS
= FetchedBytes
/Delta
;
974 LastBytes
= CurrentBytes
;
975 ElapsedTime
= (unsigned long long)Delta
;
978 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
979 // ---------------------------------------------------------------------
980 /* This is used to get accurate final transfer rate reporting. */
981 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
983 FetchedBytes
+= Size
- Resume
;