]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1e20b74be3ab48fabfae946b34c5dede0e44a617
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
)
91 // chown the partial dir
92 if(chown(partial
.c_str(), pw
->pw_uid
, gr
->gr_gid
) != 0)
93 _error
->WarningE("SetupAPTPartialDirectory", "chown to %s:root of directory %s failed", SandboxUser
.c_str(), partial
.c_str());
94 // chown the auth.conf file
95 std::string
const AuthConf
= _config
->FindFile("Dir::Etc::netrc");
96 if(AuthConf
.empty() == false && RealFileExists(AuthConf
) &&
97 chown(AuthConf
.c_str(), pw
->pw_uid
, gr
->gr_gid
) != 0)
98 _error
->WarningE("SetupAPTPartialDirectory", "chown to %s:root of file %s failed", SandboxUser
.c_str(), AuthConf
.c_str());
101 if (chmod(partial
.c_str(), 0700) != 0)
102 _error
->WarningE("SetupAPTPartialDirectory", "chmod 0700 of directory %s failed", partial
.c_str());
106 bool pkgAcquire::Setup(pkgAcquireStatus
*Progress
, string
const &Lock
)
111 string
const listDir
= _config
->FindDir("Dir::State::lists");
112 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::State"), listDir
) == false)
113 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
114 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
115 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::Cache"), archivesDir
) == false)
116 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
119 return GetLock(Lock
);
121 bool pkgAcquire::GetLock(std::string
const &Lock
)
123 if (Lock
.empty() == true)
126 // check for existence and possibly create auxiliary directories
127 string
const listDir
= _config
->FindDir("Dir::State::lists");
128 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
132 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::State"), listDir
) == false)
133 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
135 if (Lock
== archivesDir
)
137 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::Cache"), archivesDir
) == false)
138 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
141 if (_config
->FindB("Debug::NoLocking", false) == true)
144 // Lock the directory this acquire object will work in
145 LockFD
= ::GetLock(flCombine(Lock
, "lock"));
147 return _error
->Error(_("Unable to lock directory %s"), Lock
.c_str());
152 // Acquire::~pkgAcquire - Destructor /*{{{*/
153 // ---------------------------------------------------------------------
154 /* Free our memory, clean up the queues (destroy the workers) */
155 pkgAcquire::~pkgAcquire()
164 MethodConfig
*Jnk
= Configs
;
165 Configs
= Configs
->Next
;
170 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
171 // ---------------------------------------------------------------------
173 void pkgAcquire::Shutdown()
175 while (Items
.empty() == false)
177 if (Items
[0]->Status
== Item::StatFetching
)
178 Items
[0]->Status
= Item::StatError
;
185 Queues
= Queues
->Next
;
190 // Acquire::Add - Add a new item /*{{{*/
191 // ---------------------------------------------------------------------
192 /* This puts an item on the acquire list. This list is mainly for tracking
194 void pkgAcquire::Add(Item
*Itm
)
196 Items
.push_back(Itm
);
199 // Acquire::Remove - Remove a item /*{{{*/
200 // ---------------------------------------------------------------------
201 /* Remove an item from the acquire list. This is usually not used.. */
202 void pkgAcquire::Remove(Item
*Itm
)
206 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
218 // Acquire::Add - Add a worker /*{{{*/
219 // ---------------------------------------------------------------------
220 /* A list of workers is kept so that the select loop can direct their FD
222 void pkgAcquire::Add(Worker
*Work
)
224 Work
->NextAcquire
= Workers
;
228 // Acquire::Remove - Remove a worker /*{{{*/
229 // ---------------------------------------------------------------------
230 /* A worker has died. This can not be done while the select loop is running
231 as it would require that RunFds could handling a changing list state and
233 void pkgAcquire::Remove(Worker
*Work
)
238 Worker
**I
= &Workers
;
242 *I
= (*I
)->NextAcquire
;
244 I
= &(*I
)->NextAcquire
;
248 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
249 // ---------------------------------------------------------------------
250 /* This is the entry point for an item. An item calls this function when
251 it is constructed which creates a queue (based on the current queue
252 mode) and puts the item in that queue. If the system is running then
253 the queue might be started. */
254 void pkgAcquire::Enqueue(ItemDesc
&Item
)
256 // Determine which queue to put the item in
257 const MethodConfig
*Config
;
258 string Name
= QueueName(Item
.URI
,Config
);
259 if (Name
.empty() == true)
262 // Find the queue structure
264 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
267 I
= new Queue(Name
,this);
275 // See if this is a local only URI
276 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
277 Item
.Owner
->Local
= true;
278 Item
.Owner
->Status
= Item::StatIdle
;
280 // Queue it into the named queue
287 clog
<< "Fetching " << Item
.URI
<< endl
;
288 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
289 clog
<< " Queue is: " << Name
<< endl
;
293 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
294 // ---------------------------------------------------------------------
295 /* This is called when an item is finished being fetched. It removes it
296 from all the queues */
297 void pkgAcquire::Dequeue(Item
*Itm
)
302 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
304 for (; I
!= 0; I
= I
->Next
)
310 clog
<< "Dequeued from " << I
->Name
<< endl
;
318 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
319 // ---------------------------------------------------------------------
320 /* The string returned depends on the configuration settings and the
321 method parameters. Given something like http://foo.org/bar it can
322 return http://foo.org or http */
323 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
327 Config
= GetConfig(U
.Access
);
331 /* Single-Instance methods get exactly one queue per URI. This is
332 also used for the Access queue method */
333 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
336 string AccessSchema
= U
.Access
+ ':',
337 FullQueueName
= AccessSchema
+ U
.Host
;
338 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
341 for (; I
!= 0; I
= I
->Next
) {
342 // if the queue already exists, re-use it
343 if (I
->Name
== FullQueueName
)
344 return FullQueueName
;
346 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
351 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
354 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
357 return FullQueueName
;
360 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
361 // ---------------------------------------------------------------------
362 /* This locates the configuration structure for an access method. If
363 a config structure cannot be found a Worker will be created to
365 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
367 // Search for an existing config
369 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
370 if (Conf
->Access
== Access
)
373 // Create the new config class
374 Conf
= new MethodConfig
;
375 Conf
->Access
= Access
;
376 Conf
->Next
= Configs
;
379 // Create the worker to fetch the configuration
381 if (Work
.Start() == false)
384 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
385 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
386 Conf
->SingleInstance
= true;
391 // Acquire::SetFds - Deal with readable FDs /*{{{*/
392 // ---------------------------------------------------------------------
393 /* Collect FDs that have activity monitors into the fd sets */
394 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
396 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
398 if (I
->InReady
== true && I
->InFd
>= 0)
402 FD_SET(I
->InFd
,RSet
);
404 if (I
->OutReady
== true && I
->OutFd
>= 0)
408 FD_SET(I
->OutFd
,WSet
);
413 // Acquire::RunFds - Deal with active FDs /*{{{*/
414 // ---------------------------------------------------------------------
415 /* Dispatch active FDs over to the proper workers. It is very important
416 that a worker never be erased while this is running! The queue class
417 should never erase a worker except during shutdown processing. */
418 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
420 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
422 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
424 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
429 // Acquire::Run - Run the fetch sequence /*{{{*/
430 // ---------------------------------------------------------------------
431 /* This runs the queues. It manages a select loop for all of the
432 Worker tasks. The workers interact with the queues and items to
433 manage the actual fetch. */
434 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
438 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
444 bool WasCancelled
= false;
446 // Run till all things have been acquired
449 tv
.tv_usec
= PulseIntervall
;
457 SetFds(Highest
,&RFds
,&WFds
);
462 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
464 while (Res
< 0 && errno
== EINTR
);
468 _error
->Errno("select","Select has failed");
473 if (_error
->PendingError() == true)
476 // Timeout, notify the log class
477 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
479 tv
.tv_usec
= PulseIntervall
;
480 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
482 if (Log
!= 0 && Log
->Pulse(this) == false)
493 // Shut down the acquire bits
495 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
498 // Shut down the items
499 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
502 if (_error
->PendingError())
509 // Acquire::Bump - Called when an item is dequeued /*{{{*/
510 // ---------------------------------------------------------------------
511 /* This routine bumps idle queues in hopes that they will be able to fetch
513 void pkgAcquire::Bump()
515 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
519 // Acquire::WorkerStep - Step to the next worker /*{{{*/
520 // ---------------------------------------------------------------------
521 /* Not inlined to advoid including acquire-worker.h */
522 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
524 return I
->NextAcquire
;
527 // Acquire::Clean - Cleans a directory /*{{{*/
528 // ---------------------------------------------------------------------
529 /* This is a bit simplistic, it looks at every file in the dir and sees
530 if it is part of the download set. */
531 bool pkgAcquire::Clean(string Dir
)
533 // non-existing directories are by definition clean…
534 if (DirectoryExists(Dir
) == false)
538 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
540 DIR *D
= opendir(Dir
.c_str());
542 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
544 string StartDir
= SafeGetCWD();
545 if (chdir(Dir
.c_str()) != 0)
548 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
551 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
554 if (strcmp(Dir
->d_name
,"lock") == 0 ||
555 strcmp(Dir
->d_name
,"partial") == 0 ||
556 strcmp(Dir
->d_name
,".") == 0 ||
557 strcmp(Dir
->d_name
,"..") == 0)
560 // Look in the get list
561 ItemCIterator I
= Items
.begin();
562 for (; I
!= Items
.end(); ++I
)
563 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
566 // Nothing found, nuke it
567 if (I
== Items
.end())
572 if (chdir(StartDir
.c_str()) != 0)
573 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
577 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
578 // ---------------------------------------------------------------------
579 /* This is the total number of bytes needed */
580 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
582 unsigned long long Total
= 0;
583 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
584 Total
+= (*I
)->FileSize
;
588 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
589 // ---------------------------------------------------------------------
590 /* This is the number of bytes that is not local */
591 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
593 unsigned long long Total
= 0;
594 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
595 if ((*I
)->Local
== false)
596 Total
+= (*I
)->FileSize
;
600 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
601 // ---------------------------------------------------------------------
602 /* This is the number of bytes that is not local */
603 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
605 unsigned long long Total
= 0;
606 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); ++I
)
607 if ((*I
)->Local
== false)
608 Total
+= (*I
)->PartialSize
;
612 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
613 // ---------------------------------------------------------------------
615 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
617 return UriIterator(Queues
);
620 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
621 // ---------------------------------------------------------------------
623 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
625 return UriIterator(0);
628 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
629 // ---------------------------------------------------------------------
631 pkgAcquire::MethodConfig::MethodConfig() : d(NULL
), Next(0), SingleInstance(false),
632 Pipeline(false), SendConfig(false), LocalOnly(false), NeedsCleanup(false),
637 // Queue::Queue - Constructor /*{{{*/
638 // ---------------------------------------------------------------------
640 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : d(NULL
), Next(0),
641 Name(Name
), Items(0), Workers(0), Owner(Owner
), PipeDepth(0), MaxPipeDepth(1)
645 // Queue::~Queue - Destructor /*{{{*/
646 // ---------------------------------------------------------------------
648 pkgAcquire::Queue::~Queue()
660 // Queue::Enqueue - Queue an item to the queue /*{{{*/
661 // ---------------------------------------------------------------------
663 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
666 // move to the end of the queue and check for duplicates here
667 for (; *I
!= 0; I
= &(*I
)->Next
)
668 if (Item
.URI
== (*I
)->URI
)
670 Item
.Owner
->Status
= Item::StatDone
;
675 QItem
*Itm
= new QItem
;
680 Item
.Owner
->QueueCounter
++;
681 if (Items
->Next
== 0)
686 // Queue::Dequeue - Remove an item from the queue /*{{{*/
687 // ---------------------------------------------------------------------
688 /* We return true if we hit something */
689 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
691 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
692 return _error
->Error("Tried to dequeue a fetching object");
699 if ((*I
)->Owner
== Owner
)
703 Owner
->QueueCounter
--;
714 // Queue::Startup - Start the worker processes /*{{{*/
715 // ---------------------------------------------------------------------
716 /* It is possible for this to be called with a pre-existing set of
718 bool pkgAcquire::Queue::Startup()
723 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
727 Workers
= new Worker(this,Cnf
,Owner
->Log
);
729 if (Workers
->Start() == false)
732 /* When pipelining we commit 10 items. This needs to change when we
733 added other source retry to have cycle maintain a pipeline depth
735 if (Cnf
->Pipeline
== true)
736 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
744 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
745 // ---------------------------------------------------------------------
746 /* If final is true then all workers are eliminated, otherwise only workers
747 that do not need cleanup are removed */
748 bool pkgAcquire::Queue::Shutdown(bool Final
)
750 // Delete all of the workers
751 pkgAcquire::Worker
**Cur
= &Workers
;
754 pkgAcquire::Worker
*Jnk
= *Cur
;
755 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
757 *Cur
= Jnk
->NextQueue
;
762 Cur
= &(*Cur
)->NextQueue
;
768 // Queue::FindItem - Find a URI in the item list /*{{{*/
769 // ---------------------------------------------------------------------
771 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
773 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
774 if (I
->URI
== URI
&& I
->Worker
== Owner
)
779 // Queue::ItemDone - Item has been completed /*{{{*/
780 // ---------------------------------------------------------------------
781 /* The worker signals this which causes the item to be removed from the
782 queue. If this is the last queue instance then it is removed from the
784 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
787 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
788 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
790 if (Itm
->Owner
->QueueCounter
<= 1)
791 Owner
->Dequeue(Itm
->Owner
);
801 // Queue::Cycle - Queue new items into the method /*{{{*/
802 // ---------------------------------------------------------------------
803 /* This locates a new idle item and sends it to the worker. If pipelining
804 is enabled then it keeps the pipe full. */
805 bool pkgAcquire::Queue::Cycle()
807 if (Items
== 0 || Workers
== 0)
811 return _error
->Error("Pipedepth failure");
813 // Look for a queable item
815 while (PipeDepth
< (signed)MaxPipeDepth
)
817 for (; I
!= 0; I
= I
->Next
)
818 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
821 // Nothing to do, queue is idle.
826 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
828 if (Workers
->QueueItem(I
) == false)
835 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
836 // ---------------------------------------------------------------------
837 /* This is called when an item in multiple queues is dequeued */
838 void pkgAcquire::Queue::Bump()
843 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
844 // ---------------------------------------------------------------------
846 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Percent(0), Update(true), MorePulses(false)
851 // AcquireStatus::Pulse - Called periodically /*{{{*/
852 // ---------------------------------------------------------------------
853 /* This computes some internal state variables for the derived classes to
854 use. It generates the current downloaded bytes and total bytes to download
855 as well as the current CPS estimate. */
856 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
863 // Compute the total number of bytes to fetch
864 unsigned int Unknown
= 0;
865 unsigned int Count
= 0;
866 bool UnfetchedReleaseFiles
= false;
867 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
868 I
!= Owner
->ItemsEnd();
872 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
875 // Totally ignore local items
876 if ((*I
)->Local
== true)
879 // see if the method tells us to expect more
880 TotalItems
+= (*I
)->ExpectedAdditionalItems
;
882 // check if there are unfetched Release files
883 if ((*I
)->Complete
== false && (*I
)->ExpectedAdditionalItems
> 0)
884 UnfetchedReleaseFiles
= true;
886 TotalBytes
+= (*I
)->FileSize
;
887 if ((*I
)->Complete
== true)
888 CurrentBytes
+= (*I
)->FileSize
;
889 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
893 // Compute the current completion
894 unsigned long long ResumeSize
= 0;
895 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
896 I
= Owner
->WorkerStep(I
))
898 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
900 CurrentBytes
+= I
->CurrentSize
;
901 ResumeSize
+= I
->ResumePoint
;
903 // Files with unknown size always have 100% completion
904 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
905 I
->CurrentItem
->Owner
->Complete
== false)
906 TotalBytes
+= I
->CurrentSize
;
910 // Normalize the figures and account for unknown size downloads
913 if (Unknown
== Count
)
914 TotalBytes
= Unknown
;
916 // Wha?! Is not supposed to happen.
917 if (CurrentBytes
> TotalBytes
)
918 CurrentBytes
= TotalBytes
;
921 if (_config
->FindB("Debug::acquire::progress", false) == true)
922 std::clog
<< " Bytes: "
923 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
927 struct timeval NewTime
;
928 gettimeofday(&NewTime
,0);
929 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
930 NewTime
.tv_sec
- Time
.tv_sec
> 6)
932 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
933 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
935 // Compute the CPS value
939 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
940 LastBytes
= CurrentBytes
- ResumeSize
;
941 ElapsedTime
= (unsigned long long)Delta
;
945 // calculate the percentage, if we have too little data assume 1%
946 if (TotalBytes
> 0 && UnfetchedReleaseFiles
)
949 // use both files and bytes because bytes can be unreliable
950 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
951 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
953 int fd
= _config
->FindI("APT::Status-Fd",-1);
956 ostringstream status
;
959 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
960 unsigned long long ETA
= 0;
962 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
964 // only show the ETA if it makes sense
965 if (ETA
> 0 && ETA
< 172800 /* two days */ )
966 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
968 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
970 // build the status str
971 status
<< "dlstatus:" << i
972 << ":" << std::setprecision(3) << Percent
976 std::string
const dlstatus
= status
.str();
977 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
983 // AcquireStatus::Start - Called when the download is started /*{{{*/
984 // ---------------------------------------------------------------------
985 /* We just reset the counters */
986 void pkgAcquireStatus::Start()
988 gettimeofday(&Time
,0);
989 gettimeofday(&StartTime
,0);
1000 // AcquireStatus::Stop - Finished downloading /*{{{*/
1001 // ---------------------------------------------------------------------
1002 /* This accurately computes the elapsed time and the total overall CPS. */
1003 void pkgAcquireStatus::Stop()
1005 // Compute the CPS and elapsed time
1006 struct timeval NewTime
;
1007 gettimeofday(&NewTime
,0);
1009 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
1010 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
1012 // Compute the CPS value
1016 CurrentCPS
= FetchedBytes
/Delta
;
1017 LastBytes
= CurrentBytes
;
1018 ElapsedTime
= (unsigned long long)Delta
;
1021 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
1022 // ---------------------------------------------------------------------
1023 /* This is used to get accurate final transfer rate reporting. */
1024 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
1026 FetchedBytes
+= Size
- Resume
;
1030 APT_CONST
pkgAcquire::UriIterator::~UriIterator() {}
1031 APT_CONST
pkgAcquire::MethodConfig::~MethodConfig() {}
1032 APT_CONST
pkgAcquireStatus::~pkgAcquireStatus() {}