]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.41 1999/10/18 02:53:05 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>
32 // Acquire::pkgAcquire - Constructor /*{{{*/
33 // ---------------------------------------------------------------------
34 /* We grab some runtime state from the configuration space */
35 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Log
) : Log(Log
)
43 string Mode
= _config
->Find("Acquire::Queue-Mode","host");
44 if (strcasecmp(Mode
.c_str(),"host") == 0)
45 QueueMode
= QueueHost
;
46 if (strcasecmp(Mode
.c_str(),"access") == 0)
47 QueueMode
= QueueAccess
;
49 Debug
= _config
->FindB("Debug::pkgAcquire",false);
51 // This is really a stupid place for this, but people whine so much..
53 if (stat((_config
->FindDir("Dir::State::lists") + "partial/").c_str(),&St
) != 0 ||
54 S_ISDIR(St
.st_mode
) == 0)
55 _error
->Error("Lists directory %s/partial is missing",
56 _config
->FindDir("Dir::State::lists").c_str());
57 if (stat((_config
->FindDir("Dir::Cache::Archives") + "partial/").c_str(),&St
) != 0 ||
58 S_ISDIR(St
.st_mode
) == 0)
59 _error
->Error("Archive directory %s/partial is missing",
60 _config
->FindDir("Dir::Cache::Archives").c_str());
63 // Acquire::~pkgAcquire - Destructor /*{{{*/
64 // ---------------------------------------------------------------------
65 /* Free our memory, clean up the queues (destroy the workers) */
66 pkgAcquire::~pkgAcquire()
70 MethodConfig
*Jnk
= Configs
;
71 Configs
= Configs
->Next
;
78 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
79 // ---------------------------------------------------------------------
81 void pkgAcquire::Shutdown()
83 while (Items
.size() != 0)
89 Queues
= Queues
->Next
;
94 // Acquire::Add - Add a new item /*{{{*/
95 // ---------------------------------------------------------------------
96 /* This puts an item on the acquire list. This list is mainly for tracking
98 void pkgAcquire::Add(Item
*Itm
)
100 Items
.push_back(Itm
);
103 // Acquire::Remove - Remove a item /*{{{*/
104 // ---------------------------------------------------------------------
105 /* Remove an item from the acquire list. This is usually not used.. */
106 void pkgAcquire::Remove(Item
*Itm
)
108 for (vector
<Item
*>::iterator I
= Items
.begin(); I
< Items
.end(); I
++)
115 // Acquire::Add - Add a worker /*{{{*/
116 // ---------------------------------------------------------------------
117 /* A list of workers is kept so that the select loop can direct their FD
119 void pkgAcquire::Add(Worker
*Work
)
121 Work
->NextAcquire
= Workers
;
125 // Acquire::Remove - Remove a worker /*{{{*/
126 // ---------------------------------------------------------------------
127 /* A worker has died. This can not be done while the select loop is running
128 as it would require that RunFds could handling a changing list state and
130 void pkgAcquire::Remove(Worker
*Work
)
135 Worker
**I
= &Workers
;
139 *I
= (*I
)->NextAcquire
;
141 I
= &(*I
)->NextAcquire
;
145 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
146 // ---------------------------------------------------------------------
147 /* This is the entry point for an item. An item calls this function when
148 it is constructed which creates a queue (based on the current queue
149 mode) and puts the item in that queue. If the system is running then
150 the queue might be started. */
151 void pkgAcquire::Enqueue(ItemDesc
&Item
)
153 // Determine which queue to put the item in
154 const MethodConfig
*Config
;
155 string Name
= QueueName(Item
.URI
,Config
);
156 if (Name
.empty() == true)
159 // Find the queue structure
161 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
164 I
= new Queue(Name
,this);
172 // See if this is a local only URI
173 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
174 Item
.Owner
->Local
= true;
175 Item
.Owner
->Status
= Item::StatIdle
;
177 // Queue it into the named queue
184 clog
<< "Fetching " << Item
.URI
<< endl
;
185 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
186 clog
<< " Queue is: " << Name
<< endl
;
190 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
191 // ---------------------------------------------------------------------
192 /* This is called when an item is finished being fetched. It removes it
193 from all the queues */
194 void pkgAcquire::Dequeue(Item
*Itm
)
198 for (; I
!= 0; I
= I
->Next
)
199 Res
|= I
->Dequeue(Itm
);
202 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
207 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
208 // ---------------------------------------------------------------------
209 /* The string returned depends on the configuration settings and the
210 method parameters. Given something like http://foo.org/bar it can
211 return http://foo.org or http */
212 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
216 Config
= GetConfig(U
.Access
);
220 /* Single-Instance methods get exactly one queue per URI. This is
221 also used for the Access queue method */
222 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
225 return U
.Access
+ ':' + U
.Host
;
228 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
229 // ---------------------------------------------------------------------
230 /* This locates the configuration structure for an access method. If
231 a config structure cannot be found a Worker will be created to
233 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
235 // Search for an existing config
237 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
238 if (Conf
->Access
== Access
)
241 // Create the new config class
242 Conf
= new MethodConfig
;
243 Conf
->Access
= Access
;
244 Conf
->Next
= Configs
;
247 // Create the worker to fetch the configuration
249 if (Work
.Start() == false)
255 // Acquire::SetFds - Deal with readable FDs /*{{{*/
256 // ---------------------------------------------------------------------
257 /* Collect FDs that have activity monitors into the fd sets */
258 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
260 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
262 if (I
->InReady
== true && I
->InFd
>= 0)
266 FD_SET(I
->InFd
,RSet
);
268 if (I
->OutReady
== true && I
->OutFd
>= 0)
272 FD_SET(I
->OutFd
,WSet
);
277 // Acquire::RunFds - Deal with active FDs /*{{{*/
278 // ---------------------------------------------------------------------
279 /* Dispatch active FDs over to the proper workers. It is very important
280 that a worker never be erased while this is running! The queue class
281 should never erase a worker except during shutdown processing. */
282 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
284 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
286 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
288 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
293 // Acquire::Run - Run the fetch sequence /*{{{*/
294 // ---------------------------------------------------------------------
295 /* This runs the queues. It manages a select loop for all of the
296 Worker tasks. The workers interact with the queues and items to
297 manage the actual fetch. */
298 pkgAcquire::RunResult
pkgAcquire::Run()
302 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
308 bool WasCancelled
= false;
310 // Run till all things have been acquired
321 SetFds(Highest
,&RFds
,&WFds
);
326 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
328 while (Res
< 0 && errno
== EINTR
);
332 _error
->Errno("select","Select has failed");
337 if (_error
->PendingError() == true)
340 // Timeout, notify the log class
341 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
344 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
346 if (Log
!= 0 && Log
->Pulse(this) == false)
357 // Shut down the acquire bits
359 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
362 // Shut down the items
363 for (Item
**I
= Items
.begin(); I
!= Items
.end(); I
++)
366 if (_error
->PendingError())
373 // Acquire::Bump - Called when an item is dequeued /*{{{*/
374 // ---------------------------------------------------------------------
375 /* This routine bumps idle queues in hopes that they will be able to fetch
377 void pkgAcquire::Bump()
379 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
383 // Acquire::WorkerStep - Step to the next worker /*{{{*/
384 // ---------------------------------------------------------------------
385 /* Not inlined to advoid including acquire-worker.h */
386 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
388 return I
->NextAcquire
;
391 // Acquire::Clean - Cleans a directory /*{{{*/
392 // ---------------------------------------------------------------------
393 /* This is a bit simplistic, it looks at every file in the dir and sees
394 if it is part of the download set. */
395 bool pkgAcquire::Clean(string Dir
)
397 DIR *D
= opendir(Dir
.c_str());
399 return _error
->Errno("opendir","Unable to read %s",Dir
.c_str());
401 string StartDir
= SafeGetCWD();
402 if (chdir(Dir
.c_str()) != 0)
405 return _error
->Errno("chdir","Unable to change to ",Dir
.c_str());
408 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
411 if (strcmp(Dir
->d_name
,"lock") == 0 ||
412 strcmp(Dir
->d_name
,"partial") == 0 ||
413 strcmp(Dir
->d_name
,".") == 0 ||
414 strcmp(Dir
->d_name
,"..") == 0)
417 // Look in the get list
418 vector
<Item
*>::iterator I
= Items
.begin();
419 for (; I
!= Items
.end(); I
++)
420 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
423 // Nothing found, nuke it
424 if (I
== Items
.end())
428 chdir(StartDir
.c_str());
433 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
434 // ---------------------------------------------------------------------
435 /* This is the total number of bytes needed */
436 unsigned long pkgAcquire::TotalNeeded()
438 unsigned long Total
= 0;
439 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
440 Total
+= (*I
)->FileSize
;
444 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
445 // ---------------------------------------------------------------------
446 /* This is the number of bytes that is not local */
447 unsigned long pkgAcquire::FetchNeeded()
449 unsigned long Total
= 0;
450 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
451 if ((*I
)->Local
== false)
452 Total
+= (*I
)->FileSize
;
456 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
457 // ---------------------------------------------------------------------
458 /* This is the number of bytes that is not local */
459 unsigned long pkgAcquire::PartialPresent()
461 unsigned long Total
= 0;
462 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
463 if ((*I
)->Local
== false)
464 Total
+= (*I
)->PartialSize
;
468 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
469 // ---------------------------------------------------------------------
471 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
473 return UriIterator(Queues
);
476 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
477 // ---------------------------------------------------------------------
479 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
481 return UriIterator(0);
485 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
486 // ---------------------------------------------------------------------
488 pkgAcquire::MethodConfig::MethodConfig()
490 SingleInstance
= false;
498 // Queue::Queue - Constructor /*{{{*/
499 // ---------------------------------------------------------------------
501 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
511 // Queue::~Queue - Destructor /*{{{*/
512 // ---------------------------------------------------------------------
514 pkgAcquire::Queue::~Queue()
526 // Queue::Enqueue - Queue an item to the queue /*{{{*/
527 // ---------------------------------------------------------------------
529 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
532 for (; *I
!= 0; I
= &(*I
)->Next
);
535 QItem
*Itm
= new QItem
;
540 Item
.Owner
->QueueCounter
++;
541 if (Items
->Next
== 0)
545 // Queue::Dequeue - Remove an item from the queue /*{{{*/
546 // ---------------------------------------------------------------------
547 /* We return true if we hit something */
548 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
550 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
551 return _error
->Error("Tried to dequeue a fetching object");
558 if ((*I
)->Owner
== Owner
)
562 Owner
->QueueCounter
--;
573 // Queue::Startup - Start the worker processes /*{{{*/
574 // ---------------------------------------------------------------------
575 /* It is possible for this to be called with a pre-existing set of
577 bool pkgAcquire::Queue::Startup()
582 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
586 Workers
= new Worker(this,Cnf
,Owner
->Log
);
588 if (Workers
->Start() == false)
591 /* When pipelining we commit 10 items. This needs to change when we
592 added other source retry to have cycle maintain a pipeline depth
594 if (Cnf
->Pipeline
== true)
603 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
604 // ---------------------------------------------------------------------
605 /* If final is true then all workers are eliminated, otherwise only workers
606 that do not need cleanup are removed */
607 bool pkgAcquire::Queue::Shutdown(bool Final
)
609 // Delete all of the workers
610 pkgAcquire::Worker
**Cur
= &Workers
;
613 pkgAcquire::Worker
*Jnk
= *Cur
;
614 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
616 *Cur
= Jnk
->NextQueue
;
621 Cur
= &(*Cur
)->NextQueue
;
627 // Queue::FindItem - Find a URI in the item list /*{{{*/
628 // ---------------------------------------------------------------------
630 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
632 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
633 if (I
->URI
== URI
&& I
->Worker
== Owner
)
638 // Queue::ItemDone - Item has been completed /*{{{*/
639 // ---------------------------------------------------------------------
640 /* The worker signals this which causes the item to be removed from the
641 queue. If this is the last queue instance then it is removed from the
643 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
646 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
647 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
649 if (Itm
->Owner
->QueueCounter
<= 1)
650 Owner
->Dequeue(Itm
->Owner
);
660 // Queue::Cycle - Queue new items into the method /*{{{*/
661 // ---------------------------------------------------------------------
662 /* This locates a new idle item and sends it to the worker. If pipelining
663 is enabled then it keeps the pipe full. */
664 bool pkgAcquire::Queue::Cycle()
666 if (Items
== 0 || Workers
== 0)
670 return _error
->Error("Pipedepth failure");
672 // Look for a queable item
674 while (PipeDepth
< (signed)MaxPipeDepth
)
676 for (; I
!= 0; I
= I
->Next
)
677 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
680 // Nothing to do, queue is idle.
685 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
687 if (Workers
->QueueItem(I
) == false)
694 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
695 // ---------------------------------------------------------------------
696 /* This is called when an item in multiple queues is dequeued */
697 void pkgAcquire::Queue::Bump()
703 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
704 // ---------------------------------------------------------------------
706 pkgAcquireStatus::pkgAcquireStatus()
711 // AcquireStatus::Pulse - Called periodically /*{{{*/
712 // ---------------------------------------------------------------------
713 /* This computes some internal state variables for the derived classes to
714 use. It generates the current downloaded bytes and total bytes to download
715 as well as the current CPS estimate. */
716 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
723 // Compute the total number of bytes to fetch
724 unsigned int Unknown
= 0;
725 unsigned int Count
= 0;
726 for (pkgAcquire::Item
**I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
730 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
733 // Totally ignore local items
734 if ((*I
)->Local
== true)
737 TotalBytes
+= (*I
)->FileSize
;
738 if ((*I
)->Complete
== true)
739 CurrentBytes
+= (*I
)->FileSize
;
740 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
744 // Compute the current completion
745 unsigned long ResumeSize
= 0;
746 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
747 I
= Owner
->WorkerStep(I
))
748 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
750 CurrentBytes
+= I
->CurrentSize
;
751 ResumeSize
+= I
->ResumePoint
;
753 // Files with unknown size always have 100% completion
754 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
755 I
->CurrentItem
->Owner
->Complete
== false)
756 TotalBytes
+= I
->CurrentSize
;
759 // Normalize the figures and account for unknown size downloads
762 if (Unknown
== Count
)
763 TotalBytes
= Unknown
;
765 // Wha?! Is not supposed to happen.
766 if (CurrentBytes
> TotalBytes
)
767 CurrentBytes
= TotalBytes
;
770 struct timeval NewTime
;
771 gettimeofday(&NewTime
,0);
772 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
773 NewTime
.tv_sec
- Time
.tv_sec
> 6)
775 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
776 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
778 // Compute the CPS value
782 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
783 LastBytes
= CurrentBytes
- ResumeSize
;
784 ElapsedTime
= (unsigned long)Delta
;
791 // AcquireStatus::Start - Called when the download is started /*{{{*/
792 // ---------------------------------------------------------------------
793 /* We just reset the counters */
794 void pkgAcquireStatus::Start()
796 gettimeofday(&Time
,0);
797 gettimeofday(&StartTime
,0);
808 // AcquireStatus::Stop - Finished downloading /*{{{*/
809 // ---------------------------------------------------------------------
810 /* This accurately computes the elapsed time and the total overall CPS. */
811 void pkgAcquireStatus::Stop()
813 // Compute the CPS and elapsed time
814 struct timeval NewTime
;
815 gettimeofday(&NewTime
,0);
817 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
818 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
820 // Compute the CPS value
824 CurrentCPS
= FetchedBytes
/Delta
;
825 LastBytes
= CurrentBytes
;
826 ElapsedTime
= (unsigned int)Delta
;
829 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
830 // ---------------------------------------------------------------------
831 /* This is used to get accurate final transfer rate reporting. */
832 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
834 FetchedBytes
+= Size
- Resume
;