cdrom.cc 27 KB

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