]>
git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire.cc,v 1.50 2004/03/17 05:17:11 mdz Exp $
4 /* ######################################################################
6 Acquire - File Acquiration
8 The core element for the schedule 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 controlled by how the queue
11 name is derived from the URI.
13 ##################################################################### */
15 // Include Files /*{{{*/
18 #include <apt-pkg/acquire.h>
19 #include <apt-pkg/acquire-item.h>
20 #include <apt-pkg/acquire-worker.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/error.h>
23 #include <apt-pkg/strutl.h>
24 #include <apt-pkg/fileutl.h>
44 #include <sys/select.h>
53 // Acquire::pkgAcquire - Constructor /*{{{*/
54 // ---------------------------------------------------------------------
55 /* We grab some runtime state from the configuration space */
56 pkgAcquire::pkgAcquire() : LockFD(-1), d(NULL
), Queues(0), Workers(0), Configs(0), Log(NULL
), ToFetch(0),
57 Debug(_config
->FindB("Debug::pkgAcquire",false)),
62 pkgAcquire::pkgAcquire(pkgAcquireStatus
*Progress
) : LockFD(-1), d(NULL
), Queues(0), Workers(0),
63 Configs(0), Log(NULL
), ToFetch(0),
64 Debug(_config
->FindB("Debug::pkgAcquire",false)),
70 void pkgAcquire::Initialize()
72 string
const Mode
= _config
->Find("Acquire::Queue-Mode","host");
73 if (strcasecmp(Mode
.c_str(),"host") == 0)
74 QueueMode
= QueueHost
;
75 if (strcasecmp(Mode
.c_str(),"access") == 0)
76 QueueMode
= QueueAccess
;
78 // chown the auth.conf file as it will be accessed by our methods
79 std::string
const SandboxUser
= _config
->Find("APT::Sandbox::User");
80 if (getuid() == 0 && SandboxUser
.empty() == false && SandboxUser
!= "root") // if we aren't root, we can't chown, so don't try it
82 struct passwd
const * const pw
= getpwnam(SandboxUser
.c_str());
83 struct group
const * const gr
= getgrnam("root");
84 if (pw
!= NULL
&& gr
!= NULL
)
86 std::string
const AuthConf
= _config
->FindFile("Dir::Etc::netrc");
87 if(AuthConf
.empty() == false && RealFileExists(AuthConf
) &&
88 chown(AuthConf
.c_str(), pw
->pw_uid
, gr
->gr_gid
) != 0)
89 _error
->WarningE("SetupAPTPartialDirectory", "chown to %s:root of file %s failed", SandboxUser
.c_str(), AuthConf
.c_str());
94 // Acquire::GetLock - lock directory and prepare for action /*{{{*/
95 static bool SetupAPTPartialDirectory(std::string
const &grand
, std::string
const &parent
)
97 std::string
const partial
= parent
+ "partial";
98 mode_t
const mode
= umask(S_IWGRP
| S_IWOTH
);
99 bool const creation_fail
= (CreateAPTDirectoryIfNeeded(grand
, partial
) == false &&
100 CreateAPTDirectoryIfNeeded(parent
, partial
) == false);
102 if (creation_fail
== true)
105 std::string
const SandboxUser
= _config
->Find("APT::Sandbox::User");
106 if (getuid() == 0 && SandboxUser
.empty() == false && SandboxUser
!= "root") // if we aren't root, we can't chown, so don't try it
108 struct passwd
const * const pw
= getpwnam(SandboxUser
.c_str());
109 struct group
const * const gr
= getgrnam("root");
110 if (pw
!= NULL
&& gr
!= NULL
)
112 // chown the partial dir
113 if(chown(partial
.c_str(), pw
->pw_uid
, gr
->gr_gid
) != 0)
114 _error
->WarningE("SetupAPTPartialDirectory", "chown to %s:root of directory %s failed", SandboxUser
.c_str(), partial
.c_str());
117 if (chmod(partial
.c_str(), 0700) != 0)
118 _error
->WarningE("SetupAPTPartialDirectory", "chmod 0700 of directory %s failed", partial
.c_str());
122 bool pkgAcquire::Setup(pkgAcquireStatus
*Progress
, string
const &Lock
)
127 string
const listDir
= _config
->FindDir("Dir::State::lists");
128 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::State"), listDir
) == false)
129 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
130 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
131 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::Cache"), archivesDir
) == false)
132 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
135 return GetLock(Lock
);
137 bool pkgAcquire::GetLock(std::string
const &Lock
)
139 if (Lock
.empty() == true)
142 // check for existence and possibly create auxiliary directories
143 string
const listDir
= _config
->FindDir("Dir::State::lists");
144 string
const archivesDir
= _config
->FindDir("Dir::Cache::Archives");
148 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::State"), listDir
) == false)
149 return _error
->Errno("Acquire", _("List directory %spartial is missing."), listDir
.c_str());
151 if (Lock
== archivesDir
)
153 if (SetupAPTPartialDirectory(_config
->FindDir("Dir::Cache"), archivesDir
) == false)
154 return _error
->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir
.c_str());
157 if (_config
->FindB("Debug::NoLocking", false) == true)
160 // Lock the directory this acquire object will work in
163 LockFD
= ::GetLock(flCombine(Lock
, "lock"));
165 return _error
->Error(_("Unable to lock directory %s"), Lock
.c_str());
170 // Acquire::~pkgAcquire - Destructor /*{{{*/
171 // ---------------------------------------------------------------------
172 /* Free our memory, clean up the queues (destroy the workers) */
173 pkgAcquire::~pkgAcquire()
182 MethodConfig
*Jnk
= Configs
;
183 Configs
= Configs
->Next
;
188 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
189 // ---------------------------------------------------------------------
191 void pkgAcquire::Shutdown()
193 while (Items
.empty() == false)
195 if (Items
[0]->Status
== Item::StatFetching
)
196 Items
[0]->Status
= Item::StatError
;
203 Queues
= Queues
->Next
;
208 // Acquire::Add - Add a new item /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This puts an item on the acquire list. This list is mainly for tracking
212 void pkgAcquire::Add(Item
*Itm
)
214 Items
.push_back(Itm
);
217 // Acquire::Remove - Remove a item /*{{{*/
218 // ---------------------------------------------------------------------
219 /* Remove an item from the acquire list. This is usually not used.. */
220 void pkgAcquire::Remove(Item
*Itm
)
224 for (ItemIterator I
= Items
.begin(); I
!= Items
.end();)
236 // Acquire::Add - Add a worker /*{{{*/
237 // ---------------------------------------------------------------------
238 /* A list of workers is kept so that the select loop can direct their FD
240 void pkgAcquire::Add(Worker
*Work
)
242 Work
->NextAcquire
= Workers
;
246 // Acquire::Remove - Remove a worker /*{{{*/
247 // ---------------------------------------------------------------------
248 /* A worker has died. This can not be done while the select loop is running
249 as it would require that RunFds could handling a changing list state and
251 void pkgAcquire::Remove(Worker
*Work
)
256 Worker
**I
= &Workers
;
260 *I
= (*I
)->NextAcquire
;
262 I
= &(*I
)->NextAcquire
;
266 // Acquire::Enqueue - Queue an URI for fetching /*{{{*/
267 // ---------------------------------------------------------------------
268 /* This is the entry point for an item. An item calls this function when
269 it is constructed which creates a queue (based on the current queue
270 mode) and puts the item in that queue. If the system is running then
271 the queue might be started. */
272 void pkgAcquire::Enqueue(ItemDesc
&Item
)
274 // Determine which queue to put the item in
275 const MethodConfig
*Config
;
276 string Name
= QueueName(Item
.URI
,Config
);
277 if (Name
.empty() == true)
280 // Find the queue structure
282 for (; I
!= 0 && I
->Name
!= Name
; I
= I
->Next
);
285 I
= new Queue(Name
,this);
293 // See if this is a local only URI
294 if (Config
->LocalOnly
== true && Item
.Owner
->Complete
== false)
295 Item
.Owner
->Local
= true;
296 Item
.Owner
->Status
= Item::StatIdle
;
298 // Queue it into the named queue
305 clog
<< "Fetching " << Item
.URI
<< endl
;
306 clog
<< " to " << Item
.Owner
->DestFile
<< endl
;
307 clog
<< " Queue is: " << Name
<< endl
;
311 // Acquire::Dequeue - Remove an item from all queues /*{{{*/
312 // ---------------------------------------------------------------------
313 /* This is called when an item is finished being fetched. It removes it
314 from all the queues */
315 void pkgAcquire::Dequeue(Item
*Itm
)
320 clog
<< "Dequeuing " << Itm
->DestFile
<< endl
;
322 for (; I
!= 0; I
= I
->Next
)
328 clog
<< "Dequeued from " << I
->Name
<< endl
;
336 // Acquire::QueueName - Return the name of the queue for this URI /*{{{*/
337 // ---------------------------------------------------------------------
338 /* The string returned depends on the configuration settings and the
339 method parameters. Given something like http://foo.org/bar it can
340 return http://foo.org or http */
341 string
pkgAcquire::QueueName(string Uri
,MethodConfig
const *&Config
)
345 Config
= GetConfig(U
.Access
);
349 /* Single-Instance methods get exactly one queue per URI. This is
350 also used for the Access queue method */
351 if (Config
->SingleInstance
== true || QueueMode
== QueueAccess
)
354 string AccessSchema
= U
.Access
+ ':';
355 string FullQueueName
;
360 // check how many queues exist already and reuse empty ones
361 for (Queue
const *I
= Queues
; I
!= 0; I
= I
->Next
)
362 if (I
->Name
.compare(0, AccessSchema
.length(), AccessSchema
) == 0)
364 if (I
->Items
== nullptr)
369 #ifdef _SC_NPROCESSORS_ONLN
370 long cpuCount
= sysconf(_SC_NPROCESSORS_ONLN
) * 2;
374 cpuCount
= _config
->FindI("Acquire::QueueHost::Limit", cpuCount
);
376 if (cpuCount
<= 0 || existing
< cpuCount
)
377 strprintf(FullQueueName
, "%s%ld", AccessSchema
.c_str(), existing
);
380 long const randomQueue
= random() % cpuCount
;
381 strprintf(FullQueueName
, "%s%ld", AccessSchema
.c_str(), randomQueue
);
385 clog
<< "Chose random queue " << FullQueueName
<< " for " << Uri
<< endl
;
388 FullQueueName
= AccessSchema
+ U
.Host
;
390 unsigned int Instances
= 0, SchemaLength
= AccessSchema
.length();
393 for (; I
!= 0; I
= I
->Next
) {
394 // if the queue already exists, re-use it
395 if (I
->Name
== FullQueueName
)
396 return FullQueueName
;
398 if (I
->Name
.compare(0, SchemaLength
, AccessSchema
) == 0)
403 clog
<< "Found " << Instances
<< " instances of " << U
.Access
<< endl
;
406 if (Instances
>= (unsigned int)_config
->FindI("Acquire::QueueHost::Limit",10))
409 return FullQueueName
;
412 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
413 // ---------------------------------------------------------------------
414 /* This locates the configuration structure for an access method. If
415 a config structure cannot be found a Worker will be created to
417 pkgAcquire::MethodConfig
*pkgAcquire::GetConfig(string Access
)
419 // Search for an existing config
421 for (Conf
= Configs
; Conf
!= 0; Conf
= Conf
->Next
)
422 if (Conf
->Access
== Access
)
425 // Create the new config class
426 Conf
= new MethodConfig
;
427 Conf
->Access
= Access
;
428 Conf
->Next
= Configs
;
431 // Create the worker to fetch the configuration
433 if (Work
.Start() == false)
436 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
437 if(_config
->FindI("Acquire::"+Access
+"::Dl-Limit",0) > 0)
438 Conf
->SingleInstance
= true;
443 // Acquire::SetFds - Deal with readable FDs /*{{{*/
444 // ---------------------------------------------------------------------
445 /* Collect FDs that have activity monitors into the fd sets */
446 void pkgAcquire::SetFds(int &Fd
,fd_set
*RSet
,fd_set
*WSet
)
448 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
450 if (I
->InReady
== true && I
->InFd
>= 0)
454 FD_SET(I
->InFd
,RSet
);
456 if (I
->OutReady
== true && I
->OutFd
>= 0)
460 FD_SET(I
->OutFd
,WSet
);
465 // Acquire::RunFds - compatibility remove on next abi/api break /*{{{*/
466 void pkgAcquire::RunFds(fd_set
*RSet
,fd_set
*WSet
)
468 RunFdsSane(RSet
, WSet
);
471 // Acquire::RunFdsSane - Deal with active FDs /*{{{*/
472 // ---------------------------------------------------------------------
473 /* Dispatch active FDs over to the proper workers. It is very important
474 that a worker never be erased while this is running! The queue class
475 should never erase a worker except during shutdown processing. */
476 bool pkgAcquire::RunFdsSane(fd_set
*RSet
,fd_set
*WSet
)
480 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
482 if (I
->InFd
>= 0 && FD_ISSET(I
->InFd
,RSet
) != 0)
483 Res
&= I
->InFdReady();
484 if (I
->OutFd
>= 0 && FD_ISSET(I
->OutFd
,WSet
) != 0)
485 Res
&= I
->OutFdReady();
491 // Acquire::Run - Run the fetch sequence /*{{{*/
492 // ---------------------------------------------------------------------
493 /* This runs the queues. It manages a select loop for all of the
494 Worker tasks. The workers interact with the queues and items to
495 manage the actual fetch. */
496 static bool IsAccessibleBySandboxUser(std::string
const &filename
, bool const ReadWrite
)
498 // you would think this is easily to answer with faccessat, right? Wrong!
499 // It e.g. gets groups wrong, so the only thing which works reliable is trying
500 // to open the file we want to open later on…
501 if (unlikely(filename
.empty()))
504 if (ReadWrite
== false)
507 // can we read a file? Note that non-existing files are "fine"
508 int const fd
= open(filename
.c_str(), O_RDONLY
| O_CLOEXEC
);
509 if (fd
== -1 && errno
== EACCES
)
516 // the file might not exist yet and even if it does we will fix permissions,
517 // so important is here just that the directory it is in allows that
518 std::string
const dirname
= flNotFile(filename
);
519 if (unlikely(dirname
.empty()))
522 char const * const filetag
= ".apt-acquire-privs-test.XXXXXX";
523 std::string
const tmpfile_tpl
= flCombine(dirname
, filetag
);
524 std::unique_ptr
<char, decltype(std::free
) *> tmpfile
{ strdup(tmpfile_tpl
.c_str()), std::free
};
525 int const fd
= mkstemp(tmpfile
.get());
526 if (fd
== -1 && errno
== EACCES
)
528 RemoveFile("IsAccessibleBySandboxUser", tmpfile
.get());
533 static void CheckDropPrivsMustBeDisabled(pkgAcquire
const &Fetcher
)
538 std::string
const SandboxUser
= _config
->Find("APT::Sandbox::User");
539 if (SandboxUser
.empty() || SandboxUser
== "root")
542 struct passwd
const * const pw
= getpwnam(SandboxUser
.c_str());
545 _error
->Warning(_("No sandbox user '%s' on the system, can not drop privileges"), SandboxUser
.c_str());
546 _config
->Set("APT::Sandbox::User", "");
550 gid_t
const old_euid
= geteuid();
551 gid_t
const old_egid
= getegid();
553 long const ngroups_max
= sysconf(_SC_NGROUPS_MAX
);
554 std::unique_ptr
<gid_t
[]> old_gidlist(new gid_t
[ngroups_max
]);
555 if (unlikely(old_gidlist
== NULL
))
557 ssize_t old_gidlist_nr
;
558 if ((old_gidlist_nr
= getgroups(ngroups_max
, old_gidlist
.get())) < 0)
560 _error
->FatalE("getgroups", "getgroups %lu failed", ngroups_max
);
564 if (setgroups(1, &pw
->pw_gid
))
565 _error
->FatalE("setgroups", "setgroups %u failed", pw
->pw_gid
);
567 if (setegid(pw
->pw_gid
) != 0)
568 _error
->FatalE("setegid", "setegid %u failed", pw
->pw_gid
);
569 if (seteuid(pw
->pw_uid
) != 0)
570 _error
->FatalE("seteuid", "seteuid %u failed", pw
->pw_uid
);
572 for (pkgAcquire::ItemCIterator I
= Fetcher
.ItemsBegin();
573 I
!= Fetcher
.ItemsEnd(); ++I
)
575 // no need to drop privileges for a complete file
576 if ((*I
)->Complete
== true || (*I
)->Status
!= pkgAcquire::Item::StatIdle
)
579 // if destination file is inaccessible all hope is lost for privilege dropping
580 if (IsAccessibleBySandboxUser((*I
)->DestFile
, true) == false)
582 _error
->WarningE("pkgAcquire::Run", _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."),
583 (*I
)->DestFile
.c_str(), SandboxUser
.c_str());
584 _config
->Set("APT::Sandbox::User", "");
588 // if its the source file (e.g. local sources) we might be lucky
589 // by dropping the dropping only for some methods.
590 URI
const source
= (*I
)->DescURI();
591 if (source
.Access
== "file" || source
.Access
== "copy")
593 std::string
const conf
= "Binary::" + source
.Access
+ "::APT::Sandbox::User";
594 if (_config
->Exists(conf
) == true)
597 if (IsAccessibleBySandboxUser(source
.Path
, false) == false)
599 _error
->NoticeE("pkgAcquire::Run", _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."),
600 source
.Path
.c_str(), SandboxUser
.c_str());
601 _config
->CndSet("Binary::file::APT::Sandbox::User", "root");
602 _config
->CndSet("Binary::copy::APT::Sandbox::User", "root");
607 if (seteuid(old_euid
) != 0)
608 _error
->FatalE("seteuid", "seteuid %u failed", old_euid
);
609 if (setegid(old_egid
) != 0)
610 _error
->FatalE("setegid", "setegid %u failed", old_egid
);
611 if (setgroups(old_gidlist_nr
, old_gidlist
.get()))
612 _error
->FatalE("setgroups", "setgroups %u failed", 0);
614 pkgAcquire::RunResult
pkgAcquire::Run(int PulseIntervall
)
616 _error
->PushToStack();
617 CheckDropPrivsMustBeDisabled(*this);
621 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
627 bool WasCancelled
= false;
629 // Run till all things have been acquired
632 tv
.tv_usec
= PulseIntervall
;
640 SetFds(Highest
,&RFds
,&WFds
);
645 Res
= select(Highest
+1,&RFds
,&WFds
,0,&tv
);
647 while (Res
< 0 && errno
== EINTR
);
651 _error
->Errno("select","Select has failed");
655 if(RunFdsSane(&RFds
,&WFds
) == false)
658 // Timeout, notify the log class
659 if (Res
== 0 || (Log
!= 0 && Log
->Update
== true))
661 tv
.tv_usec
= PulseIntervall
;
662 for (Worker
*I
= Workers
; I
!= 0; I
= I
->NextAcquire
)
664 if (Log
!= 0 && Log
->Pulse(this) == false)
675 // Shut down the acquire bits
677 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
680 // Shut down the items
681 for (ItemIterator I
= Items
.begin(); I
!= Items
.end(); ++I
)
684 bool const newError
= _error
->PendingError();
685 _error
->MergeWithStack();
693 // Acquire::Bump - Called when an item is dequeued /*{{{*/
694 // ---------------------------------------------------------------------
695 /* This routine bumps idle queues in hopes that they will be able to fetch
697 void pkgAcquire::Bump()
699 for (Queue
*I
= Queues
; I
!= 0; I
= I
->Next
)
703 // Acquire::WorkerStep - Step to the next worker /*{{{*/
704 // ---------------------------------------------------------------------
705 /* Not inlined to advoid including acquire-worker.h */
706 pkgAcquire::Worker
*pkgAcquire::WorkerStep(Worker
*I
)
708 return I
->NextAcquire
;
711 // Acquire::Clean - Cleans a directory /*{{{*/
712 // ---------------------------------------------------------------------
713 /* This is a bit simplistic, it looks at every file in the dir and sees
714 if it is part of the download set. */
715 bool pkgAcquire::Clean(string Dir
)
717 // non-existing directories are by definition clean…
718 if (DirectoryExists(Dir
) == false)
722 return _error
->Error(_("Clean of %s is not supported"), Dir
.c_str());
724 DIR *D
= opendir(Dir
.c_str());
726 return _error
->Errno("opendir",_("Unable to read %s"),Dir
.c_str());
728 string StartDir
= SafeGetCWD();
729 if (chdir(Dir
.c_str()) != 0)
732 return _error
->Errno("chdir",_("Unable to change to %s"),Dir
.c_str());
735 for (struct dirent
*Dir
= readdir(D
); Dir
!= 0; Dir
= readdir(D
))
738 if (strcmp(Dir
->d_name
,"lock") == 0 ||
739 strcmp(Dir
->d_name
,"partial") == 0 ||
740 strcmp(Dir
->d_name
,"lost+found") == 0 ||
741 strcmp(Dir
->d_name
,".") == 0 ||
742 strcmp(Dir
->d_name
,"..") == 0)
745 // Look in the get list
746 ItemCIterator I
= Items
.begin();
747 for (; I
!= Items
.end(); ++I
)
748 if (flNotDir((*I
)->DestFile
) == Dir
->d_name
)
751 // Nothing found, nuke it
752 if (I
== Items
.end())
753 RemoveFile("Clean", Dir
->d_name
);
757 if (chdir(StartDir
.c_str()) != 0)
758 return _error
->Errno("chdir",_("Unable to change to %s"),StartDir
.c_str());
762 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
763 // ---------------------------------------------------------------------
764 /* This is the total number of bytes needed */
765 APT_PURE
unsigned long long pkgAcquire::TotalNeeded()
767 return std::accumulate(ItemsBegin(), ItemsEnd(), 0llu,
768 [](unsigned long long const T
, Item
const * const I
) {
769 return T
+ I
->FileSize
;
773 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
774 // ---------------------------------------------------------------------
775 /* This is the number of bytes that is not local */
776 APT_PURE
unsigned long long pkgAcquire::FetchNeeded()
778 return std::accumulate(ItemsBegin(), ItemsEnd(), 0llu,
779 [](unsigned long long const T
, Item
const * const I
) {
780 if (I
->Local
== false)
781 return T
+ I
->FileSize
;
787 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
788 // ---------------------------------------------------------------------
789 /* This is the number of bytes that is not local */
790 APT_PURE
unsigned long long pkgAcquire::PartialPresent()
792 return std::accumulate(ItemsBegin(), ItemsEnd(), 0llu,
793 [](unsigned long long const T
, Item
const * const I
) {
794 if (I
->Local
== false)
795 return T
+ I
->PartialSize
;
801 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
802 // ---------------------------------------------------------------------
804 pkgAcquire::UriIterator
pkgAcquire::UriBegin()
806 return UriIterator(Queues
);
809 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
810 // ---------------------------------------------------------------------
812 pkgAcquire::UriIterator
pkgAcquire::UriEnd()
814 return UriIterator(0);
817 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
818 // ---------------------------------------------------------------------
820 pkgAcquire::MethodConfig::MethodConfig() : d(NULL
), Next(0), SingleInstance(false),
821 Pipeline(false), SendConfig(false), LocalOnly(false), NeedsCleanup(false),
826 // Queue::Queue - Constructor /*{{{*/
827 // ---------------------------------------------------------------------
829 pkgAcquire::Queue::Queue(string
const &name
,pkgAcquire
* const owner
) : d(NULL
), Next(0),
830 Name(name
), Items(0), Workers(0), Owner(owner
), PipeDepth(0), MaxPipeDepth(1)
834 // Queue::~Queue - Destructor /*{{{*/
835 // ---------------------------------------------------------------------
837 pkgAcquire::Queue::~Queue()
849 // Queue::Enqueue - Queue an item to the queue /*{{{*/
850 // ---------------------------------------------------------------------
852 bool pkgAcquire::Queue::Enqueue(ItemDesc
&Item
)
855 // move to the end of the queue and check for duplicates here
856 for (; *I
!= 0; I
= &(*I
)->Next
)
857 if (Item
.URI
== (*I
)->URI
)
859 if (_config
->FindB("Debug::pkgAcquire::Worker",false) == true)
860 std::cerr
<< " @ Queue: Action combined for " << Item
.URI
<< " and " << (*I
)->URI
<< std::endl
;
861 (*I
)->Owners
.push_back(Item
.Owner
);
862 Item
.Owner
->Status
= (*I
)->Owner
->Status
;
867 QItem
*Itm
= new QItem
;
872 Item
.Owner
->QueueCounter
++;
873 if (Items
->Next
== 0)
878 // Queue::Dequeue - Remove an item from the queue /*{{{*/
879 // ---------------------------------------------------------------------
880 /* We return true if we hit something */
881 bool pkgAcquire::Queue::Dequeue(Item
*Owner
)
883 if (Owner
->Status
== pkgAcquire::Item::StatFetching
)
884 return _error
->Error("Tried to dequeue a fetching object");
891 if (Owner
== (*I
)->Owner
)
895 Owner
->QueueCounter
--;
906 // Queue::Startup - Start the worker processes /*{{{*/
907 // ---------------------------------------------------------------------
908 /* It is possible for this to be called with a pre-existing set of
910 bool pkgAcquire::Queue::Startup()
915 pkgAcquire::MethodConfig
*Cnf
= Owner
->GetConfig(U
.Access
);
919 Workers
= new Worker(this,Cnf
,Owner
->Log
);
921 if (Workers
->Start() == false)
924 /* When pipelining we commit 10 items. This needs to change when we
925 added other source retry to have cycle maintain a pipeline depth
927 if (Cnf
->Pipeline
== true)
928 MaxPipeDepth
= _config
->FindI("Acquire::Max-Pipeline-Depth",10);
936 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
937 // ---------------------------------------------------------------------
938 /* If final is true then all workers are eliminated, otherwise only workers
939 that do not need cleanup are removed */
940 bool pkgAcquire::Queue::Shutdown(bool Final
)
942 // Delete all of the workers
943 pkgAcquire::Worker
**Cur
= &Workers
;
946 pkgAcquire::Worker
*Jnk
= *Cur
;
947 if (Final
== true || Jnk
->GetConf()->NeedsCleanup
== false)
949 *Cur
= Jnk
->NextQueue
;
954 Cur
= &(*Cur
)->NextQueue
;
960 // Queue::FindItem - Find a URI in the item list /*{{{*/
961 // ---------------------------------------------------------------------
963 pkgAcquire::Queue::QItem
*pkgAcquire::Queue::FindItem(string URI
,pkgAcquire::Worker
*Owner
)
965 for (QItem
*I
= Items
; I
!= 0; I
= I
->Next
)
966 if (I
->URI
== URI
&& I
->Worker
== Owner
)
971 // Queue::ItemDone - Item has been completed /*{{{*/
972 // ---------------------------------------------------------------------
973 /* The worker signals this which causes the item to be removed from the
974 queue. If this is the last queue instance then it is removed from the
976 bool pkgAcquire::Queue::ItemDone(QItem
*Itm
)
979 for (QItem::owner_iterator O
= Itm
->Owners
.begin(); O
!= Itm
->Owners
.end(); ++O
)
981 if ((*O
)->Status
== pkgAcquire::Item::StatFetching
)
982 (*O
)->Status
= pkgAcquire::Item::StatDone
;
985 if (Itm
->Owner
->QueueCounter
<= 1)
986 Owner
->Dequeue(Itm
->Owner
);
996 // Queue::Cycle - Queue new items into the method /*{{{*/
997 // ---------------------------------------------------------------------
998 /* This locates a new idle item and sends it to the worker. If pipelining
999 is enabled then it keeps the pipe full. */
1000 bool pkgAcquire::Queue::Cycle()
1002 if (Items
== 0 || Workers
== 0)
1006 return _error
->Error("Pipedepth failure");
1008 // Look for a queable item
1010 while (PipeDepth
< (signed)MaxPipeDepth
)
1012 for (; I
!= 0; I
= I
->Next
)
1013 if (I
->Owner
->Status
== pkgAcquire::Item::StatIdle
)
1016 // Nothing to do, queue is idle.
1020 I
->Worker
= Workers
;
1021 for (auto const &O
: I
->Owners
)
1022 O
->Status
= pkgAcquire::Item::StatFetching
;
1024 if (Workers
->QueueItem(I
) == false)
1031 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
1032 // ---------------------------------------------------------------------
1033 /* This is called when an item in multiple queues is dequeued */
1034 void pkgAcquire::Queue::Bump()
1039 HashStringList
pkgAcquire::Queue::QItem::GetExpectedHashes() const /*{{{*/
1041 /* each Item can have multiple owners and each owner might have different
1042 hashes, even if that is unlikely in practice and if so at least some
1043 owners will later fail. There is one situation through which is not a
1044 failure and still needs this handling: Two owners who expect the same
1045 file, but one owner only knows the SHA1 while the other only knows SHA256. */
1046 HashStringList superhsl
;
1047 for (pkgAcquire::Queue::QItem::owner_iterator O
= Owners
.begin(); O
!= Owners
.end(); ++O
)
1049 HashStringList
const hsl
= (*O
)->GetExpectedHashes();
1050 if (hsl
.usable() == false)
1052 if (superhsl
.usable() == false)
1056 // we merge both lists - if we find disagreement send no hashes
1057 HashStringList::const_iterator hs
= hsl
.begin();
1058 for (; hs
!= hsl
.end(); ++hs
)
1059 if (superhsl
.push_back(*hs
) == false)
1061 if (hs
!= hsl
.end())
1071 APT_PURE
unsigned long long pkgAcquire::Queue::QItem::GetMaximumSize() const /*{{{*/
1073 unsigned long long Maximum
= std::numeric_limits
<unsigned long long>::max();
1074 for (auto const &O
: Owners
)
1076 if (O
->FileSize
== 0)
1078 Maximum
= std::min(Maximum
, O
->FileSize
);
1080 if (Maximum
== std::numeric_limits
<unsigned long long>::max())
1085 void pkgAcquire::Queue::QItem::SyncDestinationFiles() const /*{{{*/
1087 /* ensure that the first owner has the best partial file of all and
1088 the rest have (potentially dangling) symlinks to it so that
1089 everything (like progress reporting) finds it easily */
1090 std::string superfile
= Owner
->DestFile
;
1091 off_t supersize
= 0;
1092 for (pkgAcquire::Queue::QItem::owner_iterator O
= Owners
.begin(); O
!= Owners
.end(); ++O
)
1094 if ((*O
)->DestFile
== superfile
)
1097 if (lstat((*O
)->DestFile
.c_str(),&file
) == 0)
1099 if ((file
.st_mode
& S_IFREG
) == 0)
1100 RemoveFile("SyncDestinationFiles", (*O
)->DestFile
);
1101 else if (supersize
< file
.st_size
)
1103 supersize
= file
.st_size
;
1104 RemoveFile("SyncDestinationFiles", superfile
);
1105 rename((*O
)->DestFile
.c_str(), superfile
.c_str());
1108 RemoveFile("SyncDestinationFiles", (*O
)->DestFile
);
1109 if (symlink(superfile
.c_str(), (*O
)->DestFile
.c_str()) != 0)
1111 ; // not a problem per-se and no real alternative
1117 std::string
pkgAcquire::Queue::QItem::Custom600Headers() const /*{{{*/
1119 /* The others are relatively easy to merge, but this one?
1120 Lets not merge and see how far we can run with it…
1121 Likely, nobody will ever notice as all the items will
1122 be of the same class and hence generate the same headers. */
1123 return Owner
->Custom600Headers();
1127 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
1128 // ---------------------------------------------------------------------
1130 pkgAcquireStatus::pkgAcquireStatus() : d(NULL
), Percent(-1), Update(true), MorePulses(false)
1135 // AcquireStatus::Pulse - Called periodically /*{{{*/
1136 // ---------------------------------------------------------------------
1137 /* This computes some internal state variables for the derived classes to
1138 use. It generates the current downloaded bytes and total bytes to download
1139 as well as the current CPS estimate. */
1140 bool pkgAcquireStatus::Pulse(pkgAcquire
*Owner
)
1147 // Compute the total number of bytes to fetch
1148 unsigned int Unknown
= 0;
1149 unsigned int Count
= 0;
1150 bool ExpectAdditionalItems
= false;
1151 for (pkgAcquire::ItemCIterator I
= Owner
->ItemsBegin();
1152 I
!= Owner
->ItemsEnd();
1156 if ((*I
)->Status
== pkgAcquire::Item::StatDone
)
1159 // do we expect to acquire more files than we know of yet?
1160 if ((*I
)->ExpectedAdditionalItems
> 0)
1161 ExpectAdditionalItems
= true;
1163 TotalBytes
+= (*I
)->FileSize
;
1164 if ((*I
)->Complete
== true)
1165 CurrentBytes
+= (*I
)->FileSize
;
1166 if ((*I
)->FileSize
== 0 && (*I
)->Complete
== false)
1170 // Compute the current completion
1171 unsigned long long ResumeSize
= 0;
1172 for (pkgAcquire::Worker
*I
= Owner
->WorkersBegin(); I
!= 0;
1173 I
= Owner
->WorkerStep(I
))
1175 if (I
->CurrentItem
!= 0 && I
->CurrentItem
->Owner
->Complete
== false)
1177 CurrentBytes
+= I
->CurrentSize
;
1178 ResumeSize
+= I
->ResumePoint
;
1180 // Files with unknown size always have 100% completion
1181 if (I
->CurrentItem
->Owner
->FileSize
== 0 &&
1182 I
->CurrentItem
->Owner
->Complete
== false)
1183 TotalBytes
+= I
->CurrentSize
;
1187 // Normalize the figures and account for unknown size downloads
1188 if (TotalBytes
<= 0)
1190 if (Unknown
== Count
)
1191 TotalBytes
= Unknown
;
1193 // Wha?! Is not supposed to happen.
1194 if (CurrentBytes
> TotalBytes
)
1195 CurrentBytes
= TotalBytes
;
1198 struct timeval NewTime
;
1199 gettimeofday(&NewTime
,0);
1200 if ((NewTime
.tv_sec
- Time
.tv_sec
== 6 && NewTime
.tv_usec
> Time
.tv_usec
) ||
1201 NewTime
.tv_sec
- Time
.tv_sec
> 6)
1203 double Delta
= NewTime
.tv_sec
- Time
.tv_sec
+
1204 (NewTime
.tv_usec
- Time
.tv_usec
)/1000000.0;
1206 // Compute the CPS value
1210 CurrentCPS
= ((CurrentBytes
- ResumeSize
) - LastBytes
)/Delta
;
1211 LastBytes
= CurrentBytes
- ResumeSize
;
1212 ElapsedTime
= (unsigned long long)Delta
;
1216 double const OldPercent
= Percent
;
1217 // calculate the percentage, if we have too little data assume 1%
1218 if (ExpectAdditionalItems
)
1221 // use both files and bytes because bytes can be unreliable
1222 Percent
= (0.8 * (CurrentBytes
/float(TotalBytes
)*100.0) +
1223 0.2 * (CurrentItems
/float(TotalItems
)*100.0));
1226 if (_config
->FindB("Debug::acquire::progress", false) == true)
1230 << std::setw(5) << std::setprecision(4) << std::showpoint
<< Percent
1233 << SizeToStr(CurrentBytes
) << " / " << SizeToStr(TotalBytes
)
1235 << CurrentItems
<< " / " << TotalItems
1239 double const DiffPercent
= Percent
- OldPercent
;
1240 if (DiffPercent
< 0.001 && _config
->FindB("Acquire::Progress::Diffpercent", false) == true)
1243 int fd
= _config
->FindI("APT::Status-Fd",-1);
1246 ostringstream status
;
1249 long i
= CurrentItems
< TotalItems
? CurrentItems
+ 1 : CurrentItems
;
1250 unsigned long long ETA
= 0;
1252 ETA
= (TotalBytes
- CurrentBytes
) / CurrentCPS
;
1254 // only show the ETA if it makes sense
1255 if (ETA
> 0 && ETA
< 172800 /* two days */ )
1256 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li (%s remaining)"), i
, TotalItems
, TimeToStr(ETA
).c_str());
1258 snprintf(msg
,sizeof(msg
), _("Retrieving file %li of %li"), i
, TotalItems
);
1260 // build the status str
1261 status
<< "dlstatus:" << i
1262 << ":" << std::setprecision(3) << Percent
1266 std::string
const dlstatus
= status
.str();
1267 FileFd::Write(fd
, dlstatus
.c_str(), dlstatus
.size());
1273 // AcquireStatus::Start - Called when the download is started /*{{{*/
1274 // ---------------------------------------------------------------------
1275 /* We just reset the counters */
1276 void pkgAcquireStatus::Start()
1278 gettimeofday(&Time
,0);
1279 gettimeofday(&StartTime
,0);
1290 // AcquireStatus::Stop - Finished downloading /*{{{*/
1291 // ---------------------------------------------------------------------
1292 /* This accurately computes the elapsed time and the total overall CPS. */
1293 void pkgAcquireStatus::Stop()
1295 // Compute the CPS and elapsed time
1296 struct timeval NewTime
;
1297 gettimeofday(&NewTime
,0);
1299 double Delta
= NewTime
.tv_sec
- StartTime
.tv_sec
+
1300 (NewTime
.tv_usec
- StartTime
.tv_usec
)/1000000.0;
1302 // Compute the CPS value
1306 CurrentCPS
= FetchedBytes
/Delta
;
1307 LastBytes
= CurrentBytes
;
1308 ElapsedTime
= (unsigned long long)Delta
;
1311 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
1312 // ---------------------------------------------------------------------
1313 /* This is used to get accurate final transfer rate reporting. */
1314 void pkgAcquireStatus::Fetched(unsigned long long Size
,unsigned long long Resume
)
1316 FetchedBytes
+= Size
- Resume
;
1320 pkgAcquire::UriIterator::UriIterator(pkgAcquire::Queue
*Q
) : d(NULL
), CurQ(Q
), CurItem(0)
1322 while (CurItem
== 0 && CurQ
!= 0)
1324 CurItem
= CurQ
->Items
;
1329 APT_CONST
pkgAcquire::UriIterator::~UriIterator() {}
1330 APT_CONST
pkgAcquire::MethodConfig::~MethodConfig() {}
1331 APT_CONST
pkgAcquireStatus::~pkgAcquireStatus() {}