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
23 KiB

  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 = NULL;
  186. if (URI.empty() == false)
  187. Itm = OwnerQ->FindItem(URI,this);
  188. if (Itm != NULL)
  189. {
  190. // update used mirror
  191. string UsedMirror = LookupTag(Message,"UsedMirror", "");
  192. if (UsedMirror.empty() == false)
  193. {
  194. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  195. (*O)->UsedMirror = UsedMirror;
  196. if (Itm->Description.find(" ") != string::npos)
  197. Itm->Description.replace(0, Itm->Description.find(" "), UsedMirror);
  198. }
  199. }
  200. // Determine the message number and dispatch
  201. switch (Number)
  202. {
  203. // 100 Capabilities
  204. case 100:
  205. if (Capabilities(Message) == false)
  206. return _error->Error("Unable to process Capabilities message from %s",Access.c_str());
  207. break;
  208. // 101 Log
  209. case 101:
  210. if (Debug == true)
  211. clog << " <- (log) " << LookupTag(Message,"Message") << endl;
  212. break;
  213. // 102 Status
  214. case 102:
  215. Status = LookupTag(Message,"Message");
  216. break;
  217. // 103 Redirect
  218. case 103:
  219. {
  220. if (Itm == 0)
  221. {
  222. _error->Error("Method gave invalid 103 Redirect message");
  223. break;
  224. }
  225. std::string const NewURI = LookupTag(Message,"New-URI",URI.c_str());
  226. Itm->URI = NewURI;
  227. ItemDone();
  228. // Change the status so that it can be dequeued
  229. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  230. (*O)->Status = pkgAcquire::Item::StatIdle;
  231. // Mark the item as done (taking care of all queues)
  232. // and then put it in the main queue again
  233. std::vector<Item*> const ItmOwners = Itm->Owners;
  234. OwnerQ->ItemDone(Itm);
  235. Itm = NULL;
  236. for (pkgAcquire::Queue::QItem::owner_iterator O = ItmOwners.begin(); O != ItmOwners.end(); ++O)
  237. {
  238. pkgAcquire::Item *Owner = *O;
  239. pkgAcquire::ItemDesc &desc = Owner->GetItemDesc();
  240. // if we change site, treat it as a mirror change
  241. if (URI::SiteOnly(NewURI) != URI::SiteOnly(desc.URI))
  242. {
  243. std::string const OldSite = desc.Description.substr(0, desc.Description.find(" "));
  244. if (likely(APT::String::Startswith(desc.URI, OldSite)))
  245. {
  246. std::string const OldExtra = desc.URI.substr(OldSite.length() + 1);
  247. if (likely(APT::String::Endswith(NewURI, OldExtra)))
  248. {
  249. std::string const NewSite = NewURI.substr(0, NewURI.length() - OldExtra.length());
  250. Owner->UsedMirror = URI::ArchiveOnly(NewSite);
  251. if (desc.Description.find(" ") != string::npos)
  252. desc.Description.replace(0, desc.Description.find(" "), Owner->UsedMirror);
  253. }
  254. }
  255. }
  256. desc.URI = NewURI;
  257. OwnerQ->Owner->Enqueue(desc);
  258. if (Log != 0)
  259. Log->Done(desc);
  260. }
  261. break;
  262. }
  263. // 200 URI Start
  264. case 200:
  265. {
  266. if (Itm == 0)
  267. {
  268. _error->Error("Method gave invalid 200 URI Start message");
  269. break;
  270. }
  271. CurrentItem = Itm;
  272. CurrentSize = 0;
  273. TotalSize = strtoull(LookupTag(Message,"Size","0").c_str(), NULL, 10);
  274. ResumePoint = strtoull(LookupTag(Message,"Resume-Point","0").c_str(), NULL, 10);
  275. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  276. {
  277. (*O)->Start(Message, TotalSize);
  278. // Display update before completion
  279. if (Log != 0)
  280. {
  281. if (Log->MorePulses == true)
  282. Log->Pulse((*O)->GetOwner());
  283. Log->Fetch((*O)->GetItemDesc());
  284. }
  285. }
  286. break;
  287. }
  288. // 201 URI Done
  289. case 201:
  290. {
  291. if (Itm == 0)
  292. {
  293. _error->Error("Method gave invalid 201 URI Done message");
  294. break;
  295. }
  296. PrepareFiles("201::URIDone", Itm);
  297. // Display update before completion
  298. if (Log != 0 && Log->MorePulses == true)
  299. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  300. Log->Pulse((*O)->GetOwner());
  301. std::string const filename = LookupTag(Message, "Filename", Itm->Owner->DestFile.c_str());
  302. HashStringList ReceivedHashes;
  303. {
  304. // see if we got hashes to verify
  305. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  306. {
  307. std::string const tagname = std::string(*type) + "-Hash";
  308. std::string const hashsum = LookupTag(Message, tagname.c_str());
  309. if (hashsum.empty() == false)
  310. ReceivedHashes.push_back(HashString(*type, hashsum));
  311. }
  312. // not all methods always sent Hashes our way
  313. if (ReceivedHashes.usable() == false)
  314. {
  315. HashStringList const ExpectedHashes = Itm->GetExpectedHashes();
  316. if (ExpectedHashes.usable() == true && RealFileExists(filename))
  317. {
  318. Hashes calc(ExpectedHashes);
  319. FileFd file(filename, FileFd::ReadOnly, FileFd::None);
  320. calc.AddFD(file);
  321. ReceivedHashes = calc.GetHashStringList();
  322. }
  323. }
  324. }
  325. // only local files can refer other filenames and counting them as fetched would be unfair
  326. if (Log != NULL && filename != Itm->Owner->DestFile)
  327. Log->Fetched(ReceivedHashes.FileSize(),atoi(LookupTag(Message,"Resume-Point","0").c_str()));
  328. std::vector<Item*> const ItmOwners = Itm->Owners;
  329. OwnerQ->ItemDone(Itm);
  330. Itm = NULL;
  331. bool const isIMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false) ||
  332. StringToBool(LookupTag(Message,"Alt-IMS-Hit"),false);
  333. for (pkgAcquire::Queue::QItem::owner_iterator O = ItmOwners.begin(); O != ItmOwners.end(); ++O)
  334. {
  335. pkgAcquire::Item * const Owner = *O;
  336. HashStringList const ExpectedHashes = Owner->GetExpectedHashes();
  337. if(_config->FindB("Debug::pkgAcquire::Auth", false) == true)
  338. {
  339. std::clog << "201 URI Done: " << Owner->DescURI() << endl
  340. << "ReceivedHash:" << endl;
  341. for (HashStringList::const_iterator hs = ReceivedHashes.begin(); hs != ReceivedHashes.end(); ++hs)
  342. std::clog << "\t- " << hs->toStr() << std::endl;
  343. std::clog << "ExpectedHash:" << endl;
  344. for (HashStringList::const_iterator hs = ExpectedHashes.begin(); hs != ExpectedHashes.end(); ++hs)
  345. std::clog << "\t- " << hs->toStr() << std::endl;
  346. std::clog << endl;
  347. }
  348. // decide if what we got is what we expected
  349. bool consideredOkay = false;
  350. if (ExpectedHashes.usable())
  351. {
  352. if (ReceivedHashes.usable() == false)
  353. {
  354. /* IMS-Hits can't be checked here as we will have uncompressed file,
  355. but the hashes for the compressed file. What we have was good through
  356. so all we have to ensure later is that we are not stalled. */
  357. consideredOkay = isIMSHit;
  358. }
  359. else if (ReceivedHashes == ExpectedHashes)
  360. consideredOkay = true;
  361. else
  362. consideredOkay = false;
  363. }
  364. else if (Owner->HashesRequired() == true)
  365. consideredOkay = false;
  366. else
  367. consideredOkay = true;
  368. if (consideredOkay == true)
  369. consideredOkay = Owner->VerifyDone(Message, Config);
  370. else // hashsum mismatch
  371. Owner->Status = pkgAcquire::Item::StatAuthError;
  372. if (consideredOkay == true)
  373. {
  374. Owner->Done(Message, ReceivedHashes, Config);
  375. if (Log != 0)
  376. {
  377. if (isIMSHit)
  378. Log->IMSHit(Owner->GetItemDesc());
  379. else
  380. Log->Done(Owner->GetItemDesc());
  381. }
  382. }
  383. else
  384. {
  385. Owner->Failed(Message,Config);
  386. if (Log != 0)
  387. Log->Fail(Owner->GetItemDesc());
  388. }
  389. }
  390. ItemDone();
  391. break;
  392. }
  393. // 400 URI Failure
  394. case 400:
  395. {
  396. if (Itm == 0)
  397. {
  398. std::string const msg = LookupTag(Message,"Message");
  399. _error->Error("Method gave invalid 400 URI Failure message: %s", msg.c_str());
  400. break;
  401. }
  402. PrepareFiles("400::URIFailure", Itm);
  403. // Display update before completion
  404. if (Log != 0 && Log->MorePulses == true)
  405. for (pkgAcquire::Queue::QItem::owner_iterator O = Itm->Owners.begin(); O != Itm->Owners.end(); ++O)
  406. Log->Pulse((*O)->GetOwner());
  407. std::vector<Item*> const ItmOwners = Itm->Owners;
  408. OwnerQ->ItemDone(Itm);
  409. Itm = NULL;
  410. for (pkgAcquire::Queue::QItem::owner_iterator O = ItmOwners.begin(); O != ItmOwners.end(); ++O)
  411. {
  412. // set some status
  413. if(LookupTag(Message,"FailReason") == "Timeout" ||
  414. LookupTag(Message,"FailReason") == "TmpResolveFailure" ||
  415. LookupTag(Message,"FailReason") == "ResolveFailure" ||
  416. LookupTag(Message,"FailReason") == "ConnectionRefused")
  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 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)
  658. continue;
  659. unlink(Owner->DestFile.c_str());
  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. unlink((*O)->DestFile.c_str());
  675. }
  676. }
  677. /*}}}*/