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