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