* Fix compilation warnings:
[ntk/apt.git] / apt-pkg / deb / dpkgpm.cc
CommitLineData
c0c0b100 1// -*- mode: cpp; mode: fold -*-
03e39e59 2// Description /*{{{*/
7f9a6360 3// $Id: dpkgpm.cc,v 1.28 2004/01/27 02:25:01 mdz Exp $
03e39e59
AL
4/* ######################################################################
5
6 DPKG Package Manager - Provide an interface to dpkg
7
8 ##################################################################### */
9 /*}}}*/
10// Includes /*{{{*/
03e39e59
AL
11#include <apt-pkg/dpkgpm.h>
12#include <apt-pkg/error.h>
13#include <apt-pkg/configuration.h>
b2e465d6
AL
14#include <apt-pkg/depcache.h>
15#include <apt-pkg/strutl.h>
233b185f 16
03e39e59
AL
17#include <unistd.h>
18#include <stdlib.h>
19#include <fcntl.h>
20#include <sys/types.h>
21#include <sys/wait.h>
22#include <signal.h>
23#include <errno.h>
db0c350f 24#include <stdio.h>
75ef8f14
MV
25#include <sstream>
26#include <map>
27
28#include <config.h>
29#include <apti18n.h>
b0ebdef5 30 /*}}}*/
233b185f
AL
31
32using namespace std;
03e39e59
AL
33
34// DPkgPM::pkgDPkgPM - Constructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
b2e465d6 37pkgDPkgPM::pkgDPkgPM(pkgDepCache *Cache) : pkgPackageManager(Cache)
03e39e59
AL
38{
39}
40 /*}}}*/
41// DPkgPM::pkgDPkgPM - Destructor /*{{{*/
42// ---------------------------------------------------------------------
43/* */
44pkgDPkgPM::~pkgDPkgPM()
45{
46}
47 /*}}}*/
48// DPkgPM::Install - Install a package /*{{{*/
49// ---------------------------------------------------------------------
50/* Add an install operation to the sequence list */
51bool pkgDPkgPM::Install(PkgIterator Pkg,string File)
52{
53 if (File.empty() == true || Pkg.end() == true)
54 return _error->Error("Internal Error, No file name for %s",Pkg.Name());
55
56 List.push_back(Item(Item::Install,Pkg,File));
57 return true;
58}
59 /*}}}*/
60// DPkgPM::Configure - Configure a package /*{{{*/
61// ---------------------------------------------------------------------
62/* Add a configure operation to the sequence list */
63bool pkgDPkgPM::Configure(PkgIterator Pkg)
64{
65 if (Pkg.end() == true)
66 return false;
67
68 List.push_back(Item(Item::Configure,Pkg));
69 return true;
70}
71 /*}}}*/
72// DPkgPM::Remove - Remove a package /*{{{*/
73// ---------------------------------------------------------------------
74/* Add a remove operation to the sequence list */
fc4b5c9f 75bool pkgDPkgPM::Remove(PkgIterator Pkg,bool Purge)
03e39e59
AL
76{
77 if (Pkg.end() == true)
78 return false;
79
fc4b5c9f
AL
80 if (Purge == true)
81 List.push_back(Item(Item::Purge,Pkg));
82 else
83 List.push_back(Item(Item::Remove,Pkg));
6dd55be7
AL
84 return true;
85}
86 /*}}}*/
87// DPkgPM::RunScripts - Run a set of scripts /*{{{*/
88// ---------------------------------------------------------------------
89/* This looks for a list of script sto run from the configuration file,
90 each one is run with system from a forked child. */
91bool pkgDPkgPM::RunScripts(const char *Cnf)
92{
93 Configuration::Item const *Opts = _config->Tree(Cnf);
94 if (Opts == 0 || Opts->Child == 0)
95 return true;
96 Opts = Opts->Child;
97
98 // Fork for running the system calls
54676e1a 99 pid_t Child = ExecFork();
6dd55be7
AL
100
101 // This is the child
102 if (Child == 0)
103 {
6dd55be7
AL
104 if (chdir("/tmp/") != 0)
105 _exit(100);
106
6dd55be7
AL
107 unsigned int Count = 1;
108 for (; Opts != 0; Opts = Opts->Next, Count++)
109 {
110 if (Opts->Value.empty() == true)
111 continue;
112
113 if (system(Opts->Value.c_str()) != 0)
114 _exit(100+Count);
115 }
116 _exit(0);
117 }
118
119 // Wait for the child
120 int Status = 0;
121 while (waitpid(Child,&Status,0) != Child)
122 {
123 if (errno == EINTR)
124 continue;
125 return _error->Errno("waitpid","Couldn't wait for subprocess");
126 }
127
128 // Restore sig int/quit
129 signal(SIGQUIT,SIG_DFL);
130 signal(SIGINT,SIG_DFL);
ddc1d8d0 131
6dd55be7
AL
132 // Check for an error code.
133 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
134 {
135 unsigned int Count = WEXITSTATUS(Status);
136 if (Count > 100)
137 {
138 Count -= 100;
139 for (; Opts != 0 && Count != 1; Opts = Opts->Next, Count--);
cf544e14 140 _error->Error("Problem executing scripts %s '%s'",Cnf,Opts->Value.c_str());
6dd55be7
AL
141 }
142
143 return _error->Error("Sub-process returned an error code");
144 }
145
03e39e59
AL
146 return true;
147}
db0c350f 148 /*}}}*/
b2e465d6
AL
149// DPkgPM::SendV2Pkgs - Send version 2 package info /*{{{*/
150// ---------------------------------------------------------------------
151/* This is part of the helper script communication interface, it sends
152 very complete information down to the other end of the pipe.*/
153bool pkgDPkgPM::SendV2Pkgs(FILE *F)
154{
155 fprintf(F,"VERSION 2\n");
156
157 /* Write out all of the configuration directives by walking the
158 configuration tree */
159 const Configuration::Item *Top = _config->Tree(0);
160 for (; Top != 0;)
161 {
162 if (Top->Value.empty() == false)
163 {
164 fprintf(F,"%s=%s\n",
165 QuoteString(Top->FullTag(),"=\"\n").c_str(),
166 QuoteString(Top->Value,"\n").c_str());
167 }
168
169 if (Top->Child != 0)
170 {
171 Top = Top->Child;
172 continue;
173 }
174
175 while (Top != 0 && Top->Next == 0)
176 Top = Top->Parent;
177 if (Top != 0)
178 Top = Top->Next;
179 }
180 fprintf(F,"\n");
181
182 // Write out the package actions in order.
183 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
184 {
185 pkgDepCache::StateCache &S = Cache[I->Pkg];
186
187 fprintf(F,"%s ",I->Pkg.Name());
188 // Current version
189 if (I->Pkg->CurrentVer == 0)
190 fprintf(F,"- ");
191 else
192 fprintf(F,"%s ",I->Pkg.CurrentVer().VerStr());
193
194 // Show the compare operator
195 // Target version
196 if (S.InstallVer != 0)
197 {
198 int Comp = 2;
199 if (I->Pkg->CurrentVer != 0)
200 Comp = S.InstVerIter(Cache).CompareVer(I->Pkg.CurrentVer());
201 if (Comp < 0)
202 fprintf(F,"> ");
203 if (Comp == 0)
204 fprintf(F,"= ");
205 if (Comp > 0)
206 fprintf(F,"< ");
207 fprintf(F,"%s ",S.InstVerIter(Cache).VerStr());
208 }
209 else
210 fprintf(F,"> - ");
211
212 // Show the filename/operation
213 if (I->Op == Item::Install)
214 {
215 // No errors here..
216 if (I->File[0] != '/')
217 fprintf(F,"**ERROR**\n");
218 else
219 fprintf(F,"%s\n",I->File.c_str());
220 }
221 if (I->Op == Item::Configure)
222 fprintf(F,"**CONFIGURE**\n");
223 if (I->Op == Item::Remove ||
224 I->Op == Item::Purge)
225 fprintf(F,"**REMOVE**\n");
226
227 if (ferror(F) != 0)
228 return false;
229 }
230 return true;
231}
232 /*}}}*/
db0c350f
AL
233// DPkgPM::RunScriptsWithPkgs - Run scripts with package names on stdin /*{{{*/
234// ---------------------------------------------------------------------
235/* This looks for a list of scripts to run from the configuration file
236 each one is run and is fed on standard input a list of all .deb files
237 that are due to be installed. */
238bool pkgDPkgPM::RunScriptsWithPkgs(const char *Cnf)
239{
240 Configuration::Item const *Opts = _config->Tree(Cnf);
241 if (Opts == 0 || Opts->Child == 0)
242 return true;
243 Opts = Opts->Child;
244
245 unsigned int Count = 1;
246 for (; Opts != 0; Opts = Opts->Next, Count++)
247 {
248 if (Opts->Value.empty() == true)
249 continue;
b2e465d6
AL
250
251 // Determine the protocol version
252 string OptSec = Opts->Value;
253 string::size_type Pos;
254 if ((Pos = OptSec.find(' ')) == string::npos || Pos == 0)
255 Pos = OptSec.length();
b2e465d6
AL
256 OptSec = "DPkg::Tools::Options::" + string(Opts->Value.c_str(),Pos);
257
258 unsigned int Version = _config->FindI(OptSec+"::Version",1);
259
db0c350f
AL
260 // Create the pipes
261 int Pipes[2];
262 if (pipe(Pipes) != 0)
263 return _error->Errno("pipe","Failed to create IPC pipe to subprocess");
264 SetCloseExec(Pipes[0],true);
265 SetCloseExec(Pipes[1],true);
266
267 // Purified Fork for running the script
268 pid_t Process = ExecFork();
269 if (Process == 0)
270 {
271 // Setup the FDs
272 dup2(Pipes[0],STDIN_FILENO);
273 SetCloseExec(STDOUT_FILENO,false);
274 SetCloseExec(STDIN_FILENO,false);
275 SetCloseExec(STDERR_FILENO,false);
90ecbd7d
AL
276
277 const char *Args[4];
db0c350f 278 Args[0] = "/bin/sh";
90ecbd7d
AL
279 Args[1] = "-c";
280 Args[2] = Opts->Value.c_str();
281 Args[3] = 0;
db0c350f
AL
282 execv(Args[0],(char **)Args);
283 _exit(100);
284 }
285 close(Pipes[0]);
b2e465d6
AL
286 FILE *F = fdopen(Pipes[1],"w");
287 if (F == 0)
288 return _error->Errno("fdopen","Faild to open new FD");
289
db0c350f 290 // Feed it the filenames.
b2e465d6
AL
291 bool Die = false;
292 if (Version <= 1)
db0c350f 293 {
b2e465d6 294 for (vector<Item>::iterator I = List.begin(); I != List.end(); I++)
db0c350f 295 {
b2e465d6
AL
296 // Only deal with packages to be installed from .deb
297 if (I->Op != Item::Install)
298 continue;
299
300 // No errors here..
301 if (I->File[0] != '/')
302 continue;
303
304 /* Feed the filename of each package that is pending install
305 into the pipe. */
306 fprintf(F,"%s\n",I->File.c_str());
307 if (ferror(F) != 0)
308 {
309 Die = true;
310 break;
311 }
90ecbd7d 312 }
db0c350f 313 }
b2e465d6
AL
314 else
315 Die = !SendV2Pkgs(F);
316
317 fclose(F);
db0c350f
AL
318
319 // Clean up the sub process
320 if (ExecWait(Process,Opts->Value.c_str()) == false)
90ecbd7d 321 return _error->Error("Failure running script %s",Opts->Value.c_str());
db0c350f
AL
322 }
323
324 return true;
325}
03e39e59
AL
326 /*}}}*/
327// DPkgPM::Go - Run the sequence /*{{{*/
328// ---------------------------------------------------------------------
75ef8f14
MV
329/* This globs the operations and calls dpkg
330 *
331 * If it is called with "OutStatusFd" set to a valid file descriptor
332 * apt will report the install progress over this fd. It maps the
333 * dpkg states a package goes through to human readable (and i10n-able)
334 * names and calculates a percentage for each step.
335*/
336bool pkgDPkgPM::Go(int OutStatusFd)
03e39e59 337{
6b8147b8
MZ
338 unsigned int MaxArgs = _config->FindI("Dpkg::MaxArgs",8*1024);
339 unsigned int MaxArgBytes = _config->FindI("Dpkg::MaxArgBytes",32*1024);
aff4e2f1 340
6dd55be7
AL
341 if (RunScripts("DPkg::Pre-Invoke") == false)
342 return false;
db0c350f
AL
343
344 if (RunScriptsWithPkgs("DPkg::Pre-Install-Pkgs") == false)
345 return false;
75ef8f14
MV
346
347 // prepare the progress reporting
348 int Done = 0;
349 int Total = 0;
350 // map the dpkg states to the operations that are performed
351 // (this is sorted in the same way as Item::Ops)
352 static const struct DpkgState DpkgStatesOpMap[][5] = {
353 // Install operation
354 {
21e1008e
MV
355 {"half-installed", N_("Preparing %s")},
356 {"unpacked", N_("Unpacking %s") },
75ef8f14
MV
357 {NULL, NULL}
358 },
359 // Configure operation
360 {
21e1008e
MV
361 {"unpacked",N_("Preparing to configure %s") },
362 {"half-configured", N_("Configuring %s") },
363 { "installed", N_("Installed %s")},
75ef8f14
MV
364 {NULL, NULL}
365 },
366 // Remove operation
367 {
21e1008e
MV
368 {"half-configured", N_("Preparing for removal of %s")},
369 {"half-installed", N_("Removing %s")},
370 {"config-files", N_("Removed %s")},
75ef8f14
MV
371 {NULL, NULL}
372 },
373 // Purge operation
374 {
21e1008e
MV
375 {"config-files", N_("Preparing to completely remove %s")},
376 {"not-installed", N_("Completely removed %s")},
75ef8f14
MV
377 {NULL, NULL}
378 },
379 };
db0c350f 380
75ef8f14
MV
381 // the dpkg states that the pkg will run through, the string is
382 // the package, the vector contains the dpkg states that the package
383 // will go through
384 map<string,vector<struct DpkgState> > PackageOps;
385 // the dpkg states that are already done; the string is the package
386 // the int is the state that is already done (e.g. a package that is
387 // going to be install is already in state "half-installed")
21fd1746 388 map<string,unsigned int> PackageOpsDone;
75ef8f14
MV
389
390 // init the PackageOps map, go over the list of packages that
391 // that will be [installed|configured|removed|purged] and add
392 // them to the PackageOps map (the dpkg states it goes through)
393 // and the PackageOpsTranslations (human readable strings)
394 for (vector<Item>::iterator I = List.begin(); I != List.end();I++)
395 {
396 string name = (*I).Pkg.Name();
397 PackageOpsDone[name] = 0;
398 for(int i=0; (DpkgStatesOpMap[(*I).Op][i]).state != NULL; i++)
399 {
400 PackageOps[name].push_back(DpkgStatesOpMap[(*I).Op][i]);
401 Total++;
402 }
403 }
404
405 // this loop is runs once per operation
03e39e59
AL
406 for (vector<Item>::iterator I = List.begin(); I != List.end();)
407 {
408 vector<Item>::iterator J = I;
409 for (; J != List.end() && J->Op == I->Op; J++);
30e1eab5 410
03e39e59 411 // Generate the argument list
aff4e2f1
AL
412 const char *Args[MaxArgs + 50];
413 if (J - I > (signed)MaxArgs)
414 J = I + MaxArgs;
03e39e59 415
30e1eab5
AL
416 unsigned int n = 0;
417 unsigned long Size = 0;
43b3b626 418 string Tmp = _config->Find("Dir::Bin::dpkg","dpkg");
50914ffa 419 Args[n++] = Tmp.c_str();
30e1eab5 420 Size += strlen(Args[n-1]);
03e39e59 421
6dd55be7
AL
422 // Stick in any custom dpkg options
423 Configuration::Item const *Opts = _config->Tree("DPkg::Options");
424 if (Opts != 0)
425 {
426 Opts = Opts->Child;
427 for (; Opts != 0; Opts = Opts->Next)
428 {
429 if (Opts->Value.empty() == true)
430 continue;
431 Args[n++] = Opts->Value.c_str();
432 Size += Opts->Value.length();
433 }
434 }
435
007dc9e0 436 char status_fd_buf[20];
75ef8f14
MV
437 int fd[2];
438 pipe(fd);
439
440 Args[n++] = "--status-fd";
441 Size += strlen(Args[n-1]);
442 snprintf(status_fd_buf,sizeof(status_fd_buf),"%i", fd[1]);
443 Args[n++] = status_fd_buf;
444 Size += strlen(Args[n-1]);
007dc9e0 445
03e39e59
AL
446 switch (I->Op)
447 {
448 case Item::Remove:
449 Args[n++] = "--force-depends";
30e1eab5 450 Size += strlen(Args[n-1]);
03e39e59 451 Args[n++] = "--force-remove-essential";
30e1eab5 452 Size += strlen(Args[n-1]);
03e39e59 453 Args[n++] = "--remove";
30e1eab5 454 Size += strlen(Args[n-1]);
03e39e59
AL
455 break;
456
fc4b5c9f
AL
457 case Item::Purge:
458 Args[n++] = "--force-depends";
459 Size += strlen(Args[n-1]);
460 Args[n++] = "--force-remove-essential";
461 Size += strlen(Args[n-1]);
462 Args[n++] = "--purge";
463 Size += strlen(Args[n-1]);
464 break;
465
03e39e59
AL
466 case Item::Configure:
467 Args[n++] = "--configure";
30e1eab5 468 Size += strlen(Args[n-1]);
03e39e59
AL
469 break;
470
471 case Item::Install:
472 Args[n++] = "--unpack";
30e1eab5 473 Size += strlen(Args[n-1]);
857a1d4a
MV
474 Args[n++] = "--auto-deconfigure";
475 Size += strlen(Args[n-1]);
03e39e59
AL
476 break;
477 }
478
479 // Write in the file or package names
480 if (I->Op == Item::Install)
30e1eab5 481 {
aff4e2f1 482 for (;I != J && Size < MaxArgBytes; I++)
30e1eab5 483 {
cf544e14
AL
484 if (I->File[0] != '/')
485 return _error->Error("Internal Error, Pathname to install is not absolute '%s'",I->File.c_str());
03e39e59 486 Args[n++] = I->File.c_str();
30e1eab5
AL
487 Size += strlen(Args[n-1]);
488 }
489 }
03e39e59 490 else
30e1eab5 491 {
aff4e2f1 492 for (;I != J && Size < MaxArgBytes; I++)
30e1eab5 493 {
03e39e59 494 Args[n++] = I->Pkg.Name();
30e1eab5
AL
495 Size += strlen(Args[n-1]);
496 }
497 }
03e39e59 498 Args[n] = 0;
30e1eab5
AL
499 J = I;
500
501 if (_config->FindB("Debug::pkgDPkgPM",false) == true)
502 {
503 for (unsigned int k = 0; k != n; k++)
504 clog << Args[k] << ' ';
505 clog << endl;
506 continue;
507 }
03e39e59 508
03e39e59
AL
509 cout << flush;
510 clog << flush;
511 cerr << flush;
512
513 /* Mask off sig int/quit. We do this because dpkg also does when
514 it forks scripts. What happens is that when you hit ctrl-c it sends
515 it to all processes in the group. Since dpkg ignores the signal
516 it doesn't die but we do! So we must also ignore it */
7f9a6360
AL
517 sighandler_t old_SIGQUIT = signal(SIGQUIT,SIG_IGN);
518 sighandler_t old_SIGINT = signal(SIGINT,SIG_IGN);
75ef8f14
MV
519
520 // Fork dpkg
007dc9e0 521 pid_t Child;
75ef8f14
MV
522 _config->Set("APT::Keep-Fds::",fd[1]);
523 Child = ExecFork();
6dd55be7 524
03e39e59
AL
525 // This is the child
526 if (Child == 0)
527 {
75ef8f14
MV
528 close(fd[0]); // close the read end of the pipe
529
cf544e14 530 if (chdir(_config->FindDir("DPkg::Run-Directory","/").c_str()) != 0)
0dbb95d8 531 _exit(100);
03e39e59 532
421ff807 533 if (_config->FindB("DPkg::FlushSTDIN",true) == true && isatty(STDIN_FILENO))
8b5fe26c
AL
534 {
535 int Flags,dummy;
536 if ((Flags = fcntl(STDIN_FILENO,F_GETFL,dummy)) < 0)
537 _exit(100);
538
539 // Discard everything in stdin before forking dpkg
540 if (fcntl(STDIN_FILENO,F_SETFL,Flags | O_NONBLOCK) < 0)
541 _exit(100);
542
543 while (read(STDIN_FILENO,&dummy,1) == 1);
544
545 if (fcntl(STDIN_FILENO,F_SETFL,Flags & (~(long)O_NONBLOCK)) < 0)
546 _exit(100);
547 }
03e39e59 548
03e39e59
AL
549 /* No Job Control Stop Env is a magic dpkg var that prevents it
550 from using sigstop */
101030ab 551 putenv("DPKG_NO_TSTP=yes");
d568ed2d 552 execvp(Args[0],(char **)Args);
03e39e59 553 cerr << "Could not exec dpkg!" << endl;
0dbb95d8 554 _exit(100);
03e39e59
AL
555 }
556
75ef8f14
MV
557 // clear the Keep-Fd again
558 _config->Clear("APT::Keep-Fds",fd[1]);
559
03e39e59
AL
560 // Wait for dpkg
561 int Status = 0;
75ef8f14
MV
562
563 // we read from dpkg here
564 int _dpkgin = fd[0];
565 fcntl(_dpkgin, F_SETFL, O_NONBLOCK);
566 close(fd[1]); // close the write end of the pipe
567
568 // the read buffers for the communication with dpkg
569 char line[1024] = {0,};
570 char buf[2] = {0,0};
571
572 // the result of the waitpid call
573 int res;
574
575 while ((res=waitpid(Child,&Status, WNOHANG)) != Child) {
576 if(res < 0) {
577 // FIXME: move this to a function or something, looks ugly here
578 // error handling, waitpid returned -1
579 if (errno == EINTR)
580 continue;
581 RunScripts("DPkg::Post-Invoke");
582
583 // Restore sig int/quit
584 signal(SIGQUIT,old_SIGQUIT);
585 signal(SIGINT,old_SIGINT);
586 return _error->Errno("waitpid","Couldn't wait for subprocess");
587 }
588
589 // read a single char, make sure that the read can't block
590 // (otherwise we may leave zombies)
591 int len = read(_dpkgin, buf, 1);
592
593 // nothing to read, wait a bit for more
594 if(len <= 0)
595 {
596 usleep(1000);
03e39e59 597 continue;
75ef8f14
MV
598 }
599
600 // sanity check (should never happen)
601 if(strlen(line) >= sizeof(line)-10)
602 {
603 _error->Error("got a overlong line from dpkg: '%s'",line);
604 line[0]=0;
605 }
606 // append to line, check if we got a complete line
607 strcat(line, buf);
608 if(buf[0] != '\n')
609 continue;
610
611 if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
612 std::clog << "got from dpkg '" << line << "'" << std::endl;
613
614 // the status we output
615 ostringstream status;
616
617 /* dpkg sends strings like this:
618 'status: <pkg>: <pkg qstate>'
619 errors look like this:
620 'status: /var/cache/apt/archives/krecipes_0.8.1-0ubuntu1_i386.deb : error : trying to overwrite `/usr/share/doc/kde/HTML/en/krecipes/krectip.png', which is also in package krecipes-data
621 and conffile-prompt like this
622 'status: conffile-prompt: conffile : 'current-conffile' 'new-conffile' useredited distedited
623
624 */
eda1bb3c 625 char* list[5];
ed94a0d6
MV
626 // dpkg sends multiline error messages sometimes (see
627 // #374195 for a example. we should support this by
628 // either patching dpkg to not send multiline over the
629 // statusfd or by rewriting the code here to deal with
630 // it. for now we just ignore it and not crash
631 TokSplitString(':', line, list, sizeof(list)/sizeof(list[0]));
75ef8f14
MV
632 char *pkg = list[1];
633 char *action = _strstrip(list[2]);
ed94a0d6
MV
634 if( pkg == NULL || action == NULL)
635 {
636 if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
637 std::clog << "ignoring line: not enough ':'" << std::endl;
7882f8fe
MV
638 // reset the line buffer
639 line[0]=0;
ed94a0d6
MV
640 continue;
641 }
75ef8f14
MV
642
643 if(strncmp(action,"error",strlen("error")) == 0)
644 {
645 status << "pmerror:" << list[1]
646 << ":" << (Done/float(Total)*100.0)
647 << ":" << list[3]
648 << endl;
649 if(OutStatusFd > 0)
650 write(OutStatusFd, status.str().c_str(), status.str().size());
651 line[0]=0;
652 if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
653 std::clog << "send: '" << status.str() << "'" << endl;
654 continue;
655 }
656 if(strncmp(action,"conffile",strlen("conffile")) == 0)
657 {
658 status << "pmconffile:" << list[1]
659 << ":" << (Done/float(Total)*100.0)
660 << ":" << list[3]
661 << endl;
662 if(OutStatusFd > 0)
663 write(OutStatusFd, status.str().c_str(), status.str().size());
664 line[0]=0;
665 if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
666 std::clog << "send: '" << status.str() << "'" << endl;
667 continue;
668 }
669
670 vector<struct DpkgState> &states = PackageOps[pkg];
671 const char *next_action = NULL;
672 if(PackageOpsDone[pkg] < states.size())
673 next_action = states[PackageOpsDone[pkg]].state;
674 // check if the package moved to the next dpkg state
675 if(next_action && (strcmp(action, next_action) == 0))
676 {
677 // only read the translation if there is actually a next
678 // action
21e1008e 679 const char *translation = _(states[PackageOpsDone[pkg]].str);
75ef8f14
MV
680 char s[200];
681 snprintf(s, sizeof(s), translation, pkg);
682
683 // we moved from one dpkg state to a new one, report that
684 PackageOpsDone[pkg]++;
685 Done++;
686 // build the status str
687 status << "pmstatus:" << pkg
688 << ":" << (Done/float(Total)*100.0)
689 << ":" << s
690 << endl;
691 if(OutStatusFd > 0)
692 write(OutStatusFd, status.str().c_str(), status.str().size());
693 if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
694 std::clog << "send: '" << status.str() << "'" << endl;
695
696 }
697 if (_config->FindB("Debug::pkgDPkgProgressReporting",false) == true)
698 std::clog << "(parsed from dpkg) pkg: " << pkg
699 << " action: " << action << endl;
7f9a6360 700
75ef8f14
MV
701 // reset the line buffer
702 line[0]=0;
03e39e59 703 }
75ef8f14 704 close(_dpkgin);
03e39e59
AL
705
706 // Restore sig int/quit
7f9a6360
AL
707 signal(SIGQUIT,old_SIGQUIT);
708 signal(SIGINT,old_SIGINT);
6dd55be7
AL
709
710 // Check for an error code.
711 if (WIFEXITED(Status) == 0 || WEXITSTATUS(Status) != 0)
712 {
c70496f9
MV
713 // if it was set to "keep-dpkg-runing" then we won't return
714 // here but keep the loop going and just report it as a error
715 // for later
716 bool stopOnError = _config->FindB("Dpkg::StopOnError",true);
f956efb4 717
c70496f9
MV
718 if(stopOnError)
719 RunScripts("DPkg::Post-Invoke");
720
721 if (WIFSIGNALED(Status) != 0 && WTERMSIG(Status) == SIGSEGV)
722 _error->Error("Sub-process %s received a segmentation fault.",Args[0]);
723 else if (WIFEXITED(Status) != 0)
724 _error->Error("Sub-process %s returned an error code (%u)",Args[0],WEXITSTATUS(Status));
725 else
726 _error->Error("Sub-process %s exited unexpectedly",Args[0]);
727
728 if(stopOnError)
729 return false;
6dd55be7 730 }
03e39e59 731 }
6dd55be7
AL
732
733 if (RunScripts("DPkg::Post-Invoke") == false)
734 return false;
03e39e59
AL
735 return true;
736}
737 /*}}}*/
281daf46
AL
738// pkgDpkgPM::Reset - Dump the contents of the command list /*{{{*/
739// ---------------------------------------------------------------------
740/* */
741void pkgDPkgPM::Reset()
742{
743 List.erase(List.begin(),List.end());
744}
745 /*}}}*/