1 | /* rinstallprogress.cc |
---|
2 | * |
---|
3 | * Copyright (c) 2000, 2001 Conectiva S/A |
---|
4 | * 2002 Michael Vogt |
---|
5 | * |
---|
6 | * Author: Alfredo K. Kojima <kojima@conectiva.com.br> |
---|
7 | * Michael Vogt <mvo@debian.org> |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License as |
---|
11 | * published by the Free Software Foundation; either version 2 of the |
---|
12 | * License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
22 | * USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include "config.h" |
---|
26 | |
---|
27 | #include <unistd.h> |
---|
28 | #include <sys/fcntl.h> |
---|
29 | #include <sys/types.h> |
---|
30 | #include <sys/wait.h> |
---|
31 | #include <iostream> |
---|
32 | #include <cstdio> |
---|
33 | #include <apt-pkg/error.h> |
---|
34 | #ifdef HAVE_RPM |
---|
35 | #include <apt-pkg/configuration.h> |
---|
36 | #endif |
---|
37 | |
---|
38 | #include "rinstallprogress.h" |
---|
39 | |
---|
40 | #include "i18n.h" |
---|
41 | string RInstallProgress::finishMsg = _("\nSuccessfully applied all changes. You can close the window now."); |
---|
42 | string RInstallProgress::errorMsg = _("\nFailed to apply all changes! Scroll in the terminal buffer to see what went wrong."); |
---|
43 | string RInstallProgress::incompleteMsg = |
---|
44 | _("\nSuccessfully installed all packages of the current medium. " |
---|
45 | "To continue the installation with the next medium close " |
---|
46 | "this window."); |
---|
47 | |
---|
48 | const char* RInstallProgress::getResultStr(pkgPackageManager::OrderResult res) |
---|
49 | { |
---|
50 | int size; |
---|
51 | switch( res ) { |
---|
52 | case 0: // completed |
---|
53 | return finishMsg.c_str(); |
---|
54 | break; |
---|
55 | case 1: // failed |
---|
56 | return errorMsg.c_str(); |
---|
57 | break; |
---|
58 | case 2: // incomplete |
---|
59 | return incompleteMsg.c_str(); |
---|
60 | break; |
---|
61 | } |
---|
62 | |
---|
63 | return "Unknown install result."; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | pkgPackageManager::OrderResult RInstallProgress::start(RPackageManager *pm, |
---|
68 | int numPackages, |
---|
69 | int numPackagesTotal) |
---|
70 | { |
---|
71 | void *dummy; |
---|
72 | pkgPackageManager::OrderResult res; |
---|
73 | int ret; |
---|
74 | pid_t _child_id; |
---|
75 | |
---|
76 | //cout << "RInstallProgress::start()" << endl; |
---|
77 | |
---|
78 | #ifdef HAVE_RPM |
---|
79 | |
---|
80 | _config->Set("RPM::Interactive", "false"); |
---|
81 | |
---|
82 | res = pm->DoInstallPreFork(); |
---|
83 | if (res == pkgPackageManager::Failed) |
---|
84 | return res; |
---|
85 | |
---|
86 | /* |
---|
87 | * This will make a pipe from where we can read child's output |
---|
88 | */ |
---|
89 | int fd[2]; |
---|
90 | pipe(fd); |
---|
91 | |
---|
92 | _child_id = fork(); |
---|
93 | |
---|
94 | if (_child_id == 0) { |
---|
95 | // make the write end of the pipe to the child become the new stdout |
---|
96 | // and stderr (for the child) |
---|
97 | dup2(fd[1], 1); |
---|
98 | dup2(1, 2); |
---|
99 | close(fd[0]); |
---|
100 | close(fd[1]); |
---|
101 | |
---|
102 | res = pm->DoInstallPostFork(); |
---|
103 | // dump errors into cerr (pass it to the parent process) |
---|
104 | _error->DumpErrors(); |
---|
105 | _exit(res); |
---|
106 | } |
---|
107 | // this is where we read stuff from the child |
---|
108 | _childin = fd[0]; |
---|
109 | close(fd[1]); |
---|
110 | |
---|
111 | // make it nonblocking |
---|
112 | fcntl(_childin, F_SETFL, O_NONBLOCK); |
---|
113 | |
---|
114 | _donePackages = 0; |
---|
115 | _numPackages = numPackages; |
---|
116 | _numPackagesTotal = numPackagesTotal; |
---|
117 | |
---|
118 | #else |
---|
119 | |
---|
120 | res = pm->DoInstallPreFork(); |
---|
121 | if (res == pkgPackageManager::Failed) |
---|
122 | return res; |
---|
123 | |
---|
124 | _child_id = fork(); |
---|
125 | |
---|
126 | if (_child_id == 0) { |
---|
127 | res = pm->DoInstallPostFork(); |
---|
128 | _exit(res); |
---|
129 | } |
---|
130 | #endif |
---|
131 | |
---|
132 | startUpdate(); |
---|
133 | while (waitpid(_child_id, &ret, WNOHANG) == 0) |
---|
134 | updateInterface(); |
---|
135 | |
---|
136 | res = (pkgPackageManager::OrderResult) WEXITSTATUS(ret); |
---|
137 | |
---|
138 | finishUpdate(); |
---|
139 | |
---|
140 | #ifdef HAVE_RPM |
---|
141 | close(_childin); |
---|
142 | #endif |
---|
143 | |
---|
144 | return res; |
---|
145 | } |
---|
146 | |
---|
147 | // vim:sts=4:sw=4 |
---|