You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1054 lines
34 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-ftparchive.cc,v 1.8.2.3 2004/01/02 22:01:48 mdz Exp $
  4. /* ######################################################################
  5. apt-ftparchive - Efficient work-alike for dpkg-scanpackages
  6. Let contents be disabled from the conf
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/cmndline.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/init.h>
  16. #include <apt-pkg/fileutl.h>
  17. #include <apt-private/private-cmndline.h>
  18. #include <apt-private/private-output.h>
  19. #include <apt-private/private-main.h>
  20. #include <algorithm>
  21. #include <climits>
  22. #include <sys/time.h>
  23. #include <locale.h>
  24. #include <stdio.h>
  25. #include <sys/stat.h>
  26. #include <time.h>
  27. #include <functional>
  28. #include <iostream>
  29. #include <string>
  30. #include <vector>
  31. #include "cachedb.h"
  32. #include "override.h"
  33. #include "apt-ftparchive.h"
  34. #include "multicompress.h"
  35. #include "writer.h"
  36. #include <apti18n.h>
  37. /*}}}*/
  38. using namespace std;
  39. unsigned Quiet = 0;
  40. // struct PackageMap - List of all package files in the config file /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* */
  43. struct PackageMap
  44. {
  45. // General Stuff
  46. string BaseDir;
  47. string InternalPrefix;
  48. string FLFile;
  49. string PkgExt;
  50. string SrcExt;
  51. // Stuff for the Package File
  52. string PkgFile;
  53. string BinCacheDB;
  54. string SrcCacheDB;
  55. string BinOverride;
  56. string ExtraOverride;
  57. // We generate for this given arch
  58. string Arch;
  59. // Stuff for the Source File
  60. string SrcFile;
  61. string SrcOverride;
  62. string SrcExtraOverride;
  63. // Translation master file
  64. bool LongDesc;
  65. TranslationWriter *TransWriter;
  66. // Contents
  67. string Contents;
  68. string ContentsHead;
  69. // Random things
  70. string Tag;
  71. string PkgCompress;
  72. string CntCompress;
  73. string SrcCompress;
  74. string PathPrefix;
  75. unsigned int DeLinkLimit;
  76. mode_t Permissions;
  77. bool ContentsDone;
  78. bool PkgDone;
  79. bool SrcDone;
  80. time_t ContentsMTime;
  81. struct ContentsCompare : public binary_function<PackageMap,PackageMap,bool>
  82. {
  83. inline bool operator() (const PackageMap &x,const PackageMap &y)
  84. {return x.ContentsMTime < y.ContentsMTime;};
  85. };
  86. struct DBCompare : public binary_function<PackageMap,PackageMap,bool>
  87. {
  88. inline bool operator() (const PackageMap &x,const PackageMap &y)
  89. {return x.BinCacheDB < y.BinCacheDB;};
  90. };
  91. struct SrcDBCompare : public binary_function<PackageMap,PackageMap,bool>
  92. {
  93. inline bool operator() (const PackageMap &x,const PackageMap &y)
  94. {return x.SrcCacheDB < y.SrcCacheDB;};
  95. };
  96. void GetGeneral(Configuration &Setup,Configuration &Block);
  97. bool GenPackages(Configuration &Setup,struct CacheDB::Stats &Stats);
  98. bool GenSources(Configuration &Setup,struct CacheDB::Stats &Stats);
  99. bool GenContents(Configuration &Setup,
  100. vector<PackageMap>::iterator Begin,
  101. vector<PackageMap>::iterator End,
  102. unsigned long &Left);
  103. PackageMap() : LongDesc(true), TransWriter(NULL), DeLinkLimit(0), Permissions(1),
  104. ContentsDone(false), PkgDone(false), SrcDone(false),
  105. ContentsMTime(0) {};
  106. };
  107. /*}}}*/
  108. // PackageMap::GetGeneral - Common per-section definitions /*{{{*/
  109. // ---------------------------------------------------------------------
  110. /* */
  111. void PackageMap::GetGeneral(Configuration &Setup,Configuration &Block)
  112. {
  113. PathPrefix = Block.Find("PathPrefix");
  114. if (Block.FindB("External-Links",true) == false)
  115. DeLinkLimit = Setup.FindI("Default::DeLinkLimit", std::numeric_limits<unsigned int>::max());
  116. else
  117. DeLinkLimit = 0;
  118. PkgCompress = Block.Find("Packages::Compress",
  119. Setup.Find("Default::Packages::Compress",". gzip").c_str());
  120. CntCompress = Block.Find("Contents::Compress",
  121. Setup.Find("Default::Contents::Compress",". gzip").c_str());
  122. SrcCompress = Block.Find("Sources::Compress",
  123. Setup.Find("Default::Sources::Compress",". gzip").c_str());
  124. SrcExt = Block.Find("Sources::Extensions",
  125. Setup.Find("Default::Sources::Extensions",".dsc").c_str());
  126. PkgExt = Block.Find("Packages::Extensions",
  127. Setup.Find("Default::Packages::Extensions",".deb").c_str());
  128. Permissions = Setup.FindI("Default::FileMode",0644);
  129. if (FLFile.empty() == false)
  130. FLFile = flCombine(Setup.Find("Dir::FileListDir"),FLFile);
  131. if (Contents == " ")
  132. Contents= string();
  133. }
  134. /*}}}*/
  135. // PackageMap::GenPackages - Actually generate a Package file /*{{{*/
  136. // ---------------------------------------------------------------------
  137. /* This generates the Package File described by this object. */
  138. bool PackageMap::GenPackages(Configuration &Setup,struct CacheDB::Stats &Stats)
  139. {
  140. if (PkgFile.empty() == true)
  141. return true;
  142. string ArchiveDir = Setup.FindDir("Dir::ArchiveDir");
  143. string OverrideDir = Setup.FindDir("Dir::OverrideDir");
  144. string CacheDir = Setup.FindDir("Dir::CacheDir");
  145. struct timeval StartTime;
  146. gettimeofday(&StartTime,0);
  147. PkgDone = true;
  148. // Create a package writer object.
  149. MultiCompress Comp(flCombine(ArchiveDir,PkgFile),
  150. PkgCompress,Permissions);
  151. PackagesWriter Packages(&Comp.Input, TransWriter, flCombine(CacheDir,BinCacheDB),
  152. flCombine(OverrideDir,BinOverride),
  153. flCombine(OverrideDir,ExtraOverride),
  154. Arch);
  155. if (PkgExt.empty() == false && Packages.SetExts(PkgExt) == false)
  156. return _error->Error(_("Package extension list is too long"));
  157. if (_error->PendingError() == true)
  158. return _error->Error(_("Error processing directory %s"),BaseDir.c_str());
  159. Packages.PathPrefix = PathPrefix;
  160. Packages.DirStrip = ArchiveDir;
  161. Packages.InternalPrefix = flCombine(ArchiveDir,InternalPrefix);
  162. Packages.LongDescription = LongDesc;
  163. Packages.Stats.DeLinkBytes = Stats.DeLinkBytes;
  164. Packages.DeLinkLimit = DeLinkLimit;
  165. if (_error->PendingError() == true)
  166. return _error->Error(_("Error processing directory %s"),BaseDir.c_str());
  167. c0out << ' ' << BaseDir << ":" << flush;
  168. // Do recursive directory searching
  169. if (FLFile.empty() == true)
  170. {
  171. if (Packages.RecursiveScan(flCombine(ArchiveDir,BaseDir)) == false)
  172. return false;
  173. }
  174. else
  175. {
  176. if (Packages.LoadFileList(ArchiveDir,FLFile) == false)
  177. return false;
  178. }
  179. Packages.Output = 0; // Just in case
  180. // Finish compressing
  181. unsigned long long Size;
  182. if (Comp.Finalize(Size) == false)
  183. {
  184. c0out << endl;
  185. return _error->Error(_("Error processing directory %s"),BaseDir.c_str());
  186. }
  187. if (Size != 0)
  188. c0out << " New "
  189. << SizeToStr(Size) << "B ";
  190. else
  191. c0out << ' ';
  192. struct timeval NewTime;
  193. gettimeofday(&NewTime,0);
  194. double Delta = NewTime.tv_sec - StartTime.tv_sec +
  195. (NewTime.tv_usec - StartTime.tv_usec)/1000000.0;
  196. c0out << Packages.Stats.Packages << " files " <<
  197. /* SizeToStr(Packages.Stats.MD5Bytes) << "B/" << */
  198. SizeToStr(Packages.Stats.Bytes) << "B " <<
  199. TimeToStr((long)Delta) << endl;
  200. if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true)
  201. c0out << " Misses in Cache: " << Packages.Stats.Misses<< endl;
  202. Stats.Add(Packages.Stats);
  203. Stats.DeLinkBytes = Packages.Stats.DeLinkBytes;
  204. return !_error->PendingError();
  205. }
  206. /*}}}*/
  207. // PackageMap::GenSources - Actually generate a Source file /*{{{*/
  208. // ---------------------------------------------------------------------
  209. /* This generates the Sources File described by this object. */
  210. bool PackageMap::GenSources(Configuration &Setup,struct CacheDB::Stats &Stats)
  211. {
  212. if (SrcFile.empty() == true)
  213. return true;
  214. string ArchiveDir = Setup.FindDir("Dir::ArchiveDir");
  215. string OverrideDir = Setup.FindDir("Dir::OverrideDir");
  216. string CacheDir = Setup.FindDir("Dir::CacheDir");
  217. struct timeval StartTime;
  218. gettimeofday(&StartTime,0);
  219. SrcDone = true;
  220. // Create a package writer object.
  221. MultiCompress Comp(flCombine(ArchiveDir,SrcFile),
  222. SrcCompress,Permissions);
  223. SourcesWriter Sources(&Comp.Input, flCombine(CacheDir, SrcCacheDB),
  224. flCombine(OverrideDir,BinOverride),
  225. flCombine(OverrideDir,SrcOverride),
  226. flCombine(OverrideDir,SrcExtraOverride));
  227. if (SrcExt.empty() == false && Sources.SetExts(SrcExt) == false)
  228. return _error->Error(_("Source extension list is too long"));
  229. if (_error->PendingError() == true)
  230. return _error->Error(_("Error processing directory %s"),BaseDir.c_str());
  231. Sources.PathPrefix = PathPrefix;
  232. Sources.DirStrip = ArchiveDir;
  233. Sources.InternalPrefix = flCombine(ArchiveDir,InternalPrefix);
  234. Sources.DeLinkLimit = DeLinkLimit;
  235. Sources.Stats.DeLinkBytes = Stats.DeLinkBytes;
  236. if (_error->PendingError() == true)
  237. return _error->Error(_("Error processing directory %s"),BaseDir.c_str());
  238. c0out << ' ' << BaseDir << ":" << flush;
  239. // Do recursive directory searching
  240. if (FLFile.empty() == true)
  241. {
  242. if (Sources.RecursiveScan(flCombine(ArchiveDir,BaseDir))== false)
  243. return false;
  244. }
  245. else
  246. {
  247. if (Sources.LoadFileList(ArchiveDir,FLFile) == false)
  248. return false;
  249. }
  250. Sources.Output = 0; // Just in case
  251. // Finish compressing
  252. unsigned long long Size;
  253. if (Comp.Finalize(Size) == false)
  254. {
  255. c0out << endl;
  256. return _error->Error(_("Error processing directory %s"),BaseDir.c_str());
  257. }
  258. if (Size != 0)
  259. c0out << " New "
  260. << SizeToStr(Size) << "B ";
  261. else
  262. c0out << ' ';
  263. struct timeval NewTime;
  264. gettimeofday(&NewTime,0);
  265. double Delta = NewTime.tv_sec - StartTime.tv_sec +
  266. (NewTime.tv_usec - StartTime.tv_usec)/1000000.0;
  267. c0out << Sources.Stats.Packages << " pkgs in " <<
  268. TimeToStr((long)Delta) << endl;
  269. if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true)
  270. c0out << " Misses in Cache: " << Sources.Stats.Misses << endl;
  271. Stats.Add(Sources.Stats);
  272. Stats.DeLinkBytes = Sources.Stats.DeLinkBytes;
  273. return !_error->PendingError();
  274. }
  275. /*}}}*/
  276. // PackageMap::GenContents - Actually generate a Contents file /*{{{*/
  277. // ---------------------------------------------------------------------
  278. /* This generates the contents file partially described by this object.
  279. It searches the given iterator range for other package files that map
  280. into this contents file and includes their data as well when building. */
  281. bool PackageMap::GenContents(Configuration &Setup,
  282. vector<PackageMap>::iterator Begin,
  283. vector<PackageMap>::iterator End,
  284. unsigned long &Left)
  285. {
  286. if (Contents.empty() == true)
  287. return true;
  288. if (Left == 0)
  289. return true;
  290. string ArchiveDir = Setup.FindDir("Dir::ArchiveDir");
  291. string CacheDir = Setup.FindDir("Dir::CacheDir");
  292. string OverrideDir = Setup.FindDir("Dir::OverrideDir");
  293. struct timeval StartTime;
  294. gettimeofday(&StartTime,0);
  295. // Create a package writer object.
  296. MultiCompress Comp(flCombine(ArchiveDir,this->Contents),
  297. CntCompress,Permissions);
  298. Comp.UpdateMTime = Setup.FindI("Default::ContentsAge",10)*24*60*60;
  299. ContentsWriter Contents(&Comp.Input, "", Arch);
  300. if (PkgExt.empty() == false && Contents.SetExts(PkgExt) == false)
  301. return _error->Error(_("Package extension list is too long"));
  302. if (_error->PendingError() == true)
  303. return false;
  304. if (_error->PendingError() == true)
  305. return false;
  306. // Write the header out.
  307. if (ContentsHead.empty() == false)
  308. {
  309. FileFd Head(flCombine(OverrideDir,ContentsHead),FileFd::ReadOnly);
  310. if (_error->PendingError() == true)
  311. return false;
  312. unsigned long long Size = Head.Size();
  313. unsigned char Buf[4096];
  314. while (Size != 0)
  315. {
  316. unsigned long long ToRead = Size;
  317. if (Size > sizeof(Buf))
  318. ToRead = sizeof(Buf);
  319. if (Head.Read(Buf,ToRead) == false)
  320. return false;
  321. if (Comp.Input.Write(Buf, ToRead) == false)
  322. return _error->Errno("fwrite",_("Error writing header to contents file"));
  323. Size -= ToRead;
  324. }
  325. }
  326. /* Go over all the package file records and parse all the package
  327. files associated with this contents file into one great big honking
  328. memory structure, then dump the sorted version */
  329. c0out << ' ' << this->Contents << ":" << flush;
  330. for (vector<PackageMap>::iterator I = Begin; I != End; ++I)
  331. {
  332. if (I->Contents != this->Contents)
  333. continue;
  334. Contents.Prefix = ArchiveDir;
  335. Contents.ReadyDB(flCombine(CacheDir,I->BinCacheDB));
  336. Contents.ReadFromPkgs(flCombine(ArchiveDir,I->PkgFile),
  337. I->PkgCompress);
  338. I->ContentsDone = true;
  339. }
  340. Contents.Finish();
  341. // Finish compressing
  342. unsigned long long Size;
  343. if (Comp.Finalize(Size) == false || _error->PendingError() == true)
  344. {
  345. c0out << endl;
  346. return _error->Error(_("Error processing contents %s"),
  347. this->Contents.c_str());
  348. }
  349. if (Size != 0)
  350. {
  351. c0out << " New " << SizeToStr(Size) << "B ";
  352. if (Left > Size)
  353. Left -= Size;
  354. else
  355. Left = 0;
  356. }
  357. else
  358. c0out << ' ';
  359. struct timeval NewTime;
  360. gettimeofday(&NewTime,0);
  361. double Delta = NewTime.tv_sec - StartTime.tv_sec +
  362. (NewTime.tv_usec - StartTime.tv_usec)/1000000.0;
  363. if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true)
  364. c0out << " Misses in Cache: " << Contents.Stats.Misses<< endl;
  365. c0out << Contents.Stats.Packages << " files " <<
  366. SizeToStr(Contents.Stats.Bytes) << "B " <<
  367. TimeToStr((long)Delta) << endl;
  368. return true;
  369. }
  370. /*}}}*/
  371. // LoadTree - Load a 'tree' section from the Generate Config /*{{{*/
  372. // ---------------------------------------------------------------------
  373. /* This populates the PkgList with all the possible permutations of the
  374. section/arch lists. */
  375. static void LoadTree(vector<PackageMap> &PkgList, std::vector<TranslationWriter*> &TransList, Configuration &Setup)
  376. {
  377. // Load the defaults
  378. string DDir = Setup.Find("TreeDefault::Directory",
  379. "$(DIST)/$(SECTION)/binary-$(ARCH)/");
  380. string DSDir = Setup.Find("TreeDefault::SrcDirectory",
  381. "$(DIST)/$(SECTION)/source/");
  382. string DPkg = Setup.Find("TreeDefault::Packages",
  383. "$(DIST)/$(SECTION)/binary-$(ARCH)/Packages");
  384. string DTrans = Setup.Find("TreeDefault::Translation",
  385. "$(DIST)/$(SECTION)/i18n/Translation-en");
  386. string DIPrfx = Setup.Find("TreeDefault::InternalPrefix",
  387. "$(DIST)/$(SECTION)/");
  388. string DContents = Setup.Find("TreeDefault::Contents",
  389. "$(DIST)/$(SECTION)/Contents-$(ARCH)");
  390. string DContentsH = Setup.Find("TreeDefault::Contents::Header","");
  391. string DBCache = Setup.Find("TreeDefault::BinCacheDB",
  392. "packages-$(ARCH).db");
  393. string SrcDBCache = Setup.Find("TreeDefault::SrcCacheDB",
  394. "sources-$(SECTION).db");
  395. string DSources = Setup.Find("TreeDefault::Sources",
  396. "$(DIST)/$(SECTION)/source/Sources");
  397. string DFLFile = Setup.Find("TreeDefault::FileList", "");
  398. string DSFLFile = Setup.Find("TreeDefault::SourceFileList", "");
  399. mode_t const Permissions = Setup.FindI("Default::FileMode",0644);
  400. bool const LongDescription = Setup.FindB("Default::LongDescription",
  401. _config->FindB("APT::FTPArchive::LongDescription", true));
  402. string const TranslationCompress = Setup.Find("Default::Translation::Compress",". gzip").c_str();
  403. // Process 'tree' type sections
  404. const Configuration::Item *Top = Setup.Tree("tree");
  405. for (Top = (Top == 0?0:Top->Child); Top != 0;)
  406. {
  407. Configuration Block(Top);
  408. string Dist = Top->Tag;
  409. // Parse the sections
  410. string Tmp = Block.Find("Sections");
  411. const char *Sections = Tmp.c_str();
  412. string Section;
  413. while (ParseQuoteWord(Sections,Section) == true)
  414. {
  415. string Arch;
  416. struct SubstVar const Vars[] = {{"$(DIST)",&Dist},
  417. {"$(SECTION)",&Section},
  418. {"$(ARCH)",&Arch},
  419. {NULL, NULL}};
  420. mode_t const Perms = Block.FindI("FileMode", Permissions);
  421. bool const LongDesc = Block.FindB("LongDescription", LongDescription);
  422. TranslationWriter *TransWriter = NULL;
  423. string const Tmp2 = Block.Find("Architectures");
  424. const char *Archs = Tmp2.c_str();
  425. while (ParseQuoteWord(Archs,Arch) == true)
  426. {
  427. PackageMap Itm;
  428. Itm.Permissions = Perms;
  429. Itm.BinOverride = SubstVar(Block.Find("BinOverride"),Vars);
  430. Itm.InternalPrefix = SubstVar(Block.Find("InternalPrefix",DIPrfx.c_str()),Vars);
  431. if (stringcasecmp(Arch,"source") == 0)
  432. {
  433. Itm.SrcOverride = SubstVar(Block.Find("SrcOverride"),Vars);
  434. Itm.BaseDir = SubstVar(Block.Find("SrcDirectory",DSDir.c_str()),Vars);
  435. Itm.SrcFile = SubstVar(Block.Find("Sources",DSources.c_str()),Vars);
  436. Itm.Tag = SubstVar("$(DIST)/$(SECTION)/source",Vars);
  437. Itm.FLFile = SubstVar(Block.Find("SourceFileList",DSFLFile.c_str()),Vars);
  438. Itm.SrcExtraOverride = SubstVar(Block.Find("SrcExtraOverride"),Vars);
  439. Itm.SrcCacheDB = SubstVar(Block.Find("SrcCacheDB",SrcDBCache.c_str()),Vars);
  440. }
  441. else
  442. {
  443. Itm.BinCacheDB = SubstVar(Block.Find("BinCacheDB",DBCache.c_str()),Vars);
  444. Itm.BaseDir = SubstVar(Block.Find("Directory",DDir.c_str()),Vars);
  445. Itm.PkgFile = SubstVar(Block.Find("Packages",DPkg.c_str()),Vars);
  446. Itm.Tag = SubstVar("$(DIST)/$(SECTION)/$(ARCH)",Vars);
  447. Itm.Arch = Arch;
  448. Itm.LongDesc = LongDesc;
  449. if (TransWriter == NULL && DTrans.empty() == false && LongDesc == false && DTrans != "/dev/null")
  450. {
  451. string const TranslationFile = flCombine(Setup.FindDir("Dir::ArchiveDir"),
  452. SubstVar(Block.Find("Translation", DTrans.c_str()), Vars));
  453. string const TransCompress = Block.Find("Translation::Compress", TranslationCompress);
  454. TransWriter = new TranslationWriter(TranslationFile, TransCompress, Perms);
  455. TransList.push_back(TransWriter);
  456. }
  457. Itm.TransWriter = TransWriter;
  458. Itm.Contents = SubstVar(Block.Find("Contents",DContents.c_str()),Vars);
  459. Itm.ContentsHead = SubstVar(Block.Find("Contents::Header",DContentsH.c_str()),Vars);
  460. Itm.FLFile = SubstVar(Block.Find("FileList",DFLFile.c_str()),Vars);
  461. Itm.ExtraOverride = SubstVar(Block.Find("ExtraOverride"),Vars);
  462. }
  463. Itm.GetGeneral(Setup,Block);
  464. PkgList.push_back(Itm);
  465. }
  466. }
  467. Top = Top->Next;
  468. }
  469. }
  470. /*}}}*/
  471. static void UnloadTree(std::vector<TranslationWriter*> const &Trans) /*{{{*/
  472. {
  473. for (std::vector<TranslationWriter*>::const_reverse_iterator T = Trans.rbegin(); T != Trans.rend(); ++T)
  474. delete *T;
  475. }
  476. /*}}}*/
  477. // LoadBinDir - Load a 'bindirectory' section from the Generate Config /*{{{*/
  478. // ---------------------------------------------------------------------
  479. /* */
  480. static void LoadBinDir(vector<PackageMap> &PkgList,Configuration &Setup)
  481. {
  482. mode_t const Permissions = Setup.FindI("Default::FileMode",0644);
  483. // Process 'bindirectory' type sections
  484. const Configuration::Item *Top = Setup.Tree("bindirectory");
  485. for (Top = (Top == 0?0:Top->Child); Top != 0;)
  486. {
  487. Configuration Block(Top);
  488. PackageMap Itm;
  489. Itm.PkgFile = Block.Find("Packages");
  490. Itm.SrcFile = Block.Find("Sources");
  491. Itm.BinCacheDB = Block.Find("BinCacheDB");
  492. Itm.SrcCacheDB = Block.Find("SrcCacheDB");
  493. Itm.BinOverride = Block.Find("BinOverride");
  494. Itm.ExtraOverride = Block.Find("ExtraOverride");
  495. Itm.SrcExtraOverride = Block.Find("SrcExtraOverride");
  496. Itm.SrcOverride = Block.Find("SrcOverride");
  497. Itm.BaseDir = Top->Tag;
  498. Itm.FLFile = Block.Find("FileList");
  499. Itm.InternalPrefix = Block.Find("InternalPrefix",Top->Tag.c_str());
  500. Itm.Contents = Block.Find("Contents");
  501. Itm.ContentsHead = Block.Find("Contents::Header");
  502. Itm.Permissions = Block.FindI("FileMode", Permissions);
  503. Itm.GetGeneral(Setup,Block);
  504. PkgList.push_back(Itm);
  505. Top = Top->Next;
  506. }
  507. }
  508. /*}}}*/
  509. // ShowHelp - Show the help text /*{{{*/
  510. bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
  511. {
  512. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  513. if (_config->FindB("version") == true)
  514. return true;
  515. cout <<
  516. _("Usage: apt-ftparchive [options] command\n"
  517. "Commands: packages binarypath [overridefile [pathprefix]]\n"
  518. " sources srcpath [overridefile [pathprefix]]\n"
  519. " contents path\n"
  520. " release path\n"
  521. " generate config [groups]\n"
  522. " clean config\n"
  523. "\n"
  524. "apt-ftparchive generates index files for Debian archives. It supports\n"
  525. "many styles of generation from fully automated to functional replacements\n"
  526. "for dpkg-scanpackages and dpkg-scansources\n"
  527. "\n"
  528. "apt-ftparchive generates Package files from a tree of .debs. The\n"
  529. "Package file contains the contents of all the control fields from\n"
  530. "each package as well as the MD5 hash and filesize. An override file\n"
  531. "is supported to force the value of Priority and Section.\n"
  532. "\n"
  533. "Similarly apt-ftparchive generates Sources files from a tree of .dscs.\n"
  534. "The --source-override option can be used to specify a src override file\n"
  535. "\n"
  536. "The 'packages' and 'sources' command should be run in the root of the\n"
  537. "tree. BinaryPath should point to the base of the recursive search and \n"
  538. "override file should contain the override flags. Pathprefix is\n"
  539. "appended to the filename fields if present. Example usage from the \n"
  540. "Debian archive:\n"
  541. " apt-ftparchive packages dists/potato/main/binary-i386/ > \\\n"
  542. " dists/potato/main/binary-i386/Packages\n"
  543. "\n"
  544. "Options:\n"
  545. " -h This help text\n"
  546. " --md5 Control MD5 generation\n"
  547. " -s=? Source override file\n"
  548. " -q Quiet\n"
  549. " -d=? Select the optional caching database\n"
  550. " --no-delink Enable delinking debug mode\n"
  551. " --contents Control contents file generation\n"
  552. " -c=? Read this configuration file\n"
  553. " -o=? Set an arbitrary configuration option") << endl;
  554. return true;
  555. }
  556. /*}}}*/
  557. // SimpleGenPackages - Generate a Packages file for a directory tree /*{{{*/
  558. // ---------------------------------------------------------------------
  559. /* This emulates dpkg-scanpackages's command line interface. 'mostly' */
  560. static bool SimpleGenPackages(CommandLine &CmdL)
  561. {
  562. if (CmdL.FileSize() < 2)
  563. return ShowHelp(CmdL, nullptr);
  564. string Override;
  565. if (CmdL.FileSize() >= 3)
  566. Override = CmdL.FileList[2];
  567. // Create a package writer object.
  568. PackagesWriter Packages(NULL, NULL, _config->Find("APT::FTPArchive::DB"),
  569. Override, "", _config->Find("APT::FTPArchive::Architecture"));
  570. if (_error->PendingError() == true)
  571. return false;
  572. if (CmdL.FileSize() >= 4)
  573. Packages.PathPrefix = CmdL.FileList[3];
  574. // Do recursive directory searching
  575. if (Packages.RecursiveScan(CmdL.FileList[1]) == false)
  576. return false;
  577. // Give some stats if asked for
  578. if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true)
  579. c0out << " Misses in Cache: " << Packages.Stats.Misses<< endl;
  580. return true;
  581. }
  582. /*}}}*/
  583. // SimpleGenContents - Generate a Contents listing /*{{{*/
  584. // ---------------------------------------------------------------------
  585. /* */
  586. static bool SimpleGenContents(CommandLine &CmdL)
  587. {
  588. if (CmdL.FileSize() < 2)
  589. return ShowHelp(CmdL, nullptr);
  590. // Create a package writer object.
  591. ContentsWriter Contents(NULL, _config->Find("APT::FTPArchive::DB"), _config->Find("APT::FTPArchive::Architecture"));
  592. if (_error->PendingError() == true)
  593. return false;
  594. // Do recursive directory searching
  595. if (Contents.RecursiveScan(CmdL.FileList[1]) == false)
  596. return false;
  597. Contents.Finish();
  598. return true;
  599. }
  600. /*}}}*/
  601. // SimpleGenSources - Generate a Sources file for a directory tree /*{{{*/
  602. // ---------------------------------------------------------------------
  603. /* This emulates dpkg-scanpackages's command line interface. 'mostly' */
  604. static bool SimpleGenSources(CommandLine &CmdL)
  605. {
  606. if (CmdL.FileSize() < 2)
  607. return ShowHelp(CmdL, nullptr);
  608. string Override;
  609. if (CmdL.FileSize() >= 3)
  610. Override = CmdL.FileList[2];
  611. string SOverride;
  612. if (Override.empty() == false)
  613. SOverride = Override + ".src";
  614. SOverride = _config->Find("APT::FTPArchive::SourceOverride",
  615. SOverride.c_str());
  616. // Create a package writer object.
  617. SourcesWriter Sources(NULL, _config->Find("APT::FTPArchive::DB"),Override,SOverride);
  618. if (_error->PendingError() == true)
  619. return false;
  620. if (CmdL.FileSize() >= 4)
  621. Sources.PathPrefix = CmdL.FileList[3];
  622. // Do recursive directory searching
  623. if (Sources.RecursiveScan(CmdL.FileList[1]) == false)
  624. return false;
  625. // Give some stats if asked for
  626. if(_config->FindB("APT::FTPArchive::ShowCacheMisses", false) == true)
  627. c0out << " Misses in Cache: " << Sources.Stats.Misses<< endl;
  628. return true;
  629. }
  630. /*}}}*/
  631. // SimpleGenRelease - Generate a Release file for a directory tree /*{{{*/
  632. // ---------------------------------------------------------------------
  633. static bool SimpleGenRelease(CommandLine &CmdL)
  634. {
  635. if (CmdL.FileSize() < 2)
  636. return ShowHelp(CmdL, nullptr);
  637. string Dir = CmdL.FileList[1];
  638. ReleaseWriter Release(NULL, "");
  639. Release.DirStrip = Dir;
  640. if (_error->PendingError() == true)
  641. return false;
  642. if (Release.RecursiveScan(Dir) == false)
  643. return false;
  644. Release.Finish();
  645. return true;
  646. }
  647. /*}}}*/
  648. // DoGeneratePackagesAndSources - Helper for Generate /*{{{*/
  649. // ---------------------------------------------------------------------
  650. static bool DoGeneratePackagesAndSources(Configuration &Setup,
  651. vector<PackageMap> &PkgList,
  652. struct CacheDB::Stats &SrcStats,
  653. struct CacheDB::Stats &Stats,
  654. CommandLine &CmdL)
  655. {
  656. if (CmdL.FileSize() <= 2)
  657. {
  658. for (vector<PackageMap>::iterator I = PkgList.begin(); I != PkgList.end(); ++I)
  659. if (I->GenPackages(Setup,Stats) == false)
  660. _error->DumpErrors();
  661. for (vector<PackageMap>::iterator I = PkgList.begin(); I != PkgList.end(); ++I)
  662. if (I->GenSources(Setup,SrcStats) == false)
  663. _error->DumpErrors();
  664. }
  665. else
  666. {
  667. // Make a choice list out of the package list..
  668. RxChoiceList *List = new RxChoiceList[2*PkgList.size()+1];
  669. RxChoiceList *End = List;
  670. for (vector<PackageMap>::iterator I = PkgList.begin(); I != PkgList.end(); ++I)
  671. {
  672. End->UserData = &(*I);
  673. End->Str = I->BaseDir.c_str();
  674. End++;
  675. End->UserData = &(*I);
  676. End->Str = I->Tag.c_str();
  677. End++;
  678. }
  679. End->Str = 0;
  680. // Regex it
  681. if (RegexChoice(List,CmdL.FileList + 2,CmdL.FileList + CmdL.FileSize()) == 0)
  682. {
  683. delete [] List;
  684. return _error->Error(_("No selections matched"));
  685. }
  686. _error->DumpErrors();
  687. // Do the generation for Packages
  688. for (End = List; End->Str != 0; End++)
  689. {
  690. if (End->Hit == false)
  691. continue;
  692. PackageMap *I = (PackageMap *)End->UserData;
  693. if (I->PkgDone == true)
  694. continue;
  695. if (I->GenPackages(Setup,Stats) == false)
  696. _error->DumpErrors();
  697. }
  698. // Do the generation for Sources
  699. for (End = List; End->Str != 0; End++)
  700. {
  701. if (End->Hit == false)
  702. continue;
  703. PackageMap *I = (PackageMap *)End->UserData;
  704. if (I->SrcDone == true)
  705. continue;
  706. if (I->GenSources(Setup,SrcStats) == false)
  707. _error->DumpErrors();
  708. }
  709. delete [] List;
  710. }
  711. return true;
  712. }
  713. /*}}}*/
  714. // DoGenerateContents - Helper for Generate to generate the Contents /*{{{*/
  715. // ---------------------------------------------------------------------
  716. static bool DoGenerateContents(Configuration &Setup,
  717. vector<PackageMap> &PkgList,
  718. CommandLine &CmdL)
  719. {
  720. c1out << "Packages done, Starting contents." << endl;
  721. // Sort the contents file list by date
  722. string ArchiveDir = Setup.FindDir("Dir::ArchiveDir");
  723. for (vector<PackageMap>::iterator I = PkgList.begin(); I != PkgList.end(); ++I)
  724. {
  725. struct stat A;
  726. if (MultiCompress::GetStat(flCombine(ArchiveDir,I->Contents),
  727. I->CntCompress,A) == false)
  728. time(&I->ContentsMTime);
  729. else
  730. I->ContentsMTime = A.st_mtime;
  731. }
  732. stable_sort(PkgList.begin(),PkgList.end(),PackageMap::ContentsCompare());
  733. /* Now for Contents.. The process here is to do a make-like dependency
  734. check. Each contents file is verified to be newer than the package files
  735. that describe the debs it indexes. Since the package files contain
  736. hashes of the .debs this means they have not changed either so the
  737. contents must be up to date. */
  738. unsigned long MaxContentsChange = Setup.FindI("Default::MaxContentsChange",
  739. std::numeric_limits<unsigned int>::max())*1024;
  740. for (vector<PackageMap>::iterator I = PkgList.begin(); I != PkgList.end(); ++I)
  741. {
  742. // This record is not relevant
  743. if (I->ContentsDone == true ||
  744. I->Contents.empty() == true)
  745. continue;
  746. // Do not do everything if the user specified sections.
  747. if (CmdL.FileSize() > 2 && I->PkgDone == false)
  748. continue;
  749. struct stat A,B;
  750. if (MultiCompress::GetStat(flCombine(ArchiveDir,I->Contents),I->CntCompress,A) == true)
  751. {
  752. if (MultiCompress::GetStat(flCombine(ArchiveDir,I->PkgFile),I->PkgCompress,B) == false)
  753. {
  754. _error->Warning(_("Some files are missing in the package file group `%s'"),I->PkgFile.c_str());
  755. continue;
  756. }
  757. if (A.st_mtime > B.st_mtime)
  758. continue;
  759. }
  760. if (I->GenContents(Setup,PkgList.begin(),PkgList.end(),
  761. MaxContentsChange) == false)
  762. _error->DumpErrors();
  763. // Hit the limit?
  764. if (MaxContentsChange == 0)
  765. {
  766. c1out << "Hit contents update byte limit" << endl;
  767. break;
  768. }
  769. }
  770. return true;
  771. }
  772. /*}}}*/
  773. // Generate - Full generate, using a config file /*{{{*/
  774. // ---------------------------------------------------------------------
  775. /* */
  776. static bool Generate(CommandLine &CmdL)
  777. {
  778. struct CacheDB::Stats SrcStats;
  779. if (CmdL.FileSize() < 2)
  780. return ShowHelp(CmdL, nullptr);
  781. struct timeval StartTime;
  782. gettimeofday(&StartTime,0);
  783. struct CacheDB::Stats Stats;
  784. // Read the configuration file.
  785. Configuration Setup;
  786. if (ReadConfigFile(Setup,CmdL.FileList[1],true) == false)
  787. return false;
  788. vector<PackageMap> PkgList;
  789. std::vector<TranslationWriter*> TransList;
  790. LoadTree(PkgList, TransList, Setup);
  791. LoadBinDir(PkgList,Setup);
  792. // Sort by cache DB to improve IO locality.
  793. stable_sort(PkgList.begin(),PkgList.end(),PackageMap::DBCompare());
  794. stable_sort(PkgList.begin(),PkgList.end(),PackageMap::SrcDBCompare());
  795. // Generate packages
  796. if (_config->FindB("APT::FTPArchive::ContentsOnly", false) == false)
  797. {
  798. if(DoGeneratePackagesAndSources(Setup, PkgList, SrcStats, Stats, CmdL) == false)
  799. {
  800. UnloadTree(TransList);
  801. return false;
  802. }
  803. } else {
  804. c1out << "Skipping Packages/Sources generation" << endl;
  805. }
  806. // do Contents if needed
  807. if (_config->FindB("APT::FTPArchive::Contents", true) == true)
  808. if (DoGenerateContents(Setup, PkgList, CmdL) == false)
  809. {
  810. UnloadTree(TransList);
  811. return false;
  812. }
  813. struct timeval NewTime;
  814. gettimeofday(&NewTime,0);
  815. double Delta = NewTime.tv_sec - StartTime.tv_sec +
  816. (NewTime.tv_usec - StartTime.tv_usec)/1000000.0;
  817. c1out << "Done. " << SizeToStr(Stats.Bytes) << "B in " << Stats.Packages
  818. << " archives. Took " << TimeToStr((long)Delta) << endl;
  819. UnloadTree(TransList);
  820. return true;
  821. }
  822. /*}}}*/
  823. // Clean - Clean out the databases /*{{{*/
  824. // ---------------------------------------------------------------------
  825. /* */
  826. static bool Clean(CommandLine &CmdL)
  827. {
  828. if (CmdL.FileSize() != 2)
  829. return ShowHelp(CmdL, nullptr);
  830. // Read the configuration file.
  831. Configuration Setup;
  832. if (ReadConfigFile(Setup,CmdL.FileList[1],true) == false)
  833. return false;
  834. // we don't need translation creation here
  835. Setup.Set("TreeDefault::Translation", "/dev/null");
  836. vector<PackageMap> PkgList;
  837. std::vector<TranslationWriter*> TransList;
  838. LoadTree(PkgList, TransList, Setup);
  839. LoadBinDir(PkgList,Setup);
  840. // Sort by cache DB to improve IO locality.
  841. stable_sort(PkgList.begin(),PkgList.end(),PackageMap::DBCompare());
  842. stable_sort(PkgList.begin(),PkgList.end(),PackageMap::SrcDBCompare());
  843. string CacheDir = Setup.FindDir("Dir::CacheDir");
  844. for (vector<PackageMap>::iterator I = PkgList.begin(); I != PkgList.end(); )
  845. {
  846. if(I->BinCacheDB != "")
  847. c0out << I->BinCacheDB << endl;
  848. if(I->SrcCacheDB != "")
  849. c0out << I->SrcCacheDB << endl;
  850. CacheDB DB(flCombine(CacheDir,I->BinCacheDB));
  851. CacheDB DB_SRC(flCombine(CacheDir,I->SrcCacheDB));
  852. if (DB.Clean() == false)
  853. _error->DumpErrors();
  854. if (DB_SRC.Clean() == false)
  855. _error->DumpErrors();
  856. string CacheDB = I->BinCacheDB;
  857. string SrcCacheDB = I->SrcCacheDB;
  858. while(I != PkgList.end() &&
  859. I->BinCacheDB == CacheDB &&
  860. I->SrcCacheDB == SrcCacheDB)
  861. ++I;
  862. }
  863. return true;
  864. }
  865. /*}}}*/
  866. std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
  867. {
  868. return {
  869. {"packages",&SimpleGenPackages, nullptr},
  870. {"contents",&SimpleGenContents, nullptr},
  871. {"sources",&SimpleGenSources, nullptr},
  872. {"release",&SimpleGenRelease, nullptr},
  873. {"generate",&Generate, nullptr},
  874. {"clean",&Clean, nullptr},
  875. {nullptr, nullptr, nullptr}
  876. };
  877. }
  878. /*}}}*/
  879. int main(int argc, const char *argv[]) /*{{{*/
  880. {
  881. InitLocale();
  882. // Parse the command line and initialize the package library
  883. CommandLine CmdL;
  884. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_FTPARCHIVE, &_config, NULL, argc, argv);
  885. _config->CndSet("quiet",0);
  886. Quiet = _config->FindI("quiet",0);
  887. InitOutput(clog.rdbuf());
  888. return DispatchCommandLine(CmdL, Cmds);
  889. }
  890. /*}}}*/