cdrom.cc 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. if (DirectoryExists(".disk") == true)
  51. {
  52. if (InfoDir.empty() == true)
  53. InfoDir = CD + ".disk/";
  54. }
  55. // Don't look into directories that have been marked to ingore.
  56. if (RealFileExists(".aptignr") == true)
  57. return true;
  58. /* Check _first_ for a signature file as apt-cdrom assumes that all files
  59. under a Packages/Source file are in control of that file and stops
  60. the scanning
  61. */
  62. if (RealFileExists("Release.gpg") == true || RealFileExists("InRelease") == true)
  63. {
  64. SigList.push_back(CD);
  65. }
  66. /* Aha! We found some package files. We assume that everything under
  67. this dir is controlled by those package files so we don't look down
  68. anymore */
  69. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  70. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  71. c != compressor.end(); ++c)
  72. {
  73. if (RealFileExists(std::string("Packages").append(c->Extension).c_str()) == false)
  74. continue;
  75. if (_config->FindB("Debug::aptcdrom",false) == true)
  76. std::clog << "Found Packages in " << CD << std::endl;
  77. List.push_back(CD);
  78. // Continue down if thorough is given
  79. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  80. return true;
  81. break;
  82. }
  83. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  84. c != compressor.end(); ++c)
  85. {
  86. if (RealFileExists(std::string("Sources").append(c->Extension).c_str()) == false)
  87. continue;
  88. if (_config->FindB("Debug::aptcdrom",false) == true)
  89. std::clog << "Found Sources in " << CD << std::endl;
  90. SList.push_back(CD);
  91. // Continue down if thorough is given
  92. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  93. return true;
  94. break;
  95. }
  96. // see if we find translation indices
  97. if (DirectoryExists("i18n") == true)
  98. {
  99. D = opendir("i18n");
  100. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  101. {
  102. if(strncmp(Dir->d_name, "Translation-", strlen("Translation-")) != 0)
  103. continue;
  104. string file = Dir->d_name;
  105. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  106. c != compressor.end(); ++c)
  107. {
  108. string fileext = flExtension(file);
  109. if (file == fileext)
  110. fileext.clear();
  111. else if (fileext.empty() == false)
  112. fileext = "." + fileext;
  113. if (c->Extension == fileext)
  114. {
  115. if (_config->FindB("Debug::aptcdrom",false) == true)
  116. std::clog << "Found translation " << Dir->d_name << " in " << CD << "i18n/" << std::endl;
  117. file.erase(file.size() - fileext.size());
  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. bool couldFindAllFiles = true;
  245. // Get a list of all the inodes
  246. ino_t *Inodes = new ino_t[List.size()];
  247. for (unsigned int I = 0; I != List.size(); ++I)
  248. {
  249. struct stat Buf;
  250. bool found = false;
  251. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  252. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  253. c != compressor.end(); ++c)
  254. {
  255. std::string filename = std::string(List[I]).append(Name).append(c->Extension);
  256. if (stat(filename.c_str(), &Buf) != 0)
  257. continue;
  258. Inodes[I] = Buf.st_ino;
  259. found = true;
  260. break;
  261. }
  262. if (found == false)
  263. {
  264. _error->Errno("stat","Failed to stat %s%s",List[I].c_str(), Name);
  265. couldFindAllFiles = false;
  266. Inodes[I] = 0;
  267. }
  268. }
  269. // Look for dups
  270. for (unsigned int I = 0; I != List.size(); I++)
  271. {
  272. if (Inodes[I] == 0)
  273. continue;
  274. for (unsigned int J = I+1; J < List.size(); J++)
  275. {
  276. // No match
  277. if (Inodes[J] == 0 || Inodes[J] != Inodes[I])
  278. continue;
  279. // We score the two paths.. and erase one
  280. int ScoreA = Score(List[I]);
  281. int ScoreB = Score(List[J]);
  282. if (ScoreA < ScoreB)
  283. {
  284. List[I] = string();
  285. break;
  286. }
  287. List[J] = string();
  288. }
  289. }
  290. delete[] Inodes;
  291. // Wipe erased entries
  292. for (unsigned int I = 0; I < List.size();)
  293. {
  294. if (List[I].empty() == false)
  295. I++;
  296. else
  297. List.erase(List.begin()+I);
  298. }
  299. return couldFindAllFiles;
  300. }
  301. /*}}}*/
  302. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* This takes the list of source list expressed entires and collects
  305. similar ones to form a single entry for each dist */
  306. void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
  307. {
  308. sort(List.begin(),List.end());
  309. // Collect similar entries
  310. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  311. {
  312. // Find a space..
  313. string::size_type Space = (*I).find(' ');
  314. if (Space == string::npos)
  315. continue;
  316. string::size_type SSpace = (*I).find(' ',Space + 1);
  317. if (SSpace == string::npos)
  318. continue;
  319. string Word1 = string(*I,Space,SSpace-Space);
  320. string Prefix = string(*I,0,Space);
  321. for (vector<string>::iterator J = List.begin(); J != I; ++J)
  322. {
  323. // Find a space..
  324. string::size_type Space2 = (*J).find(' ');
  325. if (Space2 == string::npos)
  326. continue;
  327. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  328. if (SSpace2 == string::npos)
  329. continue;
  330. if (string(*J,0,Space2) != Prefix)
  331. continue;
  332. if (string(*J,Space2,SSpace2-Space2) != Word1)
  333. continue;
  334. *J += string(*I,SSpace);
  335. *I = string();
  336. }
  337. }
  338. // Wipe erased entries
  339. for (unsigned int I = 0; I < List.size();)
  340. {
  341. if (List[I].empty() == false)
  342. I++;
  343. else
  344. List.erase(List.begin()+I);
  345. }
  346. }
  347. /*}}}*/
  348. // WriteDatabase - Write the CDROM Database file /*{{{*/
  349. // ---------------------------------------------------------------------
  350. /* We rewrite the configuration class associated with the cdrom database. */
  351. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  352. {
  353. string DFile = _config->FindFile("Dir::State::cdroms");
  354. string NewFile = DFile + ".new";
  355. unlink(NewFile.c_str());
  356. ofstream Out(NewFile.c_str());
  357. if (!Out)
  358. return _error->Errno("ofstream::ofstream",
  359. "Failed to open %s.new",DFile.c_str());
  360. /* Write out all of the configuration directives by walking the
  361. configuration tree */
  362. const Configuration::Item *Top = Cnf.Tree(0);
  363. for (; Top != 0;)
  364. {
  365. // Print the config entry
  366. if (Top->Value.empty() == false)
  367. Out << Top->FullTag() + " \"" << Top->Value << "\";" << endl;
  368. if (Top->Child != 0)
  369. {
  370. Top = Top->Child;
  371. continue;
  372. }
  373. while (Top != 0 && Top->Next == 0)
  374. Top = Top->Parent;
  375. if (Top != 0)
  376. Top = Top->Next;
  377. }
  378. Out.close();
  379. if (FileExists(DFile) == true && link(DFile.c_str(),string(DFile + '~').c_str()) != 0)
  380. return _error->Errno("link", "Failed to link %s to %s~", DFile.c_str(), DFile.c_str());
  381. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  382. return _error->Errno("rename","Failed to rename %s.new to %s",
  383. DFile.c_str(),DFile.c_str());
  384. return true;
  385. }
  386. /*}}}*/
  387. // WriteSourceList - Write an updated sourcelist /*{{{*/
  388. // ---------------------------------------------------------------------
  389. /* This reads the old source list and copies it into the new one. It
  390. appends the new CDROM entires just after the first block of comments.
  391. This places them first in the file. It also removes any old entries
  392. that were the same. */
  393. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  394. {
  395. if (List.empty() == true)
  396. return true;
  397. string File = _config->FindFile("Dir::Etc::sourcelist");
  398. // Open the stream for reading
  399. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  400. ios::in );
  401. if (!F != 0)
  402. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  403. string NewFile = File + ".new";
  404. unlink(NewFile.c_str());
  405. ofstream Out(NewFile.c_str());
  406. if (!Out)
  407. return _error->Errno("ofstream::ofstream",
  408. "Failed to open %s.new",File.c_str());
  409. // Create a short uri without the path
  410. string ShortURI = "cdrom:[" + Name + "]/";
  411. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  412. string Type;
  413. if (Source == true)
  414. Type = "deb-src";
  415. else
  416. Type = "deb";
  417. char Buffer[300];
  418. int CurLine = 0;
  419. bool First = true;
  420. while (F.eof() == false)
  421. {
  422. F.getline(Buffer,sizeof(Buffer));
  423. CurLine++;
  424. if (F.fail() && !F.eof())
  425. return _error->Error(_("Line %u too long in source list %s."),
  426. CurLine,File.c_str());
  427. _strtabexpand(Buffer,sizeof(Buffer));
  428. _strstrip(Buffer);
  429. // Comment or blank
  430. if (Buffer[0] == '#' || Buffer[0] == 0)
  431. {
  432. Out << Buffer << endl;
  433. continue;
  434. }
  435. if (First == true)
  436. {
  437. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  438. {
  439. string::size_type Space = (*I).find(' ');
  440. if (Space == string::npos)
  441. return _error->Error("Internal error");
  442. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  443. " " << string(*I,Space+1) << endl;
  444. }
  445. }
  446. First = false;
  447. // Grok it
  448. string cType;
  449. string URI;
  450. const char *C = Buffer;
  451. if (ParseQuoteWord(C,cType) == false ||
  452. ParseQuoteWord(C,URI) == false)
  453. {
  454. Out << Buffer << endl;
  455. continue;
  456. }
  457. // Emit lines like this one
  458. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  459. string(URI,0,ShortURI.length()) != ShortURI2))
  460. {
  461. Out << Buffer << endl;
  462. continue;
  463. }
  464. }
  465. // Just in case the file was empty
  466. if (First == true)
  467. {
  468. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  469. {
  470. string::size_type Space = (*I).find(' ');
  471. if (Space == string::npos)
  472. return _error->Error("Internal error");
  473. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  474. " " << string(*I,Space+1) << endl;
  475. }
  476. }
  477. Out.close();
  478. rename(File.c_str(),string(File + '~').c_str());
  479. if (rename(NewFile.c_str(),File.c_str()) != 0)
  480. return _error->Errno("rename","Failed to rename %s.new to %s",
  481. File.c_str(),File.c_str());
  482. return true;
  483. }
  484. /*}}}*/
  485. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log) /*{{{*/
  486. {
  487. stringstream msg;
  488. // Startup
  489. string CDROM = _config->FindDir("Acquire::cdrom::mount");
  490. if (CDROM[0] == '.')
  491. CDROM= SafeGetCWD() + '/' + CDROM;
  492. if (log != NULL)
  493. {
  494. msg.str("");
  495. ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
  496. CDROM.c_str());
  497. log->Update(msg.str());
  498. }
  499. if (MountCdrom(CDROM) == false)
  500. return _error->Error("Failed to mount the cdrom.");
  501. // Hash the CD to get an ID
  502. if (log != NULL)
  503. log->Update(_("Identifying.. "));
  504. if (IdentCdrom(CDROM,ident) == false)
  505. {
  506. ident = "";
  507. return false;
  508. }
  509. if (log != NULL)
  510. {
  511. msg.str("");
  512. ioprintf(msg, "[%s]\n",ident.c_str());
  513. log->Update(msg.str());
  514. }
  515. // Read the database
  516. Configuration Database;
  517. string DFile = _config->FindFile("Dir::State::cdroms");
  518. if (FileExists(DFile) == true)
  519. {
  520. if (ReadConfigFile(Database,DFile) == false)
  521. return _error->Error("Unable to read the cdrom database %s",
  522. DFile.c_str());
  523. }
  524. if (log != NULL)
  525. {
  526. msg.str("");
  527. ioprintf(msg, _("Stored label: %s\n"),
  528. Database.Find("CD::"+ident).c_str());
  529. log->Update(msg.str());
  530. }
  531. // Unmount and finish
  532. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  533. {
  534. if (log != NULL)
  535. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  536. UnmountCdrom(CDROM);
  537. }
  538. return true;
  539. }
  540. /*}}}*/
  541. bool pkgCdrom::Add(pkgCdromStatus *log) /*{{{*/
  542. {
  543. stringstream msg;
  544. // Startup
  545. string CDROM = _config->FindDir("Acquire::cdrom::mount");
  546. if (CDROM[0] == '.')
  547. CDROM= SafeGetCWD() + '/' + CDROM;
  548. if(log != NULL)
  549. {
  550. log->SetTotal(STEP_LAST);
  551. msg.str("");
  552. ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  553. log->Update(msg.str(), STEP_PREPARE);
  554. }
  555. // Read the database
  556. Configuration Database;
  557. string DFile = _config->FindFile("Dir::State::cdroms");
  558. if (FileExists(DFile) == true)
  559. {
  560. if (ReadConfigFile(Database,DFile) == false)
  561. return _error->Error("Unable to read the cdrom database %s",
  562. DFile.c_str());
  563. }
  564. // Unmount the CD and get the user to put in the one they want
  565. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  566. {
  567. if(log != NULL)
  568. log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
  569. UnmountCdrom(CDROM);
  570. if(log != NULL)
  571. {
  572. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  573. if(!log->ChangeCdrom()) {
  574. // user aborted
  575. return false;
  576. }
  577. }
  578. // Mount the new CDROM
  579. if(log != NULL)
  580. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  581. if (MountCdrom(CDROM) == false)
  582. return _error->Error("Failed to mount the cdrom.");
  583. }
  584. // Hash the CD to get an ID
  585. if(log != NULL)
  586. log->Update(_("Identifying.. "), STEP_IDENT);
  587. string ID;
  588. if (IdentCdrom(CDROM,ID) == false)
  589. {
  590. if (log != NULL)
  591. log->Update("\n");
  592. return false;
  593. }
  594. if(log != NULL)
  595. {
  596. log->Update("["+ID+"]\n");
  597. log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
  598. }
  599. // Get the CD structure
  600. vector<string> List;
  601. vector<string> SourceList;
  602. vector<string> SigList;
  603. vector<string> TransList;
  604. string StartDir = SafeGetCWD();
  605. string InfoDir;
  606. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  607. {
  608. if (log != NULL)
  609. log->Update("\n");
  610. return false;
  611. }
  612. if (chdir(StartDir.c_str()) != 0)
  613. return _error->Errno("chdir","Unable to change to %s", StartDir.c_str());
  614. if (_config->FindB("Debug::aptcdrom",false) == true)
  615. {
  616. cout << "I found (binary):" << endl;
  617. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  618. cout << *I << endl;
  619. cout << "I found (source):" << endl;
  620. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  621. cout << *I << endl;
  622. cout << "I found (Signatures):" << endl;
  623. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I)
  624. cout << *I << endl;
  625. }
  626. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  627. // Fix up the list
  628. DropBinaryArch(List);
  629. DropRepeats(List,"Packages");
  630. DropRepeats(SourceList,"Sources");
  631. // FIXME: We ignore stat() errors here as we usually have only one of those in use
  632. // This has little potencial to drop 'valid' stat() errors as we know that one of these
  633. // files need to exist, but it would be better if we would check it here
  634. _error->PushToStack();
  635. DropRepeats(SigList,"Release.gpg");
  636. DropRepeats(SigList,"InRelease");
  637. _error->RevertToStack();
  638. DropRepeats(TransList,"");
  639. if(log != NULL) {
  640. msg.str("");
  641. ioprintf(msg, _("Found %zu package indexes, %zu source indexes, "
  642. "%zu translation indexes and %zu signatures\n"),
  643. List.size(), SourceList.size(), TransList.size(),
  644. SigList.size());
  645. log->Update(msg.str(), STEP_SCAN);
  646. }
  647. if (List.empty() == true && SourceList.empty() == true)
  648. {
  649. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  650. UnmountCdrom(CDROM);
  651. return _error->Error(_("Unable to locate any package files, perhaps this is not a Debian Disc or the wrong architecture?"));
  652. }
  653. // Check if the CD is in the database
  654. string Name;
  655. if (Database.Exists("CD::" + ID) == false ||
  656. _config->FindB("APT::CDROM::Rename",false) == true)
  657. {
  658. // Try to use the CDs label if at all possible
  659. if (InfoDir.empty() == false &&
  660. FileExists(InfoDir + "/info") == true)
  661. {
  662. ifstream F(string(InfoDir + "/info").c_str());
  663. if (!F == 0)
  664. getline(F,Name);
  665. if (Name.empty() == false)
  666. {
  667. // Escape special characters
  668. string::iterator J = Name.begin();
  669. for (; J != Name.end(); ++J)
  670. if (*J == '"' || *J == ']' || *J == '[')
  671. *J = '_';
  672. if(log != NULL)
  673. {
  674. msg.str("");
  675. ioprintf(msg, _("Found label '%s'\n"), Name.c_str());
  676. log->Update(msg.str());
  677. }
  678. Database.Set("CD::" + ID + "::Label",Name);
  679. }
  680. }
  681. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  682. Name.empty() == true)
  683. {
  684. if(log == NULL)
  685. {
  686. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  687. UnmountCdrom(CDROM);
  688. return _error->Error("No disc name found and no way to ask for it");
  689. }
  690. while(true) {
  691. if(!log->AskCdromName(Name)) {
  692. // user canceld
  693. return false;
  694. }
  695. cout << "Name: '" << Name << "'" << endl;
  696. if (Name.empty() == false &&
  697. Name.find('"') == string::npos &&
  698. Name.find('[') == string::npos &&
  699. Name.find(']') == string::npos)
  700. break;
  701. log->Update(_("That is not a valid name, try again.\n"));
  702. }
  703. }
  704. }
  705. else
  706. Name = Database.Find("CD::" + ID);
  707. // Escape special characters
  708. string::iterator J = Name.begin();
  709. for (; J != Name.end(); ++J)
  710. if (*J == '"' || *J == ']' || *J == '[')
  711. *J = '_';
  712. Database.Set("CD::" + ID,Name);
  713. if(log != NULL)
  714. {
  715. msg.str("");
  716. ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  717. log->Update(msg.str());
  718. log->Update(_("Copying package lists..."), STEP_COPY);
  719. }
  720. // take care of the signatures and copy them if they are ok
  721. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  722. SigVerify SignVerify;
  723. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  724. // Copy the package files to the state directory
  725. PackageCopy Copy;
  726. SourceCopy SrcCopy;
  727. TranslationsCopy TransCopy;
  728. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  729. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  730. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  731. return false;
  732. // reduce the List so that it takes less space in sources.list
  733. ReduceSourcelist(CDROM,List);
  734. ReduceSourcelist(CDROM,SourceList);
  735. // Write the database and sourcelist
  736. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  737. {
  738. if (WriteDatabase(Database) == false)
  739. return false;
  740. if(log != NULL)
  741. log->Update(_("Writing new source list\n"), STEP_WRITE);
  742. if (WriteSourceList(Name,List,false) == false ||
  743. WriteSourceList(Name,SourceList,true) == false)
  744. return false;
  745. }
  746. // Print the sourcelist entries
  747. if(log != NULL)
  748. log->Update(_("Source list entries for this disc are:\n"));
  749. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  750. {
  751. string::size_type Space = (*I).find(' ');
  752. if (Space == string::npos)
  753. {
  754. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  755. UnmountCdrom(CDROM);
  756. return _error->Error("Internal error");
  757. }
  758. if(log != NULL)
  759. {
  760. msg.str("");
  761. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  762. " " << string(*I,Space+1) << endl;
  763. log->Update(msg.str());
  764. }
  765. }
  766. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  767. {
  768. string::size_type Space = (*I).find(' ');
  769. if (Space == string::npos)
  770. {
  771. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  772. UnmountCdrom(CDROM);
  773. return _error->Error("Internal error");
  774. }
  775. if(log != NULL) {
  776. msg.str("");
  777. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  778. " " << string(*I,Space+1) << endl;
  779. log->Update(msg.str());
  780. }
  781. }
  782. // Unmount and finish
  783. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  784. if (log != NULL)
  785. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  786. UnmountCdrom(CDROM);
  787. }
  788. return true;
  789. }
  790. /*}}}*/
  791. pkgUdevCdromDevices::pkgUdevCdromDevices() /*{{{*/
  792. : libudev_handle(NULL)
  793. {
  794. }
  795. /*}}}*/
  796. bool
  797. pkgUdevCdromDevices::Dlopen() /*{{{*/
  798. {
  799. // alread open
  800. if(libudev_handle != NULL)
  801. return true;
  802. // see if we can get libudev
  803. void *h = ::dlopen("libudev.so.0", RTLD_LAZY);
  804. if(h == NULL)
  805. return false;
  806. // get the pointers to the udev structs
  807. libudev_handle = h;
  808. udev_new = (udev* (*)(void)) dlsym(h, "udev_new");
  809. udev_enumerate_add_match_property = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_property");
  810. udev_enumerate_add_match_sysattr = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_sysattr");
  811. udev_enumerate_scan_devices = (int (*)(udev_enumerate*))dlsym(h, "udev_enumerate_scan_devices");
  812. udev_enumerate_get_list_entry = (udev_list_entry* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_list_entry");
  813. udev_device_new_from_syspath = (udev_device* (*)(udev*, const char*))dlsym(h, "udev_device_new_from_syspath");
  814. udev_enumerate_get_udev = (udev* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_udev");
  815. udev_list_entry_get_name = (const char* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_name");
  816. udev_device_get_devnode = (const char* (*)(udev_device*))dlsym(h, "udev_device_get_devnode");
  817. udev_enumerate_new = (udev_enumerate* (*)(udev*))dlsym(h, "udev_enumerate_new");
  818. udev_list_entry_get_next = (udev_list_entry* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_next");
  819. udev_device_get_property_value = (const char* (*)(udev_device *, const char *))dlsym(h, "udev_device_get_property_value");
  820. return true;
  821. }
  822. /*}}}*/
  823. /*{{{*/
  824. // convenience interface, this will just call ScanForRemovable
  825. vector<CdromDevice>
  826. pkgUdevCdromDevices::Scan()
  827. {
  828. bool CdromOnly = _config->FindB("APT::cdrom::CdromOnly", true);
  829. return ScanForRemovable(CdromOnly);
  830. };
  831. /*}}}*/
  832. /*{{{*/
  833. vector<CdromDevice>
  834. pkgUdevCdromDevices::ScanForRemovable(bool CdromOnly)
  835. {
  836. vector<CdromDevice> cdrom_devices;
  837. struct udev_enumerate *enumerate;
  838. struct udev_list_entry *l, *devices;
  839. struct udev *udev_ctx;
  840. if(libudev_handle == NULL)
  841. return cdrom_devices;
  842. udev_ctx = udev_new();
  843. enumerate = udev_enumerate_new (udev_ctx);
  844. if (CdromOnly)
  845. udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
  846. else {
  847. udev_enumerate_add_match_sysattr(enumerate, "removable", "1");
  848. }
  849. udev_enumerate_scan_devices (enumerate);
  850. devices = udev_enumerate_get_list_entry (enumerate);
  851. for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
  852. {
  853. CdromDevice cdrom;
  854. struct udev_device *udevice;
  855. udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate), udev_list_entry_get_name (l));
  856. if (udevice == NULL)
  857. continue;
  858. const char* devnode = udev_device_get_devnode(udevice);
  859. // try fstab_dir first
  860. string mountpath;
  861. const char* mp = udev_device_get_property_value(udevice, "FSTAB_DIR");
  862. if (mp)
  863. mountpath = string(mp);
  864. else
  865. mountpath = FindMountPointForDevice(devnode);
  866. // fill in the struct
  867. cdrom.DeviceName = string(devnode);
  868. if (mountpath != "") {
  869. cdrom.MountPath = mountpath;
  870. string s = string(mountpath);
  871. cdrom.Mounted = IsMounted(s);
  872. } else {
  873. cdrom.Mounted = false;
  874. cdrom.MountPath = "";
  875. }
  876. cdrom_devices.push_back(cdrom);
  877. }
  878. return cdrom_devices;
  879. }
  880. /*}}}*/
  881. pkgUdevCdromDevices::~pkgUdevCdromDevices() /*{{{*/
  882. {
  883. if (libudev_handle != NULL)
  884. dlclose(libudev_handle);
  885. }
  886. /*}}}*/