]> git.saurik.com Git - apt.git/blob - apt-pkg/acquire.cc
Deal with killed acquire methods properly instead of hanging
[apt.git] / apt-pkg / acquire.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: acquire.cc,v 1.50 2004/03/17 05:17:11 mdz Exp $
4 /* ######################################################################
5
6 Acquire - File Acquiration
7
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.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #include <config.h>
17
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>
25
26 #include <algorithm>
27 #include <numeric>
28 #include <string>
29 #include <vector>
30 #include <iostream>
31 #include <sstream>
32 #include <iomanip>
33 #include <memory>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <fcntl.h>
40 #include <pwd.h>
41 #include <grp.h>
42 #include <dirent.h>
43 #include <sys/time.h>
44 #include <sys/select.h>
45 #include <errno.h>
46 #include <sys/stat.h>
47
48 #include <apti18n.h>
49 /*}}}*/
50
51 using namespace std;
52
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)),
58 Running(false)
59 {
60 Initialize();
61 }
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)),
65 Running(false)
66 {
67 Initialize();
68 SetLog(Progress);
69 }
70 void pkgAcquire::Initialize()
71 {
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;
77
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
81 {
82 struct passwd const * const pw = getpwnam(SandboxUser.c_str());
83 struct group const * const gr = getgrnam("root");
84 if (pw != NULL && gr != NULL)
85 {
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());
90 }
91 }
92 }
93 /*}}}*/
94 // Acquire::GetLock - lock directory and prepare for action /*{{{*/
95 static bool SetupAPTPartialDirectory(std::string const &grand, std::string const &parent)
96 {
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);
101 umask(mode);
102 if (creation_fail == true)
103 return false;
104
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
107 {
108 struct passwd const * const pw = getpwnam(SandboxUser.c_str());
109 struct group const * const gr = getgrnam("root");
110 if (pw != NULL && gr != NULL)
111 {
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());
115 }
116 }
117 if (chmod(partial.c_str(), 0700) != 0)
118 _error->WarningE("SetupAPTPartialDirectory", "chmod 0700 of directory %s failed", partial.c_str());
119
120 return true;
121 }
122 bool pkgAcquire::Setup(pkgAcquireStatus *Progress, string const &Lock)
123 {
124 Log = Progress;
125 if (Lock.empty())
126 {
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());
133 return true;
134 }
135 return GetLock(Lock);
136 }
137 bool pkgAcquire::GetLock(std::string const &Lock)
138 {
139 if (Lock.empty() == true)
140 return false;
141
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");
145
146 if (Lock == listDir)
147 {
148 if (SetupAPTPartialDirectory(_config->FindDir("Dir::State"), listDir) == false)
149 return _error->Errno("Acquire", _("List directory %spartial is missing."), listDir.c_str());
150 }
151 if (Lock == archivesDir)
152 {
153 if (SetupAPTPartialDirectory(_config->FindDir("Dir::Cache"), archivesDir) == false)
154 return _error->Errno("Acquire", _("Archives directory %spartial is missing."), archivesDir.c_str());
155 }
156
157 if (_config->FindB("Debug::NoLocking", false) == true)
158 return true;
159
160 // Lock the directory this acquire object will work in
161 if (LockFD != -1)
162 close(LockFD);
163 LockFD = ::GetLock(flCombine(Lock, "lock"));
164 if (LockFD == -1)
165 return _error->Error(_("Unable to lock directory %s"), Lock.c_str());
166
167 return true;
168 }
169 /*}}}*/
170 // Acquire::~pkgAcquire - Destructor /*{{{*/
171 // ---------------------------------------------------------------------
172 /* Free our memory, clean up the queues (destroy the workers) */
173 pkgAcquire::~pkgAcquire()
174 {
175 Shutdown();
176
177 if (LockFD != -1)
178 close(LockFD);
179
180 while (Configs != 0)
181 {
182 MethodConfig *Jnk = Configs;
183 Configs = Configs->Next;
184 delete Jnk;
185 }
186 }
187 /*}}}*/
188 // Acquire::Shutdown - Clean out the acquire object /*{{{*/
189 // ---------------------------------------------------------------------
190 /* */
191 void pkgAcquire::Shutdown()
192 {
193 while (Items.empty() == false)
194 {
195 if (Items[0]->Status == Item::StatFetching)
196 Items[0]->Status = Item::StatError;
197 delete Items[0];
198 }
199
200 while (Queues != 0)
201 {
202 Queue *Jnk = Queues;
203 Queues = Queues->Next;
204 delete Jnk;
205 }
206 }
207 /*}}}*/
208 // Acquire::Add - Add a new item /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This puts an item on the acquire list. This list is mainly for tracking
211 item status */
212 void pkgAcquire::Add(Item *Itm)
213 {
214 Items.push_back(Itm);
215 }
216 /*}}}*/
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)
221 {
222 Dequeue(Itm);
223
224 for (ItemIterator I = Items.begin(); I != Items.end();)
225 {
226 if (*I == Itm)
227 {
228 Items.erase(I);
229 I = Items.begin();
230 }
231 else
232 ++I;
233 }
234 }
235 /*}}}*/
236 // Acquire::Add - Add a worker /*{{{*/
237 // ---------------------------------------------------------------------
238 /* A list of workers is kept so that the select loop can direct their FD
239 usage. */
240 void pkgAcquire::Add(Worker *Work)
241 {
242 Work->NextAcquire = Workers;
243 Workers = Work;
244 }
245 /*}}}*/
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
250 it can't.. */
251 void pkgAcquire::Remove(Worker *Work)
252 {
253 if (Running == true)
254 abort();
255
256 Worker **I = &Workers;
257 for (; *I != 0;)
258 {
259 if (*I == Work)
260 *I = (*I)->NextAcquire;
261 else
262 I = &(*I)->NextAcquire;
263 }
264 }
265 /*}}}*/
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)
273 {
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)
278 return;
279
280 // Find the queue structure
281 Queue *I = Queues;
282 for (; I != 0 && I->Name != Name; I = I->Next);
283 if (I == 0)
284 {
285 I = new Queue(Name,this);
286 I->Next = Queues;
287 Queues = I;
288
289 if (Running == true)
290 I->Startup();
291 }
292
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;
297
298 // Queue it into the named queue
299 if(I->Enqueue(Item))
300 ToFetch++;
301
302 // Some trace stuff
303 if (Debug == true)
304 {
305 clog << "Fetching " << Item.URI << endl;
306 clog << " to " << Item.Owner->DestFile << endl;
307 clog << " Queue is: " << Name << endl;
308 }
309 }
310 /*}}}*/
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)
316 {
317 Queue *I = Queues;
318 bool Res = false;
319 if (Debug == true)
320 clog << "Dequeuing " << Itm->DestFile << endl;
321
322 for (; I != 0; I = I->Next)
323 {
324 if (I->Dequeue(Itm))
325 {
326 Res = true;
327 if (Debug == true)
328 clog << "Dequeued from " << I->Name << endl;
329 }
330 }
331
332 if (Res == true)
333 ToFetch--;
334 }
335 /*}}}*/
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)
342 {
343 URI U(Uri);
344
345 Config = GetConfig(U.Access);
346 if (Config == 0)
347 return string();
348
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)
352 return U.Access;
353
354 string AccessSchema = U.Access + ':',
355 FullQueueName = AccessSchema + U.Host;
356 unsigned int Instances = 0, SchemaLength = AccessSchema.length();
357
358 Queue *I = Queues;
359 for (; I != 0; I = I->Next) {
360 // if the queue already exists, re-use it
361 if (I->Name == FullQueueName)
362 return FullQueueName;
363
364 if (I->Name.compare(0, SchemaLength, AccessSchema) == 0)
365 Instances++;
366 }
367
368 if (Debug) {
369 clog << "Found " << Instances << " instances of " << U.Access << endl;
370 }
371
372 if (Instances >= (unsigned int)_config->FindI("Acquire::QueueHost::Limit",10))
373 return U.Access;
374
375 return FullQueueName;
376 }
377 /*}}}*/
378 // Acquire::GetConfig - Fetch the configuration information /*{{{*/
379 // ---------------------------------------------------------------------
380 /* This locates the configuration structure for an access method. If
381 a config structure cannot be found a Worker will be created to
382 retrieve it */
383 pkgAcquire::MethodConfig *pkgAcquire::GetConfig(string Access)
384 {
385 // Search for an existing config
386 MethodConfig *Conf;
387 for (Conf = Configs; Conf != 0; Conf = Conf->Next)
388 if (Conf->Access == Access)
389 return Conf;
390
391 // Create the new config class
392 Conf = new MethodConfig;
393 Conf->Access = Access;
394 Conf->Next = Configs;
395 Configs = Conf;
396
397 // Create the worker to fetch the configuration
398 Worker Work(Conf);
399 if (Work.Start() == false)
400 return 0;
401
402 /* if a method uses DownloadLimit, we switch to SingleInstance mode */
403 if(_config->FindI("Acquire::"+Access+"::Dl-Limit",0) > 0)
404 Conf->SingleInstance = true;
405
406 return Conf;
407 }
408 /*}}}*/
409 // Acquire::SetFds - Deal with readable FDs /*{{{*/
410 // ---------------------------------------------------------------------
411 /* Collect FDs that have activity monitors into the fd sets */
412 void pkgAcquire::SetFds(int &Fd,fd_set *RSet,fd_set *WSet)
413 {
414 for (Worker *I = Workers; I != 0; I = I->NextAcquire)
415 {
416 if (I->InReady == true && I->InFd >= 0)
417 {
418 if (Fd < I->InFd)
419 Fd = I->InFd;
420 FD_SET(I->InFd,RSet);
421 }
422 if (I->OutReady == true && I->OutFd >= 0)
423 {
424 if (Fd < I->OutFd)
425 Fd = I->OutFd;
426 FD_SET(I->OutFd,WSet);
427 }
428 }
429 }
430 /*}}}*/
431 // Acquire::RunFds - compatibility remove on next abi/api break /*{{{*/
432 void pkgAcquire::RunFds(fd_set *RSet,fd_set *WSet)
433 {
434 RunFdsSane(RSet, WSet);
435 };
436 /*}}}*/
437 // Acquire::RunFdsSane - Deal with active FDs /*{{{*/
438 // ---------------------------------------------------------------------
439 /* Dispatch active FDs over to the proper workers. It is very important
440 that a worker never be erased while this is running! The queue class
441 should never erase a worker except during shutdown processing. */
442 bool pkgAcquire::RunFdsSane(fd_set *RSet,fd_set *WSet)
443 {
444 bool Res = true;
445
446 for (Worker *I = Workers; I != 0; I = I->NextAcquire)
447 {
448 if (I->InFd >= 0 && FD_ISSET(I->InFd,RSet) != 0)
449 Res &= I->InFdReady();
450 if (I->OutFd >= 0 && FD_ISSET(I->OutFd,WSet) != 0)
451 Res &= I->OutFdReady();
452 }
453
454 return Res;
455 }
456 /*}}}*/
457 // Acquire::Run - Run the fetch sequence /*{{{*/
458 // ---------------------------------------------------------------------
459 /* This runs the queues. It manages a select loop for all of the
460 Worker tasks. The workers interact with the queues and items to
461 manage the actual fetch. */
462 static bool IsAccessibleBySandboxUser(std::string const &filename, bool const ReadWrite)
463 {
464 // you would think this is easily to answer with faccessat, right? Wrong!
465 // It e.g. gets groups wrong, so the only thing which works reliable is trying
466 // to open the file we want to open later on…
467 if (unlikely(filename.empty()))
468 return true;
469
470 if (ReadWrite == false)
471 {
472 errno = 0;
473 // can we read a file? Note that non-existing files are "fine"
474 int const fd = open(filename.c_str(), O_RDONLY | O_CLOEXEC);
475 if (fd == -1 && errno == EACCES)
476 return false;
477 close(fd);
478 return true;
479 }
480 else
481 {
482 // the file might not exist yet and even if it does we will fix permissions,
483 // so important is here just that the directory it is in allows that
484 std::string const dirname = flNotFile(filename);
485 if (unlikely(dirname.empty()))
486 return true;
487
488 char const * const filetag = ".apt-acquire-privs-test.XXXXXX";
489 std::string const tmpfile_tpl = flCombine(dirname, filetag);
490 std::unique_ptr<char, decltype(std::free) *> tmpfile { strdup(tmpfile_tpl.c_str()), std::free };
491 int const fd = mkstemp(tmpfile.get());
492 if (fd == -1 && errno == EACCES)
493 return false;
494 RemoveFile("IsAccessibleBySandboxUser", tmpfile.get());
495 close(fd);
496 return true;
497 }
498 }
499 static void CheckDropPrivsMustBeDisabled(pkgAcquire const &Fetcher)
500 {
501 if(getuid() != 0)
502 return;
503
504 std::string const SandboxUser = _config->Find("APT::Sandbox::User");
505 if (SandboxUser.empty() || SandboxUser == "root")
506 return;
507
508 struct passwd const * const pw = getpwnam(SandboxUser.c_str());
509 if (pw == NULL)
510 return;
511
512 gid_t const old_euid = geteuid();
513 gid_t const old_egid = getegid();
514
515 long const ngroups_max = sysconf(_SC_NGROUPS_MAX);
516 std::unique_ptr<gid_t[]> old_gidlist(new gid_t[ngroups_max]);
517 if (unlikely(old_gidlist == NULL))
518 return;
519 ssize_t old_gidlist_nr;
520 if ((old_gidlist_nr = getgroups(ngroups_max, old_gidlist.get())) < 0)
521 {
522 _error->FatalE("getgroups", "getgroups %lu failed", ngroups_max);
523 old_gidlist[0] = 0;
524 old_gidlist_nr = 1;
525 }
526 if (setgroups(1, &pw->pw_gid))
527 _error->FatalE("setgroups", "setgroups %u failed", pw->pw_gid);
528
529 if (setegid(pw->pw_gid) != 0)
530 _error->FatalE("setegid", "setegid %u failed", pw->pw_gid);
531 if (seteuid(pw->pw_uid) != 0)
532 _error->FatalE("seteuid", "seteuid %u failed", pw->pw_uid);
533
534 for (pkgAcquire::ItemCIterator I = Fetcher.ItemsBegin();
535 I != Fetcher.ItemsEnd(); ++I)
536 {
537 // no need to drop privileges for a complete file
538 if ((*I)->Complete == true)
539 continue;
540
541 // if destination file is inaccessible all hope is lost for privilege dropping
542 if (IsAccessibleBySandboxUser((*I)->DestFile, true) == false)
543 {
544 _error->WarningE("pkgAcquire::Run", _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."),
545 (*I)->DestFile.c_str(), SandboxUser.c_str());
546 _config->Set("APT::Sandbox::User", "");
547 break;
548 }
549
550 // if its the source file (e.g. local sources) we might be lucky
551 // by dropping the dropping only for some methods.
552 URI const source = (*I)->DescURI();
553 if (source.Access == "file" || source.Access == "copy")
554 {
555 std::string const conf = "Binary::" + source.Access + "::APT::Sandbox::User";
556 if (_config->Exists(conf) == true)
557 continue;
558
559 if (IsAccessibleBySandboxUser(source.Path, false) == false)
560 {
561 _error->NoticeE("pkgAcquire::Run", _("Can't drop privileges for downloading as file '%s' couldn't be accessed by user '%s'."),
562 source.Path.c_str(), SandboxUser.c_str());
563 _config->CndSet("Binary::file::APT::Sandbox::User", "root");
564 _config->CndSet("Binary::copy::APT::Sandbox::User", "root");
565 }
566 }
567 }
568
569 if (seteuid(old_euid) != 0)
570 _error->FatalE("seteuid", "seteuid %u failed", old_euid);
571 if (setegid(old_egid) != 0)
572 _error->FatalE("setegid", "setegid %u failed", old_egid);
573 if (setgroups(old_gidlist_nr, old_gidlist.get()))
574 _error->FatalE("setgroups", "setgroups %u failed", 0);
575 }
576 pkgAcquire::RunResult pkgAcquire::Run(int PulseIntervall)
577 {
578 _error->PushToStack();
579 CheckDropPrivsMustBeDisabled(*this);
580
581 Running = true;
582
583 for (Queue *I = Queues; I != 0; I = I->Next)
584 I->Startup();
585
586 if (Log != 0)
587 Log->Start();
588
589 bool WasCancelled = false;
590
591 // Run till all things have been acquired
592 struct timeval tv;
593 tv.tv_sec = 0;
594 tv.tv_usec = PulseIntervall;
595 while (ToFetch > 0)
596 {
597 fd_set RFds;
598 fd_set WFds;
599 int Highest = 0;
600 FD_ZERO(&RFds);
601 FD_ZERO(&WFds);
602 SetFds(Highest,&RFds,&WFds);
603
604 int Res;
605 do
606 {
607 Res = select(Highest+1,&RFds,&WFds,0,&tv);
608 }
609 while (Res < 0 && errno == EINTR);
610
611 if (Res < 0)
612 {
613 _error->Errno("select","Select has failed");
614 break;
615 }
616
617 if(RunFdsSane(&RFds,&WFds) == false)
618 break;
619
620 // Timeout, notify the log class
621 if (Res == 0 || (Log != 0 && Log->Update == true))
622 {
623 tv.tv_usec = PulseIntervall;
624 for (Worker *I = Workers; I != 0; I = I->NextAcquire)
625 I->Pulse();
626 if (Log != 0 && Log->Pulse(this) == false)
627 {
628 WasCancelled = true;
629 break;
630 }
631 }
632 }
633
634 if (Log != 0)
635 Log->Stop();
636
637 // Shut down the acquire bits
638 Running = false;
639 for (Queue *I = Queues; I != 0; I = I->Next)
640 I->Shutdown(false);
641
642 // Shut down the items
643 for (ItemIterator I = Items.begin(); I != Items.end(); ++I)
644 (*I)->Finished();
645
646 bool const newError = _error->PendingError();
647 _error->MergeWithStack();
648 if (newError)
649 return Failed;
650 if (WasCancelled)
651 return Cancelled;
652 return Continue;
653 }
654 /*}}}*/
655 // Acquire::Bump - Called when an item is dequeued /*{{{*/
656 // ---------------------------------------------------------------------
657 /* This routine bumps idle queues in hopes that they will be able to fetch
658 the dequeued item */
659 void pkgAcquire::Bump()
660 {
661 for (Queue *I = Queues; I != 0; I = I->Next)
662 I->Bump();
663 }
664 /*}}}*/
665 // Acquire::WorkerStep - Step to the next worker /*{{{*/
666 // ---------------------------------------------------------------------
667 /* Not inlined to advoid including acquire-worker.h */
668 pkgAcquire::Worker *pkgAcquire::WorkerStep(Worker *I)
669 {
670 return I->NextAcquire;
671 }
672 /*}}}*/
673 // Acquire::Clean - Cleans a directory /*{{{*/
674 // ---------------------------------------------------------------------
675 /* This is a bit simplistic, it looks at every file in the dir and sees
676 if it is part of the download set. */
677 bool pkgAcquire::Clean(string Dir)
678 {
679 // non-existing directories are by definition clean…
680 if (DirectoryExists(Dir) == false)
681 return true;
682
683 if(Dir == "/")
684 return _error->Error(_("Clean of %s is not supported"), Dir.c_str());
685
686 DIR *D = opendir(Dir.c_str());
687 if (D == 0)
688 return _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
689
690 string StartDir = SafeGetCWD();
691 if (chdir(Dir.c_str()) != 0)
692 {
693 closedir(D);
694 return _error->Errno("chdir",_("Unable to change to %s"),Dir.c_str());
695 }
696
697 for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
698 {
699 // Skip some files..
700 if (strcmp(Dir->d_name,"lock") == 0 ||
701 strcmp(Dir->d_name,"partial") == 0 ||
702 strcmp(Dir->d_name,"lost+found") == 0 ||
703 strcmp(Dir->d_name,".") == 0 ||
704 strcmp(Dir->d_name,"..") == 0)
705 continue;
706
707 // Look in the get list
708 ItemCIterator I = Items.begin();
709 for (; I != Items.end(); ++I)
710 if (flNotDir((*I)->DestFile) == Dir->d_name)
711 break;
712
713 // Nothing found, nuke it
714 if (I == Items.end())
715 RemoveFile("Clean", Dir->d_name);
716 };
717
718 closedir(D);
719 if (chdir(StartDir.c_str()) != 0)
720 return _error->Errno("chdir",_("Unable to change to %s"),StartDir.c_str());
721 return true;
722 }
723 /*}}}*/
724 // Acquire::TotalNeeded - Number of bytes to fetch /*{{{*/
725 // ---------------------------------------------------------------------
726 /* This is the total number of bytes needed */
727 APT_PURE unsigned long long pkgAcquire::TotalNeeded()
728 {
729 return std::accumulate(ItemsBegin(), ItemsEnd(), 0,
730 [](unsigned long long const T, Item const * const I) {
731 return T + I->FileSize;
732 });
733 }
734 /*}}}*/
735 // Acquire::FetchNeeded - Number of bytes needed to get /*{{{*/
736 // ---------------------------------------------------------------------
737 /* This is the number of bytes that is not local */
738 APT_PURE unsigned long long pkgAcquire::FetchNeeded()
739 {
740 return std::accumulate(ItemsBegin(), ItemsEnd(), 0,
741 [](unsigned long long const T, Item const * const I) {
742 if (I->Local == false)
743 return T + I->FileSize;
744 else
745 return T;
746 });
747 }
748 /*}}}*/
749 // Acquire::PartialPresent - Number of partial bytes we already have /*{{{*/
750 // ---------------------------------------------------------------------
751 /* This is the number of bytes that is not local */
752 APT_PURE unsigned long long pkgAcquire::PartialPresent()
753 {
754 return std::accumulate(ItemsBegin(), ItemsEnd(), 0,
755 [](unsigned long long const T, Item const * const I) {
756 if (I->Local == false)
757 return T + I->PartialSize;
758 else
759 return T;
760 });
761 }
762 /*}}}*/
763 // Acquire::UriBegin - Start iterator for the uri list /*{{{*/
764 // ---------------------------------------------------------------------
765 /* */
766 pkgAcquire::UriIterator pkgAcquire::UriBegin()
767 {
768 return UriIterator(Queues);
769 }
770 /*}}}*/
771 // Acquire::UriEnd - End iterator for the uri list /*{{{*/
772 // ---------------------------------------------------------------------
773 /* */
774 pkgAcquire::UriIterator pkgAcquire::UriEnd()
775 {
776 return UriIterator(0);
777 }
778 /*}}}*/
779 // Acquire::MethodConfig::MethodConfig - Constructor /*{{{*/
780 // ---------------------------------------------------------------------
781 /* */
782 pkgAcquire::MethodConfig::MethodConfig() : d(NULL), Next(0), SingleInstance(false),
783 Pipeline(false), SendConfig(false), LocalOnly(false), NeedsCleanup(false),
784 Removable(false)
785 {
786 }
787 /*}}}*/
788 // Queue::Queue - Constructor /*{{{*/
789 // ---------------------------------------------------------------------
790 /* */
791 pkgAcquire::Queue::Queue(string const &name,pkgAcquire * const owner) : d(NULL), Next(0),
792 Name(name), Items(0), Workers(0), Owner(owner), PipeDepth(0), MaxPipeDepth(1)
793 {
794 }
795 /*}}}*/
796 // Queue::~Queue - Destructor /*{{{*/
797 // ---------------------------------------------------------------------
798 /* */
799 pkgAcquire::Queue::~Queue()
800 {
801 Shutdown(true);
802
803 while (Items != 0)
804 {
805 QItem *Jnk = Items;
806 Items = Items->Next;
807 delete Jnk;
808 }
809 }
810 /*}}}*/
811 // Queue::Enqueue - Queue an item to the queue /*{{{*/
812 // ---------------------------------------------------------------------
813 /* */
814 bool pkgAcquire::Queue::Enqueue(ItemDesc &Item)
815 {
816 QItem **I = &Items;
817 // move to the end of the queue and check for duplicates here
818 HashStringList const hsl = Item.Owner->GetExpectedHashes();
819 for (; *I != 0; I = &(*I)->Next)
820 if (Item.URI == (*I)->URI || hsl == (*I)->Owner->GetExpectedHashes())
821 {
822 if (_config->FindB("Debug::pkgAcquire::Worker",false) == true)
823 std::cerr << " @ Queue: Action combined for " << Item.URI << " and " << (*I)->URI << std::endl;
824 (*I)->Owners.push_back(Item.Owner);
825 Item.Owner->Status = (*I)->Owner->Status;
826 return false;
827 }
828
829 // Create a new item
830 QItem *Itm = new QItem;
831 *Itm = Item;
832 Itm->Next = 0;
833 *I = Itm;
834
835 Item.Owner->QueueCounter++;
836 if (Items->Next == 0)
837 Cycle();
838 return true;
839 }
840 /*}}}*/
841 // Queue::Dequeue - Remove an item from the queue /*{{{*/
842 // ---------------------------------------------------------------------
843 /* We return true if we hit something */
844 bool pkgAcquire::Queue::Dequeue(Item *Owner)
845 {
846 if (Owner->Status == pkgAcquire::Item::StatFetching)
847 return _error->Error("Tried to dequeue a fetching object");
848
849 bool Res = false;
850
851 QItem **I = &Items;
852 for (; *I != 0;)
853 {
854 if (Owner == (*I)->Owner)
855 {
856 QItem *Jnk= *I;
857 *I = (*I)->Next;
858 Owner->QueueCounter--;
859 delete Jnk;
860 Res = true;
861 }
862 else
863 I = &(*I)->Next;
864 }
865
866 return Res;
867 }
868 /*}}}*/
869 // Queue::Startup - Start the worker processes /*{{{*/
870 // ---------------------------------------------------------------------
871 /* It is possible for this to be called with a pre-existing set of
872 workers. */
873 bool pkgAcquire::Queue::Startup()
874 {
875 if (Workers == 0)
876 {
877 URI U(Name);
878 pkgAcquire::MethodConfig *Cnf = Owner->GetConfig(U.Access);
879 if (Cnf == 0)
880 return false;
881
882 Workers = new Worker(this,Cnf,Owner->Log);
883 Owner->Add(Workers);
884 if (Workers->Start() == false)
885 return false;
886
887 /* When pipelining we commit 10 items. This needs to change when we
888 added other source retry to have cycle maintain a pipeline depth
889 on its own. */
890 if (Cnf->Pipeline == true)
891 MaxPipeDepth = _config->FindI("Acquire::Max-Pipeline-Depth",10);
892 else
893 MaxPipeDepth = 1;
894 }
895
896 return Cycle();
897 }
898 /*}}}*/
899 // Queue::Shutdown - Shutdown the worker processes /*{{{*/
900 // ---------------------------------------------------------------------
901 /* If final is true then all workers are eliminated, otherwise only workers
902 that do not need cleanup are removed */
903 bool pkgAcquire::Queue::Shutdown(bool Final)
904 {
905 // Delete all of the workers
906 pkgAcquire::Worker **Cur = &Workers;
907 while (*Cur != 0)
908 {
909 pkgAcquire::Worker *Jnk = *Cur;
910 if (Final == true || Jnk->GetConf()->NeedsCleanup == false)
911 {
912 *Cur = Jnk->NextQueue;
913 Owner->Remove(Jnk);
914 delete Jnk;
915 }
916 else
917 Cur = &(*Cur)->NextQueue;
918 }
919
920 return true;
921 }
922 /*}}}*/
923 // Queue::FindItem - Find a URI in the item list /*{{{*/
924 // ---------------------------------------------------------------------
925 /* */
926 pkgAcquire::Queue::QItem *pkgAcquire::Queue::FindItem(string URI,pkgAcquire::Worker *Owner)
927 {
928 for (QItem *I = Items; I != 0; I = I->Next)
929 if (I->URI == URI && I->Worker == Owner)
930 return I;
931 return 0;
932 }
933 /*}}}*/
934 // Queue::ItemDone - Item has been completed /*{{{*/
935 // ---------------------------------------------------------------------
936 /* The worker signals this which causes the item to be removed from the
937 queue. If this is the last queue instance then it is removed from the
938 main queue too.*/
939 bool pkgAcquire::Queue::ItemDone(QItem *Itm)
940 {
941 PipeDepth--;
942 for (QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
943 {
944 if ((*O)->Status == pkgAcquire::Item::StatFetching)
945 (*O)->Status = pkgAcquire::Item::StatDone;
946 }
947
948 if (Itm->Owner->QueueCounter <= 1)
949 Owner->Dequeue(Itm->Owner);
950 else
951 {
952 Dequeue(Itm->Owner);
953 Owner->Bump();
954 }
955
956 return Cycle();
957 }
958 /*}}}*/
959 // Queue::Cycle - Queue new items into the method /*{{{*/
960 // ---------------------------------------------------------------------
961 /* This locates a new idle item and sends it to the worker. If pipelining
962 is enabled then it keeps the pipe full. */
963 bool pkgAcquire::Queue::Cycle()
964 {
965 if (Items == 0 || Workers == 0)
966 return true;
967
968 if (PipeDepth < 0)
969 return _error->Error("Pipedepth failure");
970
971 // Look for a queable item
972 QItem *I = Items;
973 while (PipeDepth < (signed)MaxPipeDepth)
974 {
975 for (; I != 0; I = I->Next)
976 if (I->Owner->Status == pkgAcquire::Item::StatIdle)
977 break;
978
979 // Nothing to do, queue is idle.
980 if (I == 0)
981 return true;
982
983 I->Worker = Workers;
984 for (auto const &O: I->Owners)
985 O->Status = pkgAcquire::Item::StatFetching;
986 PipeDepth++;
987 if (Workers->QueueItem(I) == false)
988 return false;
989 }
990
991 return true;
992 }
993 /*}}}*/
994 // Queue::Bump - Fetch any pending objects if we are idle /*{{{*/
995 // ---------------------------------------------------------------------
996 /* This is called when an item in multiple queues is dequeued */
997 void pkgAcquire::Queue::Bump()
998 {
999 Cycle();
1000 }
1001 /*}}}*/
1002 HashStringList pkgAcquire::Queue::QItem::GetExpectedHashes() const /*{{{*/
1003 {
1004 /* each Item can have multiple owners and each owner might have different
1005 hashes, even if that is unlikely in practice and if so at least some
1006 owners will later fail. There is one situation through which is not a
1007 failure and still needs this handling: Two owners who expect the same
1008 file, but one owner only knows the SHA1 while the other only knows SHA256. */
1009 HashStringList superhsl;
1010 for (pkgAcquire::Queue::QItem::owner_iterator O = Owners.begin(); O != Owners.end(); ++O)
1011 {
1012 HashStringList const hsl = (*O)->GetExpectedHashes();
1013 if (hsl.usable() == false)
1014 continue;
1015 if (superhsl.usable() == false)
1016 superhsl = hsl;
1017 else
1018 {
1019 // we merge both lists - if we find disagreement send no hashes
1020 HashStringList::const_iterator hs = hsl.begin();
1021 for (; hs != hsl.end(); ++hs)
1022 if (superhsl.push_back(*hs) == false)
1023 break;
1024 if (hs != hsl.end())
1025 {
1026 superhsl.clear();
1027 break;
1028 }
1029 }
1030 }
1031 return superhsl;
1032 }
1033 /*}}}*/
1034 APT_PURE unsigned long long pkgAcquire::Queue::QItem::GetMaximumSize() const /*{{{*/
1035 {
1036 unsigned long long Maximum = std::numeric_limits<unsigned long long>::max();
1037 for (auto const &O: Owners)
1038 {
1039 if (O->FileSize == 0)
1040 continue;
1041 Maximum = std::min(Maximum, O->FileSize);
1042 }
1043 if (Maximum == std::numeric_limits<unsigned long long>::max())
1044 return 0;
1045 return Maximum;
1046 }
1047 /*}}}*/
1048 void pkgAcquire::Queue::QItem::SyncDestinationFiles() const /*{{{*/
1049 {
1050 /* ensure that the first owner has the best partial file of all and
1051 the rest have (potentially dangling) symlinks to it so that
1052 everything (like progress reporting) finds it easily */
1053 std::string superfile = Owner->DestFile;
1054 off_t supersize = 0;
1055 for (pkgAcquire::Queue::QItem::owner_iterator O = Owners.begin(); O != Owners.end(); ++O)
1056 {
1057 if ((*O)->DestFile == superfile)
1058 continue;
1059 struct stat file;
1060 if (lstat((*O)->DestFile.c_str(),&file) == 0)
1061 {
1062 if ((file.st_mode & S_IFREG) == 0)
1063 RemoveFile("SyncDestinationFiles", (*O)->DestFile);
1064 else if (supersize < file.st_size)
1065 {
1066 supersize = file.st_size;
1067 RemoveFile("SyncDestinationFiles", superfile);
1068 rename((*O)->DestFile.c_str(), superfile.c_str());
1069 }
1070 else
1071 RemoveFile("SyncDestinationFiles", (*O)->DestFile);
1072 if (symlink(superfile.c_str(), (*O)->DestFile.c_str()) != 0)
1073 {
1074 ; // not a problem per-se and no real alternative
1075 }
1076 }
1077 }
1078 }
1079 /*}}}*/
1080 std::string pkgAcquire::Queue::QItem::Custom600Headers() const /*{{{*/
1081 {
1082 /* The others are relatively easy to merge, but this one?
1083 Lets not merge and see how far we can run with it…
1084 Likely, nobody will ever notice as all the items will
1085 be of the same class and hence generate the same headers. */
1086 return Owner->Custom600Headers();
1087 }
1088 /*}}}*/
1089
1090 // AcquireStatus::pkgAcquireStatus - Constructor /*{{{*/
1091 // ---------------------------------------------------------------------
1092 /* */
1093 pkgAcquireStatus::pkgAcquireStatus() : d(NULL), Percent(-1), Update(true), MorePulses(false)
1094 {
1095 Start();
1096 }
1097 /*}}}*/
1098 // AcquireStatus::Pulse - Called periodically /*{{{*/
1099 // ---------------------------------------------------------------------
1100 /* This computes some internal state variables for the derived classes to
1101 use. It generates the current downloaded bytes and total bytes to download
1102 as well as the current CPS estimate. */
1103 bool pkgAcquireStatus::Pulse(pkgAcquire *Owner)
1104 {
1105 TotalBytes = 0;
1106 CurrentBytes = 0;
1107 TotalItems = 0;
1108 CurrentItems = 0;
1109
1110 // Compute the total number of bytes to fetch
1111 unsigned int Unknown = 0;
1112 unsigned int Count = 0;
1113 bool UnfetchedReleaseFiles = false;
1114 for (pkgAcquire::ItemCIterator I = Owner->ItemsBegin();
1115 I != Owner->ItemsEnd();
1116 ++I, ++Count)
1117 {
1118 TotalItems++;
1119 if ((*I)->Status == pkgAcquire::Item::StatDone)
1120 ++CurrentItems;
1121
1122 // Totally ignore local items
1123 if ((*I)->Local == true)
1124 continue;
1125
1126 // see if the method tells us to expect more
1127 TotalItems += (*I)->ExpectedAdditionalItems;
1128
1129 // check if there are unfetched Release files
1130 if ((*I)->Complete == false && (*I)->ExpectedAdditionalItems > 0)
1131 UnfetchedReleaseFiles = true;
1132
1133 TotalBytes += (*I)->FileSize;
1134 if ((*I)->Complete == true)
1135 CurrentBytes += (*I)->FileSize;
1136 if ((*I)->FileSize == 0 && (*I)->Complete == false)
1137 ++Unknown;
1138 }
1139
1140 // Compute the current completion
1141 unsigned long long ResumeSize = 0;
1142 for (pkgAcquire::Worker *I = Owner->WorkersBegin(); I != 0;
1143 I = Owner->WorkerStep(I))
1144 {
1145 if (I->CurrentItem != 0 && I->CurrentItem->Owner->Complete == false)
1146 {
1147 CurrentBytes += I->CurrentSize;
1148 ResumeSize += I->ResumePoint;
1149
1150 // Files with unknown size always have 100% completion
1151 if (I->CurrentItem->Owner->FileSize == 0 &&
1152 I->CurrentItem->Owner->Complete == false)
1153 TotalBytes += I->CurrentSize;
1154 }
1155 }
1156
1157 // Normalize the figures and account for unknown size downloads
1158 if (TotalBytes <= 0)
1159 TotalBytes = 1;
1160 if (Unknown == Count)
1161 TotalBytes = Unknown;
1162
1163 // Wha?! Is not supposed to happen.
1164 if (CurrentBytes > TotalBytes)
1165 CurrentBytes = TotalBytes;
1166
1167 // debug
1168 if (_config->FindB("Debug::acquire::progress", false) == true)
1169 std::clog << " Bytes: "
1170 << SizeToStr(CurrentBytes) << " / " << SizeToStr(TotalBytes)
1171 << std::endl;
1172
1173 // Compute the CPS
1174 struct timeval NewTime;
1175 gettimeofday(&NewTime,0);
1176 if ((NewTime.tv_sec - Time.tv_sec == 6 && NewTime.tv_usec > Time.tv_usec) ||
1177 NewTime.tv_sec - Time.tv_sec > 6)
1178 {
1179 double Delta = NewTime.tv_sec - Time.tv_sec +
1180 (NewTime.tv_usec - Time.tv_usec)/1000000.0;
1181
1182 // Compute the CPS value
1183 if (Delta < 0.01)
1184 CurrentCPS = 0;
1185 else
1186 CurrentCPS = ((CurrentBytes - ResumeSize) - LastBytes)/Delta;
1187 LastBytes = CurrentBytes - ResumeSize;
1188 ElapsedTime = (unsigned long long)Delta;
1189 Time = NewTime;
1190 }
1191
1192 double const OldPercent = Percent;
1193 // calculate the percentage, if we have too little data assume 1%
1194 if (TotalBytes > 0 && UnfetchedReleaseFiles)
1195 Percent = 0;
1196 else
1197 // use both files and bytes because bytes can be unreliable
1198 Percent = (0.8 * (CurrentBytes/float(TotalBytes)*100.0) +
1199 0.2 * (CurrentItems/float(TotalItems)*100.0));
1200 double const DiffPercent = Percent - OldPercent;
1201 if (DiffPercent < 0.001 && _config->FindB("Acquire::Progress::Diffpercent", false) == true)
1202 return true;
1203
1204 int fd = _config->FindI("APT::Status-Fd",-1);
1205 if(fd > 0)
1206 {
1207 ostringstream status;
1208
1209 char msg[200];
1210 long i = CurrentItems < TotalItems ? CurrentItems + 1 : CurrentItems;
1211 unsigned long long ETA = 0;
1212 if(CurrentCPS > 0)
1213 ETA = (TotalBytes - CurrentBytes) / CurrentCPS;
1214
1215 // only show the ETA if it makes sense
1216 if (ETA > 0 && ETA < 172800 /* two days */ )
1217 snprintf(msg,sizeof(msg), _("Retrieving file %li of %li (%s remaining)"), i, TotalItems, TimeToStr(ETA).c_str());
1218 else
1219 snprintf(msg,sizeof(msg), _("Retrieving file %li of %li"), i, TotalItems);
1220
1221 // build the status str
1222 status << "dlstatus:" << i
1223 << ":" << std::setprecision(3) << Percent
1224 << ":" << msg
1225 << endl;
1226
1227 std::string const dlstatus = status.str();
1228 FileFd::Write(fd, dlstatus.c_str(), dlstatus.size());
1229 }
1230
1231 return true;
1232 }
1233 /*}}}*/
1234 // AcquireStatus::Start - Called when the download is started /*{{{*/
1235 // ---------------------------------------------------------------------
1236 /* We just reset the counters */
1237 void pkgAcquireStatus::Start()
1238 {
1239 gettimeofday(&Time,0);
1240 gettimeofday(&StartTime,0);
1241 LastBytes = 0;
1242 CurrentCPS = 0;
1243 CurrentBytes = 0;
1244 TotalBytes = 0;
1245 FetchedBytes = 0;
1246 ElapsedTime = 0;
1247 TotalItems = 0;
1248 CurrentItems = 0;
1249 }
1250 /*}}}*/
1251 // AcquireStatus::Stop - Finished downloading /*{{{*/
1252 // ---------------------------------------------------------------------
1253 /* This accurately computes the elapsed time and the total overall CPS. */
1254 void pkgAcquireStatus::Stop()
1255 {
1256 // Compute the CPS and elapsed time
1257 struct timeval NewTime;
1258 gettimeofday(&NewTime,0);
1259
1260 double Delta = NewTime.tv_sec - StartTime.tv_sec +
1261 (NewTime.tv_usec - StartTime.tv_usec)/1000000.0;
1262
1263 // Compute the CPS value
1264 if (Delta < 0.01)
1265 CurrentCPS = 0;
1266 else
1267 CurrentCPS = FetchedBytes/Delta;
1268 LastBytes = CurrentBytes;
1269 ElapsedTime = (unsigned long long)Delta;
1270 }
1271 /*}}}*/
1272 // AcquireStatus::Fetched - Called when a byte set has been fetched /*{{{*/
1273 // ---------------------------------------------------------------------
1274 /* This is used to get accurate final transfer rate reporting. */
1275 void pkgAcquireStatus::Fetched(unsigned long long Size,unsigned long long Resume)
1276 {
1277 FetchedBytes += Size - Resume;
1278 }
1279 /*}}}*/
1280
1281 pkgAcquire::UriIterator::UriIterator(pkgAcquire::Queue *Q) : d(NULL), CurQ(Q), CurItem(0)
1282 {
1283 while (CurItem == 0 && CurQ != 0)
1284 {
1285 CurItem = CurQ->Items;
1286 CurQ = CurQ->Next;
1287 }
1288 }
1289
1290 APT_CONST pkgAcquire::UriIterator::~UriIterator() {}
1291 APT_CONST pkgAcquire::MethodConfig::~MethodConfig() {}
1292 APT_CONST pkgAcquireStatus::~pkgAcquireStatus() {}