]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.49 2001/05/27 04:28:37 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();)
128 // Acquire::Add - Add a worker /*{{{*/
129 // ---------------------------------------------------------------------
130 /* A list of workers is kept so that the select loop can direct their FD
132 void pkgAcquire::Add(Worker
*Work
)
134 Work
->NextAcquire
= Workers
;
138 // Acquire::Remove - Remove a worker /*{{{*/
139 // ---------------------------------------------------------------------
140 /* A worker has died. This can not be done while the select loop is running
141 as it would require that RunFds could handling a changing list state and
143 void pkgAcquire::Remove(Worker
*Work
)
148 Worker
**I
= &Workers
;
152 *I
= (*I
)->NextAcquire
;
154 I
= &(*I
)->NextAcquire
;
158 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
159 // ---------------------------------------------------------------------
160 /* This is the entry point for an item. An item calls this function when
161 it is constructed which creates a queue (based on the current queue
162 mode) and puts the item in that queue. If the system is running then
163 the queue might be started. */
164 void pkgAcquire::Enqueue(ItemDesc
&Item
)
166 // Determine which queue to put the item in
167 const MethodConfig
*Config
;
168 string Name
= QueueName(Item
.URI
,Config
);
169 if (Name
.empty() == true)
172 // Find the queue structure
174 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
177 I
= new Queue(Name
,this);
185 // See if this is a local only URI
186 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
187 Item
.Owner
->Local
= true;
188 Item
.Owner
->Status
= Item::StatIdle
;
190 // Queue it into the named queue
197 clog
<< "Fetching " << Item
.URI
<< endl
;
198 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
199 clog
<< " Queue is: " << Name
<< endl
;
203 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
204 // ---------------------------------------------------------------------
205 /* This is called when an item is finished being fetched. It removes it
206 from all the queues */
207 void pkgAcquire::Dequeue(Item
*Itm
)
211 for (; I
!= 0; I
= I
->Next
)
212 Res
|= I
->Dequeue(Itm
);
215 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
220 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
221 // ---------------------------------------------------------------------
222 /* The string returned depends on the configuration settings and the
223 method parameters. Given something like http://foo.org/bar it can
224 return http://foo.org or http */
225 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
229 Config
= GetConfig(U
.Access
);
233 /* Single-Instance methods get exactly one queue per URI. This is
234 also used for the Access queue method */
235 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
238 return U
.Access
+ ':' + U
.Host
;
241 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
242 // ---------------------------------------------------------------------
243 /* This locates the configuration structure for an access method. If
244 a config structure cannot be found a Worker will be created to
246 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
248 // Search for an existing config
250 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
251 if (Conf
->Access
== Access
)
254 // Create the new config class
255 Conf
= new MethodConfig
;
256 Conf
->Access
= Access
;
257 Conf
->Next
= Configs
;
260 // Create the worker to fetch the configuration
262 if (Work
.Start() == false)
268 // Acquire::SetFds - Deal with readable FDs /*{{{*/
269 // ---------------------------------------------------------------------
270 /* Collect FDs that have activity monitors into the fd sets */
271 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
273 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
275 if (I
->InReady
== true && I
->InFd
>= 0)
279 FD_SET(I
->InFd
,RSet
);
281 if (I
->OutReady
== true && I
->OutFd
>= 0)
285 FD_SET(I
->OutFd
,WSet
);
290 // Acquire::RunFds - Deal with active FDs /*{{{*/
291 // ---------------------------------------------------------------------
292 /* Dispatch active FDs over to the proper workers. It is very important
293 that a worker never be erased while this is running! The queue class
294 should never erase a worker except during shutdown processing. */
295 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
297 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
299 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
301 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
306 // Acquire::Run - Run the fetch sequence /*{{{*/
307 // ---------------------------------------------------------------------
308 /* This runs the queues. It manages a select loop for all of the
309 Worker tasks. The workers interact with the queues and items to
310 manage the actual fetch. */
311 pkgAcquire::RunResult
pkgAcquire::Run()
315 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
321 bool WasCancelled
= false;
323 // Run till all things have been acquired
334 SetFds(Highest
,&RFds
,&WFds
);
339 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
341 while (Res
< 0 && errno
== EINTR
);
345 _error
->Errno("select","Select has failed");
350 if (_error
->PendingError() == true)
353 // Timeout, notify the log class
354 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
357 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
359 if (Log
!= 0 && Log
->Pulse(this) == false)
370 // Shut down the acquire bits
372 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
375 // Shut down the items
376 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); I
++)
379 if (_error
->PendingError())
386 // Acquire::Bump - Called when an item is dequeued /*{{{*/
387 // ---------------------------------------------------------------------
388 /* This routine bumps idle queues in hopes that they will be able to fetch
390 void pkgAcquire::Bump()
392 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
396 // Acquire::WorkerStep - Step to the next worker /*{{{*/
397 // ---------------------------------------------------------------------
398 /* Not inlined to advoid including acquire-worker.h */
399 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
401 return I
->NextAcquire
;
404 // Acquire::Clean - Cleans a directory /*{{{*/
405 // ---------------------------------------------------------------------
406 /* This is a bit simplistic, it looks at every file in the dir and sees
407 if it is part of the download set. */
408 bool pkgAcquire::Clean(string Dir
)
410 DIR *D
= opendir(Dir
.c_str());
412 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
414 string StartDir
= SafeGetCWD();
415 if (chdir(Dir
.c_str()) != 0)
418 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
421 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
424 if (strcmp(Dir
->d_name
,"lock") == 0 ||
425 strcmp(Dir
->d_name
,"partial") == 0 ||
426 strcmp(Dir
->d_name
,".") == 0 ||
427 strcmp(Dir
->d_name
,"..") == 0)
430 // Look in the get list
431 ItemCIterator I
= Items
.begin();
432 for (; I
!= Items
.end(); I
++)
433 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
436 // Nothing found, nuke it
437 if (I
== Items
.end())
441 chdir(StartDir
.c_str());
446 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
447 // ---------------------------------------------------------------------
448 /* This is the total number of bytes needed */
449 double pkgAcquire::TotalNeeded()
452 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
453 Total
+= (*I
)->FileSize
;
457 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
458 // ---------------------------------------------------------------------
459 /* This is the number of bytes that is not local */
460 double pkgAcquire::FetchNeeded()
463 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
464 if ((*I
)->Local
== false)
465 Total
+= (*I
)->FileSize
;
469 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
470 // ---------------------------------------------------------------------
471 /* This is the number of bytes that is not local */
472 double pkgAcquire::PartialPresent()
475 for (ItemCIterator I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
476 if ((*I
)->Local
== false)
477 Total
+= (*I
)->PartialSize
;
481 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
482 // ---------------------------------------------------------------------
484 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
486 return UriIterator(Queues
);
489 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
490 // ---------------------------------------------------------------------
492 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
494 return UriIterator(0);
498 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
499 // ---------------------------------------------------------------------
501 pkgAcquire::MethodConfig::MethodConfig()
503 SingleInstance
= false;
512 // Queue::Queue - Constructor /*{{{*/
513 // ---------------------------------------------------------------------
515 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
525 // Queue::~Queue - Destructor /*{{{*/
526 // ---------------------------------------------------------------------
528 pkgAcquire::Queue::~Queue()
540 // Queue::Enqueue - Queue an item to the queue /*{{{*/
541 // ---------------------------------------------------------------------
543 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
546 for (; *I
!= 0; I
= &(*I
)->Next
);
549 QItem
*Itm
= new QItem
;
554 Item
.Owner
->QueueCounter
++;
555 if (Items
->Next
== 0)
559 // Queue::Dequeue - Remove an item from the queue /*{{{*/
560 // ---------------------------------------------------------------------
561 /* We return true if we hit something */
562 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
564 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
565 return _error
->Error("Tried to dequeue a fetching object");
572 if ((*I
)->Owner
== Owner
)
576 Owner
->QueueCounter
--;
587 // Queue::Startup - Start the worker processes /*{{{*/
588 // ---------------------------------------------------------------------
589 /* It is possible for this to be called with a pre-existing set of
591 bool pkgAcquire::Queue::Startup()
596 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
600 Workers
= new Worker(this,Cnf
,Owner
->Log
);
602 if (Workers
->Start() == false)
605 /* When pipelining we commit 10 items. This needs to change when we
606 added other source retry to have cycle maintain a pipeline depth
608 if (Cnf
->Pipeline
== true)
617 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
618 // ---------------------------------------------------------------------
619 /* If final is true then all workers are eliminated, otherwise only workers
620 that do not need cleanup are removed */
621 bool pkgAcquire::Queue::Shutdown(bool Final
)
623 // Delete all of the workers
624 pkgAcquire::Worker
**Cur
= &Workers
;
627 pkgAcquire::Worker
*Jnk
= *Cur
;
628 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
630 *Cur
= Jnk
->NextQueue
;
635 Cur
= &(*Cur
)->NextQueue
;
641 // Queue::FindItem - Find a URI in the item list /*{{{*/
642 // ---------------------------------------------------------------------
644 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
646 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
647 if (I
->URI
== URI
&& I
->Worker
== Owner
)
652 // Queue::ItemDone - Item has been completed /*{{{*/
653 // ---------------------------------------------------------------------
654 /* The worker signals this which causes the item to be removed from the
655 queue. If this is the last queue instance then it is removed from the
657 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
660 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
661 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
663 if (Itm
->Owner
->QueueCounter
<= 1)
664 Owner
->Dequeue(Itm
->Owner
);
674 // Queue::Cycle - Queue new items into the method /*{{{*/
675 // ---------------------------------------------------------------------
676 /* This locates a new idle item and sends it to the worker. If pipelining
677 is enabled then it keeps the pipe full. */
678 bool pkgAcquire::Queue::Cycle()
680 if (Items
== 0 || Workers
== 0)
684 return _error
->Error("Pipedepth failure");
686 // Look for a queable item
688 while (PipeDepth
< (signed)MaxPipeDepth
)
690 for (; I
!= 0; I
= I
->Next
)
691 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
694 // Nothing to do, queue is idle.
699 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
701 if (Workers
->QueueItem(I
) == false)
708 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
709 // ---------------------------------------------------------------------
710 /* This is called when an item in multiple queues is dequeued */
711 void pkgAcquire::Queue::Bump()
717 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
718 // ---------------------------------------------------------------------
720 pkgAcquireStatus::pkgAcquireStatus() : Update(true), MorePulses(false)
725 // AcquireStatus::Pulse - Called periodically /*{{{*/
726 // ---------------------------------------------------------------------
727 /* This computes some internal state variables for the derived classes to
728 use. It generates the current downloaded bytes and total bytes to download
729 as well as the current CPS estimate. */
730 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
737 // Compute the total number of bytes to fetch
738 unsigned int Unknown
= 0;
739 unsigned int Count
= 0;
740 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
744 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
747 // Totally ignore local items
748 if ((*I
)->Local
== true)
751 TotalBytes
+= (*I
)->FileSize
;
752 if ((*I
)->Complete
== true)
753 CurrentBytes
+= (*I
)->FileSize
;
754 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
758 // Compute the current completion
759 unsigned long ResumeSize
= 0;
760 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
761 I
= Owner
->WorkerStep(I
))
762 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
764 CurrentBytes
+= I
->CurrentSize
;
765 ResumeSize
+= I
->ResumePoint
;
767 // Files with unknown size always have 100% completion
768 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
769 I
->CurrentItem
->Owner
->Complete
== false)
770 TotalBytes
+= I
->CurrentSize
;
773 // Normalize the figures and account for unknown size downloads
776 if (Unknown
== Count
)
777 TotalBytes
= Unknown
;
779 // Wha?! Is not supposed to happen.
780 if (CurrentBytes
> TotalBytes
)
781 CurrentBytes
= TotalBytes
;
784 struct timeval NewTime
;
785 gettimeofday(&NewTime
,0);
786 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
787 NewTime
.tv_sec
- Time
.tv_sec
> 6)
789 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
790 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
792 // Compute the CPS value
796 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
797 LastBytes
= CurrentBytes
- ResumeSize
;
798 ElapsedTime
= (unsigned long)Delta
;
805 // AcquireStatus::Start - Called when the download is started /*{{{*/
806 // ---------------------------------------------------------------------
807 /* We just reset the counters */
808 void pkgAcquireStatus::Start()
810 gettimeofday(&Time
,0);
811 gettimeofday(&StartTime
,0);
822 // AcquireStatus::Stop - Finished downloading /*{{{*/
823 // ---------------------------------------------------------------------
824 /* This accurately computes the elapsed time and the total overall CPS. */
825 void pkgAcquireStatus::Stop()
827 // Compute the CPS and elapsed time
828 struct timeval NewTime
;
829 gettimeofday(&NewTime
,0);
831 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
832 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
834 // Compute the CPS value
838 CurrentCPS
= FetchedBytes
/Delta
;
839 LastBytes
= CurrentBytes
;
840 ElapsedTime
= (unsigned int)Delta
;
843 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
844 // ---------------------------------------------------------------------
845 /* This is used to get accurate final transfer rate reporting. */
846 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
848 FetchedBytes
+= Size
- Resume
;