cdrom.cc 27 KB

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