File: fetch.cc

package info (click to toggle)
apt-move 4.1.21
  • links: PTS
  • area: main
  • in suites: woody
  • size: 160 kB
  • ctags: 62
  • sloc: sh: 818; awk: 205; cpp: 113; makefile: 63; perl: 53
file content (132 lines) | stat: -rw-r--r-- 2,958 bytes parent folder | download
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
#include <apt-pkg/acquire-item.h>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/init.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/progress.h>
#include <apt-pkg/sourcelist.h>
#include <iostream.h>
#include <stdlib.h>
#include <unistd.h>

typedef pkgCache::VerIterator VerIterator;
typedef pkgCache::VerFileIterator VerFileIterator;
#define NotSource pkgCache::Flag::NotSource
#define NotAutomatic pkgCache::Flag::NotAutomatic

VerIterator getHighestVersion(pkgCache &cache, pkgCache::PkgIterator pkg) {
	VerIterator last(cache, 0);

	for (VerIterator i = pkg.VersionList(); !i.end(); i++) {
		for (VerFileIterator j = i.FileList(); !j.end(); j++) {
			if ((j.File()->Flags & NotSource) != 0) {
				continue;
			}
			if ((j.File()->Flags & NotAutomatic) != 0) {
				if (last.end()) {
					last = i;
				}
				continue;
			}
			return i;
		}
	}

	return last;
}

bool downloadPackages(int test, int argc, char **argv) {
	pkgCacheFile cache;
	OpTextProgress prog(*_config);
	if (!cache.Open(prog, false))
		return false;

	pkgRecords rec(cache);
	if (_error->PendingError())
		return false;

	FileFd Lock;
	if (!_config->FindB("Debug::NoLocking",false)) {
		Lock.Fd(GetLock(_config->FindDir("Dir::Cache::Archives") +
				"lock"));
		if (_error->PendingError())
			return _error->Error(
				"Unable to lock the download directory");
	}

	pkgAcquire fetcher;

	pkgSourceList list;
	if (!list.ReadMainList())
		return _error->Error("The list of sources could not be read.");

	string *filenames = new string[argc];
	for (int i = 0; i < argc; i++) {
		pkgCache::PkgIterator pkg = cache->FindPkg(argv[i]);
		if (pkg.end()) {
			cerr << "Couldn't find package " << argv[i] << endl;
			continue;
		}
		VerIterator candidateVer = getHighestVersion(cache, pkg);
		if (candidateVer == 0) {
			cerr << "Couldn't find package " << argv[i] << endl;
			continue;
		}
		new pkgAcqArchive(&fetcher, &list, &rec, candidateVer,
				  filenames[i]);
	}

	if (test) {
		pkgAcquire::UriIterator i = fetcher.UriBegin();
		for (; i != fetcher.UriEnd(); i++) {
			cout << i->URI << endl;
		}
		return true;
	}

	if (fetcher.Run() == pkgAcquire::Failed)
		return false;

	return true;
}

static void usage(const char *name) {
	cerr << "usage: " << name << " [-t] pkg..." << endl;
	exit(1);
}

int main(int argc, char **argv) {
	int test = 0;
	const char *progname = argv[0];

	int c;
	while ((c = getopt(argc, argv, "t")) != -1) {
		switch (c) {
		case 't':
			test = 1;
			break;
		default:
			usage(progname);
		}
	}
	argc -= optind;
	argv += optind;
	if (!*argv) {
		usage(progname);
	}

	pkgInitConfig(*_config);
	pkgInitSystem(*_config, _system);
	if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
		_config->Set("quiet","1");

	downloadPackages(test, argc, argv);

	if (!_error->empty()) {
		bool Errors = _error->PendingError();
		_error->DumpErrors();
		return Errors ? 100 : 0;
	}
	return 0;   
}