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