summaryrefslogtreecommitdiff
path: root/src/generic/apt/download_update_manager.cc
blob: b4b7a227c5147829d56e43c1698c7450d70a565d (plain)
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
// download_update_manager.cc
//
//   Copyright (C) 2005, 2007-2009, 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_update_manager.h"

#include "apt.h"
#include "config_signal.h"
#include "download_signal_log.h"

#include <apt-pkg/acquire-item.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/clean.h>
#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>

namespace cw = cwidget;

class my_cleaner:public pkgArchiveCleaner
{
protected:
  virtual void Erase(const char *file,
		     string pkg,
		     string ver,
		     struct stat &stat)
  {
    unlink(file);
  }
};

download_update_manager::download_update_manager()
  : log(NULL)
{
}

download_update_manager::~download_update_manager()
{
}

bool download_update_manager::prepare(OpProgress &progress,
				      pkgAcquireStatus &acqlog,
				      download_signal_log *signallog)
{
  log = signallog;

  if(apt_cache_file != NULL &&
     !(*apt_cache_file)->save_selection_list(progress))
    return false;

  // FIXME: should save_selection_list do this?
  progress.Done();

  if(src_list.ReadMainList() == false)
    {
      _error->Error(_("Couldn't read list of package sources"));
      return false;
    }

  // Abort here so we don't spew random messages below.
  if(_error->PendingError())
    return false;

  fetcher = new pkgAcquire();
  if(fetcher->Setup(&acqlog, aptcfg->FindDir("Dir::State::Lists")) == false ||
     src_list.GetIndexes(fetcher) == false)
    {
      delete fetcher;
      fetcher = NULL;
      return false;
    }

  // Run scripts
  RunScripts("APT::Update::Pre-Invoke");

  return true;
}

void download_update_manager::finish(pkgAcquire::RunResult res,
				     OpProgress *progress,
				     const sigc::slot1<void, result> &k)
{
  if(log != NULL)
    log->Complete();

  apt_close_cache();

  if(res != pkgAcquire::Continue)
    {
      k(failure);
      return;
    }

  bool failed = false;
  bool transientNetworkFailure = false;
  result rval = success;

  // We need to claim that the download failed if any source failed,
  // and invoke Finished() on any failed items.  Also, we shouldn't
  // clean the package lists if any individual item failed because it
  // makes users grumpy (see Debian bugs #201842 and #479620).
  //
  // See also apt-get.cc.
  for(pkgAcquire::ItemIterator it = fetcher->ItemsBegin();
      it != fetcher->ItemsEnd(); ++it)
    {
      if((*it)->Status == pkgAcquire::Item::StatDone)
	continue;

      (*it)->Finished();

      ::URI uri((*it)->DescURI());
      uri.User.clear();
      uri.Password.clear();
      const std::string descUri = string(uri);
      _error->Warning(_("Failed to fetch %s: %s\n"), descUri.c_str(), (*it)->ErrorText.c_str());

      if((*it)->Status == pkgAcquire::Item::StatTransientNetworkError)
	{
	  transientNetworkFailure = true;
	  continue;
	}

      failed = true;
      rval = failure;
    }

  // Clean old stuff out
  const std::string listsdir = aptcfg->FindDir("Dir::State::lists");
  if(!transientNetworkFailure && !failed &&
     (aptcfg->FindB("APT::Get::List-Cleanup", true) == true &&
      aptcfg->FindB("APT::List-Cleanup", true) == true) &&
     (fetcher->Clean(listsdir) == false ||
      fetcher->Clean(listsdir + "partial/") == false))
    {
      _error->Error(_("Couldn't clean out list directories"));
      rval = failure;
    }

  if(transientNetworkFailure == true)
    _error->Warning(_("Some index files failed to download. They have been ignored, or old ones used instead."));
  else if(failed == true)
    _error->Error(_("Some index files failed to download. They have been ignored, or old ones used instead."));

  if(rval != failure)
    {
      // Run the success scripts if all was fine
      if(!transientNetworkFailure && !failed)
	RunScripts("APT::Update::Post-Invoke-Success");

      // Run the other scripts
      RunScripts("APT::Update::Post-Invoke");
    }

  // Rebuild the apt caches as done in apt-get.  cachefile is scoped
  // so it dies before we possibly-reload the cache.  This will do a
  // little redundant work in visual mode, but avoids lots of
  // redundant work at the command-line.
  {
    pkgCacheFile cachefile;
    pkgCacheFile::RemoveCaches();
    if(!cachefile.BuildCaches(progress, true))
      {
	_error->Error(_("Couldn't rebuild package cache"));
	k(failure);
	return;
      }
  }

  bool need_forget_new = 
    aptcfg->FindB(PACKAGE "::Forget-New-On-Update", false);

  bool need_autoclean =
    aptcfg->FindB(PACKAGE "::AutoClean-After-Update", false);

  if(need_forget_new || need_autoclean)
    apt_load_cache(progress, true);

  if(apt_cache_file != NULL && need_forget_new)
    {
      (*apt_cache_file)->forget_new(NULL);
      post_forget_new_hook();
    }

  if(apt_cache_file != NULL && need_autoclean)
    {
      pre_autoclean_hook();

      my_cleaner cleaner;
      cleaner.Go(aptcfg->FindDir("Dir::Cache::archives"), *apt_cache_file);
      cleaner.Go(aptcfg->FindDir("Dir::Cache::archives")+"partial/",
		 *apt_cache_file);

      post_autoclean_hook();
    }

  k(rval);
  return;
}