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