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