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. // Wipe erased entries
  248. for (unsigned int I = 0; I < List.size();)
  249. {
  250. if (List[I].empty() == false)
  251. I++;
  252. else
  253. List.erase(List.begin()+I);
  254. }
  255. return true;
  256. }
  257. /*}}}*/
  258. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  259. // ---------------------------------------------------------------------
  260. /* This takes the list of source list expressed entires and collects
  261. similar ones to form a single entry for each dist */
  262. void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
  263. {
  264. sort(List.begin(),List.end());
  265. // Collect similar entries
  266. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  267. {
  268. // Find a space..
  269. string::size_type Space = (*I).find(' ');
  270. if (Space == string::npos)
  271. continue;
  272. string::size_type SSpace = (*I).find(' ',Space + 1);
  273. if (SSpace == string::npos)
  274. continue;
  275. string Word1 = string(*I,Space,SSpace-Space);
  276. string Prefix = string(*I,0,Space);
  277. for (vector<string>::iterator J = List.begin(); J != I; J++)
  278. {
  279. // Find a space..
  280. string::size_type Space2 = (*J).find(' ');
  281. if (Space2 == string::npos)
  282. continue;
  283. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  284. if (SSpace2 == string::npos)
  285. continue;
  286. if (string(*J,0,Space2) != Prefix)
  287. continue;
  288. if (string(*J,Space2,SSpace2-Space2) != Word1)
  289. continue;
  290. *J += string(*I,SSpace);
  291. *I = string();
  292. }
  293. }
  294. // Wipe erased entries
  295. for (unsigned int I = 0; I < List.size();)
  296. {
  297. if (List[I].empty() == false)
  298. I++;
  299. else
  300. List.erase(List.begin()+I);
  301. }
  302. }
  303. /*}}}*/
  304. // WriteDatabase - Write the CDROM Database file /*{{{*/
  305. // ---------------------------------------------------------------------
  306. /* We rewrite the configuration class associated with the cdrom database. */
  307. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  308. {
  309. string DFile = _config->FindFile("Dir::State::cdroms");
  310. string NewFile = DFile + ".new";
  311. unlink(NewFile.c_str());
  312. ofstream Out(NewFile.c_str());
  313. if (!Out)
  314. return _error->Errno("ofstream::ofstream",
  315. "Failed to open %s.new",DFile.c_str());
  316. /* Write out all of the configuration directives by walking the
  317. configuration tree */
  318. const Configuration::Item *Top = Cnf.Tree(0);
  319. for (; Top != 0;)
  320. {
  321. // Print the config entry
  322. if (Top->Value.empty() == false)
  323. Out << Top->FullTag() + " \"" << Top->Value << "\";" << endl;
  324. if (Top->Child != 0)
  325. {
  326. Top = Top->Child;
  327. continue;
  328. }
  329. while (Top != 0 && Top->Next == 0)
  330. Top = Top->Parent;
  331. if (Top != 0)
  332. Top = Top->Next;
  333. }
  334. Out.close();
  335. rename(DFile.c_str(),string(DFile + '~').c_str());
  336. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  337. return _error->Errno("rename","Failed to rename %s.new to %s",
  338. DFile.c_str(),DFile.c_str());
  339. return true;
  340. }
  341. /*}}}*/
  342. // WriteSourceList - Write an updated sourcelist /*{{{*/
  343. // ---------------------------------------------------------------------
  344. /* This reads the old source list and copies it into the new one. It
  345. appends the new CDROM entires just after the first block of comments.
  346. This places them first in the file. It also removes any old entries
  347. that were the same. */
  348. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  349. {
  350. if (List.size() == 0)
  351. return true;
  352. string File = _config->FindFile("Dir::Etc::sourcelist");
  353. // Open the stream for reading
  354. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  355. ios::in );
  356. if (!F != 0)
  357. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  358. string NewFile = File + ".new";
  359. unlink(NewFile.c_str());
  360. ofstream Out(NewFile.c_str());
  361. if (!Out)
  362. return _error->Errno("ofstream::ofstream",
  363. "Failed to open %s.new",File.c_str());
  364. // Create a short uri without the path
  365. string ShortURI = "cdrom:[" + Name + "]/";
  366. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  367. string Type;
  368. if (Source == true)
  369. Type = "deb-src";
  370. else
  371. Type = "deb";
  372. char Buffer[300];
  373. int CurLine = 0;
  374. bool First = true;
  375. while (F.eof() == false)
  376. {
  377. F.getline(Buffer,sizeof(Buffer));
  378. CurLine++;
  379. if (F.fail() && !F.eof())
  380. return _error->Error(_("Line %u too long in source list %s."),
  381. CurLine,File.c_str());
  382. _strtabexpand(Buffer,sizeof(Buffer));
  383. _strstrip(Buffer);
  384. // Comment or blank
  385. if (Buffer[0] == '#' || Buffer[0] == 0)
  386. {
  387. Out << Buffer << endl;
  388. continue;
  389. }
  390. if (First == true)
  391. {
  392. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  393. {
  394. string::size_type Space = (*I).find(' ');
  395. if (Space == string::npos)
  396. return _error->Error("Internal error");
  397. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  398. " " << string(*I,Space+1) << endl;
  399. }
  400. }
  401. First = false;
  402. // Grok it
  403. string cType;
  404. string URI;
  405. const char *C = Buffer;
  406. if (ParseQuoteWord(C,cType) == false ||
  407. ParseQuoteWord(C,URI) == false)
  408. {
  409. Out << Buffer << endl;
  410. continue;
  411. }
  412. // Emit lines like this one
  413. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  414. string(URI,0,ShortURI.length()) != ShortURI2))
  415. {
  416. Out << Buffer << endl;
  417. continue;
  418. }
  419. }
  420. // Just in case the file was empty
  421. if (First == true)
  422. {
  423. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  424. {
  425. string::size_type Space = (*I).find(' ');
  426. if (Space == string::npos)
  427. return _error->Error("Internal error");
  428. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  429. " " << string(*I,Space+1) << endl;
  430. }
  431. }
  432. Out.close();
  433. rename(File.c_str(),string(File + '~').c_str());
  434. if (rename(NewFile.c_str(),File.c_str()) != 0)
  435. return _error->Errno("rename","Failed to rename %s.new to %s",
  436. File.c_str(),File.c_str());
  437. return true;
  438. }
  439. /*}}}*/
  440. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log) /*{{{*/
  441. {
  442. stringstream msg;
  443. // Startup
  444. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  445. if (CDROM[0] == '.')
  446. CDROM= SafeGetCWD() + '/' + CDROM;
  447. if(log) {
  448. msg.str("");
  449. ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
  450. CDROM.c_str());
  451. log->Update(msg.str());
  452. }
  453. if (MountCdrom(CDROM) == false)
  454. return _error->Error("Failed to mount the cdrom.");
  455. // Hash the CD to get an ID
  456. if(log)
  457. log->Update(_("Identifying.. "));
  458. if (IdentCdrom(CDROM,ident) == false)
  459. {
  460. ident = "";
  461. return false;
  462. }
  463. msg.str("");
  464. ioprintf(msg, "[%s]\n",ident.c_str());
  465. log->Update(msg.str());
  466. // Read the database
  467. Configuration Database;
  468. string DFile = _config->FindFile("Dir::State::cdroms");
  469. if (FileExists(DFile) == true)
  470. {
  471. if (ReadConfigFile(Database,DFile) == false)
  472. return _error->Error("Unable to read the cdrom database %s",
  473. DFile.c_str());
  474. }
  475. if(log) {
  476. msg.str("");
  477. ioprintf(msg, _("Stored label: %s\n"),
  478. Database.Find("CD::"+ident).c_str());
  479. log->Update(msg.str());
  480. }
  481. // Unmount and finish
  482. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  483. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  484. UnmountCdrom(CDROM);
  485. }
  486. return true;
  487. }
  488. /*}}}*/
  489. bool pkgCdrom::Add(pkgCdromStatus *log) /*{{{*/
  490. {
  491. stringstream msg;
  492. // Startup
  493. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  494. if (CDROM[0] == '.')
  495. CDROM= SafeGetCWD() + '/' + CDROM;
  496. if(log) {
  497. log->SetTotal(STEP_LAST);
  498. msg.str("");
  499. ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  500. log->Update(msg.str(), STEP_PREPARE);
  501. }
  502. // Read the database
  503. Configuration Database;
  504. string DFile = _config->FindFile("Dir::State::cdroms");
  505. if (FileExists(DFile) == true)
  506. {
  507. if (ReadConfigFile(Database,DFile) == false)
  508. return _error->Error("Unable to read the cdrom database %s",
  509. DFile.c_str());
  510. }
  511. // Unmount the CD and get the user to put in the one they want
  512. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  513. {
  514. if(log)
  515. log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
  516. UnmountCdrom(CDROM);
  517. if(log) {
  518. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  519. if(!log->ChangeCdrom()) {
  520. // user aborted
  521. return false;
  522. }
  523. }
  524. // Mount the new CDROM
  525. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  526. if (MountCdrom(CDROM) == false)
  527. return _error->Error("Failed to mount the cdrom.");
  528. }
  529. // Hash the CD to get an ID
  530. if(log)
  531. log->Update(_("Identifying.. "), STEP_IDENT);
  532. string ID;
  533. if (IdentCdrom(CDROM,ID) == false)
  534. {
  535. log->Update("\n");
  536. return false;
  537. }
  538. if(log)
  539. log->Update("["+ID+"]\n");
  540. if(log)
  541. log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
  542. // Get the CD structure
  543. vector<string> List;
  544. vector<string> SourceList;
  545. vector<string> SigList;
  546. vector<string> TransList;
  547. string StartDir = SafeGetCWD();
  548. string InfoDir;
  549. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  550. {
  551. log->Update("\n");
  552. return false;
  553. }
  554. chdir(StartDir.c_str());
  555. if (_config->FindB("Debug::aptcdrom",false) == true)
  556. {
  557. cout << "I found (binary):" << endl;
  558. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  559. cout << *I << endl;
  560. cout << "I found (source):" << endl;
  561. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  562. cout << *I << endl;
  563. cout << "I found (Signatures):" << endl;
  564. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); I++)
  565. cout << *I << endl;
  566. }
  567. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  568. // Fix up the list
  569. DropBinaryArch(List);
  570. DropRepeats(List,"Packages");
  571. DropRepeats(SourceList,"Sources");
  572. DropRepeats(SigList,"Release.gpg");
  573. DropRepeats(TransList,"");
  574. if(log) {
  575. msg.str("");
  576. ioprintf(msg, _("Found %zu package indexes, %zu source indexes, "
  577. "%zu translation indexes and %zu signatures\n"),
  578. List.size(), SourceList.size(), TransList.size(),
  579. SigList.size());
  580. log->Update(msg.str(), STEP_SCAN);
  581. }
  582. if (List.size() == 0 && SourceList.size() == 0)
  583. {
  584. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  585. UnmountCdrom(CDROM);
  586. return _error->Error(_("Unable to locate any package files, perhaps this is not a Debian Disc or the wrong architecture?"));
  587. }
  588. // Check if the CD is in the database
  589. string Name;
  590. if (Database.Exists("CD::" + ID) == false ||
  591. _config->FindB("APT::CDROM::Rename",false) == true)
  592. {
  593. // Try to use the CDs label if at all possible
  594. if (InfoDir.empty() == false &&
  595. FileExists(InfoDir + "/info") == true)
  596. {
  597. ifstream F(string(InfoDir + "/info").c_str());
  598. if (!F == 0)
  599. getline(F,Name);
  600. if (Name.empty() == false)
  601. {
  602. // Escape special characters
  603. string::iterator J = Name.begin();
  604. for (; J != Name.end(); J++)
  605. if (*J == '"' || *J == ']' || *J == '[')
  606. *J = '_';
  607. if(log) {
  608. msg.str("");
  609. ioprintf(msg, _("Found label '%s'\n"), Name.c_str());
  610. log->Update(msg.str());
  611. }
  612. Database.Set("CD::" + ID + "::Label",Name);
  613. }
  614. }
  615. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  616. Name.empty() == true)
  617. {
  618. if(!log)
  619. {
  620. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  621. UnmountCdrom(CDROM);
  622. return _error->Error("No disc name found and no way to ask for it");
  623. }
  624. while(true) {
  625. if(!log->AskCdromName(Name)) {
  626. // user canceld
  627. return false;
  628. }
  629. cout << "Name: '" << Name << "'" << endl;
  630. if (Name.empty() == false &&
  631. Name.find('"') == string::npos &&
  632. Name.find('[') == string::npos &&
  633. Name.find(']') == string::npos)
  634. break;
  635. log->Update(_("That is not a valid name, try again.\n"));
  636. }
  637. }
  638. }
  639. else
  640. Name = Database.Find("CD::" + ID);
  641. // Escape special characters
  642. string::iterator J = Name.begin();
  643. for (; J != Name.end(); J++)
  644. if (*J == '"' || *J == ']' || *J == '[')
  645. *J = '_';
  646. Database.Set("CD::" + ID,Name);
  647. if(log) {
  648. msg.str("");
  649. ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  650. log->Update(msg.str());
  651. }
  652. log->Update(_("Copying package lists..."), STEP_COPY);
  653. // take care of the signatures and copy them if they are ok
  654. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  655. SigVerify SignVerify;
  656. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  657. // Copy the package files to the state directory
  658. PackageCopy Copy;
  659. SourceCopy SrcCopy;
  660. TranslationsCopy TransCopy;
  661. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  662. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  663. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  664. return false;
  665. // reduce the List so that it takes less space in sources.list
  666. ReduceSourcelist(CDROM,List);
  667. ReduceSourcelist(CDROM,SourceList);
  668. // Write the database and sourcelist
  669. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  670. {
  671. if (WriteDatabase(Database) == false)
  672. return false;
  673. if(log) {
  674. log->Update(_("Writing new source list\n"), STEP_WRITE);
  675. }
  676. if (WriteSourceList(Name,List,false) == false ||
  677. WriteSourceList(Name,SourceList,true) == false)
  678. return false;
  679. }
  680. // Print the sourcelist entries
  681. if(log)
  682. log->Update(_("Source list entries for this disc are:\n"));
  683. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  684. {
  685. string::size_type Space = (*I).find(' ');
  686. if (Space == string::npos)
  687. {
  688. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  689. UnmountCdrom(CDROM);
  690. return _error->Error("Internal error");
  691. }
  692. if(log) {
  693. msg.str("");
  694. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  695. " " << string(*I,Space+1) << endl;
  696. log->Update(msg.str());
  697. }
  698. }
  699. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  700. {
  701. string::size_type Space = (*I).find(' ');
  702. if (Space == string::npos)
  703. {
  704. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  705. UnmountCdrom(CDROM);
  706. return _error->Error("Internal error");
  707. }
  708. if(log) {
  709. msg.str("");
  710. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  711. " " << string(*I,Space+1) << endl;
  712. log->Update(msg.str());
  713. }
  714. }
  715. // Unmount and finish
  716. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  717. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  718. UnmountCdrom(CDROM);
  719. }
  720. return true;
  721. }
  722. /*}}}*/
  723. pkgUdevCdromDevices::pkgUdevCdromDevices() /*{{{*/
  724. : libudev_handle(NULL)
  725. {
  726. }
  727. /*}}}*/
  728. bool
  729. pkgUdevCdromDevices::Dlopen() /*{{{*/
  730. {
  731. // alread open
  732. if(libudev_handle != NULL)
  733. return true;
  734. // see if we can get libudev
  735. void *h = ::dlopen("libudev.so.0", RTLD_LAZY);
  736. if(h == NULL)
  737. return false;
  738. // get the pointers to the udev structs
  739. libudev_handle = h;
  740. udev_new = (udev* (*)(void)) dlsym(h, "udev_new");
  741. udev_enumerate_add_match_property = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_property");
  742. udev_enumerate_scan_devices = (int (*)(udev_enumerate*))dlsym(h, "udev_enumerate_scan_devices");
  743. udev_enumerate_get_list_entry = (udev_list_entry* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_list_entry");
  744. udev_device_new_from_syspath = (udev_device* (*)(udev*, const char*))dlsym(h, "udev_device_new_from_syspath");
  745. udev_enumerate_get_udev = (udev* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_udev");
  746. udev_list_entry_get_name = (const char* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_name");
  747. udev_device_get_devnode = (const char* (*)(udev_device*))dlsym(h, "udev_device_get_devnode");
  748. udev_enumerate_new = (udev_enumerate* (*)(udev*))dlsym(h, "udev_enumerate_new");
  749. udev_list_entry_get_next = (udev_list_entry* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_next");
  750. udev_device_get_property_value = (const char* (*)(udev_device *, const char *))dlsym(h, "udev_device_get_property_value");
  751. return true;
  752. }
  753. /*}}}*/
  754. vector<CdromDevice>
  755. pkgUdevCdromDevices::Scan() /*{{{*/
  756. {
  757. vector<CdromDevice> cdrom_devices;
  758. struct udev_enumerate *enumerate;
  759. struct udev_list_entry *l, *devices;
  760. struct udev *udev_ctx;
  761. if(libudev_handle == NULL)
  762. return cdrom_devices;
  763. udev_ctx = udev_new();
  764. enumerate = udev_enumerate_new (udev_ctx);
  765. udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
  766. udev_enumerate_scan_devices (enumerate);
  767. devices = udev_enumerate_get_list_entry (enumerate);
  768. for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
  769. {
  770. CdromDevice cdrom;
  771. struct udev_device *udevice;
  772. udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate), udev_list_entry_get_name (l));
  773. if (udevice == NULL)
  774. continue;
  775. const char* devnode = udev_device_get_devnode(udevice);
  776. const char* mountpath = udev_device_get_property_value(udevice, "FSTAB_DIR");
  777. // fill in the struct
  778. cdrom.DeviceName = string(devnode);
  779. if (mountpath) {
  780. cdrom.MountPath = mountpath;
  781. string s = string(mountpath);
  782. cdrom.Mounted = IsMounted(s);
  783. } else {
  784. cdrom.Mounted = false;
  785. cdrom.MountPath = "";
  786. }
  787. cdrom_devices.push_back(cdrom);
  788. }
  789. return cdrom_devices;
  790. }
  791. /*}}}*/
  792. pkgUdevCdromDevices::~pkgUdevCdromDevices() /*{{{*/
  793. {
  794. dlclose(libudev_handle);
  795. }
  796. /*}}}*/