]> git.saurik.com Git - apt.git/blob - apt-pkg/indexfile.cc
33fb48e35fae2dfcdb0a1890e6437c5cc4930cdd
[apt.git] / apt-pkg / indexfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: indexfile.cc,v 1.2.2.1 2003/12/24 23:09:17 mdz Exp $
4 /* ######################################################################
5
6 Index File - Abstraction for an index of archive/souce file.
7
8 ##################################################################### */
9 /*}}}*/
10 // Include Files /*{{{*/
11 #include<config.h>
12
13 #include <apt-pkg/configuration.h>
14 #include <apt-pkg/indexfile.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/aptconfiguration.h>
18 #include <apt-pkg/pkgcache.h>
19 #include <apt-pkg/cacheiterators.h>
20 #include <apt-pkg/srcrecords.h>
21 #include <apt-pkg/macros.h>
22
23 #include <string>
24 #include <vector>
25 #include <clocale>
26 #include <cstring>
27 /*}}}*/
28
29 // Global list of Item supported
30 static pkgIndexFile::Type *ItmList[10];
31 pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
32 unsigned long pkgIndexFile::Type::GlobalListLen = 0;
33
34 // Type::Type - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* */
37 pkgIndexFile::Type::Type()
38 {
39 ItmList[GlobalListLen] = this;
40 GlobalListLen++;
41 Label = NULL;
42 }
43 /*}}}*/
44 // Type::GetType - Locate the type by name /*{{{*/
45 // ---------------------------------------------------------------------
46 /* */
47 pkgIndexFile::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;
53 }
54 /*}}}*/
55 pkgIndexFile::pkgIndexFile(bool Trusted) : /*{{{*/
56 Trusted(Trusted)
57 {
58 }
59 /*}}}*/
60 // IndexFile::ArchiveInfo - Stub /*{{{*/
61 std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator /*Ver*/) const
62 {
63 return std::string();
64 }
65 /*}}}*/
66 // IndexFile::FindInCache - Stub /*{{{*/
67 pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
68 {
69 return pkgCache::PkgFileIterator(Cache);
70 }
71 /*}}}*/
72 // IndexFile::SourceIndex - Stub /*{{{*/
73 std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
74 pkgSrcRecords::File const &/*File*/) const
75 {
76 return std::string();
77 }
78 /*}}}*/
79 // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
80 // ---------------------------------------------------------------------
81 /* */
82 bool pkgIndexFile::TranslationsAvailable() {
83 return (APT::Configuration::getLanguages().empty() != true);
84 }
85 /*}}}*/
86 // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
87 // ---------------------------------------------------------------------
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 */
92 APT_DEPRECATED bool pkgIndexFile::CheckLanguageCode(const char *Lang)
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 /*}}}*/
103 // IndexFile::LanguageCode - Return the Language Code /*{{{*/
104 // ---------------------------------------------------------------------
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 */
108 APT_DEPRECATED std::string pkgIndexFile::LanguageCode() {
109 if (TranslationsAvailable() == false)
110 return "";
111 return APT::Configuration::getLanguages()[0];
112 }
113 /*}}}*/
114
115 // IndexTarget - Constructor /*{{{*/
116 IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
117 std::string const &LongDesc, std::string const &URI, bool const IsOptional,
118 std::map<std::string, std::string> const &Options) :
119 URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey), IsOptional(IsOptional), Options(Options)
120 {
121 }
122 /*}}}*/
123 std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
124 {
125 std::string Key;
126 switch (EnumKey)
127 {
128 #define APT_CASE(X) case X: Key = #X; break
129 APT_CASE(SITE);
130 APT_CASE(RELEASE);
131 APT_CASE(COMPONENT);
132 APT_CASE(LANGUAGE);
133 APT_CASE(ARCHITECTURE);
134 APT_CASE(BASE_URI);
135 APT_CASE(REPO_URI);
136 APT_CASE(TARGET_OF);
137 APT_CASE(CREATED_BY);
138 #undef APT_CASE
139 case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
140 }
141 std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
142 if (M == Options.end())
143 return "";
144 return M->second;
145 }
146 /*}}}*/
147 std::string IndexTarget::Format(std::string format) const /*{{{*/
148 {
149 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
150 {
151 format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
152 }
153 format = SubstVar(format, "$(METAKEY)", MetaKey);
154 format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
155 format = SubstVar(format, "$(DESCRIPTION)", Description);
156 format = SubstVar(format, "$(URI)", URI);
157 format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
158 return format;
159 }
160 /*}}}*/
161
162 pkgIndexTargetFile::pkgIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
163 pkgIndexFile(Trusted), Target(Target)
164 {
165 }
166 /*}}}*/
167 std::string pkgIndexTargetFile::ArchiveURI(std::string File) const/*{{{*/
168 {
169 return Target.Option(IndexTarget::REPO_URI) + File;
170 }
171 /*}}}*/
172 std::string pkgIndexTargetFile::Describe(bool Short) const /*{{{*/
173 {
174 if (Short)
175 return Target.Description;
176 return Target.Description + " (" + IndexFileName() + ")";
177 }
178 /*}}}*/
179 std::string pkgIndexTargetFile::IndexFileName() const /*{{{*/
180 {
181 std::string const s = Target.Option(IndexTarget::FILENAME);
182 if (FileExists(s))
183 return s;
184
185 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
186 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
187 {
188 std::string p = s + '.' + *t;
189 if (FileExists(p))
190 return p;
191 }
192 return s;
193 }
194 /*}}}*/
195 unsigned long pkgIndexTargetFile::Size() const /*{{{*/
196 {
197 unsigned long size = 0;
198
199 /* we need to ignore errors here; if the lists are absent, just return 0 */
200 _error->PushToStack();
201
202 FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
203 if (!f.Failed())
204 size = f.Size();
205
206 if (_error->PendingError() == true)
207 size = 0;
208 _error->RevertToStack();
209
210 return size;
211 }
212 /*}}}*/
213 bool pkgIndexTargetFile::Exists() const /*{{{*/
214 {
215 return FileExists(IndexFileName());
216 }
217 /*}}}*/