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