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.
 
 
 
 
 
 

926 lines
25 KiB

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