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.
 
 
 
 
 
 

1007 lines
30 KiB

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