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