cdrom.cc 28 KB

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