apt-ftparchive.cc 34 KB

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