debindexfile.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. Debian Specific sources.list types and the three sorts of Debian
  6. index files.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/debindexfile.h>
  12. #include <apt-pkg/debsrcrecords.h>
  13. #include <apt-pkg/deblistparser.h>
  14. #include <apt-pkg/debrecords.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/progress.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/acquire-item.h>
  20. #include <apt-pkg/debmetaindex.h>
  21. #include <apt-pkg/gpgv.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <apt-pkg/indexfile.h>
  24. #include <apt-pkg/mmap.h>
  25. #include <apt-pkg/pkgcache.h>
  26. #include <apt-pkg/cacheiterators.h>
  27. #include <apt-pkg/pkgcachegen.h>
  28. #include <apt-pkg/pkgrecords.h>
  29. #include <apt-pkg/srcrecords.h>
  30. #include <apt-pkg/sptr.h>
  31. #include <stdio.h>
  32. #include <iostream>
  33. #include <sstream>
  34. #include <string>
  35. #include <memory>
  36. #include <sys/stat.h>
  37. /*}}}*/
  38. // Sources Index /*{{{*/
  39. debSourcesIndex::debSourcesIndex(IndexTarget const &Target,bool const Trusted) :
  40. pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
  41. {
  42. }
  43. std::string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
  44. pkgSrcRecords::File const &File) const
  45. {
  46. // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc)
  47. std::string Res = Target.Description;
  48. Res.erase(Target.Description.rfind(' '));
  49. Res += " ";
  50. Res += Record.Package();
  51. Res += " ";
  52. Res += Record.Version();
  53. if (File.Type.empty() == false)
  54. Res += " (" + File.Type + ")";
  55. return Res;
  56. }
  57. pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
  58. {
  59. std::string const SourcesURI = IndexFileName();
  60. if (FileExists(SourcesURI))
  61. return new debSrcRecordParser(SourcesURI, this);
  62. return NULL;
  63. }
  64. bool debSourcesIndex::OpenListFile(FileFd &, std::string const &)
  65. {
  66. return true;
  67. }
  68. pkgCacheListParser * debSourcesIndex::CreateListParser(FileFd &)
  69. {
  70. return NULL;
  71. }
  72. uint8_t debSourcesIndex::GetIndexFlags() const
  73. {
  74. return 0;
  75. }
  76. /*}}}*/
  77. // Packages Index /*{{{*/
  78. debPackagesIndex::debPackagesIndex(IndexTarget const &Target, bool const Trusted) :
  79. pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
  80. {
  81. }
  82. std::string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator const &Ver) const
  83. {
  84. std::string Res = Target.Description;
  85. Res.erase(Target.Description.rfind(' '));
  86. Res += " ";
  87. Res += Ver.ParentPkg().Name();
  88. Res += " ";
  89. std::string const Dist = Target.Option(IndexTarget::RELEASE);
  90. if (Dist.empty() == false && Dist[Dist.size() - 1] != '/')
  91. Res.append(Ver.Arch()).append(" ");
  92. Res += Ver.VerStr();
  93. return Res;
  94. }
  95. uint8_t debPackagesIndex::GetIndexFlags() const
  96. {
  97. return 0;
  98. }
  99. /*}}}*/
  100. // Translation-* Index /*{{{*/
  101. debTranslationsIndex::debTranslationsIndex(IndexTarget const &Target) :
  102. pkgDebianIndexTargetFile(Target, true), d(NULL)
  103. {}
  104. bool debTranslationsIndex::HasPackages() const
  105. {
  106. return Exists();
  107. }
  108. bool debTranslationsIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
  109. {
  110. if (FileExists(FileName))
  111. return pkgDebianIndexTargetFile::OpenListFile(Pkg, FileName);
  112. return true;
  113. }
  114. uint8_t debTranslationsIndex::GetIndexFlags() const
  115. {
  116. return pkgCache::Flag::NotSource | pkgCache::Flag::NoPackages;
  117. }
  118. std::string debTranslationsIndex::GetArchitecture() const
  119. {
  120. return std::string();
  121. }
  122. pkgCacheListParser * debTranslationsIndex::CreateListParser(FileFd &Pkg)
  123. {
  124. if (Pkg.IsOpen() == false)
  125. return NULL;
  126. _error->PushToStack();
  127. pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg);
  128. bool const newError = _error->PendingError();
  129. _error->MergeWithStack();
  130. return newError ? NULL : Parser;
  131. }
  132. /*}}}*/
  133. // dpkg/status Index /*{{{*/
  134. debStatusIndex::debStatusIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL)
  135. {
  136. }
  137. std::string debStatusIndex::GetArchitecture() const
  138. {
  139. return std::string();
  140. }
  141. std::string debStatusIndex::GetComponent() const
  142. {
  143. return "now";
  144. }
  145. uint8_t debStatusIndex::GetIndexFlags() const
  146. {
  147. return pkgCache::Flag::NotSource;
  148. }
  149. /*}}}*/
  150. // DebPkgFile Index - a single .deb file as an index /*{{{*/
  151. debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile)
  152. : pkgDebianIndexRealFile(DebFile, true), d(NULL), DebFile(DebFile)
  153. {
  154. }
  155. bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile)
  156. {
  157. struct stat Buf;
  158. if (stat(debfile.c_str(), &Buf) != 0)
  159. return false;
  160. // get the control data out of the deb file via dpkg-deb -I
  161. std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
  162. std::vector<const char *> Args;
  163. Args.push_back(dpkg.c_str());
  164. Args.push_back("-I");
  165. Args.push_back(debfile.c_str());
  166. Args.push_back("control");
  167. Args.push_back(NULL);
  168. FileFd PipeFd;
  169. pid_t Child;
  170. if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
  171. return _error->Error("Popen failed");
  172. char buffer[1024];
  173. do {
  174. unsigned long long actual = 0;
  175. if (PipeFd.Read(buffer, sizeof(buffer)-1, &actual) == false)
  176. return _error->Errno("read", "Failed to read dpkg pipe");
  177. if (actual == 0)
  178. break;
  179. buffer[actual] = '\0';
  180. content << buffer;
  181. } while(true);
  182. ExecWait(Child, "Popen");
  183. content << "Filename: " << debfile << "\n";
  184. content << "Size: " << Buf.st_size << "\n";
  185. return true;
  186. }
  187. bool debDebPkgFileIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
  188. {
  189. // write the control data to a tempfile
  190. if (GetTempFile("deb-file-" + flNotDir(FileName), true, &Pkg) == NULL)
  191. return false;
  192. std::ostringstream content;
  193. if (GetContent(content, FileName) == false)
  194. return false;
  195. std::string const contentstr = content.str();
  196. if (contentstr.empty())
  197. return true;
  198. if (Pkg.Write(contentstr.c_str(), contentstr.length()) == false || Pkg.Seek(0) == false)
  199. return false;
  200. return true;
  201. }
  202. pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg)
  203. {
  204. if (Pkg.IsOpen() == false)
  205. return NULL;
  206. _error->PushToStack();
  207. pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile);
  208. bool const newError = _error->PendingError();
  209. _error->MergeWithStack();
  210. return newError ? NULL : Parser;
  211. }
  212. uint8_t debDebPkgFileIndex::GetIndexFlags() const
  213. {
  214. return pkgCache::Flag::LocalSource;
  215. }
  216. std::string debDebPkgFileIndex::GetArchitecture() const
  217. {
  218. return std::string();
  219. }
  220. std::string debDebPkgFileIndex::GetComponent() const
  221. {
  222. return "local-deb";
  223. }
  224. pkgCache::PkgFileIterator debDebPkgFileIndex::FindInCache(pkgCache &Cache) const
  225. {
  226. std::string const FileName = IndexFileName();
  227. pkgCache::PkgFileIterator File = Cache.FileBegin();
  228. for (; File.end() == false; ++File)
  229. {
  230. if (File.FileName() == NULL || FileName != File.FileName())
  231. continue;
  232. // we can't do size checks here as file size != content size
  233. return File;
  234. }
  235. return File;
  236. }
  237. /*}}}*/
  238. // DscFile Index - a single .dsc file as an index /*{{{*/
  239. debDscFileIndex::debDscFileIndex(std::string const &DscFile)
  240. : pkgDebianIndexRealFile(DscFile, true), d(NULL)
  241. {
  242. }
  243. pkgSrcRecords::Parser *debDscFileIndex::CreateSrcParser() const
  244. {
  245. if (Exists() == false)
  246. return NULL;
  247. return new debDscRecordParser(File, this);
  248. }
  249. /*}}}*/
  250. // Index File types for Debian /*{{{*/
  251. class APT_HIDDEN debIFTypeSrc : public pkgIndexFile::Type
  252. {
  253. public:
  254. debIFTypeSrc() {Label = "Debian Source Index";};
  255. };
  256. class APT_HIDDEN debIFTypePkg : public pkgIndexFile::Type
  257. {
  258. public:
  259. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
  260. {
  261. return new debRecordParser(File.FileName(),*File.Cache());
  262. };
  263. debIFTypePkg() {Label = "Debian Package Index";};
  264. };
  265. class APT_HIDDEN debIFTypeTrans : public debIFTypePkg
  266. {
  267. public:
  268. debIFTypeTrans() {Label = "Debian Translation Index";};
  269. };
  270. class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type
  271. {
  272. public:
  273. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
  274. {
  275. return new debRecordParser(File.FileName(),*File.Cache());
  276. };
  277. debIFTypeStatus() {Label = "Debian dpkg status file";};
  278. };
  279. class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
  280. {
  281. public:
  282. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
  283. {
  284. return new debDebFileRecordParser(File.FileName());
  285. };
  286. debIFTypeDebPkgFile() {Label = "Debian deb file";};
  287. };
  288. class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type
  289. {
  290. public:
  291. virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &DscFile) const APT_OVERRIDE
  292. {
  293. return new debDscRecordParser(DscFile, NULL);
  294. };
  295. debIFTypeDscFile() {Label = "Debian dsc file";};
  296. };
  297. class APT_HIDDEN debIFTypeDebianSourceDir : public pkgIndexFile::Type
  298. {
  299. public:
  300. virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &SourceDir) const APT_OVERRIDE
  301. {
  302. return new debDscRecordParser(SourceDir + std::string("/debian/control"), NULL);
  303. };
  304. debIFTypeDebianSourceDir() {Label = "Debian control file";};
  305. };
  306. APT_HIDDEN debIFTypeSrc _apt_Src;
  307. APT_HIDDEN debIFTypePkg _apt_Pkg;
  308. APT_HIDDEN debIFTypeTrans _apt_Trans;
  309. APT_HIDDEN debIFTypeStatus _apt_Status;
  310. APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile;
  311. // file based pseudo indexes
  312. APT_HIDDEN debIFTypeDscFile _apt_DscFile;
  313. APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir;
  314. const pkgIndexFile::Type *debSourcesIndex::GetType() const
  315. {
  316. return &_apt_Src;
  317. }
  318. const pkgIndexFile::Type *debPackagesIndex::GetType() const
  319. {
  320. return &_apt_Pkg;
  321. }
  322. const pkgIndexFile::Type *debTranslationsIndex::GetType() const
  323. {
  324. return &_apt_Trans;
  325. }
  326. const pkgIndexFile::Type *debStatusIndex::GetType() const
  327. {
  328. return &_apt_Status;
  329. }
  330. const pkgIndexFile::Type *debDebPkgFileIndex::GetType() const
  331. {
  332. return &_apt_DebPkgFile;
  333. }
  334. const pkgIndexFile::Type *debDscFileIndex::GetType() const
  335. {
  336. return &_apt_DscFile;
  337. }
  338. const pkgIndexFile::Type *debDebianSourceDirIndex::GetType() const
  339. {
  340. return &_apt_DebianSourceDir;
  341. }
  342. /*}}}*/
  343. debStatusIndex::~debStatusIndex() {}
  344. debPackagesIndex::~debPackagesIndex() {}
  345. debTranslationsIndex::~debTranslationsIndex() {}
  346. debSourcesIndex::~debSourcesIndex() {}
  347. debDebPkgFileIndex::~debDebPkgFileIndex() {}
  348. debDscFileIndex::~debDscFileIndex() {}