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.
 
 
 
 
 
 

758 lines
22 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: writer.cc,v 1.3 2001/05/29 04:08:09 jgg Exp $
  4. /* ######################################################################
  5. Writer
  6. The file writer classes. These write various types of output, sources,
  7. packages and contents.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #ifdef __GNUG__
  12. #pragma implementation "writer.h"
  13. #endif
  14. #include "writer.h"
  15. #include <apt-pkg/strutl.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/md5.h>
  19. #include <apt-pkg/deblistparser.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <ftw.h>
  23. #include <iostream>
  24. #include "cachedb.h"
  25. #include "apt-ftparchive.h"
  26. #include "multicompress.h"
  27. /*}}}*/
  28. using namespace std;
  29. FTWScanner *FTWScanner::Owner;
  30. // FTWScanner::FTWScanner - Constructor /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* */
  33. FTWScanner::FTWScanner()
  34. {
  35. ErrorPrinted = false;
  36. NoLinkAct = !_config->FindB("APT::FTPArchive::DeLinkAct",true);
  37. TmpExt = 0;
  38. Ext[0] = 0;
  39. RealPath = 0;
  40. long PMax = pathconf(".",_PC_PATH_MAX);
  41. if (PMax > 0)
  42. RealPath = new char[PMax];
  43. }
  44. /*}}}*/
  45. // FTWScanner::Scanner - FTW Scanner /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* This is the FTW scanner, it processes each directory element in the
  48. directory tree. */
  49. int FTWScanner::Scanner(const char *File,const struct stat *sb,int Flag)
  50. {
  51. if (Flag == FTW_DNR)
  52. {
  53. Owner->NewLine(1);
  54. c1out << "W: Unable to read directory " << File << endl;
  55. }
  56. if (Flag == FTW_NS)
  57. {
  58. Owner->NewLine(1);
  59. c1out << "W: Unable to stat " << File << endl;
  60. }
  61. if (Flag != FTW_F)
  62. return 0;
  63. // See if it is a .deb
  64. if (strlen(File) < 4)
  65. return 0;
  66. unsigned CurExt = 0;
  67. for (; Owner->Ext[CurExt] != 0; CurExt++)
  68. if (strcmp(File+strlen(File)-strlen(Owner->Ext[CurExt]),
  69. Owner->Ext[CurExt]) == 0)
  70. break;
  71. if (Owner->Ext[CurExt] == 0)
  72. return 0;
  73. /* Process it. If the file is a link then resolve it into an absolute
  74. name.. This works best if the directory components the scanner are
  75. given are not links themselves. */
  76. char Jnk[2];
  77. Owner->OriginalPath = File;
  78. if (Owner->RealPath != 0 && readlink(File,Jnk,sizeof(Jnk)) != -1 &&
  79. realpath(File,Owner->RealPath) != 0)
  80. Owner->DoPackage(Owner->RealPath);
  81. else
  82. Owner->DoPackage(File);
  83. if (_error->empty() == false)
  84. {
  85. // Print any errors or warnings found
  86. string Err;
  87. bool SeenPath = false;
  88. while (_error->empty() == false)
  89. {
  90. Owner->NewLine(1);
  91. bool Type = _error->PopMessage(Err);
  92. if (Type == true)
  93. c1out << "E: " << Err << endl;
  94. else
  95. c1out << "W: " << Err << endl;
  96. if (Err.find(File) != string::npos)
  97. SeenPath = true;
  98. }
  99. if (SeenPath == false)
  100. cerr << "E: Errors apply to file '" << File << "'" << endl;
  101. return 0;
  102. }
  103. return 0;
  104. }
  105. /*}}}*/
  106. // FTWScanner::RecursiveScan - Just scan a directory tree /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* */
  109. bool FTWScanner::RecursiveScan(string Dir)
  110. {
  111. /* If noprefix is set then jam the scan root in, so we don't generate
  112. link followed paths out of control */
  113. if (InternalPrefix.empty() == true)
  114. {
  115. if (realpath(Dir.c_str(),RealPath) == 0)
  116. return _error->Errno("realpath","Failed to resolve %s",Dir.c_str());
  117. InternalPrefix = RealPath;
  118. }
  119. // Do recursive directory searching
  120. Owner = this;
  121. int Res = ftw(Dir.c_str(),Scanner,30);
  122. // Error treewalking?
  123. if (Res != 0)
  124. {
  125. if (_error->PendingError() == false)
  126. _error->Errno("ftw","Tree walking failed");
  127. return false;
  128. }
  129. return true;
  130. }
  131. /*}}}*/
  132. // FTWScanner::LoadFileList - Load the file list from a file /*{{{*/
  133. // ---------------------------------------------------------------------
  134. /* This is an alternative to using FTW to locate files, it reads the list
  135. of files from another file. */
  136. bool FTWScanner::LoadFileList(string Dir,string File)
  137. {
  138. /* If noprefix is set then jam the scan root in, so we don't generate
  139. link followed paths out of control */
  140. if (InternalPrefix.empty() == true)
  141. {
  142. if (realpath(Dir.c_str(),RealPath) == 0)
  143. return _error->Errno("realpath","Failed to resolve %s",Dir.c_str());
  144. InternalPrefix = RealPath;
  145. }
  146. Owner = this;
  147. FILE *List = fopen(File.c_str(),"r");
  148. if (List == 0)
  149. return _error->Errno("fopen","Failed to open %s",File.c_str());
  150. /* We are a tad tricky here.. We prefix the buffer with the directory
  151. name, that way if we need a full path with just use line.. Sneaky and
  152. fully evil. */
  153. char Line[1000];
  154. char *FileStart;
  155. if (Dir.empty() == true || Dir.end()[-1] != '/')
  156. FileStart = Line + snprintf(Line,sizeof(Line),"%s/",Dir.c_str());
  157. else
  158. FileStart = Line + snprintf(Line,sizeof(Line),"%s",Dir.c_str());
  159. while (fgets(FileStart,sizeof(Line) - (FileStart - Line),List) != 0)
  160. {
  161. char *FileName = _strstrip(FileStart);
  162. if (FileName[0] == 0)
  163. continue;
  164. if (FileName[0] != '/')
  165. {
  166. if (FileName != FileStart)
  167. memmove(FileStart,FileName,strlen(FileStart));
  168. FileName = Line;
  169. }
  170. struct stat St;
  171. int Flag = FTW_F;
  172. if (stat(FileName,&St) != 0)
  173. Flag = FTW_NS;
  174. if (Scanner(FileName,&St,Flag) != 0)
  175. break;
  176. }
  177. fclose(List);
  178. return true;
  179. }
  180. /*}}}*/
  181. // FTWScanner::Delink - Delink symlinks /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* */
  184. bool FTWScanner::Delink(string &FileName,const char *OriginalPath,
  185. unsigned long &DeLinkBytes,
  186. struct stat &St)
  187. {
  188. // See if this isn't an internaly prefix'd file name.
  189. if (InternalPrefix.empty() == false &&
  190. InternalPrefix.length() < FileName.length() &&
  191. stringcmp(FileName.begin(),FileName.begin() + InternalPrefix.length(),
  192. InternalPrefix.begin(),InternalPrefix.end()) != 0)
  193. {
  194. if (DeLinkLimit != 0 && DeLinkBytes/1024 < DeLinkLimit)
  195. {
  196. // Tidy up the display
  197. if (DeLinkBytes == 0)
  198. cout << endl;
  199. NewLine(1);
  200. c1out << " DeLink " << (OriginalPath + InternalPrefix.length())
  201. << " [" << SizeToStr(St.st_size) << "B]" << endl << flush;
  202. if (NoLinkAct == false)
  203. {
  204. char OldLink[400];
  205. if (readlink(OriginalPath,OldLink,sizeof(OldLink)) == -1)
  206. _error->Errno("readlink","Failed to readlink %s",OriginalPath);
  207. else
  208. {
  209. if (unlink(OriginalPath) != 0)
  210. _error->Errno("unlink","Failed to unlink %s",OriginalPath);
  211. else
  212. {
  213. if (link(FileName.c_str(),OriginalPath) != 0)
  214. {
  215. // Panic! Restore the symlink
  216. symlink(OldLink,OriginalPath);
  217. return _error->Errno("link","*** Failed to link %s to %s",
  218. FileName.c_str(),
  219. OriginalPath);
  220. }
  221. }
  222. }
  223. }
  224. DeLinkBytes += St.st_size;
  225. if (DeLinkBytes/1024 >= DeLinkLimit)
  226. c1out << " DeLink limit of " << SizeToStr(DeLinkBytes) << "B hit." << endl;
  227. }
  228. FileName = OriginalPath;
  229. }
  230. return true;
  231. }
  232. /*}}}*/
  233. // FTWScanner::SetExts - Set extensions to support /*{{{*/
  234. // ---------------------------------------------------------------------
  235. /* */
  236. bool FTWScanner::SetExts(string Vals)
  237. {
  238. delete [] TmpExt;
  239. TmpExt = new char[Vals.length()+1];
  240. strcpy(TmpExt,Vals.c_str());
  241. return TokSplitString(' ',TmpExt,(char **)Ext,sizeof(Ext)/sizeof(Ext[0]));
  242. }
  243. /*}}}*/
  244. // PackagesWriter::PackagesWriter - Constructor /*{{{*/
  245. // ---------------------------------------------------------------------
  246. /* */
  247. PackagesWriter::PackagesWriter(string DB,string Overrides) :
  248. Db(DB),Stats(Db.Stats)
  249. {
  250. Output = stdout;
  251. Ext[0] = ".deb";
  252. Ext[1] = 0;
  253. DeLinkLimit = 0;
  254. // Process the command line options
  255. DoMD5 = _config->FindB("APT::FTPArchive::MD5",true);
  256. DoContents = _config->FindB("APT::FTPArchive::Contents",true);
  257. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  258. if (Db.Loaded() == false)
  259. DoContents = false;
  260. // Read the override file
  261. if (Overrides.empty() == false && Over.ReadOverride(Overrides) == false)
  262. return;
  263. else
  264. NoOverride = true;
  265. _error->DumpErrors();
  266. }
  267. /*}}}*/
  268. // PackagesWriter::DoPackage - Process a single package /*{{{*/
  269. // ---------------------------------------------------------------------
  270. /* This method takes a package and gets its control information and
  271. MD5 then writes out a control record with the proper fields rewritten
  272. and the path/size/hash appended. */
  273. bool PackagesWriter::DoPackage(string FileName)
  274. {
  275. // Open the archive
  276. FileFd F(FileName,FileFd::ReadOnly);
  277. if (_error->PendingError() == true)
  278. return false;
  279. // Stat the file for later
  280. struct stat St;
  281. if (fstat(F.Fd(),&St) != 0)
  282. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  283. // Pull all the data we need form the DB
  284. string MD5Res;
  285. if (Db.SetFile(FileName,St,&F) == false ||
  286. Db.LoadControl() == false ||
  287. (DoContents == true && Db.LoadContents(true) == false) ||
  288. (DoMD5 == true && Db.GetMD5(MD5Res,false) == false))
  289. return false;
  290. if (Delink(FileName,OriginalPath,Stats.DeLinkBytes,St) == false)
  291. return false;
  292. // Lookup the overide information
  293. pkgTagSection &Tags = Db.Control.Section;
  294. string Package = Tags.FindS("Package");
  295. Override::Item Tmp;
  296. Override::Item *OverItem = Over.GetItem(Package);
  297. if (Package.empty() == true)
  298. return _error->Error("Archive had no package field");
  299. // If we need to do any rewriting of the header do it now..
  300. if (OverItem == 0)
  301. {
  302. if (NoOverride == false)
  303. {
  304. NewLine(1);
  305. c1out << " " << Package << " has no override entry" << endl;
  306. }
  307. OverItem = &Tmp;
  308. Tmp.Section = Tags.FindS("Section");
  309. Tmp.Priority = Tags.FindS("Priority");
  310. }
  311. char Size[40];
  312. sprintf(Size,"%lu",St.st_size);
  313. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  314. string NewFileName;
  315. if (DirStrip.empty() == false &&
  316. FileName.length() > DirStrip.length() &&
  317. stringcmp(FileName.begin(),FileName.begin() + DirStrip.length(),
  318. DirStrip.begin(),DirStrip.end()) == 0)
  319. NewFileName = string(FileName.begin() + DirStrip.length(),FileName.end());
  320. else
  321. NewFileName = FileName;
  322. if (PathPrefix.empty() == false)
  323. NewFileName = flCombine(PathPrefix,NewFileName);
  324. // This lists all the changes to the fields we are going to make.
  325. TFRewriteData Changes[] = {{"Size",Size},
  326. {"MD5sum",MD5Res.c_str()},
  327. {"Filename",NewFileName.c_str()},
  328. {"Section",OverItem->Section.c_str()},
  329. {"Priority",OverItem->Priority.c_str()},
  330. {"Status",0},
  331. {"Optional",0},
  332. {}, // For maintainer
  333. {}, // For Suggests
  334. {}};
  335. unsigned int End = 0;
  336. for (End = 0; Changes[End].Tag != 0; End++);
  337. // Rewrite the maintainer field if necessary
  338. bool MaintFailed;
  339. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  340. if (MaintFailed == true)
  341. {
  342. if (NoOverride == false)
  343. {
  344. NewLine(1);
  345. c1out << " " << Package << " maintainer is " <<
  346. Tags.FindS("Maintainer") << " not " <<
  347. OverItem->OldMaint << endl;
  348. }
  349. }
  350. if (NewMaint.empty() == false)
  351. {
  352. Changes[End].Rewrite = NewMaint.c_str();
  353. Changes[End++].Tag = "Maintainer";
  354. }
  355. /* Get rid of the Optional tag. This is an ugly, ugly, ugly hack that
  356. dpkg-scanpackages does.. Well sort of. dpkg-scanpackages just does renaming
  357. but dpkg does this append bit. So we do the append bit, at least that way the
  358. status file and package file will remain similar. There are other transforms
  359. but optional is the only legacy one still in use for some lazy reason. */
  360. string OptionalStr = Tags.FindS("Optional");
  361. if (OptionalStr.empty() == false)
  362. {
  363. if (Tags.FindS("Suggests").empty() == false)
  364. OptionalStr = Tags.FindS("Suggests") + ", " + OptionalStr;
  365. Changes[End].Rewrite = OptionalStr.c_str();
  366. Changes[End++].Tag = "Suggests";
  367. }
  368. // Rewrite and store the fields.
  369. if (TFRewrite(Output,Tags,TFRewritePackageOrder,Changes) == false)
  370. return false;
  371. fprintf(Output,"\n");
  372. return Db.Finish();
  373. }
  374. /*}}}*/
  375. // SourcesWriter::SourcesWriter - Constructor /*{{{*/
  376. // ---------------------------------------------------------------------
  377. /* */
  378. SourcesWriter::SourcesWriter(string BOverrides,string SOverrides)
  379. {
  380. Output = stdout;
  381. Ext[0] = ".dsc";
  382. Ext[1] = 0;
  383. DeLinkLimit = 0;
  384. Buffer = 0;
  385. BufSize = 0;
  386. // Process the command line options
  387. NoOverride = _config->FindB("APT::FTPArchive::NoOverrideMsg",false);
  388. // Read the override file
  389. if (BOverrides.empty() == false && BOver.ReadOverride(BOverrides) == false)
  390. return;
  391. else
  392. NoOverride = true;
  393. if (SOverrides.empty() == false && FileExists(SOverrides) == true &&
  394. SOver.ReadOverride(SOverrides,true) == false)
  395. return;
  396. // _error->DumpErrors();
  397. }
  398. /*}}}*/
  399. // SourcesWriter::DoPackage - Process a single package /*{{{*/
  400. // ---------------------------------------------------------------------
  401. /* */
  402. bool SourcesWriter::DoPackage(string FileName)
  403. {
  404. // Open the archive
  405. FileFd F(FileName,FileFd::ReadOnly);
  406. if (_error->PendingError() == true)
  407. return false;
  408. // Stat the file for later
  409. struct stat St;
  410. if (fstat(F.Fd(),&St) != 0)
  411. return _error->Errno("fstat","Failed to stat %s",FileName.c_str());
  412. if (St.st_size > 128*1024)
  413. return _error->Error("DSC file '%s' is too large!",FileName.c_str());
  414. if (BufSize < (unsigned)St.st_size+1)
  415. {
  416. BufSize = St.st_size+1;
  417. Buffer = (char *)realloc(Buffer,St.st_size+1);
  418. }
  419. if (F.Read(Buffer,St.st_size) == false)
  420. return false;
  421. // Hash the file
  422. char *Start = Buffer;
  423. char *BlkEnd = Buffer + St.st_size;
  424. MD5Summation MD5;
  425. MD5.Add((unsigned char *)Start,BlkEnd - Start);
  426. // Add an extra \n to the end, just in case
  427. *BlkEnd++ = '\n';
  428. /* Remove the PGP trailer. Some .dsc's have this without a blank line
  429. before */
  430. const char *Key = "-----BEGIN PGP SIGNATURE-----";
  431. for (char *MsgEnd = Start; MsgEnd < BlkEnd - strlen(Key) -1; MsgEnd++)
  432. {
  433. if (*MsgEnd == '\n' && strncmp(MsgEnd+1,Key,strlen(Key)) == 0)
  434. {
  435. MsgEnd[1] = '\n';
  436. break;
  437. }
  438. }
  439. /* Read records until we locate the Source record. This neatly skips the
  440. GPG header (which is RFC822 formed) without any trouble. */
  441. pkgTagSection Tags;
  442. do
  443. {
  444. unsigned Pos;
  445. if (Tags.Scan(Start,BlkEnd - Start) == false)
  446. return _error->Error("Could not find a record in the DSC '%s'",FileName.c_str());
  447. if (Tags.Find("Source",Pos) == true)
  448. break;
  449. Start += Tags.size();
  450. }
  451. while (1);
  452. Tags.Trim();
  453. // Lookup the overide information, finding first the best priority.
  454. string BestPrio;
  455. char Buffer[1000];
  456. string Bins = Tags.FindS("Binary");
  457. Override::Item *OverItem = 0;
  458. if (Bins.empty() == false && Bins.length() < sizeof(Buffer))
  459. {
  460. strcpy(Buffer,Bins.c_str());
  461. // Ignore too-long errors.
  462. char *BinList[400];
  463. TokSplitString(',',Buffer,BinList,sizeof(BinList)/sizeof(BinList[0]));
  464. // Look at all the binaries
  465. unsigned char BestPrioV = pkgCache::State::Extra;
  466. for (unsigned I = 0; BinList[I] != 0; I++)
  467. {
  468. Override::Item *Itm = BOver.GetItem(BinList[I]);
  469. if (Itm == 0)
  470. continue;
  471. if (OverItem == 0)
  472. OverItem = Itm;
  473. unsigned char NewPrioV = debListParser::GetPrio(Itm->Priority);
  474. if (NewPrioV < BestPrioV || BestPrio.empty() == true)
  475. {
  476. BestPrioV = NewPrioV;
  477. BestPrio = Itm->Priority;
  478. }
  479. }
  480. }
  481. // If we need to do any rewriting of the header do it now..
  482. Override::Item Tmp;
  483. if (OverItem == 0)
  484. {
  485. if (NoOverride == false)
  486. {
  487. NewLine(1);
  488. c1out << " " << Tags.FindS("Source") << " has no override entry" << endl;
  489. }
  490. OverItem = &Tmp;
  491. }
  492. Override::Item *SOverItem = SOver.GetItem(Tags.FindS("Source"));
  493. if (SOverItem == 0)
  494. {
  495. SOverItem = BOver.GetItem(Tags.FindS("Source"));
  496. if (SOverItem == 0)
  497. SOverItem = OverItem;
  498. }
  499. // Add the dsc to the files hash list
  500. char Files[1000];
  501. snprintf(Files,sizeof(Files),"\n %s %lu %s\n %s",
  502. string(MD5.Result()).c_str(),St.st_size,
  503. flNotDir(FileName).c_str(),
  504. Tags.FindS("Files").c_str());
  505. // Strip the DirStrip prefix from the FileName and add the PathPrefix
  506. string NewFileName;
  507. if (DirStrip.empty() == false &&
  508. FileName.length() > DirStrip.length() &&
  509. stringcmp(DirStrip,OriginalPath,OriginalPath + DirStrip.length()) == 0)
  510. NewFileName = string(OriginalPath + DirStrip.length());
  511. else
  512. NewFileName = OriginalPath;
  513. if (PathPrefix.empty() == false)
  514. NewFileName = flCombine(PathPrefix,NewFileName);
  515. string Directory = flNotFile(OriginalPath);
  516. string Package = Tags.FindS("Source");
  517. // Perform the delinking operation over all of the files
  518. string ParseJnk;
  519. const char *C = Files;
  520. for (;isspace(*C); C++);
  521. while (*C != 0)
  522. {
  523. // Parse each of the elements
  524. if (ParseQuoteWord(C,ParseJnk) == false ||
  525. ParseQuoteWord(C,ParseJnk) == false ||
  526. ParseQuoteWord(C,ParseJnk) == false)
  527. return _error->Error("Error parsing file record");
  528. char Jnk[2];
  529. string OriginalPath = Directory + ParseJnk;
  530. if (RealPath != 0 && readlink(OriginalPath.c_str(),Jnk,sizeof(Jnk)) != -1 &&
  531. realpath(OriginalPath.c_str(),RealPath) != 0)
  532. {
  533. string RP = RealPath;
  534. if (Delink(RP,OriginalPath.c_str(),Stats.DeLinkBytes,St) == false)
  535. return false;
  536. }
  537. }
  538. Directory = flNotFile(NewFileName);
  539. if (Directory.length() > 2)
  540. Directory.erase(Directory.end()-1);
  541. // This lists all the changes to the fields we are going to make.
  542. TFRewriteData Changes[] = {{"Source",Package.c_str(),"Package"},
  543. {"Files",Files},
  544. {"Directory",Directory.c_str()},
  545. {"Section",SOverItem->Section.c_str()},
  546. {"Priority",BestPrio.c_str()},
  547. {"Status",0},
  548. {}, // For maintainer
  549. {}};
  550. unsigned int End = 0;
  551. for (End = 0; Changes[End].Tag != 0; End++);
  552. // Rewrite the maintainer field if necessary
  553. bool MaintFailed;
  554. string NewMaint = OverItem->SwapMaint(Tags.FindS("Maintainer"),MaintFailed);
  555. if (MaintFailed == true)
  556. {
  557. if (NoOverride == false)
  558. {
  559. NewLine(1);
  560. c1out << " " << Package << " maintainer is " <<
  561. Tags.FindS("Maintainer") << " not " <<
  562. OverItem->OldMaint << endl;
  563. }
  564. }
  565. if (NewMaint.empty() == false)
  566. {
  567. Changes[End].Rewrite = NewMaint.c_str();
  568. Changes[End++].Tag = "Maintainer";
  569. }
  570. // Rewrite and store the fields.
  571. if (TFRewrite(Output,Tags,TFRewriteSourceOrder,Changes) == false)
  572. return false;
  573. fprintf(Output,"\n");
  574. Stats.Packages++;
  575. return true;
  576. }
  577. /*}}}*/
  578. // ContentsWriter::ContentsWriter - Constructor /*{{{*/
  579. // ---------------------------------------------------------------------
  580. /* */
  581. ContentsWriter::ContentsWriter(string DB) :
  582. Db(DB), Stats(Db.Stats)
  583. {
  584. Ext[0] = ".deb";
  585. Ext[1] = 0;
  586. Output = stdout;
  587. }
  588. /*}}}*/
  589. // ContentsWriter::DoPackage - Process a single package /*{{{*/
  590. // ---------------------------------------------------------------------
  591. /* If Package is the empty string the control record will be parsed to
  592. determine what the package name is. */
  593. bool ContentsWriter::DoPackage(string FileName,string Package)
  594. {
  595. // Open the archive
  596. FileFd F(FileName,FileFd::ReadOnly);
  597. if (_error->PendingError() == true)
  598. return false;
  599. // Stat the file for later
  600. struct stat St;
  601. if (fstat(F.Fd(),&St) != 0)
  602. return _error->Errno("fstat","Failed too stat %s",FileName.c_str());
  603. // Ready the DB
  604. if (Db.SetFile(FileName,St,&F) == false ||
  605. Db.LoadContents(false) == false)
  606. return false;
  607. // Parse the package name
  608. if (Package.empty() == true)
  609. {
  610. if (Db.LoadControl() == false)
  611. return false;
  612. Package = Db.Control.Section.FindS("Package");
  613. }
  614. Db.Contents.Add(Gen,Package);
  615. return Db.Finish();
  616. }
  617. /*}}}*/
  618. // ContentsWriter::ReadFromPkgs - Read from a packages file /*{{{*/
  619. // ---------------------------------------------------------------------
  620. /* */
  621. bool ContentsWriter::ReadFromPkgs(string PkgFile,string PkgCompress)
  622. {
  623. MultiCompress Pkgs(PkgFile,PkgCompress,0,false);
  624. if (_error->PendingError() == true)
  625. return false;
  626. // Open the package file
  627. int CompFd = -1;
  628. int Proc = -1;
  629. if (Pkgs.OpenOld(CompFd,Proc) == false)
  630. return false;
  631. // No auto-close FD
  632. FileFd Fd(CompFd,false);
  633. pkgTagFile Tags(&Fd);
  634. if (_error->PendingError() == true)
  635. {
  636. Pkgs.CloseOld(CompFd,Proc);
  637. return false;
  638. }
  639. // Parse.
  640. pkgTagSection Section;
  641. while (Tags.Step(Section) == true)
  642. {
  643. string File = flCombine(Prefix,Section.FindS("FileName"));
  644. string Package = Section.FindS("Section");
  645. if (Package.empty() == false && Package.end()[-1] != '/')
  646. {
  647. Package += '/';
  648. Package += Section.FindS("Package");
  649. }
  650. else
  651. Package += Section.FindS("Package");
  652. DoPackage(File,Package);
  653. if (_error->empty() == false)
  654. {
  655. _error->Error("Errors apply to file '%s'",File.c_str());
  656. _error->DumpErrors();
  657. }
  658. }
  659. // Tidy the compressor
  660. if (Pkgs.CloseOld(CompFd,Proc) == false)
  661. return false;
  662. return true;
  663. }
  664. /*}}}*/