* merged with the apt--mvo branch
[ntk/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 /*{{{*/
6#ifdef __GNUG__
7#pragma implementation "apt-pkg/indexrecords.h"
8#endif
9#include <apt-pkg/indexrecords.h>
10#include <apt-pkg/tagfile.h>
11#include <apt-pkg/error.h>
12#include <apt-pkg/strutl.h>
13#include <apti18n.h>
14#include <sys/stat.h>
15
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
32const indexRecords::checkSum *indexRecords::Lookup(const string MetaKey)
33{
34 return Entries[MetaKey];
35}
36
37bool indexRecords::Load(const string Filename)
38{
39 FileFd Fd(Filename, FileFd::ReadOnly);
40 pkgTagFile TagFile(&Fd, Fd.Size() + 256); // XXX
41 if (_error->PendingError() == true)
42 {
43 ErrorText = _(("Unable to parse Release file " + Filename).c_str());
44 return false;
45 }
46
47 pkgTagSection Section;
48 if (TagFile.Step(Section) == false)
49 {
50 ErrorText = _(("No sections in Release file " + Filename).c_str());
51 return false;
52 }
53
54 const char *Start, *End;
55 Section.Get (Start, End, 0);
56 Suite = Section.FindS("Suite");
57 Dist = Section.FindS("Codename");
58// if (Dist.empty())
59// {
60// ErrorText = _(("No Codename entry in Release file " + Filename).c_str());
61// return false;
62// }
63 if (!Section.Find("MD5Sum", Start, End))
64 {
65 ErrorText = _(("No MD5Sum entry in Release file " + Filename).c_str());
66 return false;
67 }
68 string Name;
69 string MD5Hash;
70 size_t Size;
71 while (Start < End)
72 {
73 if (!parseSumData(Start, End, Name, MD5Hash, Size))
74 return false;
75 indexRecords::checkSum *Sum = new indexRecords::checkSum;
76 Sum->MetaKeyFilename = Name;
77 Sum->MD5Hash = MD5Hash;
78 Sum->Size = Size;
79 Entries[Name] = Sum;
80 }
81
82 string Strdate = Section.FindS("Date"); // FIXME: verify this somehow?
83 return true;
84}
85
a75c6a6e
MZ
86vector<string> indexRecords::MetaKeys()
87{
88 std::vector<std::string> keys;
89 std::map<string,checkSum *>::iterator I = Entries.begin();
90 while(I != Entries.end()) {
91 keys.push_back((*I).first);
92 ++I;
93 }
94 return keys;
95}
96
7db98ffc
MZ
97bool indexRecords::parseSumData(const char *&Start, const char *End,
98 string &Name, string &Hash, size_t &Size)
99{
100 Name = "";
101 Hash = "";
102 Size = 0;
103 /* Skip over the first blank */
104 while ((*Start == '\t' || *Start == ' ' || *Start == '\n')
105 && Start < End)
106 Start++;
107 if (Start >= End)
108 return false;
109
110 /* Move EntryEnd to the end of the first entry (the hash) */
111 const char *EntryEnd = Start;
112 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
113 && EntryEnd < End)
114 EntryEnd++;
115 if (EntryEnd == End)
116 return false;
117
118 Hash.append(Start, EntryEnd-Start);
119
120 /* Skip over intermediate blanks */
121 Start = EntryEnd;
122 while (*Start == '\t' || *Start == ' ')
123 Start++;
124 if (Start >= End)
125 return false;
126
127 EntryEnd = Start;
128 /* Find the end of the second entry (the size) */
129 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
130 && EntryEnd < End)
131 EntryEnd++;
132 if (EntryEnd == End)
133 return false;
134
135 Size = strtol (Start, NULL, 10);
136
137 /* Skip over intermediate blanks */
138 Start = EntryEnd;
139 while (*Start == '\t' || *Start == ' ')
140 Start++;
141 if (Start >= End)
142 return false;
143
144 EntryEnd = Start;
145 /* Find the end of the third entry (the filename) */
146 while ((*EntryEnd != '\t' && *EntryEnd != ' ' && *EntryEnd != '\n')
147 && EntryEnd < End)
148 EntryEnd++;
149
150 Name.append(Start, EntryEnd-Start);
151 Start = EntryEnd; //prepare for the next round
152 return true;
153}
154
155indexRecords::indexRecords()
156{
157}
158
159indexRecords::indexRecords(const string ExpectedDist) :
160 ExpectedDist(ExpectedDist)
161{
162}