acquire-worker.cc 22 KB

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