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

1015 lines
32 KiB

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