acquire-worker.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: acquire-worker.cc,v 1.34 2001/05/22 04:42:54 jgg Exp $
  4. /* ######################################################################
  5. Acquire Worker
  6. The worker process can startup either as a Configuration prober
  7. or as a queue runner. As a configuration prober it only reads the
  8. configuration message and
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <config.h>
  13. #include <apt-pkg/acquire.h>
  14. #include <apt-pkg/acquire-worker.h>
  15. #include <apt-pkg/acquire-item.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/fileutl.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/hashes.h>
  21. #include <algorithm>
  22. #include <string>
  23. #include <vector>
  24. #include <iostream>
  25. #include <sys/stat.h>
  26. #include <stdlib.h>
  27. #include <unistd.h>
  28. #include <signal.h>
  29. #include <stdio.h>
  30. #include <errno.h>
  31. #include <sstream>
  32. #include <apti18n.h>
  33. /*}}}*/
  34. using namespace std;
  35. // Worker::Worker - Constructor for Queue startup /*{{{*/
  36. pkgAcquire::Worker::Worker(Queue *Q, MethodConfig *Cnf, pkgAcquireStatus *log) :
  37. d(NULL), OwnerQ(Q), Log(log), Config(Cnf), Access(Cnf->Access),
  38. CurrentItem(nullptr), CurrentSize(0), TotalSize(0)
  39. {
  40. Construct();
  41. }
  42. /*}}}*/
  43. // Worker::Worker - Constructor for method config startup /*{{{*/
  44. pkgAcquire::Worker::Worker(MethodConfig *Cnf) : Worker(nullptr, Cnf, nullptr)
  45. {
  46. }
  47. /*}}}*/
  48. // Worker::Construct - Constructor helper /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* */
  51. void pkgAcquire::Worker::Construct()
  52. {
  53. NextQueue = 0;
  54. NextAcquire = 0;
  55. Process = -1;
  56. InFd = -1;
  57. OutFd = -1;
  58. OutReady = false;
  59. InReady = false;
  60. Debug = _config->FindB("Debug::pkgAcquire::Worker",false);
  61. }
  62. /*}}}*/
  63. // Worker::~Worker - Destructor /*{{{*/
  64. // ---------------------------------------------------------------------
  65. /* */
  66. pkgAcquire::Worker::~Worker()
  67. {
  68. close(InFd);
  69. close(OutFd);
  70. if (Process > 0)
  71. {
  72. /* Closing of stdin is the signal to exit and die when the process
  73. indicates it needs cleanup */
  74. if (Config->NeedsCleanup == false)
  75. kill(Process,SIGINT);
  76. ExecWait(Process,Access.c_str(),true);
  77. }
  78. }
  79. /*}}}*/
  80. // Worker::Start - Start the worker process /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* This forks the method and inits the communication channel */
  83. bool pkgAcquire::Worker::Start()
  84. {
  85. // Get the method path
  86. string Method = _config->FindDir("Dir::Bin::Methods") + Access;
  87. if (FileExists(Method) == false)
  88. {
  89. _error->Error(_("The method driver %s could not be found."),Method.c_str());
  90. if (Access == "https")
  91. _error->Notice(_("Is the package %s installed?"), "apt-transport-https");
  92. return false;
  93. }
  94. if (Debug == true)
  95. clog << "Starting method '" << Method << '\'' << endl;
  96. // Create the pipes
  97. int Pipes[4] = {-1,-1,-1,-1};
  98. if (pipe(Pipes) != 0 || pipe(Pipes+2) != 0)
  99. {
  100. _error->Errno("pipe","Failed to create IPC pipe to subprocess");
  101. for (int I = 0; I != 4; I++)
  102. close(Pipes[I]);
  103. return false;
  104. }
  105. for (int I = 0; I != 4; I++)
  106. SetCloseExec(Pipes[I],true);
  107. // Fork off the process
  108. Process = ExecFork();
  109. if (Process == 0)
  110. {
  111. // Setup the FDs
  112. dup2(Pipes[1],STDOUT_FILENO);
  113. dup2(Pipes[2],STDIN_FILENO);
  114. SetCloseExec(STDOUT_FILENO,false);
  115. SetCloseExec(STDIN_FILENO,false);
  116. SetCloseExec(STDERR_FILENO,false);
  117. const char *Args[2];
  118. Args[0] = Method.c_str();
  119. Args[1] = 0;
  120. execv(Args[0],(char **)Args);
  121. cerr << "Failed to exec method " << Args[0] << endl;
  122. _exit(100);
  123. }
  124. // Fix up our FDs
  125. InFd = Pipes[0];
  126. OutFd = Pipes[3];
  127. SetNonBlock(Pipes[0],true);
  128. SetNonBlock(Pipes[3],true);
  129. close(Pipes[1]);
  130. close(Pipes[2]);
  131. OutReady = false;
  132. InReady = true;
  133. // Read the configuration data
  134. if (WaitFd(InFd) == false ||
  135. ReadMessages() == false)
  136. return _error->Error(_("Method %s did not start correctly"),Method.c_str());
  137. RunMessages();
  138. if (OwnerQ != 0)
  139. SendConfiguration();
  140. return true;
  141. }
  142. /*}}}*/
  143. // Worker::ReadMessages - Read all pending messages into the list /*{{{*/
  144. // ---------------------------------------------------------------------
  145. /* */
  146. bool pkgAcquire::Worker::ReadMessages()
  147. {
  148. if (::ReadMessages(InFd,MessageQueue) == false)
  149. return MethodFailure();
  150. return true;
  151. }
  152. /*}}}*/
  153. // Worker::RunMessage - Empty the message queue /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* This takes the messages from the message queue and runs them through
  156. the parsers in order. */
  157. bool pkgAcquire::Worker::RunMessages()
  158. {
  159. while (MessageQueue.empty() == false)
  160. {
  161. string Message = MessageQueue.front();
  162. MessageQueue.erase(MessageQueue.begin());
  163. if (Debug == true)
  164. clog << " <- " << Access << ':' << QuoteString(Message,"\n") << endl;
  165. // Fetch the message number
  166. char *End;
  167. int Number = strtol(Message.c_str(),&End,10);
  168. if (End == Message.c_str())
  169. return _error->Error("Invalid message from method %s: %s",Access.c_str(),Message.c_str());
  170. string URI = LookupTag(Message,"URI");
  171. pkgAcquire::Queue::QItem *Itm = NULL;
  172. if (URI.empty() == false)
  173. Itm = OwnerQ->FindItem(URI,this);
  174. if (Itm != NULL)
  175. {
  176. // update used mirror
  177. string UsedMirror = LookupTag(Message,"UsedMirror", "");
  178. if (UsedMirror.empty() == false)
  179. {
  180. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  181. (*O)->UsedMirror = UsedMirror;
  182. if (Itm->Description.find(" ") != string::npos)
  183. Itm->Description.replace(0, Itm->Description.find(" "), UsedMirror);
  184. }
  185. }
  186. // Determine the message number and dispatch
  187. switch (Number)
  188. {
  189. // 100 Capabilities
  190. case 100:
  191. if (Capabilities(Message) == false)
  192. return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
  193. break;
  194. // 101 Log
  195. case 101:
  196. if (Debug == true)
  197. clog << " <- (log) " << LookupTag(Message,"Message") << endl;
  198. break;
  199. // 102 Status
  200. case 102:
  201. Status = LookupTag(Message,"Message");
  202. break;
  203. // 103 Redirect
  204. case 103:
  205. {
  206. if (Itm == 0)
  207. {
  208. _error->Error("Method gave invalid 103 Redirect message");
  209. break;
  210. }
  211. std::string const NewURI = LookupTag(Message,"New-URI",URI.c_str());
  212. Itm->URI = NewURI;
  213. ItemDone();
  214. // Change the status so that it can be dequeued
  215. for (auto const &O: Itm->Owners)
  216. O->Status = pkgAcquire::Item::StatIdle;
  217. // Mark the item as done (taking care of all queues)
  218. // and then put it in the main queue again
  219. std::vector<Item*> const ItmOwners = Itm->Owners;
  220. OwnerQ->ItemDone(Itm);
  221. Itm = NULL;
  222. for (pkgAcquire::Queue::QItem::owner_iterator O = ItmOwners.begin(); O != ItmOwners.end(); ++O)
  223. {
  224. pkgAcquire::Item *Owner = *O;
  225. pkgAcquire::ItemDesc &desc = Owner->GetItemDesc();
  226. // if we change site, treat it as a mirror change
  227. if (URI::SiteOnly(NewURI) != URI::SiteOnly(desc.URI))
  228. {
  229. std::string const OldSite = desc.Description.substr(0, desc.Description.find(" "));
  230. if (likely(APT::String::Startswith(desc.URI, OldSite)))
  231. {
  232. std::string const OldExtra = desc.URI.substr(OldSite.length() + 1);
  233. if (likely(APT::String::Endswith(NewURI, OldExtra)))
  234. {
  235. std::string const NewSite = NewURI.substr(0, NewURI.length() - OldExtra.length());
  236. Owner->UsedMirror = URI::ArchiveOnly(NewSite);
  237. if (desc.Description.find(" ") != string::npos)
  238. desc.Description.replace(0, desc.Description.find(" "), Owner->UsedMirror);
  239. }
  240. }
  241. }
  242. desc.URI = NewURI;
  243. OwnerQ->Owner->Enqueue(desc);
  244. if (Log != 0)
  245. Log->Done(desc);
  246. }
  247. break;
  248. }
  249. // 104 Warning
  250. case 104:
  251. _error->Warning("%s: %s", Itm->Owner->DescURI().c_str(), LookupTag(Message,"Message").c_str());
  252. break;
  253. // 200 URI Start
  254. case 200:
  255. {
  256. if (Itm == 0)
  257. {
  258. _error->Error("Method gave invalid 200 URI Start message");
  259. break;
  260. }
  261. CurrentItem = Itm;
  262. CurrentSize = 0;
  263. TotalSize = strtoull(LookupTag(Message,"Size","0").c_str(), NULL, 10);
  264. ResumePoint = strtoull(LookupTag(Message,"Resume-Point","0").c_str(), NULL, 10);
  265. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  266. {
  267. (*O)->Start(Message, TotalSize);
  268. // Display update before completion
  269. if (Log != 0)
  270. {
  271. if (Log->MorePulses == true)
  272. Log->Pulse((*O)->GetOwner());
  273. Log->Fetch((*O)->GetItemDesc());
  274. }
  275. }
  276. break;
  277. }
  278. // 201 URI Done
  279. case 201:
  280. {
  281. if (Itm == 0)
  282. {
  283. _error->Error("Method gave invalid 201 URI Done message");
  284. break;
  285. }
  286. PrepareFiles("201::URIDone", Itm);
  287. // Display update before completion
  288. if (Log != 0 && Log->MorePulses == true)
  289. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  290. Log->Pulse((*O)->GetOwner());
  291. HashStringList ReceivedHashes;
  292. {
  293. std::string const givenfilename = LookupTag(Message, "Filename");
  294. std::string const filename = givenfilename.empty() ? Itm->Owner->DestFile : givenfilename;
  295. // see if we got hashes to verify
  296. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  297. {
  298. std::string const tagname = std::string(*type) + "-Hash";
  299. std::string const hashsum = LookupTag(Message, tagname.c_str());
  300. if (hashsum.empty() == false)
  301. ReceivedHashes.push_back(HashString(*type, hashsum));
  302. }
  303. // not all methods always sent Hashes our way
  304. if (ReceivedHashes.usable() == false)
  305. {
  306. HashStringList const ExpectedHashes = Itm->GetExpectedHashes();
  307. if (ExpectedHashes.usable() == true && RealFileExists(filename))
  308. {
  309. Hashes calc(ExpectedHashes);
  310. FileFd file(filename, FileFd::ReadOnly, FileFd::None);
  311. calc.AddFD(file);
  312. ReceivedHashes = calc.GetHashStringList();
  313. }
  314. }
  315. // only local files can refer other filenames and counting them as fetched would be unfair
  316. if (Log != NULL && Itm->Owner->Complete == false && Itm->Owner->Local == false && givenfilename == filename)
  317. Log->Fetched(ReceivedHashes.FileSize(),atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  318. }
  319. std::vector<Item*> const ItmOwners = Itm->Owners;
  320. OwnerQ->ItemDone(Itm);
  321. Itm = NULL;
  322. bool const isIMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false) ||
  323. StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false);
  324. for (pkgAcquire::Queue::QItem::owner_iterator O = ItmOwners.begin(); O != ItmOwners.end(); ++O)
  325. {
  326. pkgAcquire::Item * const Owner = *O;
  327. HashStringList const ExpectedHashes = Owner->GetExpectedHashes();
  328. if(_config->FindB("Debug::pkgAcquire::Auth", false) == true)
  329. {
  330. std::clog << "201 URI Done: " << Owner->DescURI() << endl
  331. << "ReceivedHash:" << endl;
  332. for (HashStringList::const_iterator hs = ReceivedHashes.begin(); hs != ReceivedHashes.end(); ++hs)
  333. std::clog << "\t- " << hs->toStr() << std::endl;
  334. std::clog << "ExpectedHash:" << endl;
  335. for (HashStringList::const_iterator hs = ExpectedHashes.begin(); hs != ExpectedHashes.end(); ++hs)
  336. std::clog << "\t- " << hs->toStr() << std::endl;
  337. std::clog << endl;
  338. }
  339. // decide if what we got is what we expected
  340. bool consideredOkay = false;
  341. if (ExpectedHashes.usable())
  342. {
  343. if (ReceivedHashes.usable() == false)
  344. {
  345. /* IMS-Hits can't be checked here as we will have uncompressed file,
  346. but the hashes for the compressed file. What we have was good through
  347. so all we have to ensure later is that we are not stalled. */
  348. consideredOkay = isIMSHit;
  349. }
  350. else if (ReceivedHashes == ExpectedHashes)
  351. consideredOkay = true;
  352. else
  353. consideredOkay = false;
  354. }
  355. else if (Owner->HashesRequired() == true)
  356. consideredOkay = false;
  357. else
  358. {
  359. consideredOkay = true;
  360. // even if the hashes aren't usable to declare something secure
  361. // we can at least use them to declare it an integrity failure
  362. if (ExpectedHashes.empty() == false && ReceivedHashes != ExpectedHashes && _config->Find("Acquire::ForceHash").empty())
  363. consideredOkay = false;
  364. }
  365. if (consideredOkay == true)
  366. consideredOkay = Owner->VerifyDone(Message, Config);
  367. else // hashsum mismatch
  368. Owner->Status = pkgAcquire::Item::StatAuthError;
  369. if (consideredOkay == true)
  370. {
  371. Owner->Done(Message, ReceivedHashes, Config);
  372. if (Log != 0)
  373. {
  374. if (isIMSHit)
  375. Log->IMSHit(Owner->GetItemDesc());
  376. else
  377. Log->Done(Owner->GetItemDesc());
  378. }
  379. }
  380. else
  381. {
  382. Owner->Failed(Message,Config);
  383. if (Log != 0)
  384. Log->Fail(Owner->GetItemDesc());
  385. }
  386. }
  387. ItemDone();
  388. break;
  389. }
  390. // 400 URI Failure
  391. case 400:
  392. {
  393. if (Itm == 0)
  394. {
  395. std::string const msg = LookupTag(Message,"Message");
  396. _error->Error("Method gave invalid 400 URI Failure message: %s", msg.c_str());
  397. break;
  398. }
  399. PrepareFiles("400::URIFailure", Itm);
  400. // Display update before completion
  401. if (Log != 0 && Log->MorePulses == true)
  402. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  403. Log->Pulse((*O)->GetOwner());
  404. std::vector<Item*> const ItmOwners = Itm->Owners;
  405. OwnerQ->ItemDone(Itm);
  406. Itm = NULL;
  407. bool errTransient;
  408. {
  409. std::string const failReason = LookupTag(Message, "FailReason");
  410. std::string const reasons[] = { "Timeout", "ConnectionRefused",
  411. "ConnectionTimedOut", "ResolveFailure", "TmpResolveFailure" };
  412. errTransient = std::find(std::begin(reasons), std::end(reasons), failReason) != std::end(reasons);
  413. }
  414. for (pkgAcquire::Queue::QItem::owner_iterator O = ItmOwners.begin(); O != ItmOwners.end(); ++O)
  415. {
  416. if (errTransient)
  417. (*O)->Status = pkgAcquire::Item::StatTransientNetworkError;
  418. (*O)->Failed(Message,Config);
  419. if (Log != 0)
  420. Log->Fail((*O)->GetItemDesc());
  421. }
  422. ItemDone();
  423. break;
  424. }
  425. // 401 General Failure
  426. case 401:
  427. _error->Error("Method %s General failure: %s",Access.c_str(),LookupTag(Message,"Message").c_str());
  428. break;
  429. // 403 Media Change
  430. case 403:
  431. MediaChange(Message);
  432. break;
  433. }
  434. }
  435. return true;
  436. }
  437. /*}}}*/
  438. // Worker::Capabilities - 100 Capabilities handler /*{{{*/
  439. // ---------------------------------------------------------------------
  440. /* This parses the capabilities message and dumps it into the configuration
  441. structure. */
  442. bool pkgAcquire::Worker::Capabilities(string Message)
  443. {
  444. if (Config == 0)
  445. return true;
  446. Config->Version = LookupTag(Message,"Version");
  447. Config->SingleInstance = StringToBool(LookupTag(Message,"Single-Instance"),false);
  448. Config->Pipeline = StringToBool(LookupTag(Message,"Pipeline"),false);
  449. Config->SendConfig = StringToBool(LookupTag(Message,"Send-Config"),false);
  450. Config->LocalOnly = StringToBool(LookupTag(Message,"Local-Only"),false);
  451. Config->NeedsCleanup = StringToBool(LookupTag(Message,"Needs-Cleanup"),false);
  452. Config->Removable = StringToBool(LookupTag(Message,"Removable"),false);
  453. // Some debug text
  454. if (Debug == true)
  455. {
  456. clog << "Configured access method " << Config->Access << endl;
  457. clog << "Version:" << Config->Version <<
  458. " SingleInstance:" << Config->SingleInstance <<
  459. " Pipeline:" << Config->Pipeline <<
  460. " SendConfig:" << Config->SendConfig <<
  461. " LocalOnly: " << Config->LocalOnly <<
  462. " NeedsCleanup: " << Config->NeedsCleanup <<
  463. " Removable: " << Config->Removable << endl;
  464. }
  465. return true;
  466. }
  467. /*}}}*/
  468. // Worker::MediaChange - Request a media change /*{{{*/
  469. // ---------------------------------------------------------------------
  470. /* */
  471. bool pkgAcquire::Worker::MediaChange(string Message)
  472. {
  473. int status_fd = _config->FindI("APT::Status-Fd",-1);
  474. if(status_fd > 0)
  475. {
  476. string Media = LookupTag(Message,"Media");
  477. string Drive = LookupTag(Message,"Drive");
  478. ostringstream msg,status;
  479. ioprintf(msg,_("Please insert the disc labeled: "
  480. "'%s' "
  481. "in the drive '%s' and press [Enter]."),
  482. Media.c_str(),Drive.c_str());
  483. status << "media-change: " // message
  484. << Media << ":" // media
  485. << Drive << ":" // drive
  486. << msg.str() // l10n message
  487. << endl;
  488. std::string const dlstatus = status.str();
  489. FileFd::Write(status_fd, dlstatus.c_str(), dlstatus.size());
  490. }
  491. if (Log == 0 || Log->MediaChange(LookupTag(Message,"Media"),
  492. LookupTag(Message,"Drive")) == false)
  493. {
  494. char S[300];
  495. snprintf(S,sizeof(S),"603 Media Changed\nFailed: true\n\n");
  496. if (Debug == true)
  497. clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
  498. OutQueue += S;
  499. OutReady = true;
  500. return true;
  501. }
  502. char S[300];
  503. snprintf(S,sizeof(S),"603 Media Changed\n\n");
  504. if (Debug == true)
  505. clog << " -> " << Access << ':' << QuoteString(S,"\n") << endl;
  506. OutQueue += S;
  507. OutReady = true;
  508. return true;
  509. }
  510. /*}}}*/
  511. // Worker::SendConfiguration - Send the config to the method /*{{{*/
  512. // ---------------------------------------------------------------------
  513. /* */
  514. bool pkgAcquire::Worker::SendConfiguration()
  515. {
  516. if (Config->SendConfig == false)
  517. return true;
  518. if (OutFd == -1)
  519. return false;
  520. /* Write out all of the configuration directives by walking the
  521. configuration tree */
  522. std::ostringstream Message;
  523. Message << "601 Configuration\n";
  524. _config->Dump(Message, NULL, "Config-Item: %F=%V\n", false);
  525. Message << '\n';
  526. if (Debug == true)
  527. clog << " -> " << Access << ':' << QuoteString(Message.str(),"\n") << endl;
  528. OutQueue += Message.str();
  529. OutReady = true;
  530. return true;
  531. }
  532. /*}}}*/
  533. // Worker::QueueItem - Add an item to the outbound queue /*{{{*/
  534. // ---------------------------------------------------------------------
  535. /* Send a URI Acquire message to the method */
  536. bool pkgAcquire::Worker::QueueItem(pkgAcquire::Queue::QItem *Item)
  537. {
  538. if (OutFd == -1)
  539. return false;
  540. string Message = "600 URI Acquire\n";
  541. Message.reserve(300);
  542. Message += "URI: " + Item->URI;
  543. Message += "\nFilename: " + Item->Owner->DestFile;
  544. HashStringList const hsl = Item->GetExpectedHashes();
  545. for (HashStringList::const_iterator hs = hsl.begin(); hs != hsl.end(); ++hs)
  546. Message += "\nExpected-" + hs->HashType() + ": " + hs->HashValue();
  547. if (hsl.FileSize() == 0)
  548. {
  549. unsigned long long FileSize = Item->GetMaximumSize();
  550. if(FileSize > 0)
  551. {
  552. string MaximumSize;
  553. strprintf(MaximumSize, "%llu", FileSize);
  554. Message += "\nMaximum-Size: " + MaximumSize;
  555. }
  556. }
  557. Item->SyncDestinationFiles();
  558. Message += Item->Custom600Headers();
  559. Message += "\n\n";
  560. if (RealFileExists(Item->Owner->DestFile))
  561. {
  562. std::string const SandboxUser = _config->Find("APT::Sandbox::User");
  563. ChangeOwnerAndPermissionOfFile("Item::QueueURI", Item->Owner->DestFile.c_str(),
  564. SandboxUser.c_str(), "root", 0600);
  565. }
  566. if (Debug == true)
  567. clog << " -> " << Access << ':' << QuoteString(Message,"\n") << endl;
  568. OutQueue += Message;
  569. OutReady = true;
  570. return true;
  571. }
  572. /*}}}*/
  573. // Worker::OutFdRead - Out bound FD is ready /*{{{*/
  574. // ---------------------------------------------------------------------
  575. /* */
  576. bool pkgAcquire::Worker::OutFdReady()
  577. {
  578. int Res;
  579. do
  580. {
  581. Res = write(OutFd,OutQueue.c_str(),OutQueue.length());
  582. }
  583. while (Res < 0 && errno == EINTR);
  584. if (Res <= 0)
  585. return MethodFailure();
  586. OutQueue.erase(0,Res);
  587. if (OutQueue.empty() == true)
  588. OutReady = false;
  589. return true;
  590. }
  591. /*}}}*/
  592. // Worker::InFdRead - In bound FD is ready /*{{{*/
  593. // ---------------------------------------------------------------------
  594. /* */
  595. bool pkgAcquire::Worker::InFdReady()
  596. {
  597. if (ReadMessages() == false)
  598. return false;
  599. RunMessages();
  600. return true;
  601. }
  602. /*}}}*/
  603. // Worker::MethodFailure - Called when the method fails /*{{{*/
  604. // ---------------------------------------------------------------------
  605. /* This is called when the method is believed to have failed, probably because
  606. read returned -1. */
  607. bool pkgAcquire::Worker::MethodFailure()
  608. {
  609. _error->Error("Method %s has died unexpectedly!",Access.c_str());
  610. // do not reap the child here to show meaningfull error to the user
  611. ExecWait(Process,Access.c_str(),false);
  612. Process = -1;
  613. close(InFd);
  614. close(OutFd);
  615. InFd = -1;
  616. OutFd = -1;
  617. OutReady = false;
  618. InReady = false;
  619. OutQueue = string();
  620. MessageQueue.erase(MessageQueue.begin(),MessageQueue.end());
  621. return false;
  622. }
  623. /*}}}*/
  624. // Worker::Pulse - Called periodically /*{{{*/
  625. // ---------------------------------------------------------------------
  626. /* */
  627. void pkgAcquire::Worker::Pulse()
  628. {
  629. if (CurrentItem == 0)
  630. return;
  631. struct stat Buf;
  632. if (stat(CurrentItem->Owner->DestFile.c_str(),&Buf) != 0)
  633. return;
  634. CurrentSize = Buf.st_size;
  635. }
  636. /*}}}*/
  637. // Worker::ItemDone - Called when the current item is finished /*{{{*/
  638. // ---------------------------------------------------------------------
  639. /* */
  640. void pkgAcquire::Worker::ItemDone()
  641. {
  642. CurrentItem = 0;
  643. CurrentSize = 0;
  644. TotalSize = 0;
  645. Status = string();
  646. }
  647. /*}}}*/
  648. void pkgAcquire::Worker::PrepareFiles(char const * const caller, pkgAcquire::Queue::QItem const * const Itm)/*{{{*/
  649. {
  650. if (RealFileExists(Itm->Owner->DestFile))
  651. {
  652. ChangeOwnerAndPermissionOfFile(caller, Itm->Owner->DestFile.c_str(), "root", "root", 0644);
  653. std::string const filename = Itm->Owner->DestFile;
  654. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  655. {
  656. pkgAcquire::Item const * const Owner = *O;
  657. if (Owner->DestFile == filename || filename == "/dev/null")
  658. continue;
  659. RemoveFile("PrepareFiles", Owner->DestFile);
  660. if (link(filename.c_str(), Owner->DestFile.c_str()) != 0)
  661. {
  662. // different mounts can't happen for us as we download to lists/ by default,
  663. // but if the system is reused by others the locations can potentially be on
  664. // different disks, so use symlink as poor-men replacement.
  665. // FIXME: Real copying as last fallback, but that is costly, so offload to a method preferable
  666. if (symlink(filename.c_str(), Owner->DestFile.c_str()) != 0)
  667. _error->Error("Can't create (sym)link of file %s to %s", filename.c_str(), Owner->DestFile.c_str());
  668. }
  669. }
  670. }
  671. else
  672. {
  673. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  674. RemoveFile("PrepareFiles", (*O)->DestFile);
  675. }
  676. }
  677. /*}}}*/