]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/debrecords.cc
Don't download "optional" files not in Release :/.
[apt.git] / apt-pkg / deb / debrecords.cc
CommitLineData
f55ece0e
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
a7c835af 3// $Id: debrecords.cc,v 1.10 2001/03/13 06:51:46 jgg Exp $
f55ece0e
AL
4/* ######################################################################
5
6 Debian Package Records - Parser for debian package records
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
ea542140
DK
11#include <config.h>
12
f55ece0e 13#include <apt-pkg/debrecords.h>
2f6a2fbb 14#include <apt-pkg/debindexfile.h>
a52f938b 15#include <apt-pkg/strutl.h>
45df0ad2 16#include <apt-pkg/aptconfiguration.h>
472ff00e 17#include <apt-pkg/fileutl.h>
453b82a3
DK
18#include <apt-pkg/cacheiterators.h>
19#include <apt-pkg/pkgcache.h>
20#include <apt-pkg/tagfile.h>
2f6a2fbb 21#include <apt-pkg/error.h>
472ff00e 22
453b82a3
DK
23#include <string.h>
24#include <algorithm>
2f6a2fbb 25#include <sstream>
453b82a3
DK
26#include <string>
27#include <vector>
a52f938b 28#include <langinfo.h>
2f6a2fbb
DK
29
30#include <apti18n.h>
f55ece0e
AL
31 /*}}}*/
32
8f3ba4e8
DK
33using std::string;
34
f55ece0e 35// RecordParser::debRecordParser - Constructor /*{{{*/
2f6a2fbb 36debRecordParser::debRecordParser(string FileName,pkgCache &Cache) :
6c55f07a 37 debRecordParserBase(), d(NULL), File(FileName, FileFd::ReadOnly, FileFd::Extension),
3650e87b 38 Tags(&File)
f55ece0e
AL
39{
40}
41 /*}}}*/
42// RecordParser::Jump - Jump to a specific record /*{{{*/
03e39e59 43bool debRecordParser::Jump(pkgCache::VerFileIterator const &Ver)
f55ece0e 44{
3d8232bf
DK
45 if (Ver.end() == true)
46 return false;
f55ece0e 47 return Tags.Jump(Section,Ver->Offset);
a52f938b
OS
48}
49bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
50{
3d8232bf
DK
51 if (Desc.end() == true)
52 return false;
a52f938b 53 return Tags.Jump(Section,Desc->Offset);
f55ece0e
AL
54}
55 /*}}}*/
2f6a2fbb
DK
56debRecordParser::~debRecordParser() {}
57
6c55f07a 58debRecordParserBase::debRecordParserBase() : Parser(), d(NULL) {}
2f6a2fbb
DK
59// RecordParserBase::FileName - Return the archive filename on the site /*{{{*/
60string debRecordParserBase::FileName()
7e798dd7 61{
7974b907 62 return Section.FindS("Filename");
7e798dd7
AL
63}
64 /*}}}*/
2f6a2fbb
DK
65// RecordParserBase::Name - Return the package name /*{{{*/
66string debRecordParserBase::Name()
b2e465d6 67{
fffa3b57
JAK
68 string Result = Section.FindS("Package");
69
70 // Normalize mixed case package names to lower case, like dpkg does
71 // See Bug#807012 for details
72 std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii);
73
74 return Result;
b2e465d6
AL
75}
76 /*}}}*/
ef1e4dfd
JF
77// RecordParserBase::Display - Return the package homepage /*{{{*/
78string debRecordParserBase::Display()
79{
80 string display(Section.FindS("Name"));
81 if (display.empty())
82 display = Section.FindS("Maemo-Display-Name");
83 return display;
84}
85 /*}}}*/
2f6a2fbb
DK
86// RecordParserBase::Homepage - Return the package homepage /*{{{*/
87string debRecordParserBase::Homepage()
f27b4a70
OS
88{
89 return Section.FindS("Homepage");
90}
91 /*}}}*/
2f6a2fbb
DK
92// RecordParserBase::Hashes - return the available archive hashes /*{{{*/
93HashStringList debRecordParserBase::Hashes() const
d9b9e9e2 94{
b3501edb
DK
95 HashStringList hashes;
96 for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
97 {
98 std::string const hash = Section.FindS(*type);
99 if (hash.empty() == false)
100 hashes.push_back(HashString(*type, hash));
101 }
5da51e0e
DK
102 auto const size = Section.FindULL("Size", 0);
103 if (size != 0)
104 hashes.FileSize(size);
b3501edb 105 return hashes;
d9b9e9e2
MV
106}
107 /*}}}*/
2f6a2fbb
DK
108// RecordParserBase::Maintainer - Return the maintainer email /*{{{*/
109string debRecordParserBase::Maintainer()
7e798dd7 110{
7974b907 111 return Section.FindS("Maintainer");
7e798dd7
AL
112}
113 /*}}}*/
2f6a2fbb
DK
114// RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/
115string debRecordParserBase::RecordField(const char *fieldName)
75bda619
MV
116{
117 return Section.FindS(fieldName);
118}
2f6a2fbb
DK
119 /*}}}*/
120// RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/
121string debRecordParserBase::ShortDesc(std::string const &lang)
7e798dd7 122{
ffe3c68e
DK
123 string const Res = LongDesc(lang);
124 if (Res.empty() == true)
125 return "";
126 string::size_type const Pos = Res.find('\n');
7e798dd7
AL
127 if (Pos == string::npos)
128 return Res;
129 return string(Res,0,Pos);
130}
131 /*}}}*/
2f6a2fbb
DK
132// RecordParserBase::LongDesc - Return a longer description /*{{{*/
133string debRecordParserBase::LongDesc(std::string const &lang)
7e798dd7 134{
ffe3c68e
DK
135 string orig;
136 if (lang.empty() == true)
137 {
138 std::vector<string> const lang = APT::Configuration::getLanguages();
139 for (std::vector<string>::const_iterator l = lang.begin();
140 l != lang.end(); ++l)
141 {
142 std::string const tagname = "Description-" + *l;
143 orig = Section.FindS(tagname.c_str());
144 if (orig.empty() == false)
145 break;
146 else if (*l == "en")
147 {
148 orig = Section.FindS("Description");
149 if (orig.empty() == false)
150 break;
151 }
152 }
153 if (orig.empty() == true)
154 orig = Section.FindS("Description");
155 }
156 else
157 {
158 std::string const tagname = "Description-" + lang;
159 orig = Section.FindS(tagname.c_str());
160 if (orig.empty() == true && lang == "en")
161 orig = Section.FindS("Description");
162 }
a52f938b 163
ffe3c68e 164 char const * const codeset = nl_langinfo(CODESET);
22c5f99a 165 if (strcmp(codeset,"US-ASCII") != 0 && strcmp(codeset,"UTF-8") != 0) {
ffe3c68e
DK
166 string dest;
167 UTF8ToCodeset(codeset, orig, &dest);
168 return dest;
169 }
a52f938b 170
a52f938b 171 return orig;
7e798dd7
AL
172}
173 /*}}}*/
c2f2b862 174
2f6a2fbb
DK
175static const char * const SourceVerSeparators = " ()";
176// RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/
177string debRecordParserBase::SourcePkg()
36375005 178{
04f232fc 179 string Res = Section.FindS("Source");
c2f2b862 180 string::size_type Pos = Res.find_first_of(SourceVerSeparators);
04f232fc
AL
181 if (Pos == string::npos)
182 return Res;
183 return string(Res,0,Pos);
36375005
AL
184}
185 /*}}}*/
2f6a2fbb
DK
186// RecordParserBase::SourceVer - Return the source version number if present /*{{{*/
187string debRecordParserBase::SourceVer()
c2f2b862
MV
188{
189 string Pkg = Section.FindS("Source");
190 string::size_type Pos = Pkg.find_first_of(SourceVerSeparators);
191 if (Pos == string::npos)
192 return "";
193
194 string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos);
195 if(VerStart == string::npos)
196 return "";
197
198 string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart);
199 if(VerEnd == string::npos)
200 // Corresponds to the case of, e.g., "foo (1.2" without a closing
201 // paren. Be liberal and guess what it means.
202 return string(Pkg, VerStart);
203 else
204 return string(Pkg, VerStart, VerEnd - VerStart);
205}
206 /*}}}*/
2f6a2fbb
DK
207// RecordParserBase::GetRec - Return the whole record /*{{{*/
208void debRecordParserBase::GetRec(const char *&Start,const char *&Stop)
b2e465d6
AL
209{
210 Section.GetSection(Start,Stop);
211}
212 /*}}}*/
f3514c6d
JF
213// RecordParserBase::Find - Locate a tag /*{{{*/
214bool debRecordParserBase::Find(const char *Tag,const char *&Start, const char *&End)
215{
216 return Section.Find(Tag,Start,End);
217}
218 /*}}}*/
2f6a2fbb 219debRecordParserBase::~debRecordParserBase() {}
862bafea 220
2f6a2fbb
DK
221bool debDebFileRecordParser::LoadContent()
222{
223 // load content only once
224 if (controlContent.empty() == false)
225 return true;
226
227 std::ostringstream content;
228 if (debDebPkgFileIndex::GetContent(content, debFileName) == false)
229 return false;
230 // add two newlines to make sure the scanner finds the section,
231 // which is usually done by pkgTagFile automatically if needed.
232 content << "\n\n";
233
234 controlContent = content.str();
3650e87b 235 if (Section.Scan(controlContent.c_str(), controlContent.length(), false) == false)
2f6a2fbb
DK
236 return _error->Error(_("Unable to parse package file %s (%d)"), debFileName.c_str(), 3);
237 return true;
238}
c8a4ce6c
DK
239bool debDebFileRecordParser::Jump(pkgCache::VerFileIterator const &) { return LoadContent(); }
240bool debDebFileRecordParser::Jump(pkgCache::DescFileIterator const &) { return LoadContent(); }
241std::string debDebFileRecordParser::FileName() { return debFileName; }
242
6c55f07a 243debDebFileRecordParser::debDebFileRecordParser(std::string FileName) : debRecordParserBase(), d(NULL), debFileName(FileName) {}
c8a4ce6c 244debDebFileRecordParser::~debDebFileRecordParser() {}