cdrom.cc 27 KB

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