]> git.saurik.com Git - apt.git/blob - apt-pkg/indexfile.cc
keep compressed indexes in a low-cost format
[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/pkgcachegen.h>
20 #include <apt-pkg/cacheiterators.h>
21 #include <apt-pkg/srcrecords.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/progress.h>
24 #include <apt-pkg/deblistparser.h>
25 #include <apt-pkg/macros.h>
26
27 #include <apt-pkg/debindexfile.h>
28
29 #include <sys/stat.h>
30
31 #include <string>
32 #include <vector>
33 #include <clocale>
34 #include <cstring>
35 #include <memory>
36 /*}}}*/
37
38 // Global list of Item supported
39 static pkgIndexFile::Type *ItmList[10];
40 pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
41 unsigned long pkgIndexFile::Type::GlobalListLen = 0;
42
43 // Type::Type - Constructor /*{{{*/
44 // ---------------------------------------------------------------------
45 /* */
46 pkgIndexFile::Type::Type()
47 {
48 ItmList[GlobalListLen] = this;
49 GlobalListLen++;
50 Label = NULL;
51 }
52 /*}}}*/
53 // Type::GetType - Locate the type by name /*{{{*/
54 // ---------------------------------------------------------------------
55 /* */
56 pkgIndexFile::Type *pkgIndexFile::Type::GetType(const char *Type)
57 {
58 for (unsigned I = 0; I != GlobalListLen; I++)
59 if (strcmp(GlobalList[I]->Label,Type) == 0)
60 return GlobalList[I];
61 return 0;
62 }
63 /*}}}*/
64 pkgIndexFile::pkgIndexFile(bool const Trusted) : /*{{{*/
65 d(NULL), Trusted(Trusted)
66 {
67 }
68 /*}}}*/
69 // IndexFile::ArchiveInfo - Stub /*{{{*/
70 std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator const &Ver) const
71 {
72 debDebPkgFileIndex const * const debfile = dynamic_cast<debDebPkgFileIndex const*>(this);
73 if (debfile != nullptr)
74 return debfile->ArchiveInfo_impl(Ver);
75 return std::string();
76 }
77 /*}}}*/
78 // IndexFile::FindInCache - Stub /*{{{*/
79 pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
80 {
81 return pkgCache::PkgFileIterator(Cache);
82 }
83 /*}}}*/
84 // IndexFile::SourceIndex - Stub /*{{{*/
85 std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
86 pkgSrcRecords::File const &/*File*/) const
87 {
88 return std::string();
89 }
90 /*}}}*/
91 // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
92 bool pkgIndexFile::TranslationsAvailable() {
93 return (APT::Configuration::getLanguages().empty() != true);
94 }
95 /*}}}*/
96 // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
97 bool pkgIndexFile::CheckLanguageCode(const char * const Lang)
98 {
99 if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
100 return true;
101
102 if (strcmp(Lang,"C") != 0)
103 _error->Warning("Wrong language code %s", Lang);
104
105 return false;
106 }
107 /*}}}*/
108 // IndexFile::LanguageCode - Return the Language Code /*{{{*/
109 std::string pkgIndexFile::LanguageCode() {
110 APT_IGNORE_DEPRECATED_PUSH
111 if (TranslationsAvailable() == false)
112 return "";
113 return APT::Configuration::getLanguages()[0];
114 APT_IGNORE_DEPRECATED_POP
115 }
116 /*}}}*/
117
118 // IndexTarget - Constructor /*{{{*/
119 IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
120 std::string const &LongDesc, std::string const &URI, bool const IsOptional,
121 bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
122 URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
123 IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
124 {
125 }
126 /*}}}*/
127 std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
128 {
129 std::string Key;
130 switch (EnumKey)
131 {
132 #define APT_CASE(X) case X: Key = #X; break
133 APT_CASE(SITE);
134 APT_CASE(RELEASE);
135 APT_CASE(COMPONENT);
136 APT_CASE(LANGUAGE);
137 APT_CASE(ARCHITECTURE);
138 APT_CASE(BASE_URI);
139 APT_CASE(REPO_URI);
140 APT_CASE(TARGET_OF);
141 APT_CASE(CREATED_BY);
142 APT_CASE(PDIFFS);
143 APT_CASE(DEFAULTENABLED);
144 APT_CASE(COMPRESSIONTYPES);
145 APT_CASE(SOURCESENTRY);
146 APT_CASE(BY_HASH);
147 APT_CASE(KEEPCOMPRESSEDAS);
148 #undef APT_CASE
149 case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
150 case EXISTING_FILENAME:
151 std::string const filename = Option(FILENAME);
152 std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
153 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
154 {
155 if (t->empty())
156 continue;
157 std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
158 if (FileExists(file))
159 return file;
160 }
161 return "";
162 }
163 std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
164 if (M == Options.end())
165 return "";
166 return M->second;
167 }
168 /*}}}*/
169 bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
170 {
171 return StringToBool(Option(EnumKey));
172 }
173 /*}}}*/
174 std::string IndexTarget::Format(std::string format) const /*{{{*/
175 {
176 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
177 {
178 format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
179 }
180 format = SubstVar(format, "$(METAKEY)", MetaKey);
181 format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
182 format = SubstVar(format, "$(DESCRIPTION)", Description);
183 format = SubstVar(format, "$(URI)", URI);
184 format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
185 return format;
186 }
187 /*}}}*/
188
189 pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
190 pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
191 {
192 }
193 /*}}}*/
194 std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
195 {
196 return Target.Option(IndexTarget::REPO_URI) + File;
197 }
198 /*}}}*/
199 std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
200 {
201 if (Short)
202 return Target.Description;
203 return Target.Description + " (" + IndexFileName() + ")";
204 }
205 /*}}}*/
206 std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
207 {
208 std::string const s = Target.Option(IndexTarget::FILENAME);
209 if (FileExists(s))
210 return s;
211
212 std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
213 for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
214 {
215 std::string p = s + '.' + *t;
216 if (FileExists(p))
217 return p;
218 }
219 return s;
220 }
221 /*}}}*/
222 unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
223 {
224 unsigned long size = 0;
225
226 /* we need to ignore errors here; if the lists are absent, just return 0 */
227 _error->PushToStack();
228
229 FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
230 if (!f.Failed())
231 size = f.Size();
232
233 if (_error->PendingError() == true)
234 size = 0;
235 _error->RevertToStack();
236
237 return size;
238 }
239 /*}}}*/
240 bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
241 {
242 return FileExists(IndexFileName());
243 }
244 /*}}}*/
245 std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
246 {
247 return Target.Option(IndexTarget::ARCHITECTURE);
248 }
249 /*}}}*/
250 std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
251 {
252 return Target.Option(IndexTarget::COMPONENT);
253 }
254 /*}}}*/
255 bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
256 {
257 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
258 return _error->Error("Problem opening %s",FileName.c_str());
259 return true;
260 }
261 /*}}}*/
262 std::string pkgDebianIndexTargetFile::GetProgressDescription() const
263 {
264 return Target.Description;
265 }
266
267 pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
268 pkgDebianIndexFile(Trusted), d(NULL)
269 {
270 if (pFile == "/nonexistent/stdin")
271 File = pFile;
272 else
273 File = flAbsPath(pFile);
274 }
275 /*}}}*/
276 // IndexRealFile::Size - Return the size of the index /*{{{*/
277 unsigned long pkgDebianIndexRealFile::Size() const
278 {
279 struct stat S;
280 if (stat(File.c_str(),&S) != 0)
281 return 0;
282 return S.st_size;
283 }
284 /*}}}*/
285 bool pkgDebianIndexRealFile::Exists() const /*{{{*/
286 {
287 return FileExists(File);
288 }
289 /*}}}*/
290 std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
291 {
292 return File;
293 }
294 /*}}}*/
295 std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
296 {
297 return "file:" + File;
298 }
299 /*}}}*/
300 std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
301 {
302 return File;
303 }
304 /*}}}*/
305 std::string pkgDebianIndexRealFile::GetProgressDescription() const
306 {
307 return File;
308 }
309 bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
310 {
311 if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::None) == false)
312 return _error->Error("Problem opening %s",FileName.c_str());
313 return true;
314 }
315 /*}}}*/
316
317 pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
318 {
319 }
320 pkgDebianIndexFile::~pkgDebianIndexFile()
321 {
322 }
323 pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
324 {
325 if (Pkg.IsOpen() == false)
326 return NULL;
327 _error->PushToStack();
328 pkgCacheListParser * const Parser = new debListParser(&Pkg);
329 bool const newError = _error->PendingError();
330 _error->MergeWithStack();
331 return newError ? NULL : Parser;
332 }
333 bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
334 {
335 std::string const PackageFile = IndexFileName();
336 FileFd Pkg;
337 if (OpenListFile(Pkg, PackageFile) == false)
338 return false;
339 _error->PushToStack();
340 std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
341 bool const newError = _error->PendingError();
342 _error->MergeWithStack();
343 if (newError == false && Parser == nullptr)
344 return true;
345 if (Parser == NULL)
346 return false;
347
348 if (Prog != NULL)
349 Prog->SubProgress(0, GetProgressDescription());
350
351 if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
352 return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
353
354 // Store the IMS information
355 pkgCache::PkgFileIterator File = Gen.GetCurFile();
356 pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
357 File->Size = Pkg.FileSize();
358 File->mtime = Pkg.ModificationTime();
359
360 if (Gen.MergeList(*Parser) == false)
361 return _error->Error("Problem with MergeList %s",PackageFile.c_str());
362 return true;
363 }
364 pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
365 {
366 std::string const FileName = IndexFileName();
367 pkgCache::PkgFileIterator File = Cache.FileBegin();
368 for (; File.end() == false; ++File)
369 {
370 if (File.FileName() == NULL || FileName != File.FileName())
371 continue;
372
373 struct stat St;
374 if (stat(File.FileName(),&St) != 0)
375 {
376 if (_config->FindB("Debug::pkgCacheGen", false))
377 std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
378 return pkgCache::PkgFileIterator(Cache);
379 }
380 if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
381 {
382 if (_config->FindB("Debug::pkgCacheGen", false))
383 std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
384 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
385 << ") doesn't match for " << File.FileName() << std::endl;
386 return pkgCache::PkgFileIterator(Cache);
387 }
388 return File;
389 }
390
391 return File;
392 }
393
394 APT_CONST pkgIndexFile::~pkgIndexFile() {}
395 APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
396 APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}