1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
// download_install_manager.cc
//
// Copyright (C) 2005-2011 Daniel Burrows
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; see the file COPYING. If not, write to
// the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.
#include "download_install_manager.h"
#include "config_signal.h"
#include "download_signal_log.h"
#include "log.h"
#include <aptitude.h>
#include <apt-pkg/acquire-item.h>
#include <apt-pkg/dpkgpm.h>
#include <apt-pkg/error.h>
#include <apt-pkg/sourcelist.h>
#include <sigc++/bind.h>
#include <pthread.h>
#include <signal.h>
using namespace std;
download_install_manager::download_install_manager(bool _download_only,
const run_dpkg_in_terminal_func &_run_dpkg_in_terminal)
: log(NULL), download_only(_download_only), pm(new pkgDPkgPM(*apt_cache_file)),
run_dpkg_in_terminal(_run_dpkg_in_terminal)
{
}
download_install_manager::~download_install_manager()
{
delete pm;
}
bool download_install_manager::prepare(OpProgress &progress,
pkgAcquireStatus &acqlog,
download_signal_log *signallog)
{
log = signallog;
if(apt_cache_file == NULL)
{
_error->Error(_("The package cache is not available; unable to download and install packages."));
return false;
}
if(!(*apt_cache_file)->save_selection_list(progress))
return false;
progress.Done();
// Abort here so we don't spew random messages below.
if(_error->PendingError())
return false;
// Lock the archive directory..
FileFd lock;
if(aptcfg->FindB("Debug::NoLocking", false) == false)
{
lock.Fd(GetLock(aptcfg->FindDir("Dir::State::Lists")+"lock"));
if(_error->PendingError() == true)
{
_error->Error(_("Couldn't lock list directory..are you root?"));
return false;
}
}
if(!src_list.ReadMainList())
{
_error->Error(_("Couldn't read source list"));
return false;
}
fetcher = new pkgAcquire;
fetcher->Setup(&acqlog);
if(!pm->GetArchives(fetcher, &src_list, apt_package_records) ||
_error->PendingError())
{
_error->Error(_("Internal error: couldn't generate list of packages to download"));
delete fetcher;
fetcher = NULL;
return false;
}
return true;
}
download_manager::result download_install_manager::finish_pre_dpkg(pkgAcquire::RunResult res)
{
if(res != pkgAcquire::Continue)
return failure;
else if(download_only)
return success;
bool failed=false;
for(pkgAcquire::ItemIterator i = fetcher->ItemsBegin();
i != fetcher->ItemsEnd(); ++i)
{
if((*i)->Status == pkgAcquire::Item::StatDone &&
(*i)->Complete)
continue;
if((*i)->Status == pkgAcquire::Item::StatIdle)
continue;
failed=true;
_error->Error(_("Failed to fetch %s: %s"), (*i)->DescURI().c_str(), (*i)->ErrorText.c_str());
break;
}
if(failed && !pm->FixMissing())
{
_error->Error(_("Unable to correct for unavailable packages"));
return failure;
}
log_changes();
// Note that someone could grab the lock before dpkg takes it;
// without a more complicated synchronization protocol (and I don't
// control the code at dpkg's end), them's the breaks.
apt_cache_file->ReleaseLock();
result rval = success;
const pkgPackageManager::OrderResult pre_fork_result =
pm->DoInstallPreFork();
if(pre_fork_result == pkgPackageManager::Failed)
rval = failure;
return rval;
}
pkgPackageManager::OrderResult download_install_manager::run_dpkg(int status_fd)
{
sigset_t allsignals;
sigset_t oldsignals;
sigfillset(&allsignals);
pthread_sigmask(SIG_UNBLOCK, &allsignals, &oldsignals);
pkgPackageManager::OrderResult pmres = pm->DoInstallPostFork(status_fd);
switch(pmres)
{
case pkgPackageManager::Failed:
_error->DumpErrors();
cerr << _("A package failed to install. Trying to recover:") << endl;
system("DPKG_NO_TSTP=1 dpkg --configure -a");
break;
case pkgPackageManager::Completed:
break;
case pkgPackageManager::Incomplete:
break;
}
pthread_sigmask(SIG_SETMASK, &oldsignals, NULL);
return pmres;
}
void download_install_manager::finish_post_dpkg(pkgPackageManager::OrderResult dpkg_result,
OpProgress *progress,
const sigc::slot1<void, result> &k)
{
result rval = success;
switch(dpkg_result)
{
case pkgPackageManager::Failed:
rval = failure;
break;
case pkgPackageManager::Completed:
break;
case pkgPackageManager::Incomplete:
rval = do_again;
break;
}
fetcher->Shutdown();
// Get the archives again. This was necessary for multi-CD
// installs, according to my comments in an old commit log in the
// Subversion repository.
if(!pm->GetArchives(fetcher, &src_list, apt_package_records))
rval = failure;
else if(!apt_cache_file->GainLock())
// This really shouldn't happen.
{
_error->Error(_("Could not regain the system lock! (Perhaps another apt or dpkg is running?)"));
rval = failure;
}
if(rval != do_again)
{
apt_close_cache();
if(log != NULL)
log->Complete();
// We absolutely need to do this here. Yes, it slows things
// down, but without this we get stuff like #429388 due to
// inconsistencies between aptitude's state file and the real
// world.
//
// This implicitly updates the package state file on disk.
apt_load_cache(progress, true);
if(aptcfg->FindB(PACKAGE "::Forget-New-On-Install", false))
{
if(apt_cache_file != NULL)
{
(*apt_cache_file)->forget_new(NULL);
(*apt_cache_file)->save_selection_list(*progress);
post_forget_new_hook();
}
}
}
k(rval);
}
void download_install_manager::finish(pkgAcquire::RunResult result,
OpProgress *progress,
const sigc::slot1<void, download_manager::result> &k)
{
const download_manager::result pre_res = finish_pre_dpkg(result);
if(pre_res == success)
{
run_dpkg_in_terminal(sigc::mem_fun(*this, &download_install_manager::run_dpkg),
sigc::bind(sigc::mem_fun(*this, &download_install_manager::finish_post_dpkg),
progress,
k));
return;
}
else
{
finish_post_dpkg(pkgPackageManager::Failed,
progress,
k);
return;
}
}
|