]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.47 2001/02/20 07:03:17 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>
34 // Acquire::pkgAcquire - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* We grab some runtime state from the configuration space */
37 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Log
) : Log(Log
)
45 string Mode
= _config
->Find("Acquire::Queue-Mode","host");
46 if (strcasecmp(Mode
.c_str(),"host") == 0)
47 QueueMode
= QueueHost
;
48 if (strcasecmp(Mode
.c_str(),"access") == 0)
49 QueueMode
= QueueAccess
;
51 Debug
= _config
->FindB("Debug::pkgAcquire",false);
53 // This is really a stupid place for this
55 if (stat((_config
->FindDir("Dir::State::lists") + "partial/").c_str(),&St
) != 0 ||
56 S_ISDIR(St
.st_mode
) == 0)
57 _error
->Error(_("Lists directory %spartial is missing."),
58 _config
->FindDir("Dir::State::lists").c_str());
59 if (stat((_config
->FindDir("Dir::Cache::Archives") + "partial/").c_str(),&St
) != 0 ||
60 S_ISDIR(St
.st_mode
) == 0)
61 _error
->Error(_("Archive directory %spartial is missing."),
62 _config
->FindDir("Dir::Cache::Archives").c_str());
65 // Acquire::~pkgAcquire - Destructor /*{{{*/
66 // ---------------------------------------------------------------------
67 /* Free our memory, clean up the queues (destroy the workers) */
68 pkgAcquire::~pkgAcquire()
74 MethodConfig
*Jnk
= Configs
;
75 Configs
= Configs
->Next
;
80 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
81 // ---------------------------------------------------------------------
83 void pkgAcquire::Shutdown()
85 while (Items
.size() != 0)
91 Queues
= Queues
->Next
;
96 // Acquire::Add - Add a new item /*{{{*/
97 // ---------------------------------------------------------------------
98 /* This puts an item on the acquire list. This list is mainly for tracking
100 void pkgAcquire::Add(Item
*Itm
)
102 Items
.push_back(Itm
);
105 // Acquire::Remove - Remove a item /*{{{*/
106 // ---------------------------------------------------------------------
107 /* Remove an item from the acquire list. This is usually not used.. */
108 void pkgAcquire::Remove(Item
*Itm
)
112 for (vector
<Item
*>::iterator I
= Items
.begin(); I
< Items
.end(); I
++)
119 // Acquire::Add - Add a worker /*{{{*/
120 // ---------------------------------------------------------------------
121 /* A list of workers is kept so that the select loop can direct their FD
123 void pkgAcquire::Add(Worker
*Work
)
125 Work
->NextAcquire
= Workers
;
129 // Acquire::Remove - Remove a worker /*{{{*/
130 // ---------------------------------------------------------------------
131 /* A worker has died. This can not be done while the select loop is running
132 as it would require that RunFds could handling a changing list state and
134 void pkgAcquire::Remove(Worker
*Work
)
139 Worker
**I
= &Workers
;
143 *I
= (*I
)->NextAcquire
;
145 I
= &(*I
)->NextAcquire
;
149 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This is the entry point for an item. An item calls this function when
152 it is constructed which creates a queue (based on the current queue
153 mode) and puts the item in that queue. If the system is running then
154 the queue might be started. */
155 void pkgAcquire::Enqueue(ItemDesc
&Item
)
157 // Determine which queue to put the item in
158 const MethodConfig
*Config
;
159 string Name
= QueueName(Item
.URI
,Config
);
160 if (Name
.empty() == true)
163 // Find the queue structure
165 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
168 I
= new Queue(Name
,this);
176 // See if this is a local only URI
177 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
178 Item
.Owner
->Local
= true;
179 Item
.Owner
->Status
= Item::StatIdle
;
181 // Queue it into the named queue
188 clog
<< "Fetching " << Item
.URI
<< endl
;
189 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
190 clog
<< " Queue is: " << Name
<< endl
;
194 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This is called when an item is finished being fetched. It removes it
197 from all the queues */
198 void pkgAcquire::Dequeue(Item
*Itm
)
202 for (; I
!= 0; I
= I
->Next
)
203 Res
|= I
->Dequeue(Itm
);
206 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
211 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
212 // ---------------------------------------------------------------------
213 /* The string returned depends on the configuration settings and the
214 method parameters. Given something like http://foo.org/bar it can
215 return http://foo.org or http */
216 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
220 Config
= GetConfig(U
.Access
);
224 /* Single-Instance methods get exactly one queue per URI. This is
225 also used for the Access queue method */
226 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
229 return U
.Access
+ ':' + U
.Host
;
232 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
233 // ---------------------------------------------------------------------
234 /* This locates the configuration structure for an access method. If
235 a config structure cannot be found a Worker will be created to
237 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
239 // Search for an existing config
241 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
242 if (Conf
->Access
== Access
)
245 // Create the new config class
246 Conf
= new MethodConfig
;
247 Conf
->Access
= Access
;
248 Conf
->Next
= Configs
;
251 // Create the worker to fetch the configuration
253 if (Work
.Start() == false)
259 // Acquire::SetFds - Deal with readable FDs /*{{{*/
260 // ---------------------------------------------------------------------
261 /* Collect FDs that have activity monitors into the fd sets */
262 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
264 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
266 if (I
->InReady
== true && I
->InFd
>= 0)
270 FD_SET(I
->InFd
,RSet
);
272 if (I
->OutReady
== true && I
->OutFd
>= 0)
276 FD_SET(I
->OutFd
,WSet
);
281 // Acquire::RunFds - Deal with active FDs /*{{{*/
282 // ---------------------------------------------------------------------
283 /* Dispatch active FDs over to the proper workers. It is very important
284 that a worker never be erased while this is running! The queue class
285 should never erase a worker except during shutdown processing. */
286 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
288 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
290 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
292 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
297 // Acquire::Run - Run the fetch sequence /*{{{*/
298 // ---------------------------------------------------------------------
299 /* This runs the queues. It manages a select loop for all of the
300 Worker tasks. The workers interact with the queues and items to
301 manage the actual fetch. */
302 pkgAcquire::RunResult
pkgAcquire::Run()
306 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
312 bool WasCancelled
= false;
314 // Run till all things have been acquired
325 SetFds(Highest
,&RFds
,&WFds
);
330 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
332 while (Res
< 0 && errno
== EINTR
);
336 _error
->Errno("select","Select has failed");
341 if (_error
->PendingError() == true)
344 // Timeout, notify the log class
345 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
348 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
350 if (Log
!= 0 && Log
->Pulse(this) == false)
361 // Shut down the acquire bits
363 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
366 // Shut down the items
367 for (Item
**I
= Items
.begin(); I
!= Items
.end(); I
++)
370 if (_error
->PendingError())
377 // Acquire::Bump - Called when an item is dequeued /*{{{*/
378 // ---------------------------------------------------------------------
379 /* This routine bumps idle queues in hopes that they will be able to fetch
381 void pkgAcquire::Bump()
383 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
387 // Acquire::WorkerStep - Step to the next worker /*{{{*/
388 // ---------------------------------------------------------------------
389 /* Not inlined to advoid including acquire-worker.h */
390 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
392 return I
->NextAcquire
;
395 // Acquire::Clean - Cleans a directory /*{{{*/
396 // ---------------------------------------------------------------------
397 /* This is a bit simplistic, it looks at every file in the dir and sees
398 if it is part of the download set. */
399 bool pkgAcquire::Clean(string Dir
)
401 DIR *D
= opendir(Dir
.c_str());
403 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
405 string StartDir
= SafeGetCWD();
406 if (chdir(Dir
.c_str()) != 0)
409 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
412 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
415 if (strcmp(Dir
->d_name
,"lock") == 0 ||
416 strcmp(Dir
->d_name
,"partial") == 0 ||
417 strcmp(Dir
->d_name
,".") == 0 ||
418 strcmp(Dir
->d_name
,"..") == 0)
421 // Look in the get list
422 vector
<Item
*>::iterator I
= Items
.begin();
423 for (; I
!= Items
.end(); I
++)
424 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
427 // Nothing found, nuke it
428 if (I
== Items
.end())
432 chdir(StartDir
.c_str());
437 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
438 // ---------------------------------------------------------------------
439 /* This is the total number of bytes needed */
440 double pkgAcquire::TotalNeeded()
443 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
444 Total
+= (*I
)->FileSize
;
448 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
449 // ---------------------------------------------------------------------
450 /* This is the number of bytes that is not local */
451 double pkgAcquire::FetchNeeded()
454 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
455 if ((*I
)->Local
== false)
456 Total
+= (*I
)->FileSize
;
460 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
461 // ---------------------------------------------------------------------
462 /* This is the number of bytes that is not local */
463 double pkgAcquire::PartialPresent()
466 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
467 if ((*I
)->Local
== false)
468 Total
+= (*I
)->PartialSize
;
472 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
473 // ---------------------------------------------------------------------
475 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
477 return UriIterator(Queues
);
480 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
481 // ---------------------------------------------------------------------
483 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
485 return UriIterator(0);
489 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
490 // ---------------------------------------------------------------------
492 pkgAcquire::MethodConfig::MethodConfig()
494 SingleInstance
= false;
503 // Queue::Queue - Constructor /*{{{*/
504 // ---------------------------------------------------------------------
506 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
516 // Queue::~Queue - Destructor /*{{{*/
517 // ---------------------------------------------------------------------
519 pkgAcquire::Queue::~Queue()
531 // Queue::Enqueue - Queue an item to the queue /*{{{*/
532 // ---------------------------------------------------------------------
534 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
537 for (; *I
!= 0; I
= &(*I
)->Next
);
540 QItem
*Itm
= new QItem
;
545 Item
.Owner
->QueueCounter
++;
546 if (Items
->Next
== 0)
550 // Queue::Dequeue - Remove an item from the queue /*{{{*/
551 // ---------------------------------------------------------------------
552 /* We return true if we hit something */
553 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
555 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
556 return _error
->Error("Tried to dequeue a fetching object");
563 if ((*I
)->Owner
== Owner
)
567 Owner
->QueueCounter
--;
578 // Queue::Startup - Start the worker processes /*{{{*/
579 // ---------------------------------------------------------------------
580 /* It is possible for this to be called with a pre-existing set of
582 bool pkgAcquire::Queue::Startup()
587 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
591 Workers
= new Worker(this,Cnf
,Owner
->Log
);
593 if (Workers
->Start() == false)
596 /* When pipelining we commit 10 items. This needs to change when we
597 added other source retry to have cycle maintain a pipeline depth
599 if (Cnf
->Pipeline
== true)
608 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
609 // ---------------------------------------------------------------------
610 /* If final is true then all workers are eliminated, otherwise only workers
611 that do not need cleanup are removed */
612 bool pkgAcquire::Queue::Shutdown(bool Final
)
614 // Delete all of the workers
615 pkgAcquire::Worker
**Cur
= &Workers
;
618 pkgAcquire::Worker
*Jnk
= *Cur
;
619 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
621 *Cur
= Jnk
->NextQueue
;
626 Cur
= &(*Cur
)->NextQueue
;
632 // Queue::FindItem - Find a URI in the item list /*{{{*/
633 // ---------------------------------------------------------------------
635 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
637 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
638 if (I
->URI
== URI
&& I
->Worker
== Owner
)
643 // Queue::ItemDone - Item has been completed /*{{{*/
644 // ---------------------------------------------------------------------
645 /* The worker signals this which causes the item to be removed from the
646 queue. If this is the last queue instance then it is removed from the
648 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
651 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
652 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
654 if (Itm
->Owner
->QueueCounter
<= 1)
655 Owner
->Dequeue(Itm
->Owner
);
665 // Queue::Cycle - Queue new items into the method /*{{{*/
666 // ---------------------------------------------------------------------
667 /* This locates a new idle item and sends it to the worker. If pipelining
668 is enabled then it keeps the pipe full. */
669 bool pkgAcquire::Queue::Cycle()
671 if (Items
== 0 || Workers
== 0)
675 return _error
->Error("Pipedepth failure");
677 // Look for a queable item
679 while (PipeDepth
< (signed)MaxPipeDepth
)
681 for (; I
!= 0; I
= I
->Next
)
682 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
685 // Nothing to do, queue is idle.
690 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
692 if (Workers
->QueueItem(I
) == false)
699 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
700 // ---------------------------------------------------------------------
701 /* This is called when an item in multiple queues is dequeued */
702 void pkgAcquire::Queue::Bump()
708 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
709 // ---------------------------------------------------------------------
711 pkgAcquireStatus::pkgAcquireStatus() : Update(true), MorePulses(false)
716 // AcquireStatus::Pulse - Called periodically /*{{{*/
717 // ---------------------------------------------------------------------
718 /* This computes some internal state variables for the derived classes to
719 use. It generates the current downloaded bytes and total bytes to download
720 as well as the current CPS estimate. */
721 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
728 // Compute the total number of bytes to fetch
729 unsigned int Unknown
= 0;
730 unsigned int Count
= 0;
731 for (pkgAcquire::Item
**I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
735 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
738 // Totally ignore local items
739 if ((*I
)->Local
== true)
742 TotalBytes
+= (*I
)->FileSize
;
743 if ((*I
)->Complete
== true)
744 CurrentBytes
+= (*I
)->FileSize
;
745 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
749 // Compute the current completion
750 unsigned long ResumeSize
= 0;
751 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
752 I
= Owner
->WorkerStep(I
))
753 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
755 CurrentBytes
+= I
->CurrentSize
;
756 ResumeSize
+= I
->ResumePoint
;
758 // Files with unknown size always have 100% completion
759 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
760 I
->CurrentItem
->Owner
->Complete
== false)
761 TotalBytes
+= I
->CurrentSize
;
764 // Normalize the figures and account for unknown size downloads
767 if (Unknown
== Count
)
768 TotalBytes
= Unknown
;
770 // Wha?! Is not supposed to happen.
771 if (CurrentBytes
> TotalBytes
)
772 CurrentBytes
= TotalBytes
;
775 struct timeval NewTime
;
776 gettimeofday(&NewTime
,0);
777 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
778 NewTime
.tv_sec
- Time
.tv_sec
> 6)
780 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
781 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
783 // Compute the CPS value
787 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
788 LastBytes
= CurrentBytes
- ResumeSize
;
789 ElapsedTime
= (unsigned long)Delta
;
796 // AcquireStatus::Start - Called when the download is started /*{{{*/
797 // ---------------------------------------------------------------------
798 /* We just reset the counters */
799 void pkgAcquireStatus::Start()
801 gettimeofday(&Time
,0);
802 gettimeofday(&StartTime
,0);
813 // AcquireStatus::Stop - Finished downloading /*{{{*/
814 // ---------------------------------------------------------------------
815 /* This accurately computes the elapsed time and the total overall CPS. */
816 void pkgAcquireStatus::Stop()
818 // Compute the CPS and elapsed time
819 struct timeval NewTime
;
820 gettimeofday(&NewTime
,0);
822 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
823 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
825 // Compute the CPS value
829 CurrentCPS
= FetchedBytes
/Delta
;
830 LastBytes
= CurrentBytes
;
831 ElapsedTime
= (unsigned int)Delta
;
834 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
835 // ---------------------------------------------------------------------
836 /* This is used to get accurate final transfer rate reporting. */
837 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
839 FetchedBytes
+= Size
- Resume
;