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