]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.29 1999/03/16 00:43:55 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>
30 // Acquire::pkgAcquire - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
32 /* We grab some runtime state from the configuration space */
33 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Log
) : Log(Log
)
41 string Mode
= _config
->Find("Acquire::Queue-Mode","host");
42 if (strcasecmp(Mode
.c_str(),"host") == 0)
43 QueueMode
= QueueHost
;
44 if (strcasecmp(Mode
.c_str(),"access") == 0)
45 QueueMode
= QueueAccess
;
47 Debug
= _config
->FindB("Debug::pkgAcquire",false);
50 // Acquire::~pkgAcquire - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
52 /* Free our memory, clean up the queues (destroy the workers) */
53 pkgAcquire::~pkgAcquire()
55 while (Items
.size() != 0)
60 MethodConfig
*Jnk
= Configs
;
61 Configs
= Configs
->Next
;
68 Queues
= Queues
->Next
;
73 // Acquire::Add - Add a new item /*{{{*/
74 // ---------------------------------------------------------------------
75 /* This puts an item on the acquire list. This list is mainly for tracking
77 void pkgAcquire::Add(Item
*Itm
)
82 // Acquire::Remove - Remove a item /*{{{*/
83 // ---------------------------------------------------------------------
84 /* Remove an item from the acquire list. This is usually not used.. */
85 void pkgAcquire::Remove(Item
*Itm
)
87 for (vector
<Item
*>::iterator I
= Items
.begin(); I
< Items
.end(); I
++)
94 // Acquire::Add - Add a worker /*{{{*/
95 // ---------------------------------------------------------------------
96 /* A list of workers is kept so that the select loop can direct their FD
98 void pkgAcquire::Add(Worker
*Work
)
100 Work
->NextAcquire
= Workers
;
104 // Acquire::Remove - Remove a worker /*{{{*/
105 // ---------------------------------------------------------------------
106 /* A worker has died. This can not be done while the select loop is running
107 as it would require that RunFds could handling a changing list state and
109 void pkgAcquire::Remove(Worker
*Work
)
114 Worker
**I
= &Workers
;
118 *I
= (*I
)->NextAcquire
;
120 I
= &(*I
)->NextAcquire
;
124 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
125 // ---------------------------------------------------------------------
126 /* This is the entry point for an item. An item calls this function when
127 it is construction which creates a queue (based on the current queue
128 mode) and puts the item in that queue. If the system is running then
129 the queue might be started. */
130 void pkgAcquire::Enqueue(ItemDesc
&Item
)
132 // Determine which queue to put the item in
133 const MethodConfig
*Config
;
134 string Name
= QueueName(Item
.URI
,Config
);
135 if (Name
.empty() == true)
138 // Find the queue structure
140 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
143 I
= new Queue(Name
,this);
151 // See if this is a local only URI
152 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
153 Item
.Owner
->Local
= true;
154 Item
.Owner
->Status
= Item::StatIdle
;
156 // Queue it into the named queue
163 clog
<< "Fetching " << Item
.URI
<< endl
;
164 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
165 clog
<< " Queue is: " << Name
<< endl
;
169 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
170 // ---------------------------------------------------------------------
171 /* This is called when an item is finished being fetched. It removes it
172 from all the queues */
173 void pkgAcquire::Dequeue(Item
*Itm
)
177 for (; I
!= 0; I
= I
->Next
)
178 Res
|= I
->Dequeue(Itm
);
181 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
186 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
187 // ---------------------------------------------------------------------
188 /* The string returned depends on the configuration settings and the
189 method parameters. Given something like http://foo.org/bar it can
190 return http://foo.org or http */
191 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
195 Config
= GetConfig(U
.Access
);
199 /* Single-Instance methods get exactly one queue per URI. This is
200 also used for the Access queue method */
201 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
204 return U
.Access
+ ':' + U
.Host
;
207 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This locates the configuration structure for an access method. If
210 a config structure cannot be found a Worker will be created to
212 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
214 // Search for an existing config
216 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
217 if (Conf
->Access
== Access
)
220 // Create the new config class
221 Conf
= new MethodConfig
;
222 Conf
->Access
= Access
;
223 Conf
->Next
= Configs
;
226 // Create the worker to fetch the configuration
228 if (Work
.Start() == false)
234 // Acquire::SetFds - Deal with readable FDs /*{{{*/
235 // ---------------------------------------------------------------------
236 /* Collect FDs that have activity monitors into the fd sets */
237 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
239 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
241 if (I
->InReady
== true && I
->InFd
>= 0)
245 FD_SET(I
->InFd
,RSet
);
247 if (I
->OutReady
== true && I
->OutFd
>= 0)
251 FD_SET(I
->OutFd
,WSet
);
256 // Acquire::RunFds - Deal with active FDs /*{{{*/
257 // ---------------------------------------------------------------------
258 /* Dispatch active FDs over to the proper workers. It is very important
259 that a worker never be erased while this is running! The queue class
260 should never erase a worker except during shutdown processing. */
261 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
263 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
265 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
267 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
272 // Acquire::Run - Run the fetch sequence /*{{{*/
273 // ---------------------------------------------------------------------
274 /* This runs the queues. It manages a select loop for all of the
275 Worker tasks. The workers interact with the queues and items to
276 manage the actual fetch. */
277 bool pkgAcquire::Run()
281 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
287 // Run till all things have been acquired
298 SetFds(Highest
,&RFds
,&WFds
);
303 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
305 while (Res
< 0 && errno
== EINTR
);
309 _error
->Errno("select","Select has failed");
314 if (_error
->PendingError() == true)
317 // Timeout, notify the log class
318 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
321 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
331 // Shut down the acquire bits
333 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
336 return !_error
->PendingError();
339 // Acquire::Bump - Called when an item is dequeued /*{{{*/
340 // ---------------------------------------------------------------------
341 /* This routine bumps idle queues in hopes that they will be able to fetch
343 void pkgAcquire::Bump()
345 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
349 // Acquire::WorkerStep - Step to the next worker /*{{{*/
350 // ---------------------------------------------------------------------
351 /* Not inlined to advoid including acquire-worker.h */
352 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
354 return I
->NextAcquire
;
357 // Acquire::Clean - Cleans a directory /*{{{*/
358 // ---------------------------------------------------------------------
359 /* This is a bit simplistic, it looks at every file in the dir and sees
360 if it is part of the download set. */
361 bool pkgAcquire::Clean(string Dir
)
363 DIR *D
= opendir(Dir
.c_str());
365 return _error
->Errno("opendir","Unable to read %s",Dir
.c_str());
367 string StartDir
= SafeGetCWD();
368 if (chdir(Dir
.c_str()) != 0)
371 return _error
->Errno("chdir","Unable to change to ",Dir
.c_str());
374 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
377 if (strcmp(Dir
->d_name
,"lock") == 0 ||
378 strcmp(Dir
->d_name
,"partial") == 0 ||
379 strcmp(Dir
->d_name
,".") == 0 ||
380 strcmp(Dir
->d_name
,"..") == 0)
383 // Look in the get list
384 vector
<Item
*>::iterator I
= Items
.begin();
385 for (; I
!= Items
.end(); I
++)
386 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
389 // Nothing found, nuke it
390 if (I
== Items
.end())
394 chdir(StartDir
.c_str());
399 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
400 // ---------------------------------------------------------------------
401 /* This is the total number of bytes needed */
402 unsigned long pkgAcquire::TotalNeeded()
404 unsigned long Total
= 0;
405 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
406 Total
+= (*I
)->FileSize
;
410 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
411 // ---------------------------------------------------------------------
412 /* This is the number of bytes that is not local */
413 unsigned long pkgAcquire::FetchNeeded()
415 unsigned long Total
= 0;
416 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
417 if ((*I
)->Local
== false)
418 Total
+= (*I
)->FileSize
;
422 // pkgAcquire::UriBegin - Start iterator for the uri list /*{{{*/
423 // ---------------------------------------------------------------------
425 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
427 return UriIterator(Queues
);
430 // pkgAcquire::UriEnd - End iterator for the uri list /*{{{*/
431 // ---------------------------------------------------------------------
433 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
435 return UriIterator(0);
439 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
440 // ---------------------------------------------------------------------
442 pkgAcquire::MethodConfig::MethodConfig()
444 SingleInstance
= false;
452 // Queue::Queue - Constructor /*{{{*/
453 // ---------------------------------------------------------------------
455 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
465 // Queue::~Queue - Destructor /*{{{*/
466 // ---------------------------------------------------------------------
468 pkgAcquire::Queue::~Queue()
480 // Queue::Enqueue - Queue an item to the queue /*{{{*/
481 // ---------------------------------------------------------------------
483 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
486 for (; *I
!= 0; I
= &(*I
)->Next
);
489 QItem
*Itm
= new QItem
;
494 Item
.Owner
->QueueCounter
++;
495 if (Items
->Next
== 0)
499 // Queue::Dequeue - Remove an item from the queue /*{{{*/
500 // ---------------------------------------------------------------------
501 /* We return true if we hit something */
502 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
504 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
505 return _error
->Error("Tried to dequeue a fetching object");
512 if ((*I
)->Owner
== Owner
)
516 Owner
->QueueCounter
--;
527 // Queue::Startup - Start the worker processes /*{{{*/
528 // ---------------------------------------------------------------------
530 bool pkgAcquire::Queue::Startup()
535 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
539 Workers
= new Worker(this,Cnf
,Owner
->Log
);
541 if (Workers
->Start() == false)
544 /* When pipelining we commit 10 items. This needs to change when we
545 added other source retry to have cycle maintain a pipeline depth
547 if (Cnf
->Pipeline
== true)
555 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
556 // ---------------------------------------------------------------------
558 bool pkgAcquire::Queue::Shutdown()
560 // Delete all of the workers
563 pkgAcquire::Worker
*Jnk
= Workers
;
564 Workers
= Workers
->NextQueue
;
572 // Queue::FindItem - Find a URI in the item list /*{{{*/
573 // ---------------------------------------------------------------------
575 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
577 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
578 if (I
->URI
== URI
&& I
->Worker
== Owner
)
583 // Queue::ItemDone - Item has been completed /*{{{*/
584 // ---------------------------------------------------------------------
585 /* The worker signals this which causes the item to be removed from the
586 queue. If this is the last queue instance then it is removed from the
588 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
591 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
592 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
594 if (Itm
->Owner
->QueueCounter
<= 1)
595 Owner
->Dequeue(Itm
->Owner
);
605 // Queue::Cycle - Queue new items into the method /*{{{*/
606 // ---------------------------------------------------------------------
607 /* This locates a new idle item and sends it to the worker. If pipelining
608 is enabled then it keeps the pipe full. */
609 bool pkgAcquire::Queue::Cycle()
611 if (Items
== 0 || Workers
== 0)
615 return _error
->Error("Pipedepth failure");
617 // Look for a queable item
619 while (PipeDepth
< (signed)MaxPipeDepth
)
621 for (; I
!= 0; I
= I
->Next
)
622 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
625 // Nothing to do, queue is idle.
630 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
632 if (Workers
->QueueItem(I
) == false)
639 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
640 // ---------------------------------------------------------------------
641 /* This is called when an item in multiple queues is dequeued */
642 void pkgAcquire::Queue::Bump()
648 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
649 // ---------------------------------------------------------------------
651 pkgAcquireStatus::pkgAcquireStatus()
656 // AcquireStatus::Pulse - Called periodically /*{{{*/
657 // ---------------------------------------------------------------------
658 /* This computes some internal state variables for the derived classes to
659 use. It generates the current downloaded bytes and total bytes to download
660 as well as the current CPS estimate. */
661 void pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
668 // Compute the total number of bytes to fetch
669 unsigned int Unknown
= 0;
670 unsigned int Count
= 0;
671 for (pkgAcquire::Item
**I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
675 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
678 // Totally ignore local items
679 if ((*I
)->Local
== true)
682 TotalBytes
+= (*I
)->FileSize
;
683 if ((*I
)->Complete
== true)
684 CurrentBytes
+= (*I
)->FileSize
;
685 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
689 // Compute the current completion
690 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
691 I
= Owner
->WorkerStep(I
))
692 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
693 CurrentBytes
+= I
->CurrentSize
;
695 // Normalize the figures and account for unknown size downloads
698 if (Unknown
== Count
)
699 TotalBytes
= Unknown
;
701 TotalBytes
+= TotalBytes
/(Count
- Unknown
)*Unknown
;
704 struct timeval NewTime
;
705 gettimeofday(&NewTime
,0);
706 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
707 NewTime
.tv_sec
- Time
.tv_sec
> 6)
709 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
710 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
712 // Compute the CPS value
716 CurrentCPS
= (CurrentBytes
- LastBytes
)/Delta
;
717 LastBytes
= CurrentBytes
;
718 ElapsedTime
= (unsigned long)Delta
;
723 // AcquireStatus::Start - Called when the download is started /*{{{*/
724 // ---------------------------------------------------------------------
725 /* We just reset the counters */
726 void pkgAcquireStatus::Start()
728 gettimeofday(&Time
,0);
729 gettimeofday(&StartTime
,0);
740 // AcquireStatus::Stop - Finished downloading /*{{{*/
741 // ---------------------------------------------------------------------
742 /* This accurately computes the elapsed time and the total overall CPS. */
743 void pkgAcquireStatus::Stop()
745 // Compute the CPS and elapsed time
746 struct timeval NewTime
;
747 gettimeofday(&NewTime
,0);
749 // Compute the delta time with full accuracy
750 long usdiff
= NewTime
.tv_usec
- StartTime
.tv_usec
;
751 long sdiff
= NewTime
.tv_sec
- StartTime
.tv_sec
;
760 // Compute the CPS value
761 if (sdiff
== 0 && usdiff
== 0)
764 CurrentCPS
= FetchedBytes
/(sdiff
+ usdiff
/1000000.0);
765 LastBytes
= CurrentBytes
;
769 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
770 // ---------------------------------------------------------------------
771 /* This is used to get accurate final transfer rate reporting. */
772 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
774 FetchedBytes
+= Size
- Resume
;