]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.39 1999/10/16 19:53: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>
31 // Acquire::pkgAcquire - Constructor /*{{{*/
32 // ---------------------------------------------------------------------
33 /* We grab some runtime state from the configuration space */
34 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Log
) : Log(Log
)
42 string Mode
= _config
->Find("Acquire::Queue-Mode","host");
43 if (strcasecmp(Mode
.c_str(),"host") == 0)
44 QueueMode
= QueueHost
;
45 if (strcasecmp(Mode
.c_str(),"access") == 0)
46 QueueMode
= QueueAccess
;
48 Debug
= _config
->FindB("Debug::pkgAcquire",false);
51 // Acquire::~pkgAcquire - Destructor /*{{{*/
52 // ---------------------------------------------------------------------
53 /* Free our memory, clean up the queues (destroy the workers) */
54 pkgAcquire::~pkgAcquire()
58 MethodConfig
*Jnk
= Configs
;
59 Configs
= Configs
->Next
;
66 // pkgAcquire::Shutdown - Clean out the acquire object /*{{{*/
67 // ---------------------------------------------------------------------
69 void pkgAcquire::Shutdown()
71 while (Items
.size() != 0)
77 Queues
= Queues
->Next
;
82 // Acquire::Add - Add a new item /*{{{*/
83 // ---------------------------------------------------------------------
84 /* This puts an item on the acquire list. This list is mainly for tracking
86 void pkgAcquire::Add(Item
*Itm
)
91 // Acquire::Remove - Remove a item /*{{{*/
92 // ---------------------------------------------------------------------
93 /* Remove an item from the acquire list. This is usually not used.. */
94 void pkgAcquire::Remove(Item
*Itm
)
96 for (vector
<Item
*>::iterator I
= Items
.begin(); I
< Items
.end(); I
++)
103 // Acquire::Add - Add a worker /*{{{*/
104 // ---------------------------------------------------------------------
105 /* A list of workers is kept so that the select loop can direct their FD
107 void pkgAcquire::Add(Worker
*Work
)
109 Work
->NextAcquire
= Workers
;
113 // Acquire::Remove - Remove a worker /*{{{*/
114 // ---------------------------------------------------------------------
115 /* A worker has died. This can not be done while the select loop is running
116 as it would require that RunFds could handling a changing list state and
118 void pkgAcquire::Remove(Worker
*Work
)
123 Worker
**I
= &Workers
;
127 *I
= (*I
)->NextAcquire
;
129 I
= &(*I
)->NextAcquire
;
133 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
134 // ---------------------------------------------------------------------
135 /* This is the entry point for an item. An item calls this function when
136 it is constructed which creates a queue (based on the current queue
137 mode) and puts the item in that queue. If the system is running then
138 the queue might be started. */
139 void pkgAcquire::Enqueue(ItemDesc
&Item
)
141 // Determine which queue to put the item in
142 const MethodConfig
*Config
;
143 string Name
= QueueName(Item
.URI
,Config
);
144 if (Name
.empty() == true)
147 // Find the queue structure
149 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
152 I
= new Queue(Name
,this);
160 // See if this is a local only URI
161 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
162 Item
.Owner
->Local
= true;
163 Item
.Owner
->Status
= Item::StatIdle
;
165 // Queue it into the named queue
172 clog
<< "Fetching " << Item
.URI
<< endl
;
173 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
174 clog
<< " Queue is: " << Name
<< endl
;
178 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This is called when an item is finished being fetched. It removes it
181 from all the queues */
182 void pkgAcquire::Dequeue(Item
*Itm
)
186 for (; I
!= 0; I
= I
->Next
)
187 Res
|= I
->Dequeue(Itm
);
190 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
195 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
196 // ---------------------------------------------------------------------
197 /* The string returned depends on the configuration settings and the
198 method parameters. Given something like http://foo.org/bar it can
199 return http://foo.org or http */
200 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
204 Config
= GetConfig(U
.Access
);
208 /* Single-Instance methods get exactly one queue per URI. This is
209 also used for the Access queue method */
210 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
213 return U
.Access
+ ':' + U
.Host
;
216 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
217 // ---------------------------------------------------------------------
218 /* This locates the configuration structure for an access method. If
219 a config structure cannot be found a Worker will be created to
221 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
223 // Search for an existing config
225 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
226 if (Conf
->Access
== Access
)
229 // Create the new config class
230 Conf
= new MethodConfig
;
231 Conf
->Access
= Access
;
232 Conf
->Next
= Configs
;
235 // Create the worker to fetch the configuration
237 if (Work
.Start() == false)
243 // Acquire::SetFds - Deal with readable FDs /*{{{*/
244 // ---------------------------------------------------------------------
245 /* Collect FDs that have activity monitors into the fd sets */
246 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
248 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
250 if (I
->InReady
== true && I
->InFd
>= 0)
254 FD_SET(I
->InFd
,RSet
);
256 if (I
->OutReady
== true && I
->OutFd
>= 0)
260 FD_SET(I
->OutFd
,WSet
);
265 // Acquire::RunFds - Deal with active FDs /*{{{*/
266 // ---------------------------------------------------------------------
267 /* Dispatch active FDs over to the proper workers. It is very important
268 that a worker never be erased while this is running! The queue class
269 should never erase a worker except during shutdown processing. */
270 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
272 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
274 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
276 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
281 // Acquire::Run - Run the fetch sequence /*{{{*/
282 // ---------------------------------------------------------------------
283 /* This runs the queues. It manages a select loop for all of the
284 Worker tasks. The workers interact with the queues and items to
285 manage the actual fetch. */
286 pkgAcquire::RunResult
pkgAcquire::Run()
290 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
296 bool WasCancelled
= false;
298 // Run till all things have been acquired
309 SetFds(Highest
,&RFds
,&WFds
);
314 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
316 while (Res
< 0 && errno
== EINTR
);
320 _error
->Errno("select","Select has failed");
325 if (_error
->PendingError() == true)
328 // Timeout, notify the log class
329 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
332 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
334 if (Log
!= 0 && Log
->Pulse(this) == false)
345 // Shut down the acquire bits
347 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
350 // Shut down the items
351 for (Item
**I
= Items
.begin(); I
!= Items
.end(); I
++)
354 if (_error
->PendingError())
361 // Acquire::Bump - Called when an item is dequeued /*{{{*/
362 // ---------------------------------------------------------------------
363 /* This routine bumps idle queues in hopes that they will be able to fetch
365 void pkgAcquire::Bump()
367 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
371 // Acquire::WorkerStep - Step to the next worker /*{{{*/
372 // ---------------------------------------------------------------------
373 /* Not inlined to advoid including acquire-worker.h */
374 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
376 return I
->NextAcquire
;
379 // Acquire::Clean - Cleans a directory /*{{{*/
380 // ---------------------------------------------------------------------
381 /* This is a bit simplistic, it looks at every file in the dir and sees
382 if it is part of the download set. */
383 bool pkgAcquire::Clean(string Dir
)
385 DIR *D
= opendir(Dir
.c_str());
387 return _error
->Errno("opendir","Unable to read %s",Dir
.c_str());
389 string StartDir
= SafeGetCWD();
390 if (chdir(Dir
.c_str()) != 0)
393 return _error
->Errno("chdir","Unable to change to ",Dir
.c_str());
396 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
399 if (strcmp(Dir
->d_name
,"lock") == 0 ||
400 strcmp(Dir
->d_name
,"partial") == 0 ||
401 strcmp(Dir
->d_name
,".") == 0 ||
402 strcmp(Dir
->d_name
,"..") == 0)
405 // Look in the get list
406 vector
<Item
*>::iterator I
= Items
.begin();
407 for (; I
!= Items
.end(); I
++)
408 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
411 // Nothing found, nuke it
412 if (I
== Items
.end())
416 chdir(StartDir
.c_str());
421 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
422 // ---------------------------------------------------------------------
423 /* This is the total number of bytes needed */
424 unsigned long pkgAcquire::TotalNeeded()
426 unsigned long Total
= 0;
427 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
428 Total
+= (*I
)->FileSize
;
432 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
433 // ---------------------------------------------------------------------
434 /* This is the number of bytes that is not local */
435 unsigned long pkgAcquire::FetchNeeded()
437 unsigned long Total
= 0;
438 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
439 if ((*I
)->Local
== false)
440 Total
+= (*I
)->FileSize
;
444 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
445 // ---------------------------------------------------------------------
446 /* This is the number of bytes that is not local */
447 unsigned long pkgAcquire::PartialPresent()
449 unsigned long Total
= 0;
450 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
451 if ((*I
)->Local
== false)
452 Total
+= (*I
)->PartialSize
;
456 // pkgAcquire::UriBegin - Start iterator for the uri list /*{{{*/
457 // ---------------------------------------------------------------------
459 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
461 return UriIterator(Queues
);
464 // pkgAcquire::UriEnd - End iterator for the uri list /*{{{*/
465 // ---------------------------------------------------------------------
467 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
469 return UriIterator(0);
473 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
474 // ---------------------------------------------------------------------
476 pkgAcquire::MethodConfig::MethodConfig()
478 SingleInstance
= false;
486 // Queue::Queue - Constructor /*{{{*/
487 // ---------------------------------------------------------------------
489 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
499 // Queue::~Queue - Destructor /*{{{*/
500 // ---------------------------------------------------------------------
502 pkgAcquire::Queue::~Queue()
514 // Queue::Enqueue - Queue an item to the queue /*{{{*/
515 // ---------------------------------------------------------------------
517 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
520 for (; *I
!= 0; I
= &(*I
)->Next
);
523 QItem
*Itm
= new QItem
;
528 Item
.Owner
->QueueCounter
++;
529 if (Items
->Next
== 0)
533 // Queue::Dequeue - Remove an item from the queue /*{{{*/
534 // ---------------------------------------------------------------------
535 /* We return true if we hit something */
536 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
538 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
539 return _error
->Error("Tried to dequeue a fetching object");
546 if ((*I
)->Owner
== Owner
)
550 Owner
->QueueCounter
--;
561 // Queue::Startup - Start the worker processes /*{{{*/
562 // ---------------------------------------------------------------------
564 bool pkgAcquire::Queue::Startup()
569 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
573 Workers
= new Worker(this,Cnf
,Owner
->Log
);
575 if (Workers
->Start() == false)
578 /* When pipelining we commit 10 items. This needs to change when we
579 added other source retry to have cycle maintain a pipeline depth
581 if (Cnf
->Pipeline
== true)
589 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
590 // ---------------------------------------------------------------------
592 bool pkgAcquire::Queue::Shutdown()
594 // Delete all of the workers
597 pkgAcquire::Worker
*Jnk
= Workers
;
598 Workers
= Workers
->NextQueue
;
606 // Queue::FindItem - Find a URI in the item list /*{{{*/
607 // ---------------------------------------------------------------------
609 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
611 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
612 if (I
->URI
== URI
&& I
->Worker
== Owner
)
617 // Queue::ItemDone - Item has been completed /*{{{*/
618 // ---------------------------------------------------------------------
619 /* The worker signals this which causes the item to be removed from the
620 queue. If this is the last queue instance then it is removed from the
622 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
625 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
626 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
628 if (Itm
->Owner
->QueueCounter
<= 1)
629 Owner
->Dequeue(Itm
->Owner
);
639 // Queue::Cycle - Queue new items into the method /*{{{*/
640 // ---------------------------------------------------------------------
641 /* This locates a new idle item and sends it to the worker. If pipelining
642 is enabled then it keeps the pipe full. */
643 bool pkgAcquire::Queue::Cycle()
645 if (Items
== 0 || Workers
== 0)
649 return _error
->Error("Pipedepth failure");
651 // Look for a queable item
653 while (PipeDepth
< (signed)MaxPipeDepth
)
655 for (; I
!= 0; I
= I
->Next
)
656 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
659 // Nothing to do, queue is idle.
664 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
666 if (Workers
->QueueItem(I
) == false)
673 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
674 // ---------------------------------------------------------------------
675 /* This is called when an item in multiple queues is dequeued */
676 void pkgAcquire::Queue::Bump()
682 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
683 // ---------------------------------------------------------------------
685 pkgAcquireStatus::pkgAcquireStatus()
690 // AcquireStatus::Pulse - Called periodically /*{{{*/
691 // ---------------------------------------------------------------------
692 /* This computes some internal state variables for the derived classes to
693 use. It generates the current downloaded bytes and total bytes to download
694 as well as the current CPS estimate. */
695 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
702 // Compute the total number of bytes to fetch
703 unsigned int Unknown
= 0;
704 unsigned int Count
= 0;
705 for (pkgAcquire::Item
**I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
709 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
712 // Totally ignore local items
713 if ((*I
)->Local
== true)
716 TotalBytes
+= (*I
)->FileSize
;
717 if ((*I
)->Complete
== true)
718 CurrentBytes
+= (*I
)->FileSize
;
719 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
723 // Compute the current completion
724 unsigned long ResumeSize
= 0;
725 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
726 I
= Owner
->WorkerStep(I
))
727 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
729 CurrentBytes
+= I
->CurrentSize
;
730 ResumeSize
+= I
->ResumePoint
;
732 // Files with unknown size always have 100% completion
733 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
734 I
->CurrentItem
->Owner
->Complete
== false)
735 TotalBytes
+= I
->CurrentSize
;
738 // Normalize the figures and account for unknown size downloads
741 if (Unknown
== Count
)
742 TotalBytes
= Unknown
;
744 // Wha?! Is not supposed to happen.
745 if (CurrentBytes
> TotalBytes
)
746 CurrentBytes
= TotalBytes
;
749 struct timeval NewTime
;
750 gettimeofday(&NewTime
,0);
751 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
752 NewTime
.tv_sec
- Time
.tv_sec
> 6)
754 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
755 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
757 // Compute the CPS value
761 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
762 LastBytes
= CurrentBytes
- ResumeSize
;
763 ElapsedTime
= (unsigned long)Delta
;
770 // AcquireStatus::Start - Called when the download is started /*{{{*/
771 // ---------------------------------------------------------------------
772 /* We just reset the counters */
773 void pkgAcquireStatus::Start()
775 gettimeofday(&Time
,0);
776 gettimeofday(&StartTime
,0);
787 // AcquireStatus::Stop - Finished downloading /*{{{*/
788 // ---------------------------------------------------------------------
789 /* This accurately computes the elapsed time and the total overall CPS. */
790 void pkgAcquireStatus::Stop()
792 // Compute the CPS and elapsed time
793 struct timeval NewTime
;
794 gettimeofday(&NewTime
,0);
796 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
797 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
799 // Compute the CPS value
803 CurrentCPS
= FetchedBytes
/Delta
;
804 LastBytes
= CurrentBytes
;
805 ElapsedTime
= (unsigned int)Delta
;
808 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
809 // ---------------------------------------------------------------------
810 /* This is used to get accurate final transfer rate reporting. */
811 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
813 FetchedBytes
+= Size
- Resume
;