debindexfile.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debindexfile.cc,v 1.6 2004/01/04 07:41:30 mdz Exp $
  4. /* ######################################################################
  5. Debian Specific sources.list types and the three sorts of Debian
  6. index files.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #ifdef __GNUG__
  11. #pragma implementation "apt-pkg/debindexfile.h"
  12. #endif
  13. #include <apt-pkg/debindexfile.h>
  14. #include <apt-pkg/debsrcrecords.h>
  15. #include <apt-pkg/deblistparser.h>
  16. #include <apt-pkg/debrecords.h>
  17. #include <apt-pkg/sourcelist.h>
  18. #include <apt-pkg/configuration.h>
  19. #include <apt-pkg/progress.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/acquire-item.h>
  23. #include <sys/stat.h>
  24. /*}}}*/
  25. // SourcesIndex::debSourcesIndex - Constructor /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* */
  28. debSourcesIndex::debSourcesIndex(string URI,string Dist,string Section) :
  29. URI(URI), Dist(Dist), Section(Section)
  30. {
  31. }
  32. /*}}}*/
  33. // SourcesIndex::SourceInfo - Short 1 liner describing a source /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* The result looks like:
  36. http://foo/ stable/main src 1.1.1 (dsc) */
  37. string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
  38. pkgSrcRecords::File const &File) const
  39. {
  40. string Res;
  41. Res = ::URI::SiteOnly(URI) + ' ';
  42. if (Dist[Dist.size() - 1] == '/')
  43. {
  44. if (Dist != "/")
  45. Res += Dist;
  46. }
  47. else
  48. Res += Dist + '/' + Section;
  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. /*}}}*/
  58. // SourcesIndex::CreateSrcParser - Get a parser for the source files /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* */
  61. pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
  62. {
  63. string SourcesURI = URItoFileName(IndexURI("Sources"));
  64. return new debSrcRecordParser(_config->FindDir("Dir::State::lists") +
  65. SourcesURI,this);
  66. }
  67. /*}}}*/
  68. // SourcesIndex::Describe - Give a descriptive path to the index /*{{{*/
  69. // ---------------------------------------------------------------------
  70. /* */
  71. string debSourcesIndex::Describe(bool Short) const
  72. {
  73. char S[300];
  74. if (Short == true)
  75. snprintf(S,sizeof(S),"%s",Info("Sources").c_str());
  76. else
  77. snprintf(S,sizeof(S),"%s (%s)",Info("Sources").c_str(),
  78. IndexFile("Sources").c_str());
  79. return S;
  80. }
  81. /*}}}*/
  82. // SourcesIndex::Info - One liner describing the index URI /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* */
  85. string debSourcesIndex::Info(const char *Type) const
  86. {
  87. string Info = ::URI::SiteOnly(URI) + ' ';
  88. if (Dist[Dist.size() - 1] == '/')
  89. {
  90. if (Dist != "/")
  91. Info += Dist;
  92. }
  93. else
  94. Info += Dist + '/' + Section;
  95. Info += " ";
  96. Info += Type;
  97. return Info;
  98. }
  99. /*}}}*/
  100. // SourcesIndex::Index* - Return the URI to the index files /*{{{*/
  101. // ---------------------------------------------------------------------
  102. /* */
  103. inline string debSourcesIndex::IndexFile(const char *Type) const
  104. {
  105. return URItoFileName(IndexURI(Type));
  106. }
  107. string debSourcesIndex::IndexURI(const char *Type) const
  108. {
  109. string Res;
  110. if (Dist[Dist.size() - 1] == '/')
  111. {
  112. if (Dist != "/")
  113. Res = URI + Dist;
  114. else
  115. Res = URI;
  116. }
  117. else
  118. Res = URI + "dists/" + Dist + '/' + Section +
  119. "/source/";
  120. Res += Type;
  121. return Res;
  122. }
  123. /*}}}*/
  124. // SourcesIndex::GetIndexes - Fetch the index files /*{{{*/
  125. // ---------------------------------------------------------------------
  126. /* */
  127. bool debSourcesIndex::GetIndexes(pkgAcquire *Owner) const
  128. {
  129. new pkgAcqIndex(Owner,IndexURI("Sources"),Info("Sources"),"Sources");
  130. new pkgAcqIndexRel(Owner,IndexURI("Release"),Info("Release"),"Release");
  131. return true;
  132. }
  133. /*}}}*/
  134. // SourcesIndex::Exists - Check if the index is available /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* */
  137. bool debSourcesIndex::Exists() const
  138. {
  139. return FileExists(IndexFile("Sources"));
  140. }
  141. /*}}}*/
  142. // SourcesIndex::Size - Return the size of the index /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* */
  145. unsigned long debSourcesIndex::Size() const
  146. {
  147. struct stat S;
  148. if (stat(IndexFile("Sources").c_str(),&S) != 0)
  149. return 0;
  150. return S.st_size;
  151. }
  152. /*}}}*/
  153. // PackagesIndex::debPackagesIndex - Contructor /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* */
  156. debPackagesIndex::debPackagesIndex(string URI,string Dist,string Section) :
  157. URI(URI), Dist(Dist), Section(Section)
  158. {
  159. }
  160. /*}}}*/
  161. // PackagesIndex::ArchiveInfo - Short version of the archive url /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* This is a shorter version that is designed to be < 60 chars or so */
  164. string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator Ver) const
  165. {
  166. string Res = ::URI::SiteOnly(URI) + ' ';
  167. if (Dist[Dist.size() - 1] == '/')
  168. {
  169. if (Dist != "/")
  170. Res += Dist;
  171. }
  172. else
  173. Res += Dist + '/' + Section;
  174. Res += " ";
  175. Res += Ver.ParentPkg().Name();
  176. Res += " ";
  177. Res += Ver.VerStr();
  178. return Res;
  179. }
  180. /*}}}*/
  181. // PackagesIndex::Describe - Give a descriptive path to the index /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* This should help the user find the index in the sources.list and
  184. in the filesystem for problem solving */
  185. string debPackagesIndex::Describe(bool Short) const
  186. {
  187. char S[300];
  188. if (Short == true)
  189. snprintf(S,sizeof(S),"%s",Info("Packages").c_str());
  190. else
  191. snprintf(S,sizeof(S),"%s (%s)",Info("Packages").c_str(),
  192. IndexFile("Packages").c_str());
  193. return S;
  194. }
  195. /*}}}*/
  196. // PackagesIndex::Info - One liner describing the index URI /*{{{*/
  197. // ---------------------------------------------------------------------
  198. /* */
  199. string debPackagesIndex::Info(const char *Type) const
  200. {
  201. string Info = ::URI::SiteOnly(URI) + ' ';
  202. if (Dist[Dist.size() - 1] == '/')
  203. {
  204. if (Dist != "/")
  205. Info += Dist;
  206. }
  207. else
  208. Info += Dist + '/' + Section;
  209. Info += " ";
  210. Info += Type;
  211. return Info;
  212. }
  213. /*}}}*/
  214. // PackagesIndex::Index* - Return the URI to the index files /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* */
  217. inline string debPackagesIndex::IndexFile(const char *Type) const
  218. {
  219. return _config->FindDir("Dir::State::lists") + URItoFileName(IndexURI(Type));
  220. }
  221. string debPackagesIndex::IndexURI(const char *Type) const
  222. {
  223. string Res;
  224. if (Dist[Dist.size() - 1] == '/')
  225. {
  226. if (Dist != "/")
  227. Res = URI + Dist;
  228. else
  229. Res = URI;
  230. }
  231. else
  232. Res = URI + "dists/" + Dist + '/' + Section +
  233. "/binary-" + _config->Find("APT::Architecture") + '/';
  234. Res += Type;
  235. return Res;
  236. }
  237. /*}}}*/
  238. // PackagesIndex::GetIndexes - Fetch the index files /*{{{*/
  239. // ---------------------------------------------------------------------
  240. /* */
  241. bool debPackagesIndex::GetIndexes(pkgAcquire *Owner) const
  242. {
  243. new pkgAcqIndex(Owner,IndexURI("Packages"),Info("Packages"),"Packages");
  244. new pkgAcqIndexRel(Owner,IndexURI("Release"),Info("Release"),"Release");
  245. return true;
  246. }
  247. /*}}}*/
  248. // PackagesIndex::Exists - Check if the index is available /*{{{*/
  249. // ---------------------------------------------------------------------
  250. /* */
  251. bool debPackagesIndex::Exists() const
  252. {
  253. return FileExists(IndexFile("Packages"));
  254. }
  255. /*}}}*/
  256. // PackagesIndex::Size - Return the size of the index /*{{{*/
  257. // ---------------------------------------------------------------------
  258. /* This is really only used for progress reporting. */
  259. unsigned long debPackagesIndex::Size() const
  260. {
  261. struct stat S;
  262. if (stat(IndexFile("Packages").c_str(),&S) != 0)
  263. return 0;
  264. return S.st_size;
  265. }
  266. /*}}}*/
  267. // PackagesIndex::Merge - Load the index file into a cache /*{{{*/
  268. // ---------------------------------------------------------------------
  269. /* */
  270. bool debPackagesIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
  271. {
  272. string PackageFile = IndexFile("Packages");
  273. FileFd Pkg(PackageFile,FileFd::ReadOnly);
  274. debListParser Parser(&Pkg);
  275. if (_error->PendingError() == true)
  276. return _error->Error("Problem opening %s",PackageFile.c_str());
  277. Prog.SubProgress(0,Info("Packages"));
  278. ::URI Tmp(URI);
  279. if (Gen.SelectFile(PackageFile,Tmp.Host,*this) == false)
  280. return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
  281. // Store the IMS information
  282. pkgCache::PkgFileIterator File = Gen.GetCurFile();
  283. struct stat St;
  284. if (fstat(Pkg.Fd(),&St) != 0)
  285. return _error->Errno("fstat","Failed to stat");
  286. File->Size = St.st_size;
  287. File->mtime = St.st_mtime;
  288. if (Gen.MergeList(Parser) == false)
  289. return _error->Error("Problem with MergeList %s",PackageFile.c_str());
  290. // Check the release file
  291. string ReleaseFile = IndexFile("Release");
  292. if (FileExists(ReleaseFile) == true)
  293. {
  294. FileFd Rel(ReleaseFile,FileFd::ReadOnly);
  295. if (_error->PendingError() == true)
  296. return false;
  297. Parser.LoadReleaseInfo(File,Rel);
  298. }
  299. return true;
  300. }
  301. /*}}}*/
  302. // PackagesIndex::FindInCache - Find this index /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* */
  305. pkgCache::PkgFileIterator debPackagesIndex::FindInCache(pkgCache &Cache) const
  306. {
  307. string FileName = IndexFile("Packages");
  308. pkgCache::PkgFileIterator File = Cache.FileBegin();
  309. for (; File.end() == false; File++)
  310. {
  311. if (FileName != File.FileName())
  312. continue;
  313. struct stat St;
  314. if (stat(File.FileName(),&St) != 0)
  315. return pkgCache::PkgFileIterator(Cache);
  316. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  317. return pkgCache::PkgFileIterator(Cache);
  318. return File;
  319. }
  320. return File;
  321. }
  322. /*}}}*/
  323. // StatusIndex::debStatusIndex - Constructor /*{{{*/
  324. // ---------------------------------------------------------------------
  325. /* */
  326. debStatusIndex::debStatusIndex(string File) : File(File)
  327. {
  328. }
  329. /*}}}*/
  330. // StatusIndex::Size - Return the size of the index /*{{{*/
  331. // ---------------------------------------------------------------------
  332. /* */
  333. unsigned long debStatusIndex::Size() const
  334. {
  335. struct stat S;
  336. if (stat(File.c_str(),&S) != 0)
  337. return 0;
  338. return S.st_size;
  339. }
  340. /*}}}*/
  341. // StatusIndex::Merge - Load the index file into a cache /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* */
  344. bool debStatusIndex::Merge(pkgCacheGenerator &Gen,OpProgress &Prog) const
  345. {
  346. FileFd Pkg(File,FileFd::ReadOnly);
  347. if (_error->PendingError() == true)
  348. return false;
  349. debListParser Parser(&Pkg);
  350. if (_error->PendingError() == true)
  351. return false;
  352. Prog.SubProgress(0,File);
  353. if (Gen.SelectFile(File,string(),*this,pkgCache::Flag::NotSource) == false)
  354. return _error->Error("Problem with SelectFile %s",File.c_str());
  355. // Store the IMS information
  356. pkgCache::PkgFileIterator CFile = Gen.GetCurFile();
  357. struct stat St;
  358. if (fstat(Pkg.Fd(),&St) != 0)
  359. return _error->Errno("fstat","Failed to stat");
  360. CFile->Size = St.st_size;
  361. CFile->mtime = St.st_mtime;
  362. CFile->Archive = Gen.WriteUniqString("now");
  363. if (Gen.MergeList(Parser) == false)
  364. return _error->Error("Problem with MergeList %s",File.c_str());
  365. return true;
  366. }
  367. /*}}}*/
  368. // StatusIndex::FindInCache - Find this index /*{{{*/
  369. // ---------------------------------------------------------------------
  370. /* */
  371. pkgCache::PkgFileIterator debStatusIndex::FindInCache(pkgCache &Cache) const
  372. {
  373. pkgCache::PkgFileIterator File = Cache.FileBegin();
  374. for (; File.end() == false; File++)
  375. {
  376. if (this->File != File.FileName())
  377. continue;
  378. struct stat St;
  379. if (stat(File.FileName(),&St) != 0)
  380. return pkgCache::PkgFileIterator(Cache);
  381. if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
  382. return pkgCache::PkgFileIterator(Cache);
  383. return File;
  384. }
  385. return File;
  386. }
  387. /*}}}*/
  388. // StatusIndex::Exists - Check if the index is available /*{{{*/
  389. // ---------------------------------------------------------------------
  390. /* */
  391. bool debStatusIndex::Exists() const
  392. {
  393. // Abort if the file does not exist.
  394. return true;
  395. }
  396. /*}}}*/
  397. // Source List types for Debian /*{{{*/
  398. class debSLTypeDeb : public pkgSourceList::Type
  399. {
  400. public:
  401. bool CreateItem(vector<pkgIndexFile *> &List,string URI,
  402. string Dist,string Section,
  403. pkgSourceList::Vendor const *Vendor) const
  404. {
  405. List.push_back(new debPackagesIndex(URI,Dist,Section));
  406. return true;
  407. };
  408. debSLTypeDeb()
  409. {
  410. Name = "deb";
  411. Label = "Standard Debian binary tree";
  412. }
  413. };
  414. class debSLTypeDebSrc : public pkgSourceList::Type
  415. {
  416. public:
  417. bool CreateItem(vector<pkgIndexFile *> &List,string URI,
  418. string Dist,string Section,
  419. pkgSourceList::Vendor const *Vendor) const
  420. {
  421. List.push_back(new debSourcesIndex(URI,Dist,Section));
  422. return true;
  423. };
  424. debSLTypeDebSrc()
  425. {
  426. Name = "deb-src";
  427. Label = "Standard Debian source tree";
  428. }
  429. };
  430. debSLTypeDeb _apt_DebType;
  431. debSLTypeDebSrc _apt_DebSrcType;
  432. /*}}}*/
  433. // Index File types for Debian /*{{{*/
  434. class debIFTypeSrc : public pkgIndexFile::Type
  435. {
  436. public:
  437. debIFTypeSrc() {Label = "Debian Source Index";};
  438. };
  439. class debIFTypePkg : public pkgIndexFile::Type
  440. {
  441. public:
  442. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
  443. {
  444. return new debRecordParser(File.FileName(),*File.Cache());
  445. };
  446. debIFTypePkg() {Label = "Debian Package Index";};
  447. };
  448. class debIFTypeStatus : public pkgIndexFile::Type
  449. {
  450. public:
  451. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator File) const
  452. {
  453. return new debRecordParser(File.FileName(),*File.Cache());
  454. };
  455. debIFTypeStatus() {Label = "Debian dpkg status file";};
  456. };
  457. static debIFTypeSrc _apt_Src;
  458. static debIFTypePkg _apt_Pkg;
  459. static debIFTypeStatus _apt_Status;
  460. const pkgIndexFile::Type *debSourcesIndex::GetType() const
  461. {
  462. return &_apt_Src;
  463. }
  464. const pkgIndexFile::Type *debPackagesIndex::GetType() const
  465. {
  466. return &_apt_Pkg;
  467. }
  468. const pkgIndexFile::Type *debStatusIndex::GetType() const
  469. {
  470. return &_apt_Status;
  471. }
  472. /*}}}*/