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