]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.44 1999/12/09 05:22:33 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
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 %spartial 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 %spartial 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
)
110 for (vector
<Item
*>::iterator I
= Items
.begin(); I
< Items
.end(); I
++)
117 // Acquire::Add - Add a worker /*{{{*/
118 // ---------------------------------------------------------------------
119 /* A list of workers is kept so that the select loop can direct their FD
121 void pkgAcquire::Add(Worker
*Work
)
123 Work
->NextAcquire
= Workers
;
127 // Acquire::Remove - Remove a worker /*{{{*/
128 // ---------------------------------------------------------------------
129 /* A worker has died. This can not be done while the select loop is running
130 as it would require that RunFds could handling a changing list state and
132 void pkgAcquire::Remove(Worker
*Work
)
137 Worker
**I
= &Workers
;
141 *I
= (*I
)->NextAcquire
;
143 I
= &(*I
)->NextAcquire
;
147 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This is the entry point for an item. An item calls this function when
150 it is constructed which creates a queue (based on the current queue
151 mode) and puts the item in that queue. If the system is running then
152 the queue might be started. */
153 void pkgAcquire::Enqueue(ItemDesc
&Item
)
155 // Determine which queue to put the item in
156 const MethodConfig
*Config
;
157 string Name
= QueueName(Item
.URI
,Config
);
158 if (Name
.empty() == true)
161 // Find the queue structure
163 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
166 I
= new Queue(Name
,this);
174 // See if this is a local only URI
175 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
176 Item
.Owner
->Local
= true;
177 Item
.Owner
->Status
= Item::StatIdle
;
179 // Queue it into the named queue
186 clog
<< "Fetching " << Item
.URI
<< endl
;
187 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
188 clog
<< " Queue is: " << Name
<< endl
;
192 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
193 // ---------------------------------------------------------------------
194 /* This is called when an item is finished being fetched. It removes it
195 from all the queues */
196 void pkgAcquire::Dequeue(Item
*Itm
)
200 for (; I
!= 0; I
= I
->Next
)
201 Res
|= I
->Dequeue(Itm
);
204 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
209 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
210 // ---------------------------------------------------------------------
211 /* The string returned depends on the configuration settings and the
212 method parameters. Given something like http://foo.org/bar it can
213 return http://foo.org or http */
214 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
218 Config
= GetConfig(U
.Access
);
222 /* Single-Instance methods get exactly one queue per URI. This is
223 also used for the Access queue method */
224 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
227 return U
.Access
+ ':' + U
.Host
;
230 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
231 // ---------------------------------------------------------------------
232 /* This locates the configuration structure for an access method. If
233 a config structure cannot be found a Worker will be created to
235 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
237 // Search for an existing config
239 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
240 if (Conf
->Access
== Access
)
243 // Create the new config class
244 Conf
= new MethodConfig
;
245 Conf
->Access
= Access
;
246 Conf
->Next
= Configs
;
249 // Create the worker to fetch the configuration
251 if (Work
.Start() == false)
257 // Acquire::SetFds - Deal with readable FDs /*{{{*/
258 // ---------------------------------------------------------------------
259 /* Collect FDs that have activity monitors into the fd sets */
260 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
262 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
264 if (I
->InReady
== true && I
->InFd
>= 0)
268 FD_SET(I
->InFd
,RSet
);
270 if (I
->OutReady
== true && I
->OutFd
>= 0)
274 FD_SET(I
->OutFd
,WSet
);
279 // Acquire::RunFds - Deal with active FDs /*{{{*/
280 // ---------------------------------------------------------------------
281 /* Dispatch active FDs over to the proper workers. It is very important
282 that a worker never be erased while this is running! The queue class
283 should never erase a worker except during shutdown processing. */
284 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
286 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
288 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
290 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
295 // Acquire::Run - Run the fetch sequence /*{{{*/
296 // ---------------------------------------------------------------------
297 /* This runs the queues. It manages a select loop for all of the
298 Worker tasks. The workers interact with the queues and items to
299 manage the actual fetch. */
300 pkgAcquire::RunResult
pkgAcquire::Run()
304 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
310 bool WasCancelled
= false;
312 // Run till all things have been acquired
323 SetFds(Highest
,&RFds
,&WFds
);
328 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
330 while (Res
< 0 && errno
== EINTR
);
334 _error
->Errno("select","Select has failed");
339 if (_error
->PendingError() == true)
342 // Timeout, notify the log class
343 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
346 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
348 if (Log
!= 0 && Log
->Pulse(this) == false)
359 // Shut down the acquire bits
361 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
364 // Shut down the items
365 for (Item
**I
= Items
.begin(); I
!= Items
.end(); I
++)
368 if (_error
->PendingError())
375 // Acquire::Bump - Called when an item is dequeued /*{{{*/
376 // ---------------------------------------------------------------------
377 /* This routine bumps idle queues in hopes that they will be able to fetch
379 void pkgAcquire::Bump()
381 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
385 // Acquire::WorkerStep - Step to the next worker /*{{{*/
386 // ---------------------------------------------------------------------
387 /* Not inlined to advoid including acquire-worker.h */
388 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
390 return I
->NextAcquire
;
393 // Acquire::Clean - Cleans a directory /*{{{*/
394 // ---------------------------------------------------------------------
395 /* This is a bit simplistic, it looks at every file in the dir and sees
396 if it is part of the download set. */
397 bool pkgAcquire::Clean(string Dir
)
399 DIR *D
= opendir(Dir
.c_str());
401 return _error
->Errno("opendir","Unable to read %s",Dir
.c_str());
403 string StartDir
= SafeGetCWD();
404 if (chdir(Dir
.c_str()) != 0)
407 return _error
->Errno("chdir","Unable to change to ",Dir
.c_str());
410 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
413 if (strcmp(Dir
->d_name
,"lock") == 0 ||
414 strcmp(Dir
->d_name
,"partial") == 0 ||
415 strcmp(Dir
->d_name
,".") == 0 ||
416 strcmp(Dir
->d_name
,"..") == 0)
419 // Look in the get list
420 vector
<Item
*>::iterator I
= Items
.begin();
421 for (; I
!= Items
.end(); I
++)
422 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
425 // Nothing found, nuke it
426 if (I
== Items
.end())
430 chdir(StartDir
.c_str());
435 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
436 // ---------------------------------------------------------------------
437 /* This is the total number of bytes needed */
438 unsigned long pkgAcquire::TotalNeeded()
440 unsigned long Total
= 0;
441 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
442 Total
+= (*I
)->FileSize
;
446 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
447 // ---------------------------------------------------------------------
448 /* This is the number of bytes that is not local */
449 unsigned long pkgAcquire::FetchNeeded()
451 unsigned long Total
= 0;
452 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
453 if ((*I
)->Local
== false)
454 Total
+= (*I
)->FileSize
;
458 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
459 // ---------------------------------------------------------------------
460 /* This is the number of bytes that is not local */
461 unsigned long pkgAcquire::PartialPresent()
463 unsigned long Total
= 0;
464 for (pkgAcquire::Item
**I
= ItemsBegin(); I
!= ItemsEnd(); I
++)
465 if ((*I
)->Local
== false)
466 Total
+= (*I
)->PartialSize
;
470 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
471 // ---------------------------------------------------------------------
473 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
475 return UriIterator(Queues
);
478 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
479 // ---------------------------------------------------------------------
481 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
483 return UriIterator(0);
487 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
488 // ---------------------------------------------------------------------
490 pkgAcquire::MethodConfig::MethodConfig()
492 SingleInstance
= false;
500 // Queue::Queue - Constructor /*{{{*/
501 // ---------------------------------------------------------------------
503 pkgAcquire::Queue::Queue(string Name
,pkgAcquire
*Owner
) : Name(Name
),
513 // Queue::~Queue - Destructor /*{{{*/
514 // ---------------------------------------------------------------------
516 pkgAcquire::Queue::~Queue()
528 // Queue::Enqueue - Queue an item to the queue /*{{{*/
529 // ---------------------------------------------------------------------
531 void pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
534 for (; *I
!= 0; I
= &(*I
)->Next
);
537 QItem
*Itm
= new QItem
;
542 Item
.Owner
->QueueCounter
++;
543 if (Items
->Next
== 0)
547 // Queue::Dequeue - Remove an item from the queue /*{{{*/
548 // ---------------------------------------------------------------------
549 /* We return true if we hit something */
550 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
552 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
553 return _error
->Error("Tried to dequeue a fetching object");
560 if ((*I
)->Owner
== Owner
)
564 Owner
->QueueCounter
--;
575 // Queue::Startup - Start the worker processes /*{{{*/
576 // ---------------------------------------------------------------------
577 /* It is possible for this to be called with a pre-existing set of
579 bool pkgAcquire::Queue::Startup()
584 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
588 Workers
= new Worker(this,Cnf
,Owner
->Log
);
590 if (Workers
->Start() == false)
593 /* When pipelining we commit 10 items. This needs to change when we
594 added other source retry to have cycle maintain a pipeline depth
596 if (Cnf
->Pipeline
== true)
605 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
606 // ---------------------------------------------------------------------
607 /* If final is true then all workers are eliminated, otherwise only workers
608 that do not need cleanup are removed */
609 bool pkgAcquire::Queue::Shutdown(bool Final
)
611 // Delete all of the workers
612 pkgAcquire::Worker
**Cur
= &Workers
;
615 pkgAcquire::Worker
*Jnk
= *Cur
;
616 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
618 *Cur
= Jnk
->NextQueue
;
623 Cur
= &(*Cur
)->NextQueue
;
629 // Queue::FindItem - Find a URI in the item list /*{{{*/
630 // ---------------------------------------------------------------------
632 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
634 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
635 if (I
->URI
== URI
&& I
->Worker
== Owner
)
640 // Queue::ItemDone - Item has been completed /*{{{*/
641 // ---------------------------------------------------------------------
642 /* The worker signals this which causes the item to be removed from the
643 queue. If this is the last queue instance then it is removed from the
645 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
648 if (Itm
->Owner
->Status
== pkgAcquire::Item::StatFetching
)
649 Itm
->Owner
->Status
= pkgAcquire::Item::StatDone
;
651 if (Itm
->Owner
->QueueCounter
<= 1)
652 Owner
->Dequeue(Itm
->Owner
);
662 // Queue::Cycle - Queue new items into the method /*{{{*/
663 // ---------------------------------------------------------------------
664 /* This locates a new idle item and sends it to the worker. If pipelining
665 is enabled then it keeps the pipe full. */
666 bool pkgAcquire::Queue::Cycle()
668 if (Items
== 0 || Workers
== 0)
672 return _error
->Error("Pipedepth failure");
674 // Look for a queable item
676 while (PipeDepth
< (signed)MaxPipeDepth
)
678 for (; I
!= 0; I
= I
->Next
)
679 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
682 // Nothing to do, queue is idle.
687 I
->Owner
->Status
= pkgAcquire::Item::StatFetching
;
689 if (Workers
->QueueItem(I
) == false)
696 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
697 // ---------------------------------------------------------------------
698 /* This is called when an item in multiple queues is dequeued */
699 void pkgAcquire::Queue::Bump()
705 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
706 // ---------------------------------------------------------------------
708 pkgAcquireStatus::pkgAcquireStatus()
713 // AcquireStatus::Pulse - Called periodically /*{{{*/
714 // ---------------------------------------------------------------------
715 /* This computes some internal state variables for the derived classes to
716 use. It generates the current downloaded bytes and total bytes to download
717 as well as the current CPS estimate. */
718 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
725 // Compute the total number of bytes to fetch
726 unsigned int Unknown
= 0;
727 unsigned int Count
= 0;
728 for (pkgAcquire::Item
**I
= Owner
->ItemsBegin(); I
!= Owner
->ItemsEnd();
732 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
735 // Totally ignore local items
736 if ((*I
)->Local
== true)
739 TotalBytes
+= (*I
)->FileSize
;
740 if ((*I
)->Complete
== true)
741 CurrentBytes
+= (*I
)->FileSize
;
742 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
746 // Compute the current completion
747 unsigned long ResumeSize
= 0;
748 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
749 I
= Owner
->WorkerStep(I
))
750 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
752 CurrentBytes
+= I
->CurrentSize
;
753 ResumeSize
+= I
->ResumePoint
;
755 // Files with unknown size always have 100% completion
756 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
757 I
->CurrentItem
->Owner
->Complete
== false)
758 TotalBytes
+= I
->CurrentSize
;
761 // Normalize the figures and account for unknown size downloads
764 if (Unknown
== Count
)
765 TotalBytes
= Unknown
;
767 // Wha?! Is not supposed to happen.
768 if (CurrentBytes
> TotalBytes
)
769 CurrentBytes
= TotalBytes
;
772 struct timeval NewTime
;
773 gettimeofday(&NewTime
,0);
774 if (NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
||
775 NewTime
.tv_sec
- Time
.tv_sec
> 6)
777 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
778 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
780 // Compute the CPS value
784 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
785 LastBytes
= CurrentBytes
- ResumeSize
;
786 ElapsedTime
= (unsigned long)Delta
;
793 // AcquireStatus::Start - Called when the download is started /*{{{*/
794 // ---------------------------------------------------------------------
795 /* We just reset the counters */
796 void pkgAcquireStatus::Start()
798 gettimeofday(&Time
,0);
799 gettimeofday(&StartTime
,0);
810 // AcquireStatus::Stop - Finished downloading /*{{{*/
811 // ---------------------------------------------------------------------
812 /* This accurately computes the elapsed time and the total overall CPS. */
813 void pkgAcquireStatus::Stop()
815 // Compute the CPS and elapsed time
816 struct timeval NewTime
;
817 gettimeofday(&NewTime
,0);
819 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
820 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
822 // Compute the CPS value
826 CurrentCPS
= FetchedBytes
/Delta
;
827 LastBytes
= CurrentBytes
;
828 ElapsedTime
= (unsigned int)Delta
;
831 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
832 // ---------------------------------------------------------------------
833 /* This is used to get accurate final transfer rate reporting. */
834 void pkgAcquireStatus::Fetched(unsigned long Size
,unsigned long Resume
)
836 FetchedBytes
+= Size
- Resume
;