apt-ftparchive.cc 32 KB

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