apt-ftparchive.cc 30 KB

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