]> git.saurik.com Git - apt.git/blob - apt-pkg/indexrecords.cc
move ByHash into its own function
[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 Entries[Name] = Sum;
120 }
121 Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
122 FoundHashSum = true;
123 }
124 }
125
126 if(FoundHashSum == false)
127 {
128 strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
129 return false;
130 }
131
132 string Label = Section.FindS("Label");
133 string StrDate = Section.FindS("Date");
134 string StrValidUntil = Section.FindS("Valid-Until");
135
136 // if we have a Valid-Until header in the Release file, use it as default
137 if (StrValidUntil.empty() == false)
138 {
139 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
140 {
141 strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
142 return false;
143 }
144 }
145 // get the user settings for this archive and use what expires earlier
146 int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
147 if (Label.empty() == false)
148 MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
149 int MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
150 if (Label.empty() == false)
151 MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
152
153 if(MaxAge == 0 &&
154 (MinAge == 0 || ValidUntil == 0)) // No user settings, use the one from the Release file
155 return true;
156
157 time_t date;
158 if (RFC1123StrToTime(StrDate.c_str(), date) == false)
159 {
160 strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
161 return false;
162 }
163
164 if (MinAge != 0 && ValidUntil != 0) {
165 time_t const min_date = date + MinAge;
166 if (ValidUntil < min_date)
167 ValidUntil = min_date;
168 }
169 if (MaxAge != 0) {
170 time_t const max_date = date + MaxAge;
171 if (ValidUntil == 0 || ValidUntil > max_date)
172 ValidUntil = max_date;
173 }
174
175 return true;
176 }
177 /*}}}*/
178 std::vector<string> indexRecords::MetaKeys() /*{{{*/
179 {
180 std::vector<std::string> keys;
181 std::map<string,checkSum *>::iterator I = Entries.begin();
182 while(I != Entries.end()) {
183 keys.push_back((*I).first);
184 ++I;
185 }
186 return keys;
187 }
188 /*}}}*/
189 bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
190 string &Name, string &Hash, unsigned long long &Size)
191 {
192 Name = "";
193 Hash = "";
194 Size = 0;
195 /* Skip over the first blank */
196 while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
197 && Start < End)
198 Start++;
199 if (Start >= End)
200 return false;
201
202 /* Move EntryEnd to the end of the first entry (the hash) */
203 const char *EntryEnd = Start;
204 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
205 && EntryEnd < End)
206 EntryEnd++;
207 if (EntryEnd == End)
208 return false;
209
210 Hash.append(Start, EntryEnd-Start);
211
212 /* Skip over intermediate blanks */
213 Start = EntryEnd;
214 while (*Start == '\t' || *Start == ' ')
215 Start++;
216 if (Start >= End)
217 return false;
218
219 EntryEnd = Start;
220 /* Find the end of the second entry (the size) */
221 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
222 && EntryEnd < End)
223 EntryEnd++;
224 if (EntryEnd == End)
225 return false;
226
227 Size = strtoull (Start, NULL, 10);
228
229 /* Skip over intermediate blanks */
230 Start = EntryEnd;
231 while (*Start == '\t' || *Start == ' ')
232 Start++;
233 if (Start >= End)
234 return false;
235
236 EntryEnd = Start;
237 /* Find the end of the third entry (the filename) */
238 while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
239 *EntryEnd != '\n' && *EntryEnd != '\r')
240 && EntryEnd < End)
241 EntryEnd++;
242
243 Name.append(Start, EntryEnd-Start);
244 Start = EntryEnd; //prepare for the next round
245 return true;
246 }
247 /*}}}*/
248 indexRecords::indexRecords()
249 {
250 }
251
252 indexRecords::indexRecords(const string ExpectedDist) :
253 ExpectedDist(ExpectedDist), ValidUntil(0), SupportsAcquireByHash(false)
254 {
255 }