]>
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::AbortTransaction - Remove a transaction /*{{{*/
173 void pkgAcquire::AbortTransaction(unsigned long TransactionID
)
175 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
176 std::clog
<< "AbortTransaction: " << TransactionID
<< std::endl
;
178 std::vector
<Item
*> Transaction
;
179 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
180 if((*I
)->TransactionID
== TransactionID
)
181 Transaction
.push_back(*I
);
183 for (std::vector
<Item
*>::iterator I
= Transaction
.begin();
184 I
!= Transaction
.end(); ++I
)
186 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
187 std::clog
<< " Cancel: " << (*I
)->DestFile
<< std::endl
;
189 (*I
)->Status
= pkgAcquire::Item::StatError
;
193 // Acquire::CommitTransaction - Commit a transaction /*{{{*/
194 void pkgAcquire::CommitTransaction(unsigned long TransactionID
)
196 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
197 std::clog
<< "CommitTransaction: " << TransactionID
<< std::endl
;
199 std::vector
<Item
*> Transaction
;
200 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
201 if((*I
)->TransactionID
== TransactionID
)
202 Transaction
.push_back(*I
);
204 for (std::vector
<Item
*>::iterator I
= Transaction
.begin();
205 I
!= Transaction
.end(); ++I
)
207 if((*I
)->PartialFile
!= "" &&
208 (*I
)->Status
== pkgAcquire::Item::StatDone
)
210 if(_config
->FindB("Debug::Acquire::Transaction", false) == true)
212 << (*I
)->PartialFile
<< " -> "
213 << (*I
)->DestFile
<< std::endl
;
214 Rename((*I
)->PartialFile
, (*I
)->DestFile
);
215 chmod((*I
)->DestFile
.c_str(),0644);
221 // Acquire::Add - Add a worker /*{{{*/
222 // ---------------------------------------------------------------------
223 /* A list of workers is kept so that the select loop can direct their FD
225 void pkgAcquire::Add(Worker
*Work
)
227 Work
->NextAcquire
= Workers
;
231 // Acquire::Remove - Remove a worker /*{{{*/
232 // ---------------------------------------------------------------------
233 /* A worker has died. This can not be done while the select loop is running
234 as it would require that RunFds could handling a changing list state and
236 void pkgAcquire::Remove(Worker
*Work
)
241 Worker
**I
= &Workers
;
245 *I
= (*I
)->NextAcquire
;
247 I
= &(*I
)->NextAcquire
;
251 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
252 // ---------------------------------------------------------------------
253 /* This is the entry point for an item. An item calls this function when
254 it is constructed which creates a queue (based on the current queue
255 mode) and puts the item in that queue. If the system is running then
256 the queue might be started. */
257 void pkgAcquire::Enqueue(ItemDesc
&Item
)
259 // Determine which queue to put the item in
260 const MethodConfig
*Config
;
261 string Name
= QueueName(Item
.URI
,Config
);
262 if (Name
.empty() == true)
265 // Find the queue structure
267 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
270 I
= new Queue(Name
,this);
278 // See if this is a local only URI
279 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
280 Item
.Owner
->Local
= true;
281 Item
.Owner
->Status
= Item::StatIdle
;
283 // Queue it into the named queue
290 clog
<< "Fetching " << Item
.URI
<< endl
;
291 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
292 clog
<< " Queue is: " << Name
<< endl
;
296 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
297 // ---------------------------------------------------------------------
298 /* This is called when an item is finished being fetched. It removes it
299 from all the queues */
300 void pkgAcquire::Dequeue(Item
*Itm
)
305 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
307 for (; I
!= 0; I
= I
->Next
)
313 clog
<< "Dequeued from " << I
->Name
<< endl
;
321 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
322 // ---------------------------------------------------------------------
323 /* The string returned depends on the configuration settings and the
324 method parameters. Given something like http://foo.org/bar it can
325 return http://foo.org or http */
326 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
330 Config
= GetConfig(U
.Access
);
334 /* Single-Instance methods get exactly one queue per URI. This is
335 also used for the Access queue method */
336 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
339 string AccessSchema
= U
.Access
+ ':',
340 FullQueueName
= AccessSchema
+ U
.Host
;
341 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
344 for (; I
!= 0; I
= I
->Next
) {
345 // if the queue already exists, re-use it
346 if (I
->Name
== FullQueueName
)
347 return FullQueueName
;
349 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
354 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
357 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
360 return FullQueueName
;
363 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
364 // ---------------------------------------------------------------------
365 /* This locates the configuration structure for an access method. If
366 a config structure cannot be found a Worker will be created to
368 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
370 // Search for an existing config
372 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
373 if (Conf
->Access
== Access
)
376 // Create the new config class
377 Conf
= new MethodConfig
;
378 Conf
->Access
= Access
;
379 Conf
->Next
= Configs
;
382 // Create the worker to fetch the configuration
384 if (Work
.Start() == false)
387 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
388 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
389 Conf
->SingleInstance
= true;
394 // Acquire::SetFds - Deal with readable FDs /*{{{*/
395 // ---------------------------------------------------------------------
396 /* Collect FDs that have activity monitors into the fd sets */
397 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
399 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
401 if (I
->InReady
== true && I
->InFd
>= 0)
405 FD_SET(I
->InFd
,RSet
);
407 if (I
->OutReady
== true && I
->OutFd
>= 0)
411 FD_SET(I
->OutFd
,WSet
);
416 // Acquire::RunFds - Deal with active FDs /*{{{*/
417 // ---------------------------------------------------------------------
418 /* Dispatch active FDs over to the proper workers. It is very important
419 that a worker never be erased while this is running! The queue class
420 should never erase a worker except during shutdown processing. */
421 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
423 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
425 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
427 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
432 // Acquire::Run - Run the fetch sequence /*{{{*/
433 // ---------------------------------------------------------------------
434 /* This runs the queues. It manages a select loop for all of the
435 Worker tasks. The workers interact with the queues and items to
436 manage the actual fetch. */
437 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
441 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
447 bool WasCancelled
= false;
449 // Run till all things have been acquired
452 tv
.tv_usec
= PulseIntervall
;
460 SetFds(Highest
,&RFds
,&WFds
);
465 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
467 while (Res
< 0 && errno
== EINTR
);
471 _error
->Errno("select","Select has failed");
476 if (_error
->PendingError() == true)
479 // Timeout, notify the log class
480 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
482 tv
.tv_usec
= PulseIntervall
;
483 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
485 if (Log
!= 0 && Log
->Pulse(this) == false)
496 // Shut down the acquire bits
498 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
501 // Shut down the items
502 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
505 if (_error
->PendingError())
512 // Acquire::Bump - Called when an item is dequeued /*{{{*/
513 // ---------------------------------------------------------------------
514 /* This routine bumps idle queues in hopes that they will be able to fetch
516 void pkgAcquire::Bump()
518 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
522 // Acquire::WorkerStep - Step to the next worker /*{{{*/
523 // ---------------------------------------------------------------------
524 /* Not inlined to advoid including acquire-worker.h */
525 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
527 return I
->NextAcquire
;
530 // Acquire::Clean - Cleans a directory /*{{{*/
531 // ---------------------------------------------------------------------
532 /* This is a bit simplistic, it looks at every file in the dir and sees
533 if it is part of the download set. */
534 bool pkgAcquire::Clean(string Dir
)
536 // non-existing directories are by definition clean…
537 if (DirectoryExists(Dir
) == false)
541 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
543 DIR *D
= opendir(Dir
.c_str());
545 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
547 string StartDir
= SafeGetCWD();
548 if (chdir(Dir
.c_str()) != 0)
551 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
554 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
557 if (strcmp(Dir
->d_name
,"lock") == 0 ||
558 strcmp(Dir
->d_name
,"partial") == 0 ||
559 strcmp(Dir
->d_name
,".") == 0 ||
560 strcmp(Dir
->d_name
,"..") == 0)
563 // Look in the get list
564 ItemCIterator I
= Items
.begin();
565 for (; I
!= Items
.end(); ++I
)
566 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
569 // Nothing found, nuke it
570 if (I
== Items
.end())
575 if (chdir(StartDir
.c_str()) != 0)
576 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
580 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
581 // ---------------------------------------------------------------------
582 /* This is the total number of bytes needed */
583 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
585 unsigned long long Total
= 0;
586 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
587 Total
+= (*I
)->FileSize
;
591 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
592 // ---------------------------------------------------------------------
593 /* This is the number of bytes that is not local */
594 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
596 unsigned long long Total
= 0;
597 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
598 if ((*I
)->Local
== false)
599 Total
+= (*I
)->FileSize
;
603 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
604 // ---------------------------------------------------------------------
605 /* This is the number of bytes that is not local */
606 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
608 unsigned long long Total
= 0;
609 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
610 if ((*I
)->Local
== false)
611 Total
+= (*I
)->PartialSize
;
615 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
616 // ---------------------------------------------------------------------
618 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
620 return UriIterator(Queues
);
623 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
624 // ---------------------------------------------------------------------
626 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
628 return UriIterator(0);
631 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
632 // ---------------------------------------------------------------------
634 pkgAcquire::MethodConfig::MethodConfig()
636 SingleInstance
= false;
644 // Queue::Queue - Constructor /*{{{*/
645 // ---------------------------------------------------------------------
647 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
657 // Queue::~Queue - Destructor /*{{{*/
658 // ---------------------------------------------------------------------
660 pkgAcquire::Queue::~Queue()
672 // Queue::Enqueue - Queue an item to the queue /*{{{*/
673 // ---------------------------------------------------------------------
675 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
678 // move to the end of the queue and check for duplicates here
679 for (; *I
!= 0; I
= &(*I
)->Next
)
680 if (Item
.URI
== (*I
)->URI
)
682 Item
.Owner
->Status
= Item::StatDone
;
687 QItem
*Itm
= new QItem
;
692 Item
.Owner
->QueueCounter
++;
693 if (Items
->Next
== 0)
698 // Queue::Dequeue - Remove an item from the queue /*{{{*/
699 // ---------------------------------------------------------------------
700 /* We return true if we hit something */
701 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
703 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
704 return _error
->Error("Tried to dequeue a fetching object");
711 if ((*I
)->Owner
== Owner
)
715 Owner
->QueueCounter
--;
726 // Queue::Startup - Start the worker processes /*{{{*/
727 // ---------------------------------------------------------------------
728 /* It is possible for this to be called with a pre-existing set of
730 bool pkgAcquire::Queue::Startup()
735 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
739 Workers
= new Worker(this,Cnf
,Owner
->Log
);
741 if (Workers
->Start() == false)
744 /* When pipelining we commit 10 items. This needs to change when we
745 added other source retry to have cycle maintain a pipeline depth
747 if (Cnf
->Pipeline
== true)
748 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
756 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
757 // ---------------------------------------------------------------------
758 /* If final is true then all workers are eliminated, otherwise only workers
759 that do not need cleanup are removed */
760 bool pkgAcquire::Queue::Shutdown(bool Final
)
762 // Delete all of the workers
763 pkgAcquire::Worker
**Cur
= &Workers
;
766 pkgAcquire::Worker
*Jnk
= *Cur
;
767 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
769 *Cur
= Jnk
->NextQueue
;
774 Cur
= &(*Cur
)->NextQueue
;
780 // Queue::FindItem - Find a URI in the item list /*{{{*/
781 // ---------------------------------------------------------------------
783 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
785 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
786 if (I
->URI
== URI
&& I
->Worker
== Owner
)
791 // Queue::ItemDone - Item has been completed /*{{{*/
792 // ---------------------------------------------------------------------
793 /* The worker signals this which causes the item to be removed from the
794 queue. If this is the last queue instance then it is removed from the
796 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
799 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
800 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
802 if (Itm
->Owner
->QueueCounter
<= 1)
803 Owner
->Dequeue(Itm
->Owner
);
813 // Queue::Cycle - Queue new items into the method /*{{{*/
814 // ---------------------------------------------------------------------
815 /* This locates a new idle item and sends it to the worker. If pipelining
816 is enabled then it keeps the pipe full. */
817 bool pkgAcquire::Queue::Cycle()
819 if (Items
== 0 || Workers
== 0)
823 return _error
->Error("Pipedepth failure");
825 // Look for a queable item
827 while (PipeDepth
< (signed)MaxPipeDepth
)
829 for (; I
!= 0; I
= I
->Next
)
830 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
833 // Nothing to do, queue is idle.
838 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
840 if (Workers
->QueueItem(I
) == false)
847 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
848 // ---------------------------------------------------------------------
849 /* This is called when an item in multiple queues is dequeued */
850 void pkgAcquire::Queue::Bump()
855 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
856 // ---------------------------------------------------------------------
858 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Update(true), MorePulses(false)
863 // AcquireStatus::Pulse - Called periodically /*{{{*/
864 // ---------------------------------------------------------------------
865 /* This computes some internal state variables for the derived classes to
866 use. It generates the current downloaded bytes and total bytes to download
867 as well as the current CPS estimate. */
868 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
875 // Compute the total number of bytes to fetch
876 unsigned int Unknown
= 0;
877 unsigned int Count
= 0;
878 bool UnfetchedReleaseFiles
= false;
879 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
880 I
!= Owner
->ItemsEnd();
884 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
887 // Totally ignore local items
888 if ((*I
)->Local
== true)
891 // see if the method tells us to expect more
892 TotalItems
+= (*I
)->ExpectedAdditionalItems
;
894 // check if there are unfetched Release files
895 if ((*I
)->Complete
== false && (*I
)->ExpectedAdditionalItems
> 0)
896 UnfetchedReleaseFiles
= true;
898 TotalBytes
+= (*I
)->FileSize
;
899 if ((*I
)->Complete
== true)
900 CurrentBytes
+= (*I
)->FileSize
;
901 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
905 // Compute the current completion
906 unsigned long long ResumeSize
= 0;
907 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
908 I
= Owner
->WorkerStep(I
))
910 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
912 CurrentBytes
+= I
->CurrentSize
;
913 ResumeSize
+= I
->ResumePoint
;
915 // Files with unknown size always have 100% completion
916 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
917 I
->CurrentItem
->Owner
->Complete
== false)
918 TotalBytes
+= I
->CurrentSize
;
922 // Normalize the figures and account for unknown size downloads
925 if (Unknown
== Count
)
926 TotalBytes
= Unknown
;
928 // Wha?! Is not supposed to happen.
929 if (CurrentBytes
> TotalBytes
)
930 CurrentBytes
= TotalBytes
;
933 if (_config
->FindB("Debug::acquire::progress", false) == true)
934 std::clog
<< " Bytes: "
935 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
939 struct timeval NewTime
;
940 gettimeofday(&NewTime
,0);
941 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
942 NewTime
.tv_sec
- Time
.tv_sec
> 6)
944 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
945 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
947 // Compute the CPS value
951 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
952 LastBytes
= CurrentBytes
- ResumeSize
;
953 ElapsedTime
= (unsigned long long)Delta
;
957 // calculate the percentage, if we have too little data assume 1%
958 if (TotalBytes
> 0 && UnfetchedReleaseFiles
)
961 // use both files and bytes because bytes can be unreliable
962 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
963 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
965 int fd
= _config
->FindI("APT::Status-Fd",-1);
968 ostringstream status
;
971 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
972 unsigned long long ETA
= 0;
974 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
976 // only show the ETA if it makes sense
977 if (ETA
> 0 && ETA
< 172800 /* two days */ )
978 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
980 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
982 // build the status str
983 status
<< "dlstatus:" << i
984 << ":" << std::setprecision(3) << Percent
988 std::string
const dlstatus
= status
.str();
989 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
995 // AcquireStatus::Start - Called when the download is started /*{{{*/
996 // ---------------------------------------------------------------------
997 /* We just reset the counters */
998 void pkgAcquireStatus::Start()
1000 gettimeofday(&Time
,0);
1001 gettimeofday(&StartTime
,0);
1012 // AcquireStatus::Stop - Finished downloading /*{{{*/
1013 // ---------------------------------------------------------------------
1014 /* This accurately computes the elapsed time and the total overall CPS. */
1015 void pkgAcquireStatus::Stop()
1017 // Compute the CPS and elapsed time
1018 struct timeval NewTime
;
1019 gettimeofday(&NewTime
,0);
1021 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
1022 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
1024 // Compute the CPS value
1028 CurrentCPS
= FetchedBytes
/Delta
;
1029 LastBytes
= CurrentBytes
;
1030 ElapsedTime
= (unsigned long long)Delta
;
1033 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
1034 // ---------------------------------------------------------------------
1035 /* This is used to get accurate final transfer rate reporting. */
1036 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
1038 FetchedBytes
+= Size
- Resume
;