- backport forgotten Valid-Until patch from the obsolete experimental
[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 if (TagFile.Step(Section) == false)
59 {
60 strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
61 return false;
62 }
63
64 const char *Start, *End;
65 Section.Get (Start, End, 0);
66
67 Suite = Section.FindS("Suite");
68 Dist = Section.FindS("Codename");
69
70 int i;
71 for (i=0;HashString::SupportedHashes()[i] != NULL; i++)
72 {
73 if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
74 continue;
75
76 string Name;
77 string Hash;
78 size_t Size;
79 while (Start < End)
80 {
81 if (!parseSumData(Start, End, Name, Hash, Size))
82 return false;
83 indexRecords::checkSum *Sum = new indexRecords::checkSum;
84 Sum->MetaKeyFilename = Name;
85 Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);
86 Sum->Size = Size;
87 Entries[Name] = Sum;
88 }
89 break;
90 }
91
92 if(HashString::SupportedHashes()[i] == NULL)
93 {
94 strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
95 return false;
96 }
97
98 string Label = Section.FindS("Label");
99 string StrDate = Section.FindS("Date");
100 string StrValidUntil = Section.FindS("Valid-Until");
101
102 // if we have a Valid-Until header in the Release file, use it as default
103 if (StrValidUntil.empty() == false)
104 {
105 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
106 {
107 strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
108 return false;
109 }
110 }
111 // get the user settings for this archive and use what expires earlier
112 int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
113 if (Label.empty() == true)
114 MaxAge = _config->FindI(string("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
115
116 if(MaxAge == 0) // No user settings, use the one from the Release file
117 return true;
118
119 time_t date;
120 if (RFC1123StrToTime(StrDate.c_str(), date) == false)
121 {
122 strprintf(ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
123 return false;
124 }
125 date += 24*60*60*MaxAge;
126
127 if (ValidUntil == 0 || ValidUntil > date)
128 ValidUntil = date;
129
130 return true;
131 }
132 /*}}}*/
133 vector<string> indexRecords::MetaKeys() /*{{{*/
134 {
135 std::vector<std::string> keys;
136 std::map<string,checkSum *>::iterator I = Entries.begin();
137 while(I != Entries.end()) {
138 keys.push_back((*I).first);
139 ++I;
140 }
141 return keys;
142 }
143 /*}}}*/
144 bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
145 string &Name, string &Hash, size_t &Size)
146 {
147 Name = "";
148 Hash = "";
149 Size = 0;
150 /* Skip over the first blank */
151 while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
152 && Start < End)
153 Start++;
154 if (Start >= End)
155 return false;
156
157 /* Move EntryEnd to the end of the first entry (the hash) */
158 const char *EntryEnd = Start;
159 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
160 && EntryEnd < End)
161 EntryEnd++;
162 if (EntryEnd == End)
163 return false;
164
165 Hash.append(Start, EntryEnd-Start);
166
167 /* Skip over intermediate blanks */
168 Start = EntryEnd;
169 while (*Start == '\t' || *Start == ' ')
170 Start++;
171 if (Start >= End)
172 return false;
173
174 EntryEnd = Start;
175 /* Find the end of the second entry (the size) */
176 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
177 && EntryEnd < End)
178 EntryEnd++;
179 if (EntryEnd == End)
180 return false;
181
182 Size = strtol (Start, NULL, 10);
183
184 /* Skip over intermediate blanks */
185 Start = EntryEnd;
186 while (*Start == '\t' || *Start == ' ')
187 Start++;
188 if (Start >= End)
189 return false;
190
191 EntryEnd = Start;
192 /* Find the end of the third entry (the filename) */
193 while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
194 && EntryEnd < End)
195 EntryEnd++;
196
197 Name.append(Start, EntryEnd-Start);
198 Start = EntryEnd; //prepare for the next round
199 return true;
200 }
201 /*}}}*/
202 indexRecords::indexRecords()
203 {
204 }
205
206 indexRecords::indexRecords(const string ExpectedDist) :
207 ExpectedDist(ExpectedDist), ValidUntil(0)
208 {
209 }