]>
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 schedual 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 controled by how the queue
11 name is derived from the URI.
13 ##################################################################### */
15 // Include Files /*{{{*/
17 #pragma implementation "apt-pkg/acquire.h"
19 #include <apt-pkg/acquire.h>
20 #include <apt-pkg/acquire-item.h>
21 #include <apt-pkg/acquire-worker.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/strutl.h>
38 // Acquire::pkgAcquire - Constructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* We grab some runtime state from the configuration space */
41 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Log
) : Log(Log
)
49 string Mode
= _config
->Find("Acquire::Queue-Mode","host");
50 if (strcasecmp(Mode
.c_str(),"host") == 0)
51 QueueMode
= QueueHost
;
52 if (strcasecmp(Mode
.c_str(),"access") == 0)
53 QueueMode
= QueueAccess
;
55 Debug
= _config
->FindB("Debug::pkgAcquire",false);
57 // This is really a stupid place for this
59 if (stat((_config
->FindDir("Dir::State::lists") + "partial/").c_str(),&St
) != 0 ||
60 S_ISDIR(St
.st_mode
) == 0)
61 _error
->Error(_("Lists directory %spartial is missing."),
62 _config
->FindDir("Dir::State::lists").c_str());
63 if (stat((_config
->FindDir("Dir::Cache::Archives") + "partial/").c_str(),&St
) != 0 ||
64 S_ISDIR(St
.st_mode
) == 0)
65 _error
->Error(_("Archive directory %spartial is missing."),
66 _config
->FindDir("Dir::Cache::Archives").c_str());
69 // Acquire::~pkgAcquire - Destructor /*{{{*/
70 // ---------------------------------------------------------------------
71 /* Free our memory, clean up the queues (destroy the workers) */
72 pkgAcquire::~pkgAcquire()
78 MethodConfig
*Jnk
= Configs
;
79 Configs
= Configs
->Next
;
84 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
85 // ---------------------------------------------------------------------
87 void pkgAcquire::Shutdown()
89 while (Items
.size() != 0)
91 if (Items
[0]->Status
== Item::StatFetching
)
92 Items
[0]->Status
= Item::StatError
;
99 Queues
= Queues
->Next
;
104 // Acquire::Add - Add a new item /*{{{*/
105 // ---------------------------------------------------------------------
106 /* This puts an item on the acquire list. This list is mainly for tracking
108 void pkgAcquire::Add(Item
*Itm
)
110 Items
.push_back(Itm
);
113 // Acquire::Remove - Remove a item /*{{{*/
114 // ---------------------------------------------------------------------
115 /* Remove an item from the acquire list. This is usually not used.. */
116 void pkgAcquire::Remove(Item
*Itm
)
120 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
132 // Acquire::Add - Add a worker /*{{{*/
133 // ---------------------------------------------------------------------
134 /* A list of workers is kept so that the select loop can direct their FD
136 void pkgAcquire::Add(Worker
*Work
)
138 Work
->NextAcquire
= Workers
;
142 // Acquire::Remove - Remove a worker /*{{{*/
143 // ---------------------------------------------------------------------
144 /* A worker has died. This can not be done while the select loop is running
145 as it would require that RunFds could handling a changing list state and
147 void pkgAcquire::Remove(Worker
*Work
)
152 Worker
**I
= &Workers
;
156 *I
= (*I
)->NextAcquire
;
158 I
= &(*I
)->NextAcquire
;
162 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
163 // ---------------------------------------------------------------------
164 /* This is the entry point for an item. An item calls this function when
165 it is constructed which creates a queue (based on the current queue
166 mode) and puts the item in that queue. If the system is running then
167 the queue might be started. */
168 void pkgAcquire::Enqueue(ItemDesc
&Item
)
170 // Determine which queue to put the item in
171 const MethodConfig
*Config
;
172 string Name
= QueueName(Item
.URI
,Config
);
173 if (Name
.empty() == true)
176 // Find the queue structure
178 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
181 I
= new Queue(Name
,this);
189 // See if this is a local only URI
190 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
191 Item
.Owner
->Local
= true;
192 Item
.Owner
->Status
= Item::StatIdle
;
194 // Queue it into the named queue
201 clog
<< "Fetching " << Item
.URI
<< endl
;
202 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
203 clog
<< " Queue is: " << Name
<< endl
;
207 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This is called when an item is finished being fetched. It removes it
210 from all the queues */
211 void pkgAcquire::Dequeue(Item
*Itm
)
215 for (; I
!= 0; I
= I
->Next
)
216 Res
|= I
->Dequeue(Itm
);
219 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
224 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
225 // ---------------------------------------------------------------------
226 /* The string returned depends on the configuration settings and the
227 method parameters. Given something like http://foo.org/bar it can
228 return http://foo.org or http */
229 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
233 Config
= GetConfig(U
.Access
);
237 /* Single-Instance methods get exactly one queue per URI. This is
238 also used for the Access queue method */
239 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
242 return U
.Access
+ ':' + U
.Host
;
245 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
246 // ---------------------------------------------------------------------
247 /* This locates the configuration structure for an access method. If
248 a config structure cannot be found a Worker will be created to
250 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
252 // Search for an existing config
254 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
255 if (Conf
->Access
== Access
)
258 // Create the new config class
259 Conf
= new MethodConfig
;
260 Conf
->Access
= Access
;
261 Conf
->Next
= Configs
;
264 // Create the worker to fetch the configuration
266 if (Work
.Start() == false)
269 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
270 if(_config
->FindI("Acquire::"+Access
+"::DlLimit",0) > 0)
271 Conf
->SingleInstance
= true;
276 // Acquire::SetFds - Deal with readable FDs /*{{{*/
277 // ---------------------------------------------------------------------
278 /* Collect FDs that have activity monitors into the fd sets */
279 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
281 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
283 if (I
->InReady
== true && I
->InFd
>= 0)
287 FD_SET(I
->InFd
,RSet
);
289 if (I
->OutReady
== true && I
->OutFd
>= 0)
293 FD_SET(I
->OutFd
,WSet
);
298 // Acquire::RunFds - Deal with active FDs /*{{{*/
299 // ---------------------------------------------------------------------
300 /* Dispatch active FDs over to the proper workers. It is very important
301 that a worker never be erased while this is running! The queue class
302 should never erase a worker except during shutdown processing. */
303 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
305 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
307 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
309 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
314 // Acquire::Run - Run the fetch sequence /*{{{*/
315 // ---------------------------------------------------------------------
316 /* This runs the queues. It manages a select loop for all of the
317 Worker tasks. The workers interact with the queues and items to
318 manage the actual fetch. */
319 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
323 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
329 bool WasCancelled
= false;
331 // Run till all things have been acquired
334 tv
.tv_usec
= PulseIntervall
;
342 SetFds(Highest
,&RFds
,&WFds
);
347 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
349 while (Res
< 0 && errno
== EINTR
);
353 _error
->Errno("select","Select has failed");
358 if (_error
->PendingError() == true)
361 // Timeout, notify the log class
362 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
364 tv
.tv_usec
= PulseIntervall
;
365 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
367 if (Log
!= 0 && Log
->Pulse(this) == false)
378 // Shut down the acquire bits
380 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
383 // Shut down the items
384 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); I
++)
387 if (_error
->PendingError())
394 // Acquire::Bump - Called when an item is dequeued /*{{{*/
395 // ---------------------------------------------------------------------
396 /* This routine bumps idle queues in hopes that they will be able to fetch
398 void pkgAcquire::Bump()
400 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
404 // Acquire::WorkerStep - Step to the next worker /*{{{*/
405 // ---------------------------------------------------------------------
406 /* Not inlined to advoid including acquire-worker.h */
407 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
409 return I
->NextAcquire
;
412 // Acquire::Clean - Cleans a directory /*{{{*/
413 // ---------------------------------------------------------------------
414 /* This is a bit simplistic, it looks at every file in the dir and sees
415 if it is part of the download set. */
416 bool pkgAcquire::Clean(string Dir
)
418 DIR *D
= opendir(Dir
.c_str());
420 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
422 string StartDir
= SafeGetCWD();
423 if (chdir(Dir
.c_str()) != 0)
426 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
429 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
432 if (strcmp(Dir
->d_name
,"lock") == 0 ||
433 strcmp(Dir
->d_name
,"partial") == 0 ||
434 strcmp(Dir
->d_name
,".") == 0 ||
435 strcmp(Dir
->d_name
,"..") == 0)
438 // Look in the get list
439 ItemCIterator I
= Items
.begin();
440 for (; I
!= Items
.end(); I
++)
441 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
444 // Nothing found, nuke it
445 if (I
== Items
.end())
449 chdir(StartDir
.c_str());
454 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
455 // ---------------------------------------------------------------------
456 /* This is the total number of bytes needed */
457 double pkgAcquire::TotalNeeded()
460 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
461 Total
+= (*I
)->FileSize
;
465 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
466 // ---------------------------------------------------------------------
467 /* This is the number of bytes that is not local */
468 double pkgAcquire::FetchNeeded()
471 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
472 if ((*I
)->Local
== false)
473 Total
+= (*I
)->FileSize
;
477 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
478 // ---------------------------------------------------------------------
479 /* This is the number of bytes that is not local */
480 double pkgAcquire::PartialPresent()
483 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
484 if ((*I
)->Local
== false)
485 Total
+= (*I
)->PartialSize
;
489 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
490 // ---------------------------------------------------------------------
492 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
494 return UriIterator(Queues
);
497 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
498 // ---------------------------------------------------------------------
500 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
502 return UriIterator(0);
506 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
507 // ---------------------------------------------------------------------
509 pkgAcquire::MethodConfig::MethodConfig()
511 SingleInstance
= false;
520 // Queue::Queue - Constructor /*{{{*/
521 // ---------------------------------------------------------------------
523 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
533 // Queue::~Queue - Destructor /*{{{*/
534 // ---------------------------------------------------------------------
536 pkgAcquire::Queue::~Queue()
548 // Queue::Enqueue - Queue an item to the queue /*{{{*/
549 // ---------------------------------------------------------------------
551 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
554 for (; *I
!= 0; I
= &(*I
)->Next
);
557 QItem
*Itm
= new QItem
;
562 Item
.Owner
->QueueCounter
++;
563 if (Items
->Next
== 0)
567 // Queue::Dequeue - Remove an item from the queue /*{{{*/
568 // ---------------------------------------------------------------------
569 /* We return true if we hit something */
570 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
572 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
573 return _error
->Error("Tried to dequeue a fetching object");
580 if ((*I
)->Owner
== Owner
)
584 Owner
->QueueCounter
--;
595 // Queue::Startup - Start the worker processes /*{{{*/
596 // ---------------------------------------------------------------------
597 /* It is possible for this to be called with a pre-existing set of
599 bool pkgAcquire::Queue::Startup()
604 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
608 Workers
= new Worker(this,Cnf
,Owner
->Log
);
610 if (Workers
->Start() == false)
613 /* When pipelining we commit 10 items. This needs to change when we
614 added other source retry to have cycle maintain a pipeline depth
616 if (Cnf
->Pipeline
== true)
625 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
626 // ---------------------------------------------------------------------
627 /* If final is true then all workers are eliminated, otherwise only workers
628 that do not need cleanup are removed */
629 bool pkgAcquire::Queue::Shutdown(bool Final
)
631 // Delete all of the workers
632 pkgAcquire::Worker
**Cur
= &Workers
;
635 pkgAcquire::Worker
*Jnk
= *Cur
;
636 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
638 *Cur
= Jnk
->NextQueue
;
643 Cur
= &(*Cur
)->NextQueue
;
649 // Queue::FindItem - Find a URI in the item list /*{{{*/
650 // ---------------------------------------------------------------------
652 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
654 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
655 if (I
->URI
== URI
&& I
->Worker
== Owner
)
660 // Queue::ItemDone - Item has been completed /*{{{*/
661 // ---------------------------------------------------------------------
662 /* The worker signals this which causes the item to be removed from the
663 queue. If this is the last queue instance then it is removed from the
665 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
668 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
669 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
671 if (Itm
->Owner
->QueueCounter
<= 1)
672 Owner
->Dequeue(Itm
->Owner
);
682 // Queue::Cycle - Queue new items into the method /*{{{*/
683 // ---------------------------------------------------------------------
684 /* This locates a new idle item and sends it to the worker. If pipelining
685 is enabled then it keeps the pipe full. */
686 bool pkgAcquire::Queue::Cycle()
688 if (Items
== 0 || Workers
== 0)
692 return _error
->Error("Pipedepth failure");
694 // Look for a queable item
696 while (PipeDepth
< (signed)MaxPipeDepth
)
698 for (; I
!= 0; I
= I
->Next
)
699 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
702 // Nothing to do, queue is idle.
707 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
709 if (Workers
->QueueItem(I
) == false)
716 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
717 // ---------------------------------------------------------------------
718 /* This is called when an item in multiple queues is dequeued */
719 void pkgAcquire::Queue::Bump()
725 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
726 // ---------------------------------------------------------------------
728 pkgAcquireStatus::pkgAcquireStatus() : Update(true), MorePulses(false)
733 // AcquireStatus::Pulse - Called periodically /*{{{*/
734 // ---------------------------------------------------------------------
735 /* This computes some internal state variables for the derived classes to
736 use. It generates the current downloaded bytes and total bytes to download
737 as well as the current CPS estimate. */
738 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
745 // Compute the total number of bytes to fetch
746 unsigned int Unknown
= 0;
747 unsigned int Count
= 0;
748 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
752 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
755 // Totally ignore local items
756 if ((*I
)->Local
== true)
759 TotalBytes
+= (*I
)->FileSize
;
760 if ((*I
)->Complete
== true)
761 CurrentBytes
+= (*I
)->FileSize
;
762 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
766 // Compute the current completion
767 unsigned long ResumeSize
= 0;
768 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
769 I
= Owner
->WorkerStep(I
))
770 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
772 CurrentBytes
+= I
->CurrentSize
;
773 ResumeSize
+= I
->ResumePoint
;
775 // Files with unknown size always have 100% completion
776 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
777 I
->CurrentItem
->Owner
->Complete
== false)
778 TotalBytes
+= I
->CurrentSize
;
781 // Normalize the figures and account for unknown size downloads
784 if (Unknown
== Count
)
785 TotalBytes
= Unknown
;
787 // Wha?! Is not supposed to happen.
788 if (CurrentBytes
> TotalBytes
)
789 CurrentBytes
= TotalBytes
;
792 struct timeval NewTime
;
793 gettimeofday(&NewTime
,0);
794 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
795 NewTime
.tv_sec
- Time
.tv_sec
> 6)
797 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
798 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
800 // Compute the CPS value
804 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
805 LastBytes
= CurrentBytes
- ResumeSize
;
806 ElapsedTime
= (unsigned long)Delta
;
813 // AcquireStatus::Start - Called when the download is started /*{{{*/
814 // ---------------------------------------------------------------------
815 /* We just reset the counters */
816 void pkgAcquireStatus::Start()
818 gettimeofday(&Time
,0);
819 gettimeofday(&StartTime
,0);
830 // AcquireStatus::Stop - Finished downloading /*{{{*/
831 // ---------------------------------------------------------------------
832 /* This accurately computes the elapsed time and the total overall CPS. */
833 void pkgAcquireStatus::Stop()
835 // Compute the CPS and elapsed time
836 struct timeval NewTime
;
837 gettimeofday(&NewTime
,0);
839 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
840 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
842 // Compute the CPS value
846 CurrentCPS
= FetchedBytes
/Delta
;
847 LastBytes
= CurrentBytes
;
848 ElapsedTime
= (unsigned int)Delta
;
851 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
852 // ---------------------------------------------------------------------
853 /* This is used to get accurate final transfer rate reporting. */
854 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
856 FetchedBytes
+= Size
- Resume
;