]> git.saurik.com Git - apt.git/blame - apt-pkg/indexrecords.cc
hide first pdiff merge failure debug message
[apt.git] / apt-pkg / indexrecords.cc
CommitLineData
7db98ffc
MZ
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: indexrecords.cc,v 1.1.2.4 2003/12/30 02:11:43 mdz Exp $
4 /*}}}*/
5// Include Files /*{{{*/
ea542140
DK
6#include<config.h>
7
7db98ffc
MZ
8#include <apt-pkg/indexrecords.h>
9#include <apt-pkg/tagfile.h>
10#include <apt-pkg/error.h>
11#include <apt-pkg/strutl.h>
1ddb8596 12#include <apt-pkg/configuration.h>
472ff00e
DK
13#include <apt-pkg/fileutl.h>
14#include <apt-pkg/hashes.h>
233b7808 15#include <apt-pkg/gpgv.h>
472ff00e 16
453b82a3
DK
17#include <stdlib.h>
18#include <time.h>
1ddb8596 19#include <clocale>
453b82a3
DK
20#include <map>
21#include <string>
22#include <utility>
23#include <vector>
1ddb8596 24
ea542140 25#include <apti18n.h>
92fcbfc1 26 /*}}}*/
8f3ba4e8
DK
27
28using std::string;
29
a02db58f 30APT_PURE string indexRecords::GetDist() const
7db98ffc
MZ
31{
32 return this->Dist;
33}
34
a02db58f 35APT_PURE string indexRecords::GetSuite() const
7014e148
MV
36{
37 return this->Suite;
38}
39
a2fdb57f
MV
40APT_PURE bool indexRecords::GetSupportsAcquireByHash() const
41{
42 return this->SupportsAcquireByHash;
43}
44
a02db58f 45APT_PURE bool indexRecords::CheckDist(const string MaybeDist) const
7db98ffc
MZ
46{
47 return (this->Dist == MaybeDist
48 || this->Suite == MaybeDist);
49}
50
a02db58f 51APT_PURE string indexRecords::GetExpectedDist() const
7db98ffc
MZ
52{
53 return this->ExpectedDist;
54}
55
a02db58f 56APT_PURE time_t indexRecords::GetValidUntil() const
1ddb8596
DK
57{
58 return this->ValidUntil;
59}
60
1dca8dc5 61APT_PURE indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
7db98ffc 62{
d90d3a05
DK
63 std::map<std::string, indexRecords::checkSum* >::const_iterator sum = Entries.find(MetaKey);
64 if (sum == Entries.end())
65 return NULL;
66 return sum->second;
7db98ffc
MZ
67}
68
a02db58f 69APT_PURE bool indexRecords::Exists(string const &MetaKey) const
e1430400
DK
70{
71 return Entries.count(MetaKey) == 1;
72}
73
92fcbfc1 74bool indexRecords::Load(const string Filename) /*{{{*/
7db98ffc 75{
233b7808
DK
76 FileFd Fd;
77 if (OpenMaybeClearSignedFile(Filename, Fd) == false)
78 return false;
79
233d79a5 80 pkgTagFile TagFile(&Fd, Fd.Size());
7db98ffc
MZ
81 if (_error->PendingError() == true)
82 {
d4cd303e 83 strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
7db98ffc
MZ
84 return false;
85 }
86
87 pkgTagSection Section;
7db98ffc 88 const char *Start, *End;
91c4cc14
DK
89 if (TagFile.Step(Section) == false)
90 {
91 strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
92 return false;
93 }
a2fdb57f
MV
94 // FIXME: find better tag name
95 SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false);
495e5cb2 96
7db98ffc
MZ
97 Suite = Section.FindS("Suite");
98 Dist = Section.FindS("Codename");
495e5cb2 99
50d98a1b
MV
100 bool FoundHashSum = false;
101 for (int i=0;HashString::SupportedHashes()[i] != NULL; i++)
7db98ffc 102 {
495e5cb2
MV
103 if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
104 continue;
105
106 string Name;
107 string Hash;
650faab0 108 unsigned long long Size;
495e5cb2
MV
109 while (Start < End)
110 {
111 if (!parseSumData(Start, End, Name, Hash, Size))
112 return false;
50d98a1b
MV
113
114 if (Entries.find(Name) == Entries.end())
115 {
116 indexRecords::checkSum *Sum = new indexRecords::checkSum;
117 Sum->MetaKeyFilename = Name;
118 Sum->Size = Size;
23397c9d
DK
119 std::string SizeStr;
120 strprintf(SizeStr, "%llu", Size);
121 Sum->Hashes.push_back(HashString("Checksum-FileSize", SizeStr));
586d8704 122 APT_IGNORE_DEPRECATED(Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);)
50d98a1b
MV
123 Entries[Name] = Sum;
124 }
125 Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
126 FoundHashSum = true;
495e5cb2 127 }
7db98ffc 128 }
495e5cb2 129
50d98a1b 130 if(FoundHashSum == false)
7db98ffc 131 {
d4cd303e 132 strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
495e5cb2 133 return false;
bbde96a6 134 }
495e5cb2 135
1ddb8596 136 string Label = Section.FindS("Label");
0323317c 137 string StrDate = Section.FindS("Date");
1ddb8596
DK
138 string StrValidUntil = Section.FindS("Valid-Until");
139
bbde96a6 140 // if we have a Valid-Until header in the Release file, use it as default
0323317c 141 if (StrValidUntil.empty() == false)
1ddb8596 142 {
0323317c 143 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
1ddb8596 144 {
0323317c 145 strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
1ddb8596
DK
146 return false;
147 }
bbde96a6
DK
148 }
149 // get the user settings for this archive and use what expires earlier
b02fffa6 150 int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
884a4c0a 151 if (Label.empty() == false)
e788a834 152 MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
89500a25
DK
153 int MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
154 if (Label.empty() == false)
e788a834 155 MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
0323317c 156
89500a25
DK
157 if(MaxAge == 0 &&
158 (MinAge == 0 || ValidUntil == 0)) // No user settings, use the one from the Release file
bbde96a6
DK
159 return true;
160
161 time_t date;
162 if (RFC1123StrToTime(StrDate.c_str(), date) == false)
163 {
164 strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
165 return false;
1ddb8596 166 }
bbde96a6 167
89500a25
DK
168 if (MinAge != 0 && ValidUntil != 0) {
169 time_t const min_date = date + MinAge;
170 if (ValidUntil < min_date)
171 ValidUntil = min_date;
172 }
173 if (MaxAge != 0) {
174 time_t const max_date = date + MaxAge;
175 if (ValidUntil == 0 || ValidUntil > max_date)
176 ValidUntil = max_date;
177 }
495e5cb2 178
7db98ffc
MZ
179 return true;
180}
92fcbfc1 181 /*}}}*/
8f3ba4e8 182std::vector<string> indexRecords::MetaKeys() /*{{{*/
a75c6a6e
MZ
183{
184 std::vector<std::string> keys;
185 std::map<string,checkSum *>::iterator I = Entries.begin();
186 while(I != Entries.end()) {
187 keys.push_back((*I).first);
188 ++I;
189 }
190 return keys;
191}
92fcbfc1
DK
192 /*}}}*/
193bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
650faab0 194 string &Name, string &Hash, unsigned long long &Size)
7db98ffc
MZ
195{
196 Name = "";
197 Hash = "";
198 Size = 0;
199 /* Skip over the first blank */
8d326119 200 while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
7db98ffc
MZ
201 && Start < End)
202 Start++;
203 if (Start >= End)
204 return false;
205
206 /* Move EntryEnd to the end of the first entry (the hash) */
207 const char *EntryEnd = Start;
208 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
209 && EntryEnd < End)
210 EntryEnd++;
211 if (EntryEnd == End)
212 return false;
213
214 Hash.append(Start, EntryEnd-Start);
215
216 /* Skip over intermediate blanks */
217 Start = EntryEnd;
218 while (*Start == '\t' || *Start == ' ')
219 Start++;
220 if (Start >= End)
221 return false;
222
223 EntryEnd = Start;
224 /* Find the end of the second entry (the size) */
225 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
226 && EntryEnd < End)
227 EntryEnd++;
228 if (EntryEnd == End)
229 return false;
230
650faab0 231 Size = strtoull (Start, NULL, 10);
7db98ffc
MZ
232
233 /* Skip over intermediate blanks */
234 Start = EntryEnd;
235 while (*Start == '\t' || *Start == ' ')
236 Start++;
237 if (Start >= End)
238 return false;
239
240 EntryEnd = Start;
241 /* Find the end of the third entry (the filename) */
8d326119
MV
242 while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
243 *EntryEnd != '\n' && *EntryEnd != '\r')
7db98ffc
MZ
244 && EntryEnd < End)
245 EntryEnd++;
246
247 Name.append(Start, EntryEnd-Start);
248 Start = EntryEnd; //prepare for the next round
249 return true;
250}
92fcbfc1 251 /*}}}*/
07cb47e7
DK
252
253APT_PURE bool indexRecords::IsAlwaysTrusted() const
254{
255 if (Trusted == ALWAYS_TRUSTED)
256 return true;
257 return false;
258}
259APT_PURE bool indexRecords::IsNeverTrusted() const
260{
261 if (Trusted == NEVER_TRUSTED)
262 return true;
263 return false;
264}
265void indexRecords::SetTrusted(bool const Trusted)
7db98ffc 266{
07cb47e7
DK
267 if (Trusted == true)
268 this->Trusted = ALWAYS_TRUSTED;
269 else
270 this->Trusted = NEVER_TRUSTED;
7db98ffc
MZ
271}
272
fa5404ab 273#if APT_PKG_ABI >= 413
07cb47e7
DK
274indexRecords::indexRecords(const string &ExpectedDist) :
275 Trusted(CHECK_TRUST), d(NULL), ExpectedDist(ExpectedDist), ValidUntil(0),
276 SupportsAcquireByHash(false)
7db98ffc
MZ
277{
278}
fa5404ab
DK
279#else
280indexRecords::indexRecords() :
281 Trusted(CHECK_TRUST), d(NULL), ExpectedDist(""), ValidUntil(0),
282 SupportsAcquireByHash(false)
283{
284}
285indexRecords::indexRecords(const string ExpectedDist) :
286 Trusted(CHECK_TRUST), d(NULL), ExpectedDist(ExpectedDist), ValidUntil(0),
287 SupportsAcquireByHash(false)
288{
289}
290#endif
862bafea
DK
291
292indexRecords::~indexRecords() {}