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