]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
e197037db830625431860490cf63efac12d7c1a3
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.35 1999/06/06 06:58:36 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()
56 while (Items
.size() != 0)
61 MethodConfig
*Jnk
= Configs
;
62 Configs
= Configs
->Next
;
69 Queues
= Queues
->Next
;
74 // Acquire::Add - Add a new item /*{{{*/
75 // ---------------------------------------------------------------------
76 /* This puts an item on the acquire list. This list is mainly for tracking
78 void pkgAcquire::Add(Item
*Itm
)
83 // Acquire::Remove - Remove a item /*{{{*/
84 // ---------------------------------------------------------------------
85 /* Remove an item from the acquire list. This is usually not used.. */
86 void pkgAcquire::Remove(Item
*Itm
)
88 for (vector
<Item
*>::iterator I
= Items
.begin(); I
< Items
.end(); I
++)
95 // Acquire::Add - Add a worker /*{{{*/
96 // ---------------------------------------------------------------------
97 /* A list of workers is kept so that the select loop can direct their FD
99 void pkgAcquire::Add(Worker
*Work
)
101 Work
->NextAcquire
= Workers
;
105 // Acquire::Remove - Remove a worker /*{{{*/
106 // ---------------------------------------------------------------------
107 /* A worker has died. This can not be done while the select loop is running
108 as it would require that RunFds could handling a changing list state and
110 void pkgAcquire::Remove(Worker
*Work
)
115 Worker
**I
= &Workers
;
119 *I
= (*I
)->NextAcquire
;
121 I
= &(*I
)->NextAcquire
;
125 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
126 // ---------------------------------------------------------------------
127 /* This is the entry point for an item. An item calls this function when
128 it is construction which creates a queue (based on the current queue
129 mode) and puts the item in that queue. If the system is running then
130 the queue might be started. */
131 void pkgAcquire::Enqueue(ItemDesc
&Item
)
133 // Determine which queue to put the item in
134 const MethodConfig
*Config
;
135 string Name
= QueueName(Item
.URI
,Config
);
136 if (Name
.empty() == true)
139 // Find the queue structure
141 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
144 I
= new Queue(Name
,this);
152 // See if this is a local only URI
153 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
154 Item
.Owner
->Local
= true;
155 Item
.Owner
->Status
= Item::StatIdle
;
157 // Queue it into the named queue
164 clog
<< "Fetching " << Item
.URI
<< endl
;
165 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
166 clog
<< " Queue is: " << Name
<< endl
;
170 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This is called when an item is finished being fetched. It removes it
173 from all the queues */
174 void pkgAcquire::Dequeue(Item
*Itm
)
178 for (; I
!= 0; I
= I
->Next
)
179 Res
|= I
->Dequeue(Itm
);
182 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
187 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
188 // ---------------------------------------------------------------------
189 /* The string returned depends on the configuration settings and the
190 method parameters. Given something like http://foo.org/bar it can
191 return http://foo.org or http */
192 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
196 Config
= GetConfig(U
.Access
);
200 /* Single-Instance methods get exactly one queue per URI. This is
201 also used for the Access queue method */
202 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
205 return U
.Access
+ ':' + U
.Host
;
208 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This locates the configuration structure for an access method. If
211 a config structure cannot be found a Worker will be created to
213 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
215 // Search for an existing config
217 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
218 if (Conf
->Access
== Access
)
221 // Create the new config class
222 Conf
= new MethodConfig
;
223 Conf
->Access
= Access
;
224 Conf
->Next
= Configs
;
227 // Create the worker to fetch the configuration
229 if (Work
.Start() == false)
235 // Acquire::SetFds - Deal with readable FDs /*{{{*/
236 // ---------------------------------------------------------------------
237 /* Collect FDs that have activity monitors into the fd sets */
238 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
240 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
242 if (I
->InReady
== true && I
->InFd
>= 0)
246 FD_SET(I
->InFd
,RSet
);
248 if (I
->OutReady
== true && I
->OutFd
>= 0)
252 FD_SET(I
->OutFd
,WSet
);
257 // Acquire::RunFds - Deal with active FDs /*{{{*/
258 // ---------------------------------------------------------------------
259 /* Dispatch active FDs over to the proper workers. It is very important
260 that a worker never be erased while this is running! The queue class
261 should never erase a worker except during shutdown processing. */
262 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
264 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
266 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
268 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
273 // Acquire::Run - Run the fetch sequence /*{{{*/
274 // ---------------------------------------------------------------------
275 /* This runs the queues. It manages a select loop for all of the
276 Worker tasks. The workers interact with the queues and items to
277 manage the actual fetch. */
278 bool pkgAcquire::Run()
282 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
288 // Run till all things have been acquired
299 SetFds(Highest
,&RFds
,&WFds
);
304 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
306 while (Res
< 0 && errno
== EINTR
);
310 _error
->Errno("select","Select has failed");
315 if (_error
->PendingError() == true)
318 // Timeout, notify the log class
319 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
322 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
332 // Shut down the acquire bits
334 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
337 // Shut down the items
338 for (Item
**I
= Items
.begin(); I
!= Items
.end(); I
++)
341 return !_error
->PendingError();
344 // Acquire::Bump - Called when an item is dequeued /*{{{*/
345 // ---------------------------------------------------------------------
346 /* This routine bumps idle queues in hopes that they will be able to fetch
348 void pkgAcquire::Bump()
350 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
354 // Acquire::WorkerStep - Step to the next worker /*{{{*/
355 // ---------------------------------------------------------------------
356 /* Not inlined to advoid including acquire-worker.h */
357 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
359 return I
->NextAcquire
;
362 // Acquire::Clean - Cleans a directory /*{{{*/
363 // ---------------------------------------------------------------------
364 /* This is a bit simplistic, it looks at every file in the dir and sees
365 if it is part of the download set. */
366 bool pkgAcquire::Clean(string Dir
)
368 DIR *D
= opendir(Dir
.c_str());
370 return _error
->Errno("opendir","Unable to read %s",Dir
.c_str());
372 string StartDir
= SafeGetCWD();
373 if (chdir(Dir
.c_str()) != 0)
376 return _error
->Errno("chdir","Unable to change to ",Dir
.c_str());
379 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
382 if (strcmp(Dir
->d_name
,"lock") == 0 ||
383 strcmp(Dir
->d_name
,"partial") == 0 ||
384 strcmp(Dir
->d_name
,".") == 0 ||
385 strcmp(Dir
->d_name
,"..") == 0)
388 // Look in the get list
389 vector
<Item
*>::iterator I
= Items
.begin();
390 for (; I
!= Items
.end(); I
++)
391 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
394 // Nothing found, nuke it
395 if (I
== Items
.end())
399 chdir(StartDir
.c_str());
404 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
405 // ---------------------------------------------------------------------
406 /* This is the total number of bytes needed */
407 unsigned long pkgAcquire::TotalNeeded()
409 unsigned long Total
= 0;
410 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
411 Total
+= (*I
)->FileSize
;
415 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
416 // ---------------------------------------------------------------------
417 /* This is the number of bytes that is not local */
418 unsigned long pkgAcquire::FetchNeeded()
420 unsigned long Total
= 0;
421 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
422 if ((*I
)->Local
== false)
423 Total
+= (*I
)->FileSize
;
427 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
428 // ---------------------------------------------------------------------
429 /* This is the number of bytes that is not local */
430 unsigned long pkgAcquire::PartialPresent()
432 unsigned long Total
= 0;
433 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
434 if ((*I
)->Local
== false)
435 Total
+= (*I
)->PartialSize
;
439 // pkgAcquire::UriBegin - Start iterator for the uri list /*{{{*/
440 // ---------------------------------------------------------------------
442 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
444 return UriIterator(Queues
);
447 // pkgAcquire::UriEnd - End iterator for the uri list /*{{{*/
448 // ---------------------------------------------------------------------
450 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
452 return UriIterator(0);
456 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
457 // ---------------------------------------------------------------------
459 pkgAcquire::MethodConfig::MethodConfig()
461 SingleInstance
= false;
469 // Queue::Queue - Constructor /*{{{*/
470 // ---------------------------------------------------------------------
472 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
482 // Queue::~Queue - Destructor /*{{{*/
483 // ---------------------------------------------------------------------
485 pkgAcquire::Queue::~Queue()
497 // Queue::Enqueue - Queue an item to the queue /*{{{*/
498 // ---------------------------------------------------------------------
500 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
503 for (; *I
!= 0; I
= &(*I
)->Next
);
506 QItem
*Itm
= new QItem
;
511 Item
.Owner
->QueueCounter
++;
512 if (Items
->Next
== 0)
516 // Queue::Dequeue - Remove an item from the queue /*{{{*/
517 // ---------------------------------------------------------------------
518 /* We return true if we hit something */
519 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
521 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
522 return _error
->Error("Tried to dequeue a fetching object");
529 if ((*I
)->Owner
== Owner
)
533 Owner
->QueueCounter
--;
544 // Queue::Startup - Start the worker processes /*{{{*/
545 // ---------------------------------------------------------------------
547 bool pkgAcquire::Queue::Startup()
552 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
556 Workers
= new Worker(this,Cnf
,Owner
->Log
);
558 if (Workers
->Start() == false)
561 /* When pipelining we commit 10 items. This needs to change when we
562 added other source retry to have cycle maintain a pipeline depth
564 if (Cnf
->Pipeline
== true)
572 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
573 // ---------------------------------------------------------------------
575 bool pkgAcquire::Queue::Shutdown()
577 // Delete all of the workers
580 pkgAcquire::Worker
*Jnk
= Workers
;
581 Workers
= Workers
->NextQueue
;
589 // Queue::FindItem - Find a URI in the item list /*{{{*/
590 // ---------------------------------------------------------------------
592 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
594 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
595 if (I
->URI
== URI
&& I
->Worker
== Owner
)
600 // Queue::ItemDone - Item has been completed /*{{{*/
601 // ---------------------------------------------------------------------
602 /* The worker signals this which causes the item to be removed from the
603 queue. If this is the last queue instance then it is removed from the
605 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
608 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
609 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
611 if (Itm
->Owner
->QueueCounter
<= 1)
612 Owner
->Dequeue(Itm
->Owner
);
622 // Queue::Cycle - Queue new items into the method /*{{{*/
623 // ---------------------------------------------------------------------
624 /* This locates a new idle item and sends it to the worker. If pipelining
625 is enabled then it keeps the pipe full. */
626 bool pkgAcquire::Queue::Cycle()
628 if (Items
== 0 || Workers
== 0)
632 return _error
->Error("Pipedepth failure");
634 // Look for a queable item
636 while (PipeDepth
< (signed)MaxPipeDepth
)
638 for (; I
!= 0; I
= I
->Next
)
639 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
642 // Nothing to do, queue is idle.
647 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
649 if (Workers
->QueueItem(I
) == false)
656 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
657 // ---------------------------------------------------------------------
658 /* This is called when an item in multiple queues is dequeued */
659 void pkgAcquire::Queue::Bump()
665 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
666 // ---------------------------------------------------------------------
668 pkgAcquireStatus::pkgAcquireStatus()
673 // AcquireStatus::Pulse - Called periodically /*{{{*/
674 // ---------------------------------------------------------------------
675 /* This computes some internal state variables for the derived classes to
676 use. It generates the current downloaded bytes and total bytes to download
677 as well as the current CPS estimate. */
678 void pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
685 // Compute the total number of bytes to fetch
686 unsigned int Unknown
= 0;
687 unsigned int Count
= 0;
688 for (pkgAcquire::Item
**I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
692 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
695 // Totally ignore local items
696 if ((*I
)->Local
== true)
699 TotalBytes
+= (*I
)->FileSize
;
700 if ((*I
)->Complete
== true)
701 CurrentBytes
+= (*I
)->FileSize
;
702 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
706 // Compute the current completion
707 unsigned long ResumeSize
= 0;
708 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
709 I
= Owner
->WorkerStep(I
))
710 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
712 CurrentBytes
+= I
->CurrentSize
;
713 ResumeSize
+= I
->ResumePoint
;
715 // Files with unknown size always have 100% completion
716 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
717 I
->CurrentItem
->Owner
->Complete
== false)
718 TotalBytes
+= I
->CurrentSize
;
721 // Normalize the figures and account for unknown size downloads
724 if (Unknown
== Count
)
725 TotalBytes
= Unknown
;
728 struct timeval NewTime
;
729 gettimeofday(&NewTime
,0);
730 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
731 NewTime
.tv_sec
- Time
.tv_sec
> 6)
733 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
734 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
736 // Compute the CPS value
740 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
741 LastBytes
= CurrentBytes
- ResumeSize
;
742 ElapsedTime
= (unsigned long)Delta
;
747 // AcquireStatus::Start - Called when the download is started /*{{{*/
748 // ---------------------------------------------------------------------
749 /* We just reset the counters */
750 void pkgAcquireStatus::Start()
752 gettimeofday(&Time
,0);
753 gettimeofday(&StartTime
,0);
764 // AcquireStatus::Stop - Finished downloading /*{{{*/
765 // ---------------------------------------------------------------------
766 /* This accurately computes the elapsed time and the total overall CPS. */
767 void pkgAcquireStatus::Stop()
769 // Compute the CPS and elapsed time
770 struct timeval NewTime
;
771 gettimeofday(&NewTime
,0);
773 // Compute the delta time with full accuracy
774 long usdiff
= NewTime
.tv_usec
- StartTime
.tv_usec
;
775 long sdiff
= NewTime
.tv_sec
- StartTime
.tv_sec
;
784 // Compute the CPS value
785 if (sdiff
== 0 && usdiff
== 0)
788 CurrentCPS
= FetchedBytes
/(sdiff
+ usdiff
/1000000.0);
789 LastBytes
= CurrentBytes
;
793 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
794 // ---------------------------------------------------------------------
795 /* This is used to get accurate final transfer rate reporting. */
796 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
798 FetchedBytes
+= Size
- Resume
;