]>
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
)
81 // check for existence and possibly create auxiliary directories
82 string
const listDir
= _config
->FindDir("Dir::State::lists");
83 string
const partialListDir
= listDir
+ "partial/";
84 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
85 string
const partialArchivesDir
= archivesDir
+ "partial/";
87 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::State"), partialListDir
) == false &&
88 CreateAPTDirectoryIfNeeded(listDir
, partialListDir
) == false)
89 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
91 if (CreateAPTDirectoryIfNeeded(_config
->FindDir("Dir::Cache"), partialArchivesDir
) == false &&
92 CreateAPTDirectoryIfNeeded(archivesDir
, partialArchivesDir
) == false)
93 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
95 if (Lock
.empty() == true || _config
->FindB("Debug::NoLocking", false) == true)
98 // Lock the directory this acquire object will work in
99 LockFD
= GetLock(flCombine(Lock
, "lock"));
101 return _error
->Error(_("Unable to lock directory %s"), Lock
.c_str());
106 // Acquire::~pkgAcquire - Destructor /*{{{*/
107 // ---------------------------------------------------------------------
108 /* Free our memory, clean up the queues (destroy the workers) */
109 pkgAcquire::~pkgAcquire()
118 MethodConfig
*Jnk
= Configs
;
119 Configs
= Configs
->Next
;
124 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
125 // ---------------------------------------------------------------------
127 void pkgAcquire::Shutdown()
129 while (Items
.empty() == false)
131 if (Items
[0]->Status
== Item::StatFetching
)
132 Items
[0]->Status
= Item::StatError
;
139 Queues
= Queues
->Next
;
144 // Acquire::Add - Add a new item /*{{{*/
145 // ---------------------------------------------------------------------
146 /* This puts an item on the acquire list. This list is mainly for tracking
148 void pkgAcquire::Add(Item
*Itm
)
150 Items
.push_back(Itm
);
153 // Acquire::Remove - Remove a item /*{{{*/
154 // ---------------------------------------------------------------------
155 /* Remove an item from the acquire list. This is usually not used.. */
156 void pkgAcquire::Remove(Item
*Itm
)
160 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
172 // Acquire::Add - Add a worker /*{{{*/
173 // ---------------------------------------------------------------------
174 /* A list of workers is kept so that the select loop can direct their FD
176 void pkgAcquire::Add(Worker
*Work
)
178 Work
->NextAcquire
= Workers
;
182 // Acquire::Remove - Remove a worker /*{{{*/
183 // ---------------------------------------------------------------------
184 /* A worker has died. This can not be done while the select loop is running
185 as it would require that RunFds could handling a changing list state and
187 void pkgAcquire::Remove(Worker
*Work
)
192 Worker
**I
= &Workers
;
196 *I
= (*I
)->NextAcquire
;
198 I
= &(*I
)->NextAcquire
;
202 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
203 // ---------------------------------------------------------------------
204 /* This is the entry point for an item. An item calls this function when
205 it is constructed which creates a queue (based on the current queue
206 mode) and puts the item in that queue. If the system is running then
207 the queue might be started. */
208 void pkgAcquire::Enqueue(ItemDesc
&Item
)
210 // Determine which queue to put the item in
211 const MethodConfig
*Config
;
212 string Name
= QueueName(Item
.URI
,Config
);
213 if (Name
.empty() == true)
216 // Find the queue structure
218 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
221 I
= new Queue(Name
,this);
229 // See if this is a local only URI
230 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
231 Item
.Owner
->Local
= true;
232 Item
.Owner
->Status
= Item::StatIdle
;
234 // Queue it into the named queue
241 clog
<< "Fetching " << Item
.URI
<< endl
;
242 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
243 clog
<< " Queue is: " << Name
<< endl
;
247 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
248 // ---------------------------------------------------------------------
249 /* This is called when an item is finished being fetched. It removes it
250 from all the queues */
251 void pkgAcquire::Dequeue(Item
*Itm
)
256 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
258 for (; I
!= 0; I
= I
->Next
)
264 clog
<< "Dequeued from " << I
->Name
<< endl
;
272 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
273 // ---------------------------------------------------------------------
274 /* The string returned depends on the configuration settings and the
275 method parameters. Given something like http://foo.org/bar it can
276 return http://foo.org or http */
277 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
281 Config
= GetConfig(U
.Access
);
285 /* Single-Instance methods get exactly one queue per URI. This is
286 also used for the Access queue method */
287 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
290 string AccessSchema
= U
.Access
+ ':',
291 FullQueueName
= AccessSchema
+ U
.Host
;
292 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
295 for (; I
!= 0; I
= I
->Next
) {
296 // if the queue already exists, re-use it
297 if (I
->Name
== FullQueueName
)
298 return FullQueueName
;
300 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
305 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
308 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
311 return FullQueueName
;
314 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This locates the configuration structure for an access method. If
317 a config structure cannot be found a Worker will be created to
319 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
321 // Search for an existing config
323 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
324 if (Conf
->Access
== Access
)
327 // Create the new config class
328 Conf
= new MethodConfig
;
329 Conf
->Access
= Access
;
330 Conf
->Next
= Configs
;
333 // Create the worker to fetch the configuration
335 if (Work
.Start() == false)
338 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
339 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
340 Conf
->SingleInstance
= true;
345 // Acquire::SetFds - Deal with readable FDs /*{{{*/
346 // ---------------------------------------------------------------------
347 /* Collect FDs that have activity monitors into the fd sets */
348 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
350 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
352 if (I
->InReady
== true && I
->InFd
>= 0)
356 FD_SET(I
->InFd
,RSet
);
358 if (I
->OutReady
== true && I
->OutFd
>= 0)
362 FD_SET(I
->OutFd
,WSet
);
367 // Acquire::RunFds - Deal with active FDs /*{{{*/
368 // ---------------------------------------------------------------------
369 /* Dispatch active FDs over to the proper workers. It is very important
370 that a worker never be erased while this is running! The queue class
371 should never erase a worker except during shutdown processing. */
372 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
374 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
376 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
378 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
383 // Acquire::Run - Run the fetch sequence /*{{{*/
384 // ---------------------------------------------------------------------
385 /* This runs the queues. It manages a select loop for all of the
386 Worker tasks. The workers interact with the queues and items to
387 manage the actual fetch. */
388 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
392 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
398 bool WasCancelled
= false;
400 // Run till all things have been acquired
403 tv
.tv_usec
= PulseIntervall
;
411 SetFds(Highest
,&RFds
,&WFds
);
416 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
418 while (Res
< 0 && errno
== EINTR
);
422 _error
->Errno("select","Select has failed");
427 if (_error
->PendingError() == true)
430 // Timeout, notify the log class
431 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
433 tv
.tv_usec
= PulseIntervall
;
434 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
436 if (Log
!= 0 && Log
->Pulse(this) == false)
447 // Shut down the acquire bits
449 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
452 // Shut down the items
453 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
456 if (_error
->PendingError())
463 // Acquire::Bump - Called when an item is dequeued /*{{{*/
464 // ---------------------------------------------------------------------
465 /* This routine bumps idle queues in hopes that they will be able to fetch
467 void pkgAcquire::Bump()
469 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
473 // Acquire::WorkerStep - Step to the next worker /*{{{*/
474 // ---------------------------------------------------------------------
475 /* Not inlined to advoid including acquire-worker.h */
476 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
478 return I
->NextAcquire
;
481 // Acquire::Clean - Cleans a directory /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This is a bit simplistic, it looks at every file in the dir and sees
484 if it is part of the download set. */
485 bool pkgAcquire::Clean(string Dir
)
487 // non-existing directories are by definition clean…
488 if (DirectoryExists(Dir
) == false)
492 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
494 DIR *D
= opendir(Dir
.c_str());
496 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
498 string StartDir
= SafeGetCWD();
499 if (chdir(Dir
.c_str()) != 0)
502 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
505 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
508 if (strcmp(Dir
->d_name
,"lock") == 0 ||
509 strcmp(Dir
->d_name
,"partial") == 0 ||
510 strcmp(Dir
->d_name
,".") == 0 ||
511 strcmp(Dir
->d_name
,"..") == 0)
514 // Look in the get list
515 ItemCIterator I
= Items
.begin();
516 for (; I
!= Items
.end(); ++I
)
517 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
520 // Nothing found, nuke it
521 if (I
== Items
.end())
526 if (chdir(StartDir
.c_str()) != 0)
527 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
531 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
532 // ---------------------------------------------------------------------
533 /* This is the total number of bytes needed */
534 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
536 unsigned long long Total
= 0;
537 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
538 Total
+= (*I
)->FileSize
;
542 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
543 // ---------------------------------------------------------------------
544 /* This is the number of bytes that is not local */
545 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
547 unsigned long long Total
= 0;
548 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
549 if ((*I
)->Local
== false)
550 Total
+= (*I
)->FileSize
;
554 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
555 // ---------------------------------------------------------------------
556 /* This is the number of bytes that is not local */
557 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
559 unsigned long long Total
= 0;
560 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
561 if ((*I
)->Local
== false)
562 Total
+= (*I
)->PartialSize
;
566 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
567 // ---------------------------------------------------------------------
569 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
571 return UriIterator(Queues
);
574 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
575 // ---------------------------------------------------------------------
577 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
579 return UriIterator(0);
582 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
583 // ---------------------------------------------------------------------
585 pkgAcquire::MethodConfig::MethodConfig()
587 SingleInstance
= false;
595 // Queue::Queue - Constructor /*{{{*/
596 // ---------------------------------------------------------------------
598 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
608 // Queue::~Queue - Destructor /*{{{*/
609 // ---------------------------------------------------------------------
611 pkgAcquire::Queue::~Queue()
623 // Queue::Enqueue - Queue an item to the queue /*{{{*/
624 // ---------------------------------------------------------------------
626 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
629 // move to the end of the queue and check for duplicates here
630 for (; *I
!= 0; I
= &(*I
)->Next
)
631 if (Item
.URI
== (*I
)->URI
)
633 Item
.Owner
->Status
= Item::StatDone
;
638 QItem
*Itm
= new QItem
;
643 Item
.Owner
->QueueCounter
++;
644 if (Items
->Next
== 0)
649 // Queue::Dequeue - Remove an item from the queue /*{{{*/
650 // ---------------------------------------------------------------------
651 /* We return true if we hit something */
652 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
654 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
655 return _error
->Error("Tried to dequeue a fetching object");
662 if ((*I
)->Owner
== Owner
)
666 Owner
->QueueCounter
--;
677 // Queue::Startup - Start the worker processes /*{{{*/
678 // ---------------------------------------------------------------------
679 /* It is possible for this to be called with a pre-existing set of
681 bool pkgAcquire::Queue::Startup()
686 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
690 Workers
= new Worker(this,Cnf
,Owner
->Log
);
692 if (Workers
->Start() == false)
695 /* When pipelining we commit 10 items. This needs to change when we
696 added other source retry to have cycle maintain a pipeline depth
698 if (Cnf
->Pipeline
== true)
699 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
707 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
708 // ---------------------------------------------------------------------
709 /* If final is true then all workers are eliminated, otherwise only workers
710 that do not need cleanup are removed */
711 bool pkgAcquire::Queue::Shutdown(bool Final
)
713 // Delete all of the workers
714 pkgAcquire::Worker
**Cur
= &Workers
;
717 pkgAcquire::Worker
*Jnk
= *Cur
;
718 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
720 *Cur
= Jnk
->NextQueue
;
725 Cur
= &(*Cur
)->NextQueue
;
731 // Queue::FindItem - Find a URI in the item list /*{{{*/
732 // ---------------------------------------------------------------------
734 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
736 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
737 if (I
->URI
== URI
&& I
->Worker
== Owner
)
742 // Queue::ItemDone - Item has been completed /*{{{*/
743 // ---------------------------------------------------------------------
744 /* The worker signals this which causes the item to be removed from the
745 queue. If this is the last queue instance then it is removed from the
747 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
750 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
751 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
753 if (Itm
->Owner
->QueueCounter
<= 1)
754 Owner
->Dequeue(Itm
->Owner
);
764 // Queue::Cycle - Queue new items into the method /*{{{*/
765 // ---------------------------------------------------------------------
766 /* This locates a new idle item and sends it to the worker. If pipelining
767 is enabled then it keeps the pipe full. */
768 bool pkgAcquire::Queue::Cycle()
770 if (Items
== 0 || Workers
== 0)
774 return _error
->Error("Pipedepth failure");
776 // Look for a queable item
778 while (PipeDepth
< (signed)MaxPipeDepth
)
780 for (; I
!= 0; I
= I
->Next
)
781 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
784 // Nothing to do, queue is idle.
789 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
791 if (Workers
->QueueItem(I
) == false)
798 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
799 // ---------------------------------------------------------------------
800 /* This is called when an item in multiple queues is dequeued */
801 void pkgAcquire::Queue::Bump()
806 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
807 // ---------------------------------------------------------------------
809 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Update(true), MorePulses(false)
814 // AcquireStatus::Pulse - Called periodically /*{{{*/
815 // ---------------------------------------------------------------------
816 /* This computes some internal state variables for the derived classes to
817 use. It generates the current downloaded bytes and total bytes to download
818 as well as the current CPS estimate. */
819 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
826 // Compute the total number of bytes to fetch
827 unsigned int Unknown
= 0;
828 unsigned int Count
= 0;
829 bool UnfetchedReleaseFiles
= false;
830 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
831 I
!= Owner
->ItemsEnd();
835 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
838 // Totally ignore local items
839 if ((*I
)->Local
== true)
842 // see if the method tells us to expect more
843 TotalItems
+= (*I
)->ExpectedAdditionalItems
;
845 // check if there are unfetched Release files
846 if ((*I
)->Complete
== false && (*I
)->ExpectedAdditionalItems
> 0)
847 UnfetchedReleaseFiles
= true;
849 TotalBytes
+= (*I
)->FileSize
;
850 if ((*I
)->Complete
== true)
851 CurrentBytes
+= (*I
)->FileSize
;
852 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
856 // Compute the current completion
857 unsigned long long ResumeSize
= 0;
858 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
859 I
= Owner
->WorkerStep(I
))
861 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
863 CurrentBytes
+= I
->CurrentSize
;
864 ResumeSize
+= I
->ResumePoint
;
866 // Files with unknown size always have 100% completion
867 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
868 I
->CurrentItem
->Owner
->Complete
== false)
869 TotalBytes
+= I
->CurrentSize
;
873 // Normalize the figures and account for unknown size downloads
876 if (Unknown
== Count
)
877 TotalBytes
= Unknown
;
879 // Wha?! Is not supposed to happen.
880 if (CurrentBytes
> TotalBytes
)
881 CurrentBytes
= TotalBytes
;
884 if (_config
->FindB("Debug::acquire::progress", false) == true)
885 std::clog
<< " Bytes: "
886 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
890 struct timeval NewTime
;
891 gettimeofday(&NewTime
,0);
892 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
893 NewTime
.tv_sec
- Time
.tv_sec
> 6)
895 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
896 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
898 // Compute the CPS value
902 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
903 LastBytes
= CurrentBytes
- ResumeSize
;
904 ElapsedTime
= (unsigned long long)Delta
;
908 // calculate the percentage, if we have too little data assume 1%
909 if (TotalBytes
> 0 && UnfetchedReleaseFiles
)
912 // use both files and bytes because bytes can be unreliable
913 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
914 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
916 int fd
= _config
->FindI("APT::Status-Fd",-1);
919 ostringstream status
;
922 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
923 unsigned long long ETA
= 0;
925 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
927 // only show the ETA if it makes sense
928 if (ETA
> 0 && ETA
< 172800 /* two days */ )
929 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
931 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
933 // build the status str
934 status
<< "dlstatus:" << i
935 << ":" << std::setprecision(3) << Percent
939 std::string
const dlstatus
= status
.str();
940 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
946 // AcquireStatus::Start - Called when the download is started /*{{{*/
947 // ---------------------------------------------------------------------
948 /* We just reset the counters */
949 void pkgAcquireStatus::Start()
951 gettimeofday(&Time
,0);
952 gettimeofday(&StartTime
,0);
963 // AcquireStatus::Stop - Finished downloading /*{{{*/
964 // ---------------------------------------------------------------------
965 /* This accurately computes the elapsed time and the total overall CPS. */
966 void pkgAcquireStatus::Stop()
968 // Compute the CPS and elapsed time
969 struct timeval NewTime
;
970 gettimeofday(&NewTime
,0);
972 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
973 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
975 // Compute the CPS value
979 CurrentCPS
= FetchedBytes
/Delta
;
980 LastBytes
= CurrentBytes
;
981 ElapsedTime
= (unsigned long long)Delta
;
984 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
985 // ---------------------------------------------------------------------
986 /* This is used to get accurate final transfer rate reporting. */
987 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
989 FetchedBytes
+= Size
- Resume
;