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