summaryrefslogtreecommitdiff
path: root/src/cmdline/cmdline_download.cc
blob: e3ccb80cf7951503636e6c06ea9e213ec92f7ad8 (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
// cmdline_download.cc
//
//  Copyright 2004 Daniel Burrows

#include "cmdline_download.h"

#include "cmdline_common.h"
#include "cmdline_progress.h"
#include "cmdline_util.h"
#include "terminal.h"
#include "text_progress.h"

#include <aptitude.h>

#include <generic/apt/apt.h>
#include <generic/apt/config_signal.h>
#include <generic/apt/download_signal_log.h>
#include <generic/apt/matching/match.h>
#include <generic/apt/matching/parse.h>
#include <generic/apt/matching/pattern.h>
#include <generic/apt/pkg_acqfile.h>

#include <apt-pkg/acquire.h>
#include <apt-pkg/error.h>
#include <apt-pkg/progress.h>
#include <apt-pkg/sourcelist.h>

#include <stdio.h>

using aptitude::controllers::acquire_download_progress;
using aptitude::cmdline::create_cmdline_download_progress;
using aptitude::cmdline::create_terminal;
using aptitude::cmdline::make_text_progress;
using aptitude::cmdline::terminal_io;
using aptitude::cmdline::terminal_locale;
using boost::shared_ptr;

// Download stuff to the current directory
int cmdline_download(int argc, char *argv[])
{
  shared_ptr<terminal_io> term = create_terminal();

  if(argc<=1)
    {
      printf(_("download: you must specify at least one package to download\n"));
      return -1;
    }

  _error->DumpErrors();

  shared_ptr<OpProgress> progress = make_text_progress(false, term, term, term);
  apt_init(progress.get(), false);

  if(_error->PendingError())
    {
      _error->DumpErrors();
      return -1;
    }

  pkgSourceList list;
  if(!list.ReadMainList())
    {
      _error->Error(_("Couldn't read source list"));

      _error->DumpErrors();
      return -1;
    }

  std::pair<download_signal_log *, boost::shared_ptr<acquire_download_progress> >
    progress_display = create_cmdline_download_progress(term, term, term, term);

  pkgAcquire fetcher;
  fetcher.Setup(progress_display.first);
  string filenames[(*apt_cache_file)->Head().PackageCount];
  string default_release = aptcfg->Find("APT::Default-Release");

  for(int i=1; i<argc; ++i)
    {
      cmdline_version_source source;
      string name, sourcestr;
      if(!cmdline_parse_source(argv[i], source, name, sourcestr))
	continue;

      if(source == cmdline_version_cand && !default_release.empty())
	{
	  source = cmdline_version_archive;
	  sourcestr = default_release;
	}

      pkgset packages;
      if(aptitude::cmdline::pkgset_from_string(&packages, name) == false)
        continue;

      for(pkgset::const_iterator it = packages.begin();
          it != packages.end();
          ++it)
	{
	  const pkgCache::PkgIterator pkg = *it;

	  pkgCache::VerIterator ver=cmdline_find_ver(pkg, source, sourcestr);

	  if(ver.end())
	    continue;

	  if(!ver.Downloadable())
	    _error->Error(_("No downloadable files for %s version %s; perhaps it is a local or obsolete package?"),
			  name.c_str(), ver.VerStr());

	  get_archive(&fetcher, &list, apt_package_records,
		      ver, ".", filenames[pkg->ID]);
	}
    }

  if(fetcher.Run()!=pkgAcquire::Continue)
    // We failed or were cancelled
    {
      _error->DumpErrors();
      return -1;
    }

  return 0;
}