cdrom.cc 26 KB

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