apt-ftparchive.cc 30 KB

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