]> git.saurik.com Git - apt.git/blame - apt-pkg/indexfile.cc
headers are for declarations only
[apt.git] / apt-pkg / indexfile.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: indexfile.cc,v 1.2.2.1 2003/12/24 23:09:17 mdz Exp $
b2e465d6
AL
4/* ######################################################################
5
6 Index File - Abstraction for an index of archive/souce file.
7
8 ##################################################################### */
9 /*}}}*/
10// Include Files /*{{{*/
ea542140
DK
11#include<config.h>
12
e3c1cfc7 13#include <apt-pkg/configuration.h>
b2e465d6
AL
14#include <apt-pkg/indexfile.h>
15#include <apt-pkg/error.h>
e3c1cfc7 16#include <apt-pkg/fileutl.h>
45df0ad2 17#include <apt-pkg/aptconfiguration.h>
453b82a3
DK
18#include <apt-pkg/pkgcache.h>
19#include <apt-pkg/cacheiterators.h>
20#include <apt-pkg/srcrecords.h>
21#include <apt-pkg/macros.h>
a52f938b 22
453b82a3
DK
23#include <string>
24#include <vector>
a52f938b 25#include <clocale>
4f333a8b 26#include <cstring>
b2e465d6
AL
27 /*}}}*/
28
29// Global list of Item supported
30static pkgIndexFile::Type *ItmList[10];
31pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
32unsigned long pkgIndexFile::Type::GlobalListLen = 0;
33
34// Type::Type - Constructor /*{{{*/
35// ---------------------------------------------------------------------
36/* */
37pkgIndexFile::Type::Type()
38{
39 ItmList[GlobalListLen] = this;
dd688285
DK
40 GlobalListLen++;
41 Label = NULL;
b2e465d6
AL
42}
43 /*}}}*/
44// Type::GetType - Locate the type by name /*{{{*/
45// ---------------------------------------------------------------------
46/* */
47pkgIndexFile::Type *pkgIndexFile::Type::GetType(const char *Type)
48{
49 for (unsigned I = 0; I != GlobalListLen; I++)
50 if (strcmp(GlobalList[I]->Label,Type) == 0)
51 return GlobalList[I];
52 return 0;
e3c1cfc7
DK
53}
54 /*}}}*/
55pkgIndexFile::pkgIndexFile(bool Trusted) : /*{{{*/
6c55f07a 56 d(NULL), Trusted(Trusted)
e3c1cfc7 57{
b2e465d6
AL
58}
59 /*}}}*/
b2e465d6 60// IndexFile::ArchiveInfo - Stub /*{{{*/
65512241 61std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator /*Ver*/) const
b2e465d6 62{
8f3ba4e8 63 return std::string();
b2e465d6
AL
64}
65 /*}}}*/
66// IndexFile::FindInCache - Stub /*{{{*/
b2e465d6
AL
67pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
68{
69 return pkgCache::PkgFileIterator(Cache);
70}
71 /*}}}*/
72// IndexFile::SourceIndex - Stub /*{{{*/
65512241
DK
73std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
74 pkgSrcRecords::File const &/*File*/) const
b2e465d6 75{
8f3ba4e8 76 return std::string();
b2e465d6
AL
77}
78 /*}}}*/
45df0ad2 79// IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
a52f938b
OS
80// ---------------------------------------------------------------------
81/* */
45df0ad2
DK
82bool pkgIndexFile::TranslationsAvailable() {
83 return (APT::Configuration::getLanguages().empty() != true);
a52f938b
OS
84}
85 /*}}}*/
45df0ad2 86// IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
a52f938b 87// ---------------------------------------------------------------------
45df0ad2
DK
88/* No intern need for this method anymore as the check for correctness
89 is already done in getLanguages(). Note also that this check is
90 rather bad (doesn't take three character like ast into account).
91 TODO: Remove method with next API break */
453b82a3 92APT_DEPRECATED bool pkgIndexFile::CheckLanguageCode(const char *Lang)
a52f938b
OS
93{
94 if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
95 return true;
96
97 if (strcmp(Lang,"C") != 0)
98 _error->Warning("Wrong language code %s", Lang);
99
100 return false;
101}
102 /*}}}*/
45df0ad2 103// IndexFile::LanguageCode - Return the Language Code /*{{{*/
a52f938b 104// ---------------------------------------------------------------------
45df0ad2
DK
105/* As we have now possibly more than one LanguageCode this method is
106 supersided by a) private classmembers or b) getLanguages().
107 TODO: Remove method with next API break */
453b82a3 108APT_DEPRECATED std::string pkgIndexFile::LanguageCode() {
45df0ad2
DK
109 if (TranslationsAvailable() == false)
110 return "";
111 return APT::Configuration::getLanguages()[0];
a52f938b
OS
112}
113 /*}}}*/
e3c1cfc7
DK
114
115// IndexTarget - Constructor /*{{{*/
116IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
117 std::string const &LongDesc, std::string const &URI, bool const IsOptional,
653ef26c
DK
118 bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
119 URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
120 IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
e3c1cfc7
DK
121{
122}
123 /*}}}*/
001c76fe 124std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
e3c1cfc7 125{
001c76fe
DK
126 std::string Key;
127 switch (EnumKey)
128 {
129#define APT_CASE(X) case X: Key = #X; break
130 APT_CASE(SITE);
131 APT_CASE(RELEASE);
132 APT_CASE(COMPONENT);
133 APT_CASE(LANGUAGE);
134 APT_CASE(ARCHITECTURE);
135 APT_CASE(BASE_URI);
136 APT_CASE(REPO_URI);
8881b11e 137 APT_CASE(TARGET_OF);
001c76fe
DK
138 APT_CASE(CREATED_BY);
139#undef APT_CASE
8881b11e 140 case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
3fd89e62
DK
141 case EXISTING_FILENAME:
142 std::string const filename = Option(FILENAME);
143 std::vector<std::string> const types = APT::Configuration::getCompressionTypes();
144 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
145 {
146 if (t->empty())
147 continue;
148 std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
149 if (FileExists(file))
150 return file;
151 }
152 return "";
001c76fe 153 }
e3c1cfc7
DK
154 std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
155 if (M == Options.end())
156 return "";
157 return M->second;
158}
159 /*}}}*/
8881b11e
DK
160std::string IndexTarget::Format(std::string format) const /*{{{*/
161{
162 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
163 {
164 format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
165 }
166 format = SubstVar(format, "$(METAKEY)", MetaKey);
167 format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
168 format = SubstVar(format, "$(DESCRIPTION)", Description);
169 format = SubstVar(format, "$(URI)", URI);
170 format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
171 return format;
172}
173 /*}}}*/
e3c1cfc7
DK
174
175pkgIndexTargetFile::pkgIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
6c55f07a 176 pkgIndexFile(Trusted), d(NULL), Target(Target)
e3c1cfc7
DK
177{
178}
179 /*}}}*/
180std::string pkgIndexTargetFile::ArchiveURI(std::string File) const/*{{{*/
181{
001c76fe 182 return Target.Option(IndexTarget::REPO_URI) + File;
e3c1cfc7
DK
183}
184 /*}}}*/
185std::string pkgIndexTargetFile::Describe(bool Short) const /*{{{*/
186{
187 if (Short)
188 return Target.Description;
189 return Target.Description + " (" + IndexFileName() + ")";
190}
191 /*}}}*/
192std::string pkgIndexTargetFile::IndexFileName() const /*{{{*/
193{
8881b11e 194 std::string const s = Target.Option(IndexTarget::FILENAME);
e3c1cfc7
DK
195 if (FileExists(s))
196 return s;
197
198 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
199 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
200 {
201 std::string p = s + '.' + *t;
202 if (FileExists(p))
203 return p;
204 }
205 return s;
206}
207 /*}}}*/
208unsigned long pkgIndexTargetFile::Size() const /*{{{*/
209{
210 unsigned long size = 0;
211
212 /* we need to ignore errors here; if the lists are absent, just return 0 */
213 _error->PushToStack();
214
215 FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
216 if (!f.Failed())
217 size = f.Size();
218
219 if (_error->PendingError() == true)
220 size = 0;
221 _error->RevertToStack();
222
223 return size;
224}
225 /*}}}*/
226bool pkgIndexTargetFile::Exists() const /*{{{*/
227{
228 return FileExists(IndexFileName());
229}
230 /*}}}*/
c8a4ce6c
DK
231
232APT_CONST pkgIndexFile::~pkgIndexFile() {}
233APT_CONST pkgIndexTargetFile::~pkgIndexTargetFile() {}