* methods/mirror.cc:
[ntk/apt.git] / methods / mirror.cc
CommitLineData
5f6b130d
MV
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: mirror.cc,v 1.59 2004/05/08 19:42:35 mdz Exp $
4/* ######################################################################
5
6 Mirror Aquire Method - This is the Mirror aquire method for APT.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
11#include <apt-pkg/fileutl.h>
12#include <apt-pkg/acquire-method.h>
13#include <apt-pkg/acquire-item.h>
14#include <apt-pkg/acquire.h>
15#include <apt-pkg/error.h>
16#include <apt-pkg/hashes.h>
17
18#include <fstream>
19#include <iostream>
14e097c1
MV
20#include <stdarg.h>
21
5f6b130d
MV
22using namespace std;
23
24#include "mirror.h"
25#include "http.h"
26
27 /*}}}*/
28
86c17f0a
MV
29/*
30 * TODO:
86c17f0a 31 * - better method to download than having a pkgAcquire interface here
3ed91a17
MV
32 * - support keeping the mirror file around (evil listclearer strikes again)
33 * -> /var/lib/apt/mirrors dir? how to cleanup? by time?
34 * - provide some TTL time until the mirror file is get again (1h? 6h?)
ef1e6d88 35 * - deal with runing as non-root (we can't write to the lists dir then)
86c17f0a 36 * - testing :)
86c17f0a
MV
37 */
38
5f6b130d 39MirrorMethod::MirrorMethod()
14e097c1 40 : HttpMethod(), HasMirrorFile(false)
5f6b130d
MV
41{
42#if 0
43 HasMirrorFile=true;
14e097c1
MV
44 BaseUri="mirror://people.ubuntu.com/~mvo/mirror/mirrors";
45 MirrorFile="/var/lib/apt/lists/people.ubuntu.com_%7emvo_apt_mirror_mirrors";
5f6b130d
MV
46 Mirror="http://de.archive.ubuntu.com/ubuntu/";
47#endif
48};
49
14e097c1
MV
50// HttpMethod::Configuration - Handle a configuration message /*{{{*/
51// ---------------------------------------------------------------------
52/* We stash the desired pipeline depth */
53bool MirrorMethod::Configuration(string Message)
54{
55 if (pkgAcqMethod::Configuration(Message) == false)
56 return false;
57 Debug = _config->FindB("Debug::Acquire::mirror",false);
58
59 return true;
60}
61 /*}}}*/
62
63
5f6b130d
MV
64bool MirrorMethod::GetMirrorFile(string uri)
65{
66 string Marker = _config->Find("Acquire::Mirror::MagicMarker","///");
67 BaseUri = uri.substr(0,uri.find(Marker));
14e097c1
MV
68
69 string fetch = BaseUri;
70 fetch.replace(0,strlen("mirror://"),"http://");
5f6b130d
MV
71
72 MirrorFile = _config->FindDir("Dir::State::lists") + URItoFileName(BaseUri);
73
14e097c1
MV
74 if(Debug)
75 {
76 cerr << "base-uri: " << BaseUri << endl;
77 cerr << "mirror-file: " << MirrorFile << endl;
78 }
5f6b130d
MV
79
80 // FIXME: fetch it with curl
81 pkgAcquire Fetcher;
14e097c1 82 new pkgAcqFile(&Fetcher, fetch, "", 0, "", "", "", MirrorFile);
5f6b130d 83 bool res = (Fetcher.Run() == pkgAcquire::Continue);
5f6b130d
MV
84
85 if(res)
86 HasMirrorFile = true;
87 Fetcher.Shutdown();
88 return true;
89}
90
91bool MirrorMethod::SelectMirror()
92{
93 ifstream in(MirrorFile.c_str());
94 getline(in, Mirror);
14e097c1
MV
95 if(Debug)
96 cerr << "Using mirror: " << Mirror << endl;
97 return true;
5f6b130d
MV
98}
99
100// MirrorMethod::Fetch - Fetch an item /*{{{*/
101// ---------------------------------------------------------------------
102/* This adds an item to the pipeline. We keep the pipeline at a fixed
103 depth. */
104bool MirrorMethod::Fetch(FetchItem *Itm)
105{
106 // get mirror information
107 if(!HasMirrorFile)
108 {
109 GetMirrorFile(Itm->Uri);
110 SelectMirror();
111 }
112
ef1e6d88
MV
113 for (FetchItem *I = Queue; I != 0; I = I->Next)
114 {
115 if(I->Uri.find("mirror://") != string::npos)
116 I->Uri.replace(0,BaseUri.size(),Mirror);
117 }
5f6b130d 118
14e097c1
MV
119 // now run the real fetcher
120 return HttpMethod::Fetch(Itm);
5f6b130d
MV
121};
122
14e097c1
MV
123void MirrorMethod::Fail(string Err,bool Transient)
124{
125 if(Queue->Uri.find("http://") != string::npos)
126 Queue->Uri.replace(0,Mirror.size(), BaseUri);
127 pkgAcqMethod::Fail(Err, Transient);
128}
129
130void MirrorMethod::URIStart(FetchResult &Res)
131{
132 if(Queue->Uri.find("http://") != string::npos)
133 Queue->Uri.replace(0,Mirror.size(), BaseUri);
134 pkgAcqMethod::URIStart(Res);
135}
136
137void MirrorMethod::URIDone(FetchResult &Res,FetchResult *Alt)
138{
139 if(Queue->Uri.find("http://") != string::npos)
140 Queue->Uri.replace(0,Mirror.size(), BaseUri);
141 pkgAcqMethod::URIDone(Res, Alt);
142}
143
144
5f6b130d
MV
145int main()
146{
147 setlocale(LC_ALL, "");
148
149 MirrorMethod Mth;
150
14e097c1 151 return Mth.Loop();
5f6b130d
MV
152}
153
154