122194e94adfc7f8a7673344405f5424bf58cd83
[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::CheckDist(const string MaybeDist) const
41 {
42 return (this->Dist == MaybeDist
43 || this->Suite == MaybeDist);
44 }
45
46 APT_PURE string indexRecords::GetExpectedDist() const
47 {
48 return this->ExpectedDist;
49 }
50
51 APT_PURE time_t indexRecords::GetValidUntil() const
52 {
53 return this->ValidUntil;
54 }
55
56 APT_PURE indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
57 {
58 std::map<std::string, indexRecords::checkSum* >::const_iterator sum = Entries.find(MetaKey);
59 if (sum == Entries.end())
60 return NULL;
61 return sum->second;
62 }
63
64 APT_PURE bool indexRecords::Exists(string const &MetaKey) const
65 {
66 return Entries.count(MetaKey) == 1;
67 }
68
69 bool indexRecords::Load(const string Filename) /*{{{*/
70 {
71 FileFd Fd;
72 if (OpenMaybeClearSignedFile(Filename, Fd) == false)
73 return false;
74
75 pkgTagFile TagFile(&Fd, Fd.Size());
76 if (_error->PendingError() == true)
77 {
78 strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
79 return false;
80 }
81
82 pkgTagSection Section;
83 const char *Start, *End;
84 if (TagFile.Step(Section) == false)
85 {
86 strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
87 return false;
88 }
89
90 Suite = Section.FindS("Suite");
91 Dist = Section.FindS("Codename");
92
93 bool FoundHashSum = false;
94 for (int i=0;HashString::SupportedHashes()[i] != NULL; i++)
95 {
96 if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
97 continue;
98
99 string Name;
100 string Hash;
101 unsigned long long Size;
102 while (Start < End)
103 {
104 if (!parseSumData(Start, End, Name, Hash, Size))
105 return false;
106
107 if (Entries.find(Name) == Entries.end())
108 {
109 indexRecords::checkSum *Sum = new indexRecords::checkSum;
110 Sum->MetaKeyFilename = Name;
111 Sum->Size = Size;
112 Entries[Name] = Sum;
113 }
114 Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
115 FoundHashSum = true;
116 }
117 }
118
119 if(FoundHashSum == false)
120 {
121 strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
122 return false;
123 }
124
125 string Label = Section.FindS("Label");
126 string StrDate = Section.FindS("Date");
127 string StrValidUntil = Section.FindS("Valid-Until");
128
129 // if we have a Valid-Until header in the Release file, use it as default
130 if (StrValidUntil.empty() == false)
131 {
132 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
133 {
134 strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
135 return false;
136 }
137 }
138 // get the user settings for this archive and use what expires earlier
139 int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
140 if (Label.empty() == false)
141 MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
142 int MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
143 if (Label.empty() == false)
144 MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
145
146 if(MaxAge == 0 &&
147 (MinAge == 0 || ValidUntil == 0)) // No user settings, use the one from the Release file
148 return true;
149
150 time_t date;
151 if (RFC1123StrToTime(StrDate.c_str(), date) == false)
152 {
153 strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
154 return false;
155 }
156
157 if (MinAge != 0 && ValidUntil != 0) {
158 time_t const min_date = date + MinAge;
159 if (ValidUntil < min_date)
160 ValidUntil = min_date;
161 }
162 if (MaxAge != 0) {
163 time_t const max_date = date + MaxAge;
164 if (ValidUntil == 0 || ValidUntil > max_date)
165 ValidUntil = max_date;
166 }
167
168 return true;
169 }
170 /*}}}*/
171 std::vector<string> indexRecords::MetaKeys() /*{{{*/
172 {
173 std::vector<std::string> keys;
174 std::map<string,checkSum *>::iterator I = Entries.begin();
175 while(I != Entries.end()) {
176 keys.push_back((*I).first);
177 ++I;
178 }
179 return keys;
180 }
181 /*}}}*/
182 bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
183 string &Name, string &Hash, unsigned long long &Size)
184 {
185 Name = "";
186 Hash = "";
187 Size = 0;
188 /* Skip over the first blank */
189 while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
190 && Start < End)
191 Start++;
192 if (Start >= End)
193 return false;
194
195 /* Move EntryEnd to the end of the first entry (the hash) */
196 const char *EntryEnd = Start;
197 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
198 && EntryEnd < End)
199 EntryEnd++;
200 if (EntryEnd == End)
201 return false;
202
203 Hash.append(Start, EntryEnd-Start);
204
205 /* Skip over intermediate blanks */
206 Start = EntryEnd;
207 while (*Start == '\t' || *Start == ' ')
208 Start++;
209 if (Start >= End)
210 return false;
211
212 EntryEnd = Start;
213 /* Find the end of the second entry (the size) */
214 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
215 && EntryEnd < End)
216 EntryEnd++;
217 if (EntryEnd == End)
218 return false;
219
220 Size = strtoull (Start, NULL, 10);
221
222 /* Skip over intermediate blanks */
223 Start = EntryEnd;
224 while (*Start == '\t' || *Start == ' ')
225 Start++;
226 if (Start >= End)
227 return false;
228
229 EntryEnd = Start;
230 /* Find the end of the third entry (the filename) */
231 while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
232 *EntryEnd != '\n' && *EntryEnd != '\r')
233 && EntryEnd < End)
234 EntryEnd++;
235
236 Name.append(Start, EntryEnd-Start);
237 Start = EntryEnd; //prepare for the next round
238 return true;
239 }
240 /*}}}*/
241 indexRecords::indexRecords()
242 {
243 }
244
245 indexRecords::indexRecords(const string ExpectedDist) :
246 ExpectedDist(ExpectedDist), ValidUntil(0)
247 {
248 }