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.
 
 
 
 
 
 

1001 lines
28 KiB

  1. /*
  2. */
  3. #include <config.h>
  4. #include <apt-pkg/aptconfiguration.h>
  5. #include <apt-pkg/cdrom.h>
  6. #include <apt-pkg/cdromutl.h>
  7. #include <apt-pkg/configuration.h>
  8. #include <apt-pkg/error.h>
  9. #include <apt-pkg/fileutl.h>
  10. #include <apt-pkg/indexcopy.h>
  11. #include <apt-pkg/strutl.h>
  12. #include <algorithm>
  13. #include <fstream>
  14. #include <iostream>
  15. #include <iostream>
  16. #include <sstream>
  17. #include <string>
  18. #include <vector>
  19. #include <dirent.h>
  20. #include <dlfcn.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <apti18n.h>
  26. #ifdef HAVE_UDEV
  27. #include <libudev.h>
  28. #endif
  29. using namespace std;
  30. // FindPackages - Find the package files on the CDROM /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* We look over the cdrom for package files. This is a recursive
  33. search that short circuits when it his a package file in the dir.
  34. This speeds it up greatly as the majority of the size is in the
  35. binary-* sub dirs. */
  36. bool pkgCdrom::FindPackages(string CD,
  37. vector<string> &List,
  38. vector<string> &SList,
  39. vector<string> &SigList,
  40. vector<string> &TransList,
  41. string &InfoDir, pkgCdromStatus *log,
  42. unsigned int Depth)
  43. {
  44. static ino_t Inodes[9];
  45. DIR *D;
  46. // if we have a look we "pulse" now
  47. if(log)
  48. log->Update();
  49. if (Depth >= 7)
  50. return true;
  51. if (CD[CD.length()-1] != '/')
  52. CD += '/';
  53. if (chdir(CD.c_str()) != 0)
  54. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  55. // Look for a .disk subdirectory
  56. if (InfoDir.empty() == true)
  57. {
  58. if (DirectoryExists(".disk") == true)
  59. InfoDir = InfoDir + CD + ".disk/";
  60. }
  61. // Don't look into directories that have been marked to ignore.
  62. if (RealFileExists(".aptignr") == true)
  63. return true;
  64. /* Check _first_ for a signature file as apt-cdrom assumes that all files
  65. under a Packages/Source file are in control of that file and stops
  66. the scanning
  67. */
  68. if (RealFileExists("Release.gpg") == true || RealFileExists("InRelease") == true)
  69. {
  70. SigList.push_back(CD);
  71. }
  72. /* Aha! We found some package files. We assume that everything under
  73. this dir is controlled by those package files so we don't look down
  74. anymore */
  75. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  76. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  77. c != compressor.end(); ++c)
  78. {
  79. if (RealFileExists(std::string("Packages").append(c->Extension).c_str()) == false)
  80. continue;
  81. if (_config->FindB("Debug::aptcdrom",false) == true)
  82. std::clog << "Found Packages in " << CD << std::endl;
  83. List.push_back(CD);
  84. // Continue down if thorough is given
  85. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  86. return true;
  87. break;
  88. }
  89. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  90. c != compressor.end(); ++c)
  91. {
  92. if (RealFileExists(std::string("Sources").append(c->Extension).c_str()) == false)
  93. continue;
  94. if (_config->FindB("Debug::aptcdrom",false) == true)
  95. std::clog << "Found Sources in " << CD << std::endl;
  96. SList.push_back(CD);
  97. // Continue down if thorough is given
  98. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  99. return true;
  100. break;
  101. }
  102. // see if we find translation indices
  103. if (DirectoryExists("i18n") == true)
  104. {
  105. D = opendir("i18n");
  106. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  107. {
  108. if(strncmp(Dir->d_name, "Translation-", strlen("Translation-")) != 0)
  109. continue;
  110. string file = Dir->d_name;
  111. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  112. c != compressor.end(); ++c)
  113. {
  114. string fileext = flExtension(file);
  115. if (file == fileext)
  116. fileext.clear();
  117. else if (fileext.empty() == false)
  118. fileext = "." + fileext;
  119. if (c->Extension == fileext)
  120. {
  121. if (_config->FindB("Debug::aptcdrom",false) == true)
  122. std::clog << "Found translation " << Dir->d_name << " in " << CD << "i18n/" << std::endl;
  123. file.erase(file.size() - fileext.size());
  124. TransList.push_back(CD + "i18n/" + file);
  125. break;
  126. }
  127. }
  128. }
  129. closedir(D);
  130. }
  131. D = opendir(".");
  132. if (D == 0)
  133. return _error->Errno("opendir","Unable to read %s",CD.c_str());
  134. // Run over the directory
  135. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  136. {
  137. // Skip some files..
  138. if (strcmp(Dir->d_name,".") == 0 ||
  139. strcmp(Dir->d_name,"..") == 0 ||
  140. strcmp(Dir->d_name,".disk") == 0 ||
  141. strcmp(Dir->d_name,"debian-installer") == 0)
  142. continue;
  143. // See if the name is a sub directory
  144. struct stat Buf;
  145. if (stat(Dir->d_name,&Buf) != 0)
  146. continue;
  147. if (S_ISDIR(Buf.st_mode) == 0)
  148. continue;
  149. unsigned int I;
  150. for (I = 0; I != Depth; I++)
  151. if (Inodes[I] == Buf.st_ino)
  152. break;
  153. if (I != Depth)
  154. continue;
  155. // Store the inodes weve seen
  156. Inodes[Depth] = Buf.st_ino;
  157. // Descend
  158. if (FindPackages(CD + Dir->d_name,List,SList,SigList,TransList,InfoDir,log,Depth+1) == false)
  159. break;
  160. if (chdir(CD.c_str()) != 0)
  161. {
  162. _error->Errno("chdir","Unable to change to %s", CD.c_str());
  163. closedir(D);
  164. return false;
  165. }
  166. };
  167. closedir(D);
  168. return !_error->PendingError();
  169. }
  170. /*}}}*/
  171. // Score - We compute a 'score' for a path /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* Paths are scored based on how close they come to what I consider
  174. normal. That is ones that have 'dist' 'stable' 'testing' will score
  175. higher than ones without. */
  176. int pkgCdrom::Score(string Path)
  177. {
  178. int Res = 0;
  179. if (Path.find("stable/") != string::npos)
  180. Res += 29;
  181. if (Path.find("/binary-") != string::npos)
  182. Res += 20;
  183. if (Path.find("testing/") != string::npos)
  184. Res += 28;
  185. if (Path.find("unstable/") != string::npos)
  186. Res += 27;
  187. if (Path.find("/dists/") != string::npos)
  188. Res += 40;
  189. if (Path.find("/main/") != string::npos)
  190. Res += 20;
  191. if (Path.find("/contrib/") != string::npos)
  192. Res += 20;
  193. if (Path.find("/non-free/") != string::npos)
  194. Res += 20;
  195. if (Path.find("/non-US/") != string::npos)
  196. Res += 20;
  197. if (Path.find("/source/") != string::npos)
  198. Res += 10;
  199. if (Path.find("/debian/") != string::npos)
  200. Res -= 10;
  201. // check for symlinks in the patch leading to the actual file
  202. // a symlink gets a big penalty
  203. struct stat Buf;
  204. string statPath = flNotFile(Path);
  205. string cdromPath = _config->FindDir("Acquire::cdrom::mount");
  206. while(statPath != cdromPath && statPath != "./") {
  207. statPath.resize(statPath.size()-1); // remove the trailing '/'
  208. if (lstat(statPath.c_str(),&Buf) == 0) {
  209. if(S_ISLNK(Buf.st_mode)) {
  210. Res -= 60;
  211. break;
  212. }
  213. }
  214. statPath = flNotFile(statPath); // descent
  215. }
  216. return Res;
  217. }
  218. /*}}}*/
  219. // DropBinaryArch - Dump dirs with a string like /binary-<foo>/ /*{{{*/
  220. // ---------------------------------------------------------------------
  221. /* Here we drop everything that is not this machines arch */
  222. bool pkgCdrom::DropBinaryArch(vector<string> &List)
  223. {
  224. for (unsigned int I = 0; I < List.size(); I++)
  225. {
  226. const char *Str = List[I].c_str();
  227. const char *Start, *End;
  228. if ((Start = strstr(Str,"/binary-")) == 0)
  229. continue;
  230. // Between Start and End is the architecture
  231. Start += 8;
  232. if ((End = strstr(Start,"/")) != 0 && Start != End &&
  233. APT::Configuration::checkArchitecture(string(Start, End)) == true)
  234. continue; // okay, architecture is accepted
  235. // not accepted -> Erase it
  236. List.erase(List.begin() + I);
  237. --I; // the next entry is at the same index after the erase
  238. }
  239. return true;
  240. }
  241. /*}}}*/
  242. // DropTranslation - Dump unwanted Translation-<lang> files /*{{{*/
  243. // ---------------------------------------------------------------------
  244. /* Here we drop everything that is not configured in Acquire::Languages */
  245. bool pkgCdrom::DropTranslation(vector<string> &List)
  246. {
  247. for (unsigned int I = 0; I < List.size(); I++)
  248. {
  249. const char *Start;
  250. if ((Start = strstr(List[I].c_str(), "/Translation-")) == NULL)
  251. continue;
  252. Start += strlen("/Translation-");
  253. if (APT::Configuration::checkLanguage(Start, true) == true)
  254. continue;
  255. // not accepted -> Erase it
  256. List.erase(List.begin() + I);
  257. --I; // the next entry is at the same index after the erase
  258. }
  259. return true;
  260. }
  261. /*}}}*/
  262. // DropRepeats - Drop repeated files resulting from symlinks /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* Here we go and stat every file that we found and strip dup inodes. */
  265. bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
  266. {
  267. bool couldFindAllFiles = true;
  268. // Get a list of all the inodes
  269. ino_t *Inodes = new ino_t[List.size()];
  270. for (unsigned int I = 0; I != List.size(); ++I)
  271. {
  272. struct stat Buf;
  273. bool found = false;
  274. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  275. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  276. c != compressor.end(); ++c)
  277. {
  278. std::string const filename = List[I] + Name + c->Extension;
  279. if (stat(filename.c_str(), &Buf) != 0)
  280. continue;
  281. Inodes[I] = Buf.st_ino;
  282. found = true;
  283. break;
  284. }
  285. if (found == false)
  286. {
  287. _error->Errno("stat","Failed to stat %s%s",List[I].c_str(), Name);
  288. couldFindAllFiles = false;
  289. Inodes[I] = 0;
  290. }
  291. }
  292. // Look for dups
  293. for (unsigned int I = 0; I != List.size(); I++)
  294. {
  295. if (Inodes[I] == 0)
  296. continue;
  297. for (unsigned int J = I+1; J < List.size(); J++)
  298. {
  299. // No match
  300. if (Inodes[J] == 0 || Inodes[J] != Inodes[I])
  301. continue;
  302. // We score the two paths.. and erase one
  303. int ScoreA = Score(List[I]);
  304. int ScoreB = Score(List[J]);
  305. if (ScoreA < ScoreB)
  306. {
  307. List[I] = string();
  308. break;
  309. }
  310. List[J] = string();
  311. }
  312. }
  313. delete[] Inodes;
  314. // Wipe erased entries
  315. for (unsigned int I = 0; I < List.size();)
  316. {
  317. if (List[I].empty() == false)
  318. I++;
  319. else
  320. List.erase(List.begin()+I);
  321. }
  322. return couldFindAllFiles;
  323. }
  324. /*}}}*/
  325. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  326. // ---------------------------------------------------------------------
  327. /* This takes the list of source list expressed entries and collects
  328. similar ones to form a single entry for each dist */
  329. void pkgCdrom::ReduceSourcelist(string /*CD*/,vector<string> &List)
  330. {
  331. sort(List.begin(),List.end());
  332. // Collect similar entries
  333. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  334. {
  335. // Find a space..
  336. string::size_type Space = (*I).find(' ');
  337. if (Space == string::npos)
  338. continue;
  339. string::size_type SSpace = (*I).find(' ',Space + 1);
  340. if (SSpace == string::npos)
  341. continue;
  342. string Word1 = string(*I,Space,SSpace-Space);
  343. string Prefix = string(*I,0,Space);
  344. string Component = string(*I,SSpace);
  345. for (vector<string>::iterator J = List.begin(); J != I; ++J)
  346. {
  347. // Find a space..
  348. string::size_type Space2 = (*J).find(' ');
  349. if (Space2 == string::npos)
  350. continue;
  351. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  352. if (SSpace2 == string::npos)
  353. continue;
  354. if (string(*J,0,Space2) != Prefix)
  355. continue;
  356. if (string(*J,Space2,SSpace2-Space2) != Word1)
  357. continue;
  358. string Component2 = string(*J, SSpace2) + " ";
  359. if (Component2.find(Component + " ") == std::string::npos)
  360. *J += Component;
  361. I->clear();
  362. }
  363. }
  364. // Wipe erased entries
  365. for (unsigned int I = 0; I < List.size();)
  366. {
  367. if (List[I].empty() == false)
  368. I++;
  369. else
  370. List.erase(List.begin()+I);
  371. }
  372. }
  373. /*}}}*/
  374. // WriteDatabase - Write the CDROM Database file /*{{{*/
  375. // ---------------------------------------------------------------------
  376. /* We rewrite the configuration class associated with the cdrom database. */
  377. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  378. {
  379. string DFile = _config->FindFile("Dir::State::cdroms");
  380. string NewFile = DFile + ".new";
  381. RemoveFile("WriteDatabase", NewFile);
  382. ofstream Out(NewFile.c_str());
  383. if (!Out)
  384. return _error->Errno("ofstream::ofstream",
  385. "Failed to open %s.new",DFile.c_str());
  386. /* Write out all of the configuration directives by walking the
  387. configuration tree */
  388. Cnf.Dump(Out, NULL, "%F \"%v\";\n", false);
  389. Out.close();
  390. if (FileExists(DFile) == true)
  391. rename(DFile.c_str(), (DFile + '~').c_str());
  392. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  393. return _error->Errno("rename","Failed to rename %s.new to %s",
  394. DFile.c_str(),DFile.c_str());
  395. return true;
  396. }
  397. /*}}}*/
  398. // WriteSourceList - Write an updated sourcelist /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* This reads the old source list and copies it into the new one. It
  401. appends the new CDROM entries just after the first block of comments.
  402. This places them first in the file. It also removes any old entries
  403. that were the same. */
  404. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  405. {
  406. if (List.empty() == true)
  407. return true;
  408. string File = _config->FindFile("Dir::Etc::sourcelist");
  409. // Open the stream for reading
  410. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  411. ios::in );
  412. if (F.fail() == true)
  413. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  414. string NewFile = File + ".new";
  415. RemoveFile("WriteDatabase", NewFile);
  416. ofstream Out(NewFile.c_str());
  417. if (!Out)
  418. return _error->Errno("ofstream::ofstream",
  419. "Failed to open %s.new",File.c_str());
  420. // Create a short uri without the path
  421. string ShortURI = "cdrom:[" + Name + "]/";
  422. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  423. string Type;
  424. if (Source == true)
  425. Type = "deb-src";
  426. else
  427. Type = "deb";
  428. char Buffer[300];
  429. int CurLine = 0;
  430. bool First = true;
  431. while (F.eof() == false)
  432. {
  433. F.getline(Buffer,sizeof(Buffer));
  434. CurLine++;
  435. if (F.fail() && !F.eof())
  436. return _error->Error(_("Line %u too long in source list %s."),
  437. CurLine,File.c_str());
  438. _strtabexpand(Buffer,sizeof(Buffer));
  439. _strstrip(Buffer);
  440. // Comment or blank
  441. if (Buffer[0] == '#' || Buffer[0] == 0)
  442. {
  443. Out << Buffer << endl;
  444. continue;
  445. }
  446. if (First == true)
  447. {
  448. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  449. {
  450. string::size_type Space = (*I).find(' ');
  451. if (Space == string::npos)
  452. return _error->Error("Internal error");
  453. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  454. " " << string(*I,Space+1) << endl;
  455. }
  456. }
  457. First = false;
  458. // Grok it
  459. string cType;
  460. string URI;
  461. const char *C = Buffer;
  462. if (ParseQuoteWord(C,cType) == false ||
  463. ParseQuoteWord(C,URI) == false)
  464. {
  465. Out << Buffer << endl;
  466. continue;
  467. }
  468. // Emit lines like this one
  469. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  470. string(URI,0,ShortURI.length()) != ShortURI2))
  471. {
  472. Out << Buffer << endl;
  473. continue;
  474. }
  475. }
  476. // Just in case the file was empty
  477. if (First == true)
  478. {
  479. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  480. {
  481. string::size_type Space = (*I).find(' ');
  482. if (Space == string::npos)
  483. return _error->Error("Internal error");
  484. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  485. " " << string(*I,Space+1) << endl;
  486. }
  487. }
  488. Out.close();
  489. rename(File.c_str(), (File + '~').c_str());
  490. if (rename(NewFile.c_str(),File.c_str()) != 0)
  491. return _error->Errno("rename","Failed to rename %s.new to %s",
  492. File.c_str(),File.c_str());
  493. return true;
  494. }
  495. /*}}}*/
  496. bool pkgCdrom::UnmountCDROM(std::string const &CDROM, pkgCdromStatus * const log)/*{{{*/
  497. {
  498. if (_config->FindB("APT::CDROM::NoMount",false) == true)
  499. return true;
  500. if (log != NULL)
  501. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  502. return UnmountCdrom(CDROM);
  503. }
  504. /*}}}*/
  505. bool pkgCdrom::MountAndIdentCDROM(Configuration &Database, std::string &CDROM, std::string &ident, pkgCdromStatus * const log, bool const interactive)/*{{{*/
  506. {
  507. // Startup
  508. CDROM = _config->FindDir("Acquire::cdrom::mount");
  509. if (CDROM[0] == '.')
  510. CDROM= SafeGetCWD() + '/' + CDROM;
  511. if (log != NULL)
  512. {
  513. string msg;
  514. log->SetTotal(STEP_LAST);
  515. strprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  516. log->Update(msg, STEP_PREPARE);
  517. }
  518. // Unmount the CD and get the user to put in the one they want
  519. if (_config->FindB("APT::CDROM::NoMount", false) == false)
  520. {
  521. if (interactive == true)
  522. {
  523. UnmountCDROM(CDROM, log);
  524. if(log != NULL)
  525. {
  526. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  527. if(!log->ChangeCdrom()) {
  528. // user aborted
  529. return false;
  530. }
  531. }
  532. }
  533. // Mount the new CDROM
  534. if(log != NULL)
  535. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  536. if (MountCdrom(CDROM) == false)
  537. return _error->Error("Failed to mount the cdrom.");
  538. }
  539. if (IsMounted(CDROM) == false)
  540. return _error->Error("Failed to mount the cdrom.");
  541. // Hash the CD to get an ID
  542. if (log != NULL)
  543. log->Update(_("Identifying... "), STEP_IDENT);
  544. if (IdentCdrom(CDROM,ident) == false)
  545. {
  546. ident = "";
  547. if (log != NULL)
  548. log->Update("\n");
  549. UnmountCDROM(CDROM, NULL);
  550. return false;
  551. }
  552. if (log != NULL)
  553. {
  554. string msg;
  555. strprintf(msg, "[%s]\n", ident.c_str());
  556. log->Update(msg);
  557. }
  558. // Read the database
  559. string DFile = _config->FindFile("Dir::State::cdroms");
  560. if (FileExists(DFile) == true)
  561. {
  562. if (ReadConfigFile(Database,DFile) == false)
  563. {
  564. UnmountCDROM(CDROM, NULL);
  565. return _error->Error("Unable to read the cdrom database %s",
  566. DFile.c_str());
  567. }
  568. }
  569. return true;
  570. }
  571. /*}}}*/
  572. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log) /*{{{*/
  573. {
  574. Configuration Database;
  575. std::string CDROM;
  576. if (MountAndIdentCDROM(Database, CDROM, ident, log, false) == false)
  577. return false;
  578. if (log != NULL)
  579. {
  580. string msg;
  581. strprintf(msg, _("Stored label: %s\n"),
  582. Database.Find("CD::"+ident).c_str());
  583. log->Update(msg);
  584. }
  585. // Unmount and finish
  586. UnmountCDROM(CDROM, log);
  587. return true;
  588. }
  589. /*}}}*/
  590. bool pkgCdrom::Add(pkgCdromStatus *log) /*{{{*/
  591. {
  592. Configuration Database;
  593. std::string ID, CDROM;
  594. if (MountAndIdentCDROM(Database, CDROM, ID, log, true) == false)
  595. return false;
  596. if(log != NULL)
  597. log->Update(_("Scanning disc for index files...\n"),STEP_SCAN);
  598. // Get the CD structure
  599. vector<string> List;
  600. vector<string> SourceList;
  601. vector<string> SigList;
  602. vector<string> TransList;
  603. string StartDir = SafeGetCWD();
  604. string InfoDir;
  605. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  606. {
  607. if (log != NULL)
  608. log->Update("\n");
  609. UnmountCDROM(CDROM, NULL);
  610. return false;
  611. }
  612. if (chdir(StartDir.c_str()) != 0)
  613. {
  614. UnmountCDROM(CDROM, NULL);
  615. return _error->Errno("chdir","Unable to change to %s", StartDir.c_str());
  616. }
  617. if (_config->FindB("Debug::aptcdrom",false) == true)
  618. {
  619. cout << "I found (binary):" << endl;
  620. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  621. cout << *I << endl;
  622. cout << "I found (source):" << endl;
  623. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  624. cout << *I << endl;
  625. cout << "I found (Signatures):" << endl;
  626. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I)
  627. cout << *I << endl;
  628. }
  629. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  630. // Fix up the list
  631. DropBinaryArch(List);
  632. DropRepeats(List,"Packages");
  633. DropRepeats(SourceList,"Sources");
  634. // FIXME: We ignore stat() errors here as we usually have only one of those in use
  635. // This has little potential to drop 'valid' stat() errors as we know that one of these
  636. // files need to exist, but it would be better if we would check it here
  637. _error->PushToStack();
  638. DropRepeats(SigList,"Release.gpg");
  639. DropRepeats(SigList,"InRelease");
  640. _error->RevertToStack();
  641. DropRepeats(TransList,"");
  642. if (_config->FindB("APT::CDROM::DropTranslation", true) == true)
  643. DropTranslation(TransList);
  644. if(log != NULL) {
  645. string msg;
  646. strprintf(msg, _("Found %zu package indexes, %zu source indexes, "
  647. "%zu translation indexes and %zu signatures\n"),
  648. List.size(), SourceList.size(), TransList.size(),
  649. SigList.size());
  650. log->Update(msg, STEP_SCAN);
  651. }
  652. if (List.empty() == true && SourceList.empty() == true)
  653. {
  654. UnmountCDROM(CDROM, NULL);
  655. return _error->Error(_("Unable to locate any package files, perhaps this is not a Debian Disc or the wrong architecture?"));
  656. }
  657. // Check if the CD is in the database
  658. string Name;
  659. if (Database.Exists("CD::" + ID) == false ||
  660. _config->FindB("APT::CDROM::Rename",false) == true)
  661. {
  662. // Try to use the CDs label if at all possible
  663. if (InfoDir.empty() == false &&
  664. FileExists(InfoDir + "/info") == true)
  665. {
  666. ifstream F((InfoDir + "/info").c_str());
  667. if (F.good() == true)
  668. getline(F,Name);
  669. if (Name.empty() == false)
  670. {
  671. // Escape special characters
  672. string::iterator J = Name.begin();
  673. for (; J != Name.end(); ++J)
  674. if (*J == '"' || *J == ']' || *J == '[')
  675. *J = '_';
  676. if(log != NULL)
  677. {
  678. string msg;
  679. strprintf(msg, _("Found label '%s'\n"), Name.c_str());
  680. log->Update(msg);
  681. }
  682. Database.Set("CD::" + ID + "::Label",Name);
  683. }
  684. }
  685. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  686. Name.empty() == true)
  687. {
  688. if(log == NULL)
  689. {
  690. UnmountCDROM(CDROM, NULL);
  691. return _error->Error("No disc name found and no way to ask for it");
  692. }
  693. while(true) {
  694. if(!log->AskCdromName(Name)) {
  695. // user canceld
  696. UnmountCDROM(CDROM, NULL);
  697. return false;
  698. }
  699. cout << "Name: '" << Name << "'" << endl;
  700. if (Name.empty() == false &&
  701. Name.find('"') == string::npos &&
  702. Name.find('[') == string::npos &&
  703. Name.find(']') == string::npos)
  704. break;
  705. log->Update(_("That is not a valid name, try again.\n"));
  706. }
  707. }
  708. }
  709. else
  710. Name = Database.Find("CD::" + ID);
  711. // Escape special characters
  712. string::iterator J = Name.begin();
  713. for (; J != Name.end(); ++J)
  714. if (*J == '"' || *J == ']' || *J == '[')
  715. *J = '_';
  716. Database.Set("CD::" + ID,Name);
  717. if(log != NULL)
  718. {
  719. string msg;
  720. strprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  721. log->Update(msg);
  722. log->Update(_("Copying package lists..."), STEP_COPY);
  723. }
  724. // check for existence and possibly create state directory for copying
  725. string const listDir = _config->FindDir("Dir::State::lists");
  726. string const partialListDir = listDir + "partial/";
  727. mode_t const mode = umask(S_IWGRP | S_IWOTH);
  728. bool const creation_fail = (CreateAPTDirectoryIfNeeded(_config->FindDir("Dir::State"), partialListDir) == false &&
  729. CreateAPTDirectoryIfNeeded(listDir, partialListDir) == false);
  730. umask(mode);
  731. if (creation_fail == true)
  732. {
  733. UnmountCDROM(CDROM, NULL);
  734. return _error->Errno("cdrom", _("List directory %s is missing."), (listDir + "partial").c_str());
  735. }
  736. // take care of the signatures and copy them if they are ok
  737. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  738. SigVerify SignVerify;
  739. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  740. // Copy the package files to the state directory
  741. PackageCopy Copy;
  742. SourceCopy SrcCopy;
  743. TranslationsCopy TransCopy;
  744. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  745. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  746. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  747. {
  748. UnmountCDROM(CDROM, NULL);
  749. return false;
  750. }
  751. // reduce the List so that it takes less space in sources.list
  752. ReduceSourcelist(CDROM,List);
  753. ReduceSourcelist(CDROM,SourceList);
  754. // Write the database and sourcelist
  755. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  756. {
  757. if (WriteDatabase(Database) == false)
  758. {
  759. UnmountCDROM(CDROM, NULL);
  760. return false;
  761. }
  762. if(log != NULL)
  763. log->Update(_("Writing new source list\n"), STEP_WRITE);
  764. if (WriteSourceList(Name,List,false) == false ||
  765. WriteSourceList(Name,SourceList,true) == false)
  766. {
  767. UnmountCDROM(CDROM, NULL);
  768. return false;
  769. }
  770. }
  771. // Print the sourcelist entries
  772. if(log != NULL)
  773. log->Update(_("Source list entries for this disc are:\n"));
  774. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  775. {
  776. string::size_type Space = (*I).find(' ');
  777. if (Space == string::npos)
  778. {
  779. UnmountCDROM(CDROM, NULL);
  780. return _error->Error("Internal error");
  781. }
  782. if(log != NULL)
  783. {
  784. stringstream msg;
  785. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  786. " " << string(*I,Space+1) << endl;
  787. log->Update(msg.str());
  788. }
  789. }
  790. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  791. {
  792. string::size_type Space = (*I).find(' ');
  793. if (Space == string::npos)
  794. {
  795. UnmountCDROM(CDROM, NULL);
  796. return _error->Error("Internal error");
  797. }
  798. if(log != NULL) {
  799. stringstream msg;
  800. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  801. " " << string(*I,Space+1) << endl;
  802. log->Update(msg.str());
  803. }
  804. }
  805. // Unmount and finish
  806. UnmountCDROM(CDROM, log);
  807. return true;
  808. }
  809. /*}}}*/
  810. pkgUdevCdromDevices::pkgUdevCdromDevices() /*{{{*/
  811. : d(NULL)
  812. {
  813. }
  814. /*}}}*/
  815. bool pkgUdevCdromDevices::Dlopen() /*{{{*/
  816. {
  817. return true;
  818. }
  819. /*}}}*/
  820. // convenience interface, this will just call ScanForRemovable /*{{{*/
  821. vector<CdromDevice> pkgUdevCdromDevices::Scan()
  822. {
  823. bool CdromOnly = _config->FindB("APT::cdrom::CdromOnly", true);
  824. return ScanForRemovable(CdromOnly);
  825. }
  826. /*}}}*/
  827. vector<CdromDevice> pkgUdevCdromDevices::ScanForRemovable(bool CdromOnly)/*{{{*/
  828. {
  829. vector<CdromDevice> cdrom_devices;
  830. #ifdef HAVE_UDEV
  831. struct udev_enumerate *enumerate;
  832. struct udev_list_entry *l, *devices;
  833. struct udev *udev_ctx;
  834. udev_ctx = udev_new();
  835. enumerate = udev_enumerate_new (udev_ctx);
  836. if (CdromOnly)
  837. udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
  838. else {
  839. udev_enumerate_add_match_sysattr(enumerate, "removable", "1");
  840. }
  841. udev_enumerate_scan_devices (enumerate);
  842. devices = udev_enumerate_get_list_entry (enumerate);
  843. for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
  844. {
  845. CdromDevice cdrom;
  846. struct udev_device *udevice;
  847. udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate), udev_list_entry_get_name (l));
  848. if (udevice == NULL)
  849. continue;
  850. const char* devnode = udev_device_get_devnode(udevice);
  851. // try fstab_dir first
  852. string mountpath;
  853. const char* mp = udev_device_get_property_value(udevice, "FSTAB_DIR");
  854. if (mp)
  855. mountpath = string(mp);
  856. else
  857. mountpath = FindMountPointForDevice(devnode);
  858. // fill in the struct
  859. cdrom.DeviceName = string(devnode);
  860. if (mountpath != "") {
  861. cdrom.MountPath = mountpath;
  862. string s = mountpath;
  863. cdrom.Mounted = IsMounted(s);
  864. } else {
  865. cdrom.Mounted = false;
  866. cdrom.MountPath = "";
  867. }
  868. cdrom_devices.push_back(cdrom);
  869. }
  870. #endif
  871. return cdrom_devices;
  872. }
  873. /*}}}*/
  874. pkgUdevCdromDevices::~pkgUdevCdromDevices() /*{{{*/
  875. {
  876. }
  877. /*}}}*/
  878. pkgCdromStatus::pkgCdromStatus() : d(NULL), totalSteps(0) {}
  879. pkgCdromStatus::~pkgCdromStatus() {}
  880. pkgCdrom::pkgCdrom() : d(NULL) {}
  881. pkgCdrom::~pkgCdrom() {}