]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debindexfile.cc
Don't download "optional" files not in Release :/.
[apt.git] / apt-pkg / deb / debindexfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $
4 /* ######################################################################
5
6 Debian Specific sources.list types and the three sorts of Debian
7 index files.
8
9 ##################################################################### */
10 /*}}}*/
11 // Include Files /*{{{*/
12 #include <config.h>
13
14 #include <apt-pkg/debindexfile.h>
15 #include <apt-pkg/debsrcrecords.h>
16 #include <apt-pkg/deblistparser.h>
17 #include <apt-pkg/debrecords.h>
18 #include <apt-pkg/configuration.h>
19 #include <apt-pkg/error.h>
20 #include <apt-pkg/fileutl.h>
21 #include <apt-pkg/indexfile.h>
22 #include <apt-pkg/pkgcache.h>
23 #include <apt-pkg/cacheiterators.h>
24 #include <apt-pkg/pkgrecords.h>
25 #include <apt-pkg/srcrecords.h>
26
27 #include <stdio.h>
28 #include <iostream>
29 #include <string>
30 #include <sstream>
31
32 #include <sys/stat.h>
33 #include <unistd.h>
34 /*}}}*/
35
36 // Sources Index /*{{{*/
37 debSourcesIndex::debSourcesIndex(IndexTarget const &Target,bool const Trusted) :
38 pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
39 {
40 }
41 std::string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
42 pkgSrcRecords::File const &File) const
43 {
44 // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc)
45 std::string Res = Target.Description;
46 Res.erase(Target.Description.rfind(' '));
47
48 Res += " ";
49 Res += Record.Package();
50 Res += " ";
51 Res += Record.Version();
52 if (File.Type.empty() == false)
53 Res += " (" + File.Type + ")";
54 return Res;
55 }
56 pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
57 {
58 std::string const SourcesURI = IndexFileName();
59 if (FileExists(SourcesURI))
60 return new debSrcRecordParser(SourcesURI, this);
61 return NULL;
62 }
63 bool debSourcesIndex::OpenListFile(FileFd &, std::string const &)
64 {
65 return true;
66 }
67 pkgCacheListParser * debSourcesIndex::CreateListParser(FileFd &)
68 {
69 return NULL;
70 }
71 uint8_t debSourcesIndex::GetIndexFlags() const
72 {
73 return 0;
74 }
75 /*}}}*/
76 // Packages Index /*{{{*/
77 debPackagesIndex::debPackagesIndex(IndexTarget const &Target, bool const Trusted) :
78 pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
79 {
80 }
81 std::string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator const &Ver) const
82 {
83 std::string Res = Target.Description;
84 {
85 auto const space = Target.Description.rfind(' ');
86 if (space != std::string::npos)
87 Res.erase(space);
88 }
89
90 Res += " ";
91 Res += Ver.ParentPkg().Name();
92 Res += " ";
93 std::string const Dist = Target.Option(IndexTarget::RELEASE);
94 if (Dist.empty() == false && Dist[Dist.size() - 1] != '/')
95 Res.append(Ver.Arch()).append(" ");
96 Res += Ver.VerStr();
97 return Res;
98 }
99 uint8_t debPackagesIndex::GetIndexFlags() const
100 {
101 return 0;
102 }
103 /*}}}*/
104 // Translation-* Index /*{{{*/
105 debTranslationsIndex::debTranslationsIndex(IndexTarget const &Target) :
106 pkgDebianIndexTargetFile(Target, true), d(NULL)
107 {}
108 bool debTranslationsIndex::HasPackages() const
109 {
110 return Exists();
111 }
112 bool debTranslationsIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
113 {
114 if (FileExists(FileName))
115 return pkgDebianIndexTargetFile::OpenListFile(Pkg, FileName);
116 return true;
117 }
118 uint8_t debTranslationsIndex::GetIndexFlags() const
119 {
120 return pkgCache::Flag::NotSource | pkgCache::Flag::NoPackages;
121 }
122 std::string debTranslationsIndex::GetArchitecture() const
123 {
124 return std::string();
125 }
126 pkgCacheListParser * debTranslationsIndex::CreateListParser(FileFd &Pkg)
127 {
128 if (Pkg.IsOpen() == false)
129 return nullptr;
130 _error->PushToStack();
131 pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg);
132 bool const newError = _error->PendingError();
133 _error->MergeWithStack();
134 if (newError)
135 {
136 delete Parser;
137 _error->ReturnError();
138 return nullptr;
139 }
140 else
141 return Parser;
142 }
143 /*}}}*/
144 // dpkg/status Index /*{{{*/
145 debStatusIndex::debStatusIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL)
146 {
147 }
148 std::string debStatusIndex::GetArchitecture() const
149 {
150 return std::string();
151 }
152 std::string debStatusIndex::GetComponent() const
153 {
154 return "now";
155 }
156 uint8_t debStatusIndex::GetIndexFlags() const
157 {
158 return pkgCache::Flag::NotSource;
159 }
160
161 pkgCacheListParser * debStatusIndex::CreateListParser(FileFd &Pkg)
162 {
163 if (Pkg.IsOpen() == false)
164 return nullptr;
165 _error->PushToStack();
166 pkgCacheListParser * const Parser = new debStatusListParser(&Pkg);
167 bool const newError = _error->PendingError();
168 _error->MergeWithStack();
169 if (newError)
170 {
171 delete Parser;
172 _error->ReturnError();
173 return nullptr;
174 }
175 else
176 return Parser;
177 }
178 /*}}}*/
179 // DebPkgFile Index - a single .deb file as an index /*{{{*/
180 debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile)
181 : pkgDebianIndexRealFile(DebFile, true), d(NULL), DebFile(DebFile)
182 {
183 }
184 bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile)
185 {
186 struct stat Buf;
187 if (stat(debfile.c_str(), &Buf) != 0)
188 return false;
189
190 // get the control data out of the deb file via dpkg-deb -I
191 std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
192 std::vector<const char *> Args;
193 Args.push_back(dpkg.c_str());
194 Args.push_back("-I");
195 Args.push_back(debfile.c_str());
196 Args.push_back("control");
197 Args.push_back(NULL);
198 FileFd PipeFd;
199 pid_t Child;
200 if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
201 return _error->Error("Popen failed");
202
203 content << "Filename: " << debfile << "\n";
204 content << "Size: " << std::to_string(Buf.st_size) << "\n";
205 bool first_line_seen = false;
206 char buffer[1024];
207 do {
208 unsigned long long actual = 0;
209 if (PipeFd.Read(buffer, sizeof(buffer)-1, &actual) == false)
210 return _error->Errno("read", "Failed to read dpkg pipe");
211 if (actual == 0)
212 break;
213 buffer[actual] = '\0';
214 char const * b = buffer;
215 if (first_line_seen == false)
216 {
217 for (; *b != '\0' && (*b == '\n' || *b == '\r'); ++b)
218 /* skip over leading newlines */;
219 if (*b == '\0')
220 continue;
221 first_line_seen = true;
222 }
223 content << b;
224 } while(true);
225 ExecWait(Child, "Popen");
226
227 return true;
228 }
229 bool debDebPkgFileIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
230 {
231 // write the control data to a tempfile
232 if (GetTempFile("deb-file-" + flNotDir(FileName), true, &Pkg) == NULL)
233 return false;
234 std::ostringstream content;
235 if (GetContent(content, FileName) == false)
236 return false;
237 std::string const contentstr = content.str();
238 if (contentstr.empty())
239 return true;
240 if (Pkg.Write(contentstr.c_str(), contentstr.length()) == false || Pkg.Seek(0) == false)
241 return false;
242 return true;
243 }
244 pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg)
245 {
246 if (Pkg.IsOpen() == false)
247 return nullptr;
248 _error->PushToStack();
249 pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile);
250 bool const newError = _error->PendingError();
251 _error->MergeWithStack();
252 if (newError)
253 {
254 delete Parser;
255 _error->ReturnError();
256 return nullptr;
257 }
258 else
259 return Parser;
260 }
261 uint8_t debDebPkgFileIndex::GetIndexFlags() const
262 {
263 return pkgCache::Flag::LocalSource;
264 }
265 std::string debDebPkgFileIndex::GetArchitecture() const
266 {
267 return std::string();
268 }
269 std::string debDebPkgFileIndex::GetComponent() const
270 {
271 return "local-deb";
272 }
273 pkgCache::PkgFileIterator debDebPkgFileIndex::FindInCache(pkgCache &Cache) const
274 {
275 std::string const FileName = IndexFileName();
276 pkgCache::PkgFileIterator File = Cache.FileBegin();
277 for (; File.end() == false; ++File)
278 {
279 if (File.FileName() == NULL || FileName != File.FileName())
280 continue;
281 // we can't do size checks here as file size != content size
282 return File;
283 }
284
285 return File;
286 }
287 std::string debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator const &Ver) const
288 {
289 std::string Res = IndexFileName() + " ";
290 Res.append(Ver.ParentPkg().Name()).append(" ");
291 Res.append(Ver.Arch()).append(" ");
292 Res.append(Ver.VerStr());
293 return Res;
294 }
295 /*}}}*/
296 // DscFile Index - a single .dsc file as an index /*{{{*/
297 debDscFileIndex::debDscFileIndex(std::string const &DscFile)
298 : pkgDebianIndexRealFile(DscFile, true), d(NULL)
299 {
300 }
301 pkgSrcRecords::Parser *debDscFileIndex::CreateSrcParser() const
302 {
303 if (Exists() == false)
304 return NULL;
305 return new debDscRecordParser(File, this);
306 }
307 std::string debDscFileIndex::GetComponent() const
308 {
309 return "local-dsc";
310 }
311 std::string debDscFileIndex::GetArchitecture() const
312 {
313 return "source";
314 }
315 uint8_t debDscFileIndex::GetIndexFlags() const
316 {
317 return pkgCache::Flag::LocalSource;
318 }
319 /*}}}*/
320 // ControlFile Index - a directory with a debian/control file /*{{{*/
321 std::string debDebianSourceDirIndex::GetComponent() const
322 {
323 return "local-control";
324 }
325 /*}}}*/
326 // String Package Index - a string of Packages file content /*{{{*/
327 std::string debStringPackageIndex::GetArchitecture() const
328 {
329 return std::string();
330 }
331 std::string debStringPackageIndex::GetComponent() const
332 {
333 return "apt-tmp-index";
334 }
335 uint8_t debStringPackageIndex::GetIndexFlags() const
336 {
337 return pkgCache::Flag::NotSource;
338 }
339 const pkgIndexFile::Type *debStringPackageIndex::GetType() const
340 {
341 return pkgIndexFile::Type::GetType("Debian Package Index");
342 }
343 debStringPackageIndex::debStringPackageIndex(std::string const &content) :
344 pkgDebianIndexRealFile("", false), d(NULL)
345 {
346 char fn[1024];
347 std::string const tempdir = GetTempDir();
348 snprintf(fn, sizeof(fn), "%s/%s.XXXXXX", tempdir.c_str(), "apt-tmp-index");
349 int const fd = mkstemp(fn);
350 File = fn;
351 FileFd::Write(fd, content.data(), content.length());
352 close(fd);
353 }
354 debStringPackageIndex::~debStringPackageIndex()
355 {
356 RemoveFile("~debStringPackageIndex", File);
357 }
358 /*}}}*/
359
360 // Index File types for Debian /*{{{*/
361 class APT_HIDDEN debIFTypeSrc : public pkgIndexFile::Type
362 {
363 public:
364 debIFTypeSrc() {Label = "Debian Source Index";};
365 };
366 class APT_HIDDEN debIFTypePkg : public pkgIndexFile::Type
367 {
368 public:
369 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
370 {
371 return new debRecordParser(File.FileName(),*File.Cache());
372 };
373 debIFTypePkg() {Label = "Debian Package Index";};
374 };
375 class APT_HIDDEN debIFTypeTrans : public debIFTypePkg
376 {
377 public:
378 debIFTypeTrans() {Label = "Debian Translation Index";};
379 };
380 class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type
381 {
382 public:
383 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
384 {
385 return new debRecordParser(File.FileName(),*File.Cache());
386 };
387 debIFTypeStatus() {Label = "Debian dpkg status file";};
388 };
389 class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
390 {
391 public:
392 virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
393 {
394 return new debDebFileRecordParser(File.FileName());
395 };
396 debIFTypeDebPkgFile() {Label = "Debian deb file";};
397 };
398 class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type
399 {
400 public:
401 virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &DscFile) const APT_OVERRIDE
402 {
403 return new debDscRecordParser(DscFile, NULL);
404 };
405 debIFTypeDscFile() {Label = "Debian dsc file";};
406 };
407 class APT_HIDDEN debIFTypeDebianSourceDir : public pkgIndexFile::Type
408 {
409 public:
410 virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &SourceDir) const APT_OVERRIDE
411 {
412 return new debDscRecordParser(SourceDir + std::string("/debian/control"), NULL);
413 };
414 debIFTypeDebianSourceDir() {Label = "Debian control file";};
415 };
416
417 APT_HIDDEN debIFTypeSrc _apt_Src;
418 APT_HIDDEN debIFTypePkg _apt_Pkg;
419 APT_HIDDEN debIFTypeTrans _apt_Trans;
420 APT_HIDDEN debIFTypeStatus _apt_Status;
421 APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile;
422 // file based pseudo indexes
423 APT_HIDDEN debIFTypeDscFile _apt_DscFile;
424 APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir;
425
426 const pkgIndexFile::Type *debSourcesIndex::GetType() const
427 {
428 return &_apt_Src;
429 }
430 const pkgIndexFile::Type *debPackagesIndex::GetType() const
431 {
432 return &_apt_Pkg;
433 }
434 const pkgIndexFile::Type *debTranslationsIndex::GetType() const
435 {
436 return &_apt_Trans;
437 }
438 const pkgIndexFile::Type *debStatusIndex::GetType() const
439 {
440 return &_apt_Status;
441 }
442 const pkgIndexFile::Type *debDebPkgFileIndex::GetType() const
443 {
444 return &_apt_DebPkgFile;
445 }
446 const pkgIndexFile::Type *debDscFileIndex::GetType() const
447 {
448 return &_apt_DscFile;
449 }
450 const pkgIndexFile::Type *debDebianSourceDirIndex::GetType() const
451 {
452 return &_apt_DebianSourceDir;
453 }
454 /*}}}*/
455
456 debStatusIndex::~debStatusIndex() {}
457 debPackagesIndex::~debPackagesIndex() {}
458 debTranslationsIndex::~debTranslationsIndex() {}
459 debSourcesIndex::~debSourcesIndex() {}
460
461 debDebPkgFileIndex::~debDebPkgFileIndex() {}
462 debDscFileIndex::~debDscFileIndex() {}