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