acquire-worker.cc 22 KB

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