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