cdrom.cc 28 KB

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