]>
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>
40 #include <sys/select.h>
43 #include <sys/types.h>
50 // Acquire::pkgAcquire - Constructor /*{{{*/
51 // ---------------------------------------------------------------------
52 /* We grab some runtime state from the configuration space */
53 pkgAcquire::pkgAcquire() : LockFD(-1), Queues(0), Workers(0), Configs(0), Log(NULL
), ToFetch(0),
54 Debug(_config
->FindB("Debug::pkgAcquire",false)),
57 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
58 if (strcasecmp(Mode
.c_str(),"host") == 0)
59 QueueMode
= QueueHost
;
60 if (strcasecmp(Mode
.c_str(),"access") == 0)
61 QueueMode
= QueueAccess
;
63 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Progress
) : LockFD(-1), Queues(0), Workers(0),
64 Configs(0), Log(NULL
), ToFetch(0),
65 Debug(_config
->FindB("Debug::pkgAcquire",false)),
68 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
69 if (strcasecmp(Mode
.c_str(),"host") == 0)
70 QueueMode
= QueueHost
;
71 if (strcasecmp(Mode
.c_str(),"access") == 0)
72 QueueMode
= QueueAccess
;
76 // Acquire::GetLock - lock directory and prepare for action /*{{{*/
77 static bool SetupAPTPartialDirectory(std::string
const &grand
, std::string
const &parent
)
79 std::string
const partial
= parent
+ "partial";
80 if (CreateAPTDirectoryIfNeeded(grand
, partial
) == false &&
81 CreateAPTDirectoryIfNeeded(parent
, partial
) == false)
84 if (getuid() == 0) // if we aren't root, we can't chown, so don't try it
86 std::string SandboxUser
= _config
->Find("APT::Sandbox::User");
87 struct passwd
*pw
= getpwnam(SandboxUser
.c_str());
88 struct group
*gr
= getgrnam("root");
89 if (pw
!= NULL
&& gr
!= NULL
&& chown(partial
.c_str(), pw
->pw_uid
, gr
->gr_gid
) != 0)
90 _error
->WarningE("SetupAPTPartialDirectory", "chown to %s:root of directory %s failed", SandboxUser
.c_str(), partial
.c_str());
92 if (chmod(partial
.c_str(), 0700) != 0)
93 _error
->WarningE("SetupAPTPartialDirectory", "chmod 0700 of directory %s failed", partial
.c_str());
97 bool pkgAcquire::Setup(pkgAcquireStatus
*Progress
, string
const &Lock
)
102 string
const listDir
= _config
->FindDir("Dir::State::lists");
103 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::State"), listDir
) == false)
104 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
105 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
106 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::Cache"), archivesDir
) == false)
107 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
110 return GetLock(Lock
);
112 bool pkgAcquire::GetLock(std::string
const &Lock
)
114 if (Lock
.empty() == true)
117 // check for existence and possibly create auxiliary directories
118 string
const listDir
= _config
->FindDir("Dir::State::lists");
119 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
123 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::State"), listDir
) == false)
124 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
126 if (Lock
== archivesDir
)
128 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::Cache"), archivesDir
) == false)
129 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
132 if (_config
->FindB("Debug::NoLocking", false) == true)
135 // Lock the directory this acquire object will work in
136 LockFD
= ::GetLock(flCombine(Lock
, "lock"));
138 return _error
->Error(_("Unable to lock directory %s"), Lock
.c_str());
143 // Acquire::~pkgAcquire - Destructor /*{{{*/
144 // ---------------------------------------------------------------------
145 /* Free our memory, clean up the queues (destroy the workers) */
146 pkgAcquire::~pkgAcquire()
155 MethodConfig
*Jnk
= Configs
;
156 Configs
= Configs
->Next
;
161 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
162 // ---------------------------------------------------------------------
164 void pkgAcquire::Shutdown()
166 while (Items
.empty() == false)
168 if (Items
[0]->Status
== Item::StatFetching
)
169 Items
[0]->Status
= Item::StatError
;
176 Queues
= Queues
->Next
;
181 // Acquire::Add - Add a new item /*{{{*/
182 // ---------------------------------------------------------------------
183 /* This puts an item on the acquire list. This list is mainly for tracking
185 void pkgAcquire::Add(Item
*Itm
)
187 Items
.push_back(Itm
);
190 // Acquire::Remove - Remove a item /*{{{*/
191 // ---------------------------------------------------------------------
192 /* Remove an item from the acquire list. This is usually not used.. */
193 void pkgAcquire::Remove(Item
*Itm
)
197 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
209 // Acquire::Add - Add a worker /*{{{*/
210 // ---------------------------------------------------------------------
211 /* A list of workers is kept so that the select loop can direct their FD
213 void pkgAcquire::Add(Worker
*Work
)
215 Work
->NextAcquire
= Workers
;
219 // Acquire::Remove - Remove a worker /*{{{*/
220 // ---------------------------------------------------------------------
221 /* A worker has died. This can not be done while the select loop is running
222 as it would require that RunFds could handling a changing list state and
224 void pkgAcquire::Remove(Worker
*Work
)
229 Worker
**I
= &Workers
;
233 *I
= (*I
)->NextAcquire
;
235 I
= &(*I
)->NextAcquire
;
239 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
240 // ---------------------------------------------------------------------
241 /* This is the entry point for an item. An item calls this function when
242 it is constructed which creates a queue (based on the current queue
243 mode) and puts the item in that queue. If the system is running then
244 the queue might be started. */
245 void pkgAcquire::Enqueue(ItemDesc
&Item
)
247 // Determine which queue to put the item in
248 const MethodConfig
*Config
;
249 string Name
= QueueName(Item
.URI
,Config
);
250 if (Name
.empty() == true)
253 // Find the queue structure
255 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
258 I
= new Queue(Name
,this);
266 // See if this is a local only URI
267 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
268 Item
.Owner
->Local
= true;
269 Item
.Owner
->Status
= Item::StatIdle
;
271 // Queue it into the named queue
278 clog
<< "Fetching " << Item
.URI
<< endl
;
279 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
280 clog
<< " Queue is: " << Name
<< endl
;
284 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
285 // ---------------------------------------------------------------------
286 /* This is called when an item is finished being fetched. It removes it
287 from all the queues */
288 void pkgAcquire::Dequeue(Item
*Itm
)
293 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
295 for (; I
!= 0; I
= I
->Next
)
301 clog
<< "Dequeued from " << I
->Name
<< endl
;
309 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
310 // ---------------------------------------------------------------------
311 /* The string returned depends on the configuration settings and the
312 method parameters. Given something like http://foo.org/bar it can
313 return http://foo.org or http */
314 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
318 Config
= GetConfig(U
.Access
);
322 /* Single-Instance methods get exactly one queue per URI. This is
323 also used for the Access queue method */
324 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
327 string AccessSchema
= U
.Access
+ ':',
328 FullQueueName
= AccessSchema
+ U
.Host
;
329 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
332 for (; I
!= 0; I
= I
->Next
) {
333 // if the queue already exists, re-use it
334 if (I
->Name
== FullQueueName
)
335 return FullQueueName
;
337 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
342 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
345 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
348 return FullQueueName
;
351 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
352 // ---------------------------------------------------------------------
353 /* This locates the configuration structure for an access method. If
354 a config structure cannot be found a Worker will be created to
356 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
358 // Search for an existing config
360 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
361 if (Conf
->Access
== Access
)
364 // Create the new config class
365 Conf
= new MethodConfig
;
366 Conf
->Access
= Access
;
367 Conf
->Next
= Configs
;
370 // Create the worker to fetch the configuration
372 if (Work
.Start() == false)
375 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
376 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
377 Conf
->SingleInstance
= true;
382 // Acquire::SetFds - Deal with readable FDs /*{{{*/
383 // ---------------------------------------------------------------------
384 /* Collect FDs that have activity monitors into the fd sets */
385 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
387 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
389 if (I
->InReady
== true && I
->InFd
>= 0)
393 FD_SET(I
->InFd
,RSet
);
395 if (I
->OutReady
== true && I
->OutFd
>= 0)
399 FD_SET(I
->OutFd
,WSet
);
404 // Acquire::RunFds - Deal with active FDs /*{{{*/
405 // ---------------------------------------------------------------------
406 /* Dispatch active FDs over to the proper workers. It is very important
407 that a worker never be erased while this is running! The queue class
408 should never erase a worker except during shutdown processing. */
409 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
411 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
413 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
415 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
420 // Acquire::Run - Run the fetch sequence /*{{{*/
421 // ---------------------------------------------------------------------
422 /* This runs the queues. It manages a select loop for all of the
423 Worker tasks. The workers interact with the queues and items to
424 manage the actual fetch. */
425 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
429 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
435 bool WasCancelled
= false;
437 // Run till all things have been acquired
440 tv
.tv_usec
= PulseIntervall
;
448 SetFds(Highest
,&RFds
,&WFds
);
453 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
455 while (Res
< 0 && errno
== EINTR
);
459 _error
->Errno("select","Select has failed");
464 if (_error
->PendingError() == true)
467 // Timeout, notify the log class
468 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
470 tv
.tv_usec
= PulseIntervall
;
471 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
473 if (Log
!= 0 && Log
->Pulse(this) == false)
484 // Shut down the acquire bits
486 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
489 // Shut down the items
490 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
493 if (_error
->PendingError())
500 // Acquire::Bump - Called when an item is dequeued /*{{{*/
501 // ---------------------------------------------------------------------
502 /* This routine bumps idle queues in hopes that they will be able to fetch
504 void pkgAcquire::Bump()
506 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
510 // Acquire::WorkerStep - Step to the next worker /*{{{*/
511 // ---------------------------------------------------------------------
512 /* Not inlined to advoid including acquire-worker.h */
513 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
515 return I
->NextAcquire
;
518 // Acquire::Clean - Cleans a directory /*{{{*/
519 // ---------------------------------------------------------------------
520 /* This is a bit simplistic, it looks at every file in the dir and sees
521 if it is part of the download set. */
522 bool pkgAcquire::Clean(string Dir
)
524 // non-existing directories are by definition clean…
525 if (DirectoryExists(Dir
) == false)
529 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
531 DIR *D
= opendir(Dir
.c_str());
533 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
535 string StartDir
= SafeGetCWD();
536 if (chdir(Dir
.c_str()) != 0)
539 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
542 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
545 if (strcmp(Dir
->d_name
,"lock") == 0 ||
546 strcmp(Dir
->d_name
,"partial") == 0 ||
547 strcmp(Dir
->d_name
,".") == 0 ||
548 strcmp(Dir
->d_name
,"..") == 0)
551 // Look in the get list
552 ItemCIterator I
= Items
.begin();
553 for (; I
!= Items
.end(); ++I
)
554 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
557 // Nothing found, nuke it
558 if (I
== Items
.end())
563 if (chdir(StartDir
.c_str()) != 0)
564 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
568 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
569 // ---------------------------------------------------------------------
570 /* This is the total number of bytes needed */
571 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
573 unsigned long long Total
= 0;
574 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
575 Total
+= (*I
)->FileSize
;
579 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
580 // ---------------------------------------------------------------------
581 /* This is the number of bytes that is not local */
582 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
584 unsigned long long Total
= 0;
585 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
586 if ((*I
)->Local
== false)
587 Total
+= (*I
)->FileSize
;
591 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
592 // ---------------------------------------------------------------------
593 /* This is the number of bytes that is not local */
594 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
596 unsigned long long Total
= 0;
597 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
598 if ((*I
)->Local
== false)
599 Total
+= (*I
)->PartialSize
;
603 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
604 // ---------------------------------------------------------------------
606 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
608 return UriIterator(Queues
);
611 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
612 // ---------------------------------------------------------------------
614 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
616 return UriIterator(0);
619 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
620 // ---------------------------------------------------------------------
622 pkgAcquire::MethodConfig::MethodConfig() : d(NULL
), Next(0), SingleInstance(false),
623 Pipeline(false), SendConfig(false), LocalOnly(false), NeedsCleanup(false),
628 // Queue::Queue - Constructor /*{{{*/
629 // ---------------------------------------------------------------------
631 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : d(NULL
), Next(0),
632 Name(Name
), Items(0), Workers(0), Owner(Owner
), PipeDepth(0), MaxPipeDepth(1)
636 // Queue::~Queue - Destructor /*{{{*/
637 // ---------------------------------------------------------------------
639 pkgAcquire::Queue::~Queue()
651 // Queue::Enqueue - Queue an item to the queue /*{{{*/
652 // ---------------------------------------------------------------------
654 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
657 // move to the end of the queue and check for duplicates here
658 for (; *I
!= 0; I
= &(*I
)->Next
)
659 if (Item
.URI
== (*I
)->URI
)
661 Item
.Owner
->Status
= Item::StatDone
;
666 QItem
*Itm
= new QItem
;
671 Item
.Owner
->QueueCounter
++;
672 if (Items
->Next
== 0)
677 // Queue::Dequeue - Remove an item from the queue /*{{{*/
678 // ---------------------------------------------------------------------
679 /* We return true if we hit something */
680 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
682 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
683 return _error
->Error("Tried to dequeue a fetching object");
690 if ((*I
)->Owner
== Owner
)
694 Owner
->QueueCounter
--;
705 // Queue::Startup - Start the worker processes /*{{{*/
706 // ---------------------------------------------------------------------
707 /* It is possible for this to be called with a pre-existing set of
709 bool pkgAcquire::Queue::Startup()
714 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
718 Workers
= new Worker(this,Cnf
,Owner
->Log
);
720 if (Workers
->Start() == false)
723 /* When pipelining we commit 10 items. This needs to change when we
724 added other source retry to have cycle maintain a pipeline depth
726 if (Cnf
->Pipeline
== true)
727 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
735 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
736 // ---------------------------------------------------------------------
737 /* If final is true then all workers are eliminated, otherwise only workers
738 that do not need cleanup are removed */
739 bool pkgAcquire::Queue::Shutdown(bool Final
)
741 // Delete all of the workers
742 pkgAcquire::Worker
**Cur
= &Workers
;
745 pkgAcquire::Worker
*Jnk
= *Cur
;
746 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
748 *Cur
= Jnk
->NextQueue
;
753 Cur
= &(*Cur
)->NextQueue
;
759 // Queue::FindItem - Find a URI in the item list /*{{{*/
760 // ---------------------------------------------------------------------
762 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
764 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
765 if (I
->URI
== URI
&& I
->Worker
== Owner
)
770 // Queue::ItemDone - Item has been completed /*{{{*/
771 // ---------------------------------------------------------------------
772 /* The worker signals this which causes the item to be removed from the
773 queue. If this is the last queue instance then it is removed from the
775 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
778 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
779 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
781 if (Itm
->Owner
->QueueCounter
<= 1)
782 Owner
->Dequeue(Itm
->Owner
);
792 // Queue::Cycle - Queue new items into the method /*{{{*/
793 // ---------------------------------------------------------------------
794 /* This locates a new idle item and sends it to the worker. If pipelining
795 is enabled then it keeps the pipe full. */
796 bool pkgAcquire::Queue::Cycle()
798 if (Items
== 0 || Workers
== 0)
802 return _error
->Error("Pipedepth failure");
804 // Look for a queable item
806 while (PipeDepth
< (signed)MaxPipeDepth
)
808 for (; I
!= 0; I
= I
->Next
)
809 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
812 // Nothing to do, queue is idle.
817 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
819 if (Workers
->QueueItem(I
) == false)
826 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
827 // ---------------------------------------------------------------------
828 /* This is called when an item in multiple queues is dequeued */
829 void pkgAcquire::Queue::Bump()
834 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
835 // ---------------------------------------------------------------------
837 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Percent(0), Update(true), MorePulses(false)
842 // AcquireStatus::Pulse - Called periodically /*{{{*/
843 // ---------------------------------------------------------------------
844 /* This computes some internal state variables for the derived classes to
845 use. It generates the current downloaded bytes and total bytes to download
846 as well as the current CPS estimate. */
847 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
854 // Compute the total number of bytes to fetch
855 unsigned int Unknown
= 0;
856 unsigned int Count
= 0;
857 bool UnfetchedReleaseFiles
= false;
858 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
859 I
!= Owner
->ItemsEnd();
863 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
866 // Totally ignore local items
867 if ((*I
)->Local
== true)
870 // see if the method tells us to expect more
871 TotalItems
+= (*I
)->ExpectedAdditionalItems
;
873 // check if there are unfetched Release files
874 if ((*I
)->Complete
== false && (*I
)->ExpectedAdditionalItems
> 0)
875 UnfetchedReleaseFiles
= true;
877 TotalBytes
+= (*I
)->FileSize
;
878 if ((*I
)->Complete
== true)
879 CurrentBytes
+= (*I
)->FileSize
;
880 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
884 // Compute the current completion
885 unsigned long long ResumeSize
= 0;
886 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
887 I
= Owner
->WorkerStep(I
))
889 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
891 CurrentBytes
+= I
->CurrentSize
;
892 ResumeSize
+= I
->ResumePoint
;
894 // Files with unknown size always have 100% completion
895 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
896 I
->CurrentItem
->Owner
->Complete
== false)
897 TotalBytes
+= I
->CurrentSize
;
901 // Normalize the figures and account for unknown size downloads
904 if (Unknown
== Count
)
905 TotalBytes
= Unknown
;
907 // Wha?! Is not supposed to happen.
908 if (CurrentBytes
> TotalBytes
)
909 CurrentBytes
= TotalBytes
;
912 if (_config
->FindB("Debug::acquire::progress", false) == true)
913 std::clog
<< " Bytes: "
914 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
918 struct timeval NewTime
;
919 gettimeofday(&NewTime
,0);
920 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
921 NewTime
.tv_sec
- Time
.tv_sec
> 6)
923 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
924 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
926 // Compute the CPS value
930 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
931 LastBytes
= CurrentBytes
- ResumeSize
;
932 ElapsedTime
= (unsigned long long)Delta
;
936 // calculate the percentage, if we have too little data assume 1%
937 if (TotalBytes
> 0 && UnfetchedReleaseFiles
)
940 // use both files and bytes because bytes can be unreliable
941 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
942 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
944 int fd
= _config
->FindI("APT::Status-Fd",-1);
947 ostringstream status
;
950 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
951 unsigned long long ETA
= 0;
953 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
955 // only show the ETA if it makes sense
956 if (ETA
> 0 && ETA
< 172800 /* two days */ )
957 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
959 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
961 // build the status str
962 status
<< "dlstatus:" << i
963 << ":" << std::setprecision(3) << Percent
967 std::string
const dlstatus
= status
.str();
968 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
974 // AcquireStatus::Start - Called when the download is started /*{{{*/
975 // ---------------------------------------------------------------------
976 /* We just reset the counters */
977 void pkgAcquireStatus::Start()
979 gettimeofday(&Time
,0);
980 gettimeofday(&StartTime
,0);
991 // AcquireStatus::Stop - Finished downloading /*{{{*/
992 // ---------------------------------------------------------------------
993 /* This accurately computes the elapsed time and the total overall CPS. */
994 void pkgAcquireStatus::Stop()
996 // Compute the CPS and elapsed time
997 struct timeval NewTime
;
998 gettimeofday(&NewTime
,0);
1000 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
1001 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
1003 // Compute the CPS value
1007 CurrentCPS
= FetchedBytes
/Delta
;
1008 LastBytes
= CurrentBytes
;
1009 ElapsedTime
= (unsigned long long)Delta
;
1012 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
1013 // ---------------------------------------------------------------------
1014 /* This is used to get accurate final transfer rate reporting. */
1015 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
1017 FetchedBytes
+= Size
- Resume
;
1021 APT_CONST
pkgAcquire::UriIterator::~UriIterator() {}
1022 APT_CONST
pkgAcquire::MethodConfig::~MethodConfig() {}
1023 APT_CONST
pkgAcquireStatus::~pkgAcquireStatus() {}