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