]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
ae3ba2e01caf46287274c9f4a8149a8b875fd086
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.48 2001/05/22 04:17:18 jgg 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)
95 Queues
= Queues
->Next
;
100 // Acquire::Add - Add a new item /*{{{*/
101 // ---------------------------------------------------------------------
102 /* This puts an item on the acquire list. This list is mainly for tracking
104 void pkgAcquire::Add(Item
*Itm
)
106 Items
.push_back(Itm
);
109 // Acquire::Remove - Remove a item /*{{{*/
110 // ---------------------------------------------------------------------
111 /* Remove an item from the acquire list. This is usually not used.. */
112 void pkgAcquire::Remove(Item
*Itm
)
116 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); I
++)
126 // Acquire::Add - Add a worker /*{{{*/
127 // ---------------------------------------------------------------------
128 /* A list of workers is kept so that the select loop can direct their FD
130 void pkgAcquire::Add(Worker
*Work
)
132 Work
->NextAcquire
= Workers
;
136 // Acquire::Remove - Remove a worker /*{{{*/
137 // ---------------------------------------------------------------------
138 /* A worker has died. This can not be done while the select loop is running
139 as it would require that RunFds could handling a changing list state and
141 void pkgAcquire::Remove(Worker
*Work
)
146 Worker
**I
= &Workers
;
150 *I
= (*I
)->NextAcquire
;
152 I
= &(*I
)->NextAcquire
;
156 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This is the entry point for an item. An item calls this function when
159 it is constructed which creates a queue (based on the current queue
160 mode) and puts the item in that queue. If the system is running then
161 the queue might be started. */
162 void pkgAcquire::Enqueue(ItemDesc
&Item
)
164 // Determine which queue to put the item in
165 const MethodConfig
*Config
;
166 string Name
= QueueName(Item
.URI
,Config
);
167 if (Name
.empty() == true)
170 // Find the queue structure
172 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
175 I
= new Queue(Name
,this);
183 // See if this is a local only URI
184 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
185 Item
.Owner
->Local
= true;
186 Item
.Owner
->Status
= Item::StatIdle
;
188 // Queue it into the named queue
195 clog
<< "Fetching " << Item
.URI
<< endl
;
196 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
197 clog
<< " Queue is: " << Name
<< endl
;
201 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
202 // ---------------------------------------------------------------------
203 /* This is called when an item is finished being fetched. It removes it
204 from all the queues */
205 void pkgAcquire::Dequeue(Item
*Itm
)
209 for (; I
!= 0; I
= I
->Next
)
210 Res
|= I
->Dequeue(Itm
);
213 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
218 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
219 // ---------------------------------------------------------------------
220 /* The string returned depends on the configuration settings and the
221 method parameters. Given something like http://foo.org/bar it can
222 return http://foo.org or http */
223 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
227 Config
= GetConfig(U
.Access
);
231 /* Single-Instance methods get exactly one queue per URI. This is
232 also used for the Access queue method */
233 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
236 return U
.Access
+ ':' + U
.Host
;
239 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
240 // ---------------------------------------------------------------------
241 /* This locates the configuration structure for an access method. If
242 a config structure cannot be found a Worker will be created to
244 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
246 // Search for an existing config
248 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
249 if (Conf
->Access
== Access
)
252 // Create the new config class
253 Conf
= new MethodConfig
;
254 Conf
->Access
= Access
;
255 Conf
->Next
= Configs
;
258 // Create the worker to fetch the configuration
260 if (Work
.Start() == false)
266 // Acquire::SetFds - Deal with readable FDs /*{{{*/
267 // ---------------------------------------------------------------------
268 /* Collect FDs that have activity monitors into the fd sets */
269 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
271 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
273 if (I
->InReady
== true && I
->InFd
>= 0)
277 FD_SET(I
->InFd
,RSet
);
279 if (I
->OutReady
== true && I
->OutFd
>= 0)
283 FD_SET(I
->OutFd
,WSet
);
288 // Acquire::RunFds - Deal with active FDs /*{{{*/
289 // ---------------------------------------------------------------------
290 /* Dispatch active FDs over to the proper workers. It is very important
291 that a worker never be erased while this is running! The queue class
292 should never erase a worker except during shutdown processing. */
293 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
295 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
297 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
299 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
304 // Acquire::Run - Run the fetch sequence /*{{{*/
305 // ---------------------------------------------------------------------
306 /* This runs the queues. It manages a select loop for all of the
307 Worker tasks. The workers interact with the queues and items to
308 manage the actual fetch. */
309 pkgAcquire::RunResult
pkgAcquire::Run()
313 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
319 bool WasCancelled
= false;
321 // Run till all things have been acquired
332 SetFds(Highest
,&RFds
,&WFds
);
337 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
339 while (Res
< 0 && errno
== EINTR
);
343 _error
->Errno("select","Select has failed");
348 if (_error
->PendingError() == true)
351 // Timeout, notify the log class
352 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
355 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
357 if (Log
!= 0 && Log
->Pulse(this) == false)
368 // Shut down the acquire bits
370 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
373 // Shut down the items
374 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); I
++)
377 if (_error
->PendingError())
384 // Acquire::Bump - Called when an item is dequeued /*{{{*/
385 // ---------------------------------------------------------------------
386 /* This routine bumps idle queues in hopes that they will be able to fetch
388 void pkgAcquire::Bump()
390 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
394 // Acquire::WorkerStep - Step to the next worker /*{{{*/
395 // ---------------------------------------------------------------------
396 /* Not inlined to advoid including acquire-worker.h */
397 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
399 return I
->NextAcquire
;
402 // Acquire::Clean - Cleans a directory /*{{{*/
403 // ---------------------------------------------------------------------
404 /* This is a bit simplistic, it looks at every file in the dir and sees
405 if it is part of the download set. */
406 bool pkgAcquire::Clean(string Dir
)
408 DIR *D
= opendir(Dir
.c_str());
410 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
412 string StartDir
= SafeGetCWD();
413 if (chdir(Dir
.c_str()) != 0)
416 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
419 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
422 if (strcmp(Dir
->d_name
,"lock") == 0 ||
423 strcmp(Dir
->d_name
,"partial") == 0 ||
424 strcmp(Dir
->d_name
,".") == 0 ||
425 strcmp(Dir
->d_name
,"..") == 0)
428 // Look in the get list
429 ItemCIterator I
= Items
.begin();
430 for (; I
!= Items
.end(); I
++)
431 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
434 // Nothing found, nuke it
435 if (I
== Items
.end())
439 chdir(StartDir
.c_str());
444 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
445 // ---------------------------------------------------------------------
446 /* This is the total number of bytes needed */
447 double pkgAcquire::TotalNeeded()
450 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
451 Total
+= (*I
)->FileSize
;
455 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
456 // ---------------------------------------------------------------------
457 /* This is the number of bytes that is not local */
458 double pkgAcquire::FetchNeeded()
461 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
462 if ((*I
)->Local
== false)
463 Total
+= (*I
)->FileSize
;
467 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
468 // ---------------------------------------------------------------------
469 /* This is the number of bytes that is not local */
470 double pkgAcquire::PartialPresent()
473 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
474 if ((*I
)->Local
== false)
475 Total
+= (*I
)->PartialSize
;
479 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
480 // ---------------------------------------------------------------------
482 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
484 return UriIterator(Queues
);
487 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
488 // ---------------------------------------------------------------------
490 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
492 return UriIterator(0);
496 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
497 // ---------------------------------------------------------------------
499 pkgAcquire::MethodConfig::MethodConfig()
501 SingleInstance
= false;
510 // Queue::Queue - Constructor /*{{{*/
511 // ---------------------------------------------------------------------
513 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
523 // Queue::~Queue - Destructor /*{{{*/
524 // ---------------------------------------------------------------------
526 pkgAcquire::Queue::~Queue()
538 // Queue::Enqueue - Queue an item to the queue /*{{{*/
539 // ---------------------------------------------------------------------
541 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
544 for (; *I
!= 0; I
= &(*I
)->Next
);
547 QItem
*Itm
= new QItem
;
552 Item
.Owner
->QueueCounter
++;
553 if (Items
->Next
== 0)
557 // Queue::Dequeue - Remove an item from the queue /*{{{*/
558 // ---------------------------------------------------------------------
559 /* We return true if we hit something */
560 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
562 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
563 return _error
->Error("Tried to dequeue a fetching object");
570 if ((*I
)->Owner
== Owner
)
574 Owner
->QueueCounter
--;
585 // Queue::Startup - Start the worker processes /*{{{*/
586 // ---------------------------------------------------------------------
587 /* It is possible for this to be called with a pre-existing set of
589 bool pkgAcquire::Queue::Startup()
594 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
598 Workers
= new Worker(this,Cnf
,Owner
->Log
);
600 if (Workers
->Start() == false)
603 /* When pipelining we commit 10 items. This needs to change when we
604 added other source retry to have cycle maintain a pipeline depth
606 if (Cnf
->Pipeline
== true)
615 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
616 // ---------------------------------------------------------------------
617 /* If final is true then all workers are eliminated, otherwise only workers
618 that do not need cleanup are removed */
619 bool pkgAcquire::Queue::Shutdown(bool Final
)
621 // Delete all of the workers
622 pkgAcquire::Worker
**Cur
= &Workers
;
625 pkgAcquire::Worker
*Jnk
= *Cur
;
626 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
628 *Cur
= Jnk
->NextQueue
;
633 Cur
= &(*Cur
)->NextQueue
;
639 // Queue::FindItem - Find a URI in the item list /*{{{*/
640 // ---------------------------------------------------------------------
642 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
644 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
645 if (I
->URI
== URI
&& I
->Worker
== Owner
)
650 // Queue::ItemDone - Item has been completed /*{{{*/
651 // ---------------------------------------------------------------------
652 /* The worker signals this which causes the item to be removed from the
653 queue. If this is the last queue instance then it is removed from the
655 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
658 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
659 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
661 if (Itm
->Owner
->QueueCounter
<= 1)
662 Owner
->Dequeue(Itm
->Owner
);
672 // Queue::Cycle - Queue new items into the method /*{{{*/
673 // ---------------------------------------------------------------------
674 /* This locates a new idle item and sends it to the worker. If pipelining
675 is enabled then it keeps the pipe full. */
676 bool pkgAcquire::Queue::Cycle()
678 if (Items
== 0 || Workers
== 0)
682 return _error
->Error("Pipedepth failure");
684 // Look for a queable item
686 while (PipeDepth
< (signed)MaxPipeDepth
)
688 for (; I
!= 0; I
= I
->Next
)
689 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
692 // Nothing to do, queue is idle.
697 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
699 if (Workers
->QueueItem(I
) == false)
706 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
707 // ---------------------------------------------------------------------
708 /* This is called when an item in multiple queues is dequeued */
709 void pkgAcquire::Queue::Bump()
715 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
716 // ---------------------------------------------------------------------
718 pkgAcquireStatus::pkgAcquireStatus() : Update(true), MorePulses(false)
723 // AcquireStatus::Pulse - Called periodically /*{{{*/
724 // ---------------------------------------------------------------------
725 /* This computes some internal state variables for the derived classes to
726 use. It generates the current downloaded bytes and total bytes to download
727 as well as the current CPS estimate. */
728 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
735 // Compute the total number of bytes to fetch
736 unsigned int Unknown
= 0;
737 unsigned int Count
= 0;
738 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
742 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
745 // Totally ignore local items
746 if ((*I
)->Local
== true)
749 TotalBytes
+= (*I
)->FileSize
;
750 if ((*I
)->Complete
== true)
751 CurrentBytes
+= (*I
)->FileSize
;
752 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
756 // Compute the current completion
757 unsigned long ResumeSize
= 0;
758 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
759 I
= Owner
->WorkerStep(I
))
760 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
762 CurrentBytes
+= I
->CurrentSize
;
763 ResumeSize
+= I
->ResumePoint
;
765 // Files with unknown size always have 100% completion
766 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
767 I
->CurrentItem
->Owner
->Complete
== false)
768 TotalBytes
+= I
->CurrentSize
;
771 // Normalize the figures and account for unknown size downloads
774 if (Unknown
== Count
)
775 TotalBytes
= Unknown
;
777 // Wha?! Is not supposed to happen.
778 if (CurrentBytes
> TotalBytes
)
779 CurrentBytes
= TotalBytes
;
782 struct timeval NewTime
;
783 gettimeofday(&NewTime
,0);
784 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
785 NewTime
.tv_sec
- Time
.tv_sec
> 6)
787 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
788 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
790 // Compute the CPS value
794 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
795 LastBytes
= CurrentBytes
- ResumeSize
;
796 ElapsedTime
= (unsigned long)Delta
;
803 // AcquireStatus::Start - Called when the download is started /*{{{*/
804 // ---------------------------------------------------------------------
805 /* We just reset the counters */
806 void pkgAcquireStatus::Start()
808 gettimeofday(&Time
,0);
809 gettimeofday(&StartTime
,0);
820 // AcquireStatus::Stop - Finished downloading /*{{{*/
821 // ---------------------------------------------------------------------
822 /* This accurately computes the elapsed time and the total overall CPS. */
823 void pkgAcquireStatus::Stop()
825 // Compute the CPS and elapsed time
826 struct timeval NewTime
;
827 gettimeofday(&NewTime
,0);
829 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
830 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
832 // Compute the CPS value
836 CurrentCPS
= FetchedBytes
/Delta
;
837 LastBytes
= CurrentBytes
;
838 ElapsedTime
= (unsigned int)Delta
;
841 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
842 // ---------------------------------------------------------------------
843 /* This is used to get accurate final transfer rate reporting. */
844 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
846 FetchedBytes
+= Size
- Resume
;