]> git.saurik.com Git - apt.git/blame - apt-pkg/indexrecords.cc
merged lp:~mvo/apt/apt-get-changelog
[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 /*{{{*/
7db98ffc
MZ
6#include <apt-pkg/indexrecords.h>
7#include <apt-pkg/tagfile.h>
8#include <apt-pkg/error.h>
9#include <apt-pkg/strutl.h>
1ddb8596 10#include <apt-pkg/configuration.h>
7db98ffc
MZ
11#include <apti18n.h>
12#include <sys/stat.h>
1ddb8596
DK
13#include <clocale>
14
92fcbfc1 15 /*}}}*/
7db98ffc
MZ
16string indexRecords::GetDist() const
17{
18 return this->Dist;
19}
20
21bool indexRecords::CheckDist(const string MaybeDist) const
22{
23 return (this->Dist == MaybeDist
24 || this->Suite == MaybeDist);
25}
26
27string indexRecords::GetExpectedDist() const
28{
29 return this->ExpectedDist;
30}
31
1ddb8596
DK
32time_t indexRecords::GetValidUntil() const
33{
34 return this->ValidUntil;
35}
36
7db98ffc
MZ
37const indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
38{
39 return Entries[MetaKey];
40}
41
e1430400
DK
42bool indexRecords::Exists(string const &MetaKey) const
43{
44 return Entries.count(MetaKey) == 1;
45}
46
92fcbfc1 47bool indexRecords::Load(const string Filename) /*{{{*/
7db98ffc
MZ
48{
49 FileFd Fd(Filename, FileFd::ReadOnly);
50 pkgTagFile TagFile(&Fd, Fd.Size() + 256); // XXX
51 if (_error->PendingError() == true)
52 {
d4cd303e 53 strprintf(ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
7db98ffc
MZ
54 return false;
55 }
56
57 pkgTagSection Section;
58 if (TagFile.Step(Section) == false)
59 {
d4cd303e 60 strprintf(ErrorText, _("No sections in Release file %s"), Filename.c_str());
7db98ffc
MZ
61 return false;
62 }
63
64 const char *Start, *End;
65 Section.Get (Start, End, 0);
495e5cb2 66
7db98ffc
MZ
67 Suite = Section.FindS("Suite");
68 Dist = Section.FindS("Codename");
495e5cb2
MV
69
70 int i;
71 for (i=0;HashString::SupportedHashes()[i] != NULL; i++)
7db98ffc 72 {
495e5cb2
MV
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;
7db98ffc 90 }
495e5cb2
MV
91
92 if(HashString::SupportedHashes()[i] == NULL)
7db98ffc 93 {
d4cd303e 94 strprintf(ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
495e5cb2 95 return false;
bbde96a6 96 }
495e5cb2 97
1ddb8596 98 string Label = Section.FindS("Label");
0323317c 99 string StrDate = Section.FindS("Date");
1ddb8596
DK
100 string StrValidUntil = Section.FindS("Valid-Until");
101
bbde96a6 102 // if we have a Valid-Until header in the Release file, use it as default
0323317c 103 if (StrValidUntil.empty() == false)
1ddb8596 104 {
0323317c 105 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
1ddb8596 106 {
0323317c 107 strprintf(ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
1ddb8596
DK
108 return false;
109 }
bbde96a6
DK
110 }
111 // get the user settings for this archive and use what expires earlier
b02fffa6 112 int MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
bbde96a6 113 if (Label.empty() == true)
b02fffa6 114 MaxAge = _config->FindI(string("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
0323317c 115
bbde96a6
DK
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;
1ddb8596 124 }
bbde96a6
DK
125 date += 24*60*60*MaxAge;
126
127 if (ValidUntil == 0 || ValidUntil > date)
128 ValidUntil = date;
495e5cb2 129
7db98ffc
MZ
130 return true;
131}
92fcbfc1
DK
132 /*}}}*/
133vector<string> indexRecords::MetaKeys() /*{{{*/
a75c6a6e
MZ
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}
92fcbfc1
DK
143 /*}}}*/
144bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
7db98ffc
MZ
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}
92fcbfc1 201 /*}}}*/
7db98ffc
MZ
202indexRecords::indexRecords()
203{
204}
205
206indexRecords::indexRecords(const string ExpectedDist) :
1ddb8596 207 ExpectedDist(ExpectedDist), ValidUntil(0)
7db98ffc
MZ
208{
209}