]> git.saurik.com Git - apt.git/blob - apt-pkg/indexrecords.cc
* apt-pkg/indexrecords.cc:
[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, use it
98 if (!StrValidUntil.empty())
99 {
100 // set ValidUntil based on the information in the Release file
101 if(!StrToTime(StrValidUntil, ValidUntil))
102 {
103 ErrorText = _(("Invalid 'Valid-Until' entry in Release file " + Filename).c_str());
104 return false;
105 }
106 } else {
107 // if we don't have a valid-until string, check if we have a default
108 if (!Label.empty())
109 {
110 int MaxAge = _config->FindI(string("apt::acquire::max-default-age::"+Label).c_str(),0);
111 if(MaxAge > 0 && !StrToTime(StrDate, ValidUntil))
112 {
113 ErrorText = _(("Invalid 'Date' entry in Release file " + Filename).c_str());
114 return false;
115 }
116 ValidUntil += 24*60*60*MaxAge;
117 }
118 }
119
120 return true;
121 }
122 /*}}}*/
123 vector<string> indexRecords::MetaKeys() /*{{{*/
124 {
125 std::vector<std::string> keys;
126 std::map<string,checkSum *>::iterator I = Entries.begin();
127 while(I != Entries.end()) {
128 keys.push_back((*I).first);
129 ++I;
130 }
131 return keys;
132 }
133 /*}}}*/
134 bool indexRecords::parseSumData(const char *&Start, const char *End, /*{{{*/
135 string &Name, string &Hash, size_t &Size)
136 {
137 Name = "";
138 Hash = "";
139 Size = 0;
140 /* Skip over the first blank */
141 while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
142 && Start < End)
143 Start++;
144 if (Start >= End)
145 return false;
146
147 /* Move EntryEnd to the end of the first entry (the hash) */
148 const char *EntryEnd = Start;
149 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
150 && EntryEnd < End)
151 EntryEnd++;
152 if (EntryEnd == End)
153 return false;
154
155 Hash.append(Start, EntryEnd-Start);
156
157 /* Skip over intermediate blanks */
158 Start = EntryEnd;
159 while (*Start == '\t' || *Start == ' ')
160 Start++;
161 if (Start >= End)
162 return false;
163
164 EntryEnd = Start;
165 /* Find the end of the second entry (the size) */
166 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
167 && EntryEnd < End)
168 EntryEnd++;
169 if (EntryEnd == End)
170 return false;
171
172 Size = strtol (Start, NULL, 10);
173
174 /* Skip over intermediate blanks */
175 Start = EntryEnd;
176 while (*Start == '\t' || *Start == ' ')
177 Start++;
178 if (Start >= End)
179 return false;
180
181 EntryEnd = Start;
182 /* Find the end of the third entry (the filename) */
183 while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
184 && EntryEnd < End)
185 EntryEnd++;
186
187 Name.append(Start, EntryEnd-Start);
188 Start = EntryEnd; //prepare for the next round
189 return true;
190 }
191 /*}}}*/
192 indexRecords::indexRecords()
193 {
194 }
195
196 indexRecords::indexRecords(const string ExpectedDist) :
197 ExpectedDist(ExpectedDist), ValidUntil(0)
198 {
199 }