cdrom.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. if (DirectoryExists(".disk") == true)
  51. {
  52. if (InfoDir.empty() == true)
  53. InfoDir = CD + ".disk/";
  54. }
  55. // Don't look into directories that have been marked to ingore.
  56. if (RealFileExists(".aptignr") == true)
  57. return true;
  58. /* Check _first_ for a signature file as apt-cdrom assumes that all files
  59. under a Packages/Source file are in control of that file and stops
  60. the scanning
  61. */
  62. if (RealFileExists("Release.gpg") == true || RealFileExists("InRelease") == true)
  63. {
  64. SigList.push_back(CD);
  65. }
  66. /* Aha! We found some package files. We assume that everything under
  67. this dir is controlled by those package files so we don't look down
  68. anymore */
  69. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  70. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  71. c != compressor.end(); ++c)
  72. {
  73. if (RealFileExists(std::string("Packages").append(c->Extension).c_str()) == false)
  74. continue;
  75. if (_config->FindB("Debug::aptcdrom",false) == true)
  76. std::clog << "Found Packages in " << CD << std::endl;
  77. List.push_back(CD);
  78. // Continue down if thorough is given
  79. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  80. return true;
  81. break;
  82. }
  83. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  84. c != compressor.end(); ++c)
  85. {
  86. if (RealFileExists(std::string("Sources").append(c->Extension).c_str()) == false)
  87. continue;
  88. if (_config->FindB("Debug::aptcdrom",false) == true)
  89. std::clog << "Found Sources in " << CD << std::endl;
  90. SList.push_back(CD);
  91. // Continue down if thorough is given
  92. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  93. return true;
  94. break;
  95. }
  96. // see if we find translation indices
  97. if (DirectoryExists("i18n") == true)
  98. {
  99. D = opendir("i18n");
  100. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  101. {
  102. if(strncmp(Dir->d_name, "Translation-", strlen("Translation-")) != 0)
  103. continue;
  104. string file = Dir->d_name;
  105. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  106. c != compressor.end(); ++c)
  107. {
  108. string fileext = flExtension(file);
  109. if (file == fileext)
  110. fileext.clear();
  111. else if (fileext.empty() == false)
  112. fileext = "." + fileext;
  113. if (c->Extension == fileext)
  114. {
  115. if (_config->FindB("Debug::aptcdrom",false) == true)
  116. std::clog << "Found translation " << Dir->d_name << " in " << CD << "i18n/" << std::endl;
  117. file.erase(file.size() - fileext.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. bool couldFindAllFiles = true;
  245. // Get a list of all the inodes
  246. ino_t *Inodes = new ino_t[List.size()];
  247. for (unsigned int I = 0; I != List.size(); ++I)
  248. {
  249. struct stat Buf;
  250. bool found = false;
  251. std::vector<APT::Configuration::Compressor> const compressor = APT::Configuration::getCompressors();
  252. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
  253. c != compressor.end(); ++c)
  254. {
  255. std::string filename = std::string(List[I]).append(Name).append(c->Extension);
  256. if (stat(filename.c_str(), &Buf) != 0)
  257. continue;
  258. Inodes[I] = Buf.st_ino;
  259. found = true;
  260. break;
  261. }
  262. if (found == false)
  263. {
  264. _error->Errno("stat","Failed to stat %s%s",List[I].c_str(), Name);
  265. couldFindAllFiles = false;
  266. Inodes[I] = 0;
  267. }
  268. }
  269. // Look for dups
  270. for (unsigned int I = 0; I != List.size(); I++)
  271. {
  272. if (Inodes[I] == 0)
  273. continue;
  274. for (unsigned int J = I+1; J < List.size(); J++)
  275. {
  276. // No match
  277. if (Inodes[J] == 0 || Inodes[J] != Inodes[I])
  278. continue;
  279. // We score the two paths.. and erase one
  280. int ScoreA = Score(List[I]);
  281. int ScoreB = Score(List[J]);
  282. if (ScoreA < ScoreB)
  283. {
  284. List[I] = string();
  285. break;
  286. }
  287. List[J] = string();
  288. }
  289. }
  290. delete[] Inodes;
  291. // Wipe erased entries
  292. for (unsigned int I = 0; I < List.size();)
  293. {
  294. if (List[I].empty() == false)
  295. I++;
  296. else
  297. List.erase(List.begin()+I);
  298. }
  299. return couldFindAllFiles;
  300. }
  301. /*}}}*/
  302. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* This takes the list of source list expressed entires and collects
  305. similar ones to form a single entry for each dist */
  306. void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
  307. {
  308. sort(List.begin(),List.end());
  309. // Collect similar entries
  310. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  311. {
  312. // Find a space..
  313. string::size_type Space = (*I).find(' ');
  314. if (Space == string::npos)
  315. continue;
  316. string::size_type SSpace = (*I).find(' ',Space + 1);
  317. if (SSpace == string::npos)
  318. continue;
  319. string Word1 = string(*I,Space,SSpace-Space);
  320. string Prefix = string(*I,0,Space);
  321. for (vector<string>::iterator J = List.begin(); J != I; ++J)
  322. {
  323. // Find a space..
  324. string::size_type Space2 = (*J).find(' ');
  325. if (Space2 == string::npos)
  326. continue;
  327. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  328. if (SSpace2 == string::npos)
  329. continue;
  330. if (string(*J,0,Space2) != Prefix)
  331. continue;
  332. if (string(*J,Space2,SSpace2-Space2) != Word1)
  333. continue;
  334. *J += string(*I,SSpace);
  335. *I = string();
  336. }
  337. }
  338. // Wipe erased entries
  339. for (unsigned int I = 0; I < List.size();)
  340. {
  341. if (List[I].empty() == false)
  342. I++;
  343. else
  344. List.erase(List.begin()+I);
  345. }
  346. }
  347. /*}}}*/
  348. // WriteDatabase - Write the CDROM Database file /*{{{*/
  349. // ---------------------------------------------------------------------
  350. /* We rewrite the configuration class associated with the cdrom database. */
  351. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  352. {
  353. string DFile = _config->FindFile("Dir::State::cdroms");
  354. string NewFile = DFile + ".new";
  355. unlink(NewFile.c_str());
  356. ofstream Out(NewFile.c_str());
  357. if (!Out)
  358. return _error->Errno("ofstream::ofstream",
  359. "Failed to open %s.new",DFile.c_str());
  360. /* Write out all of the configuration directives by walking the
  361. configuration tree */
  362. const Configuration::Item *Top = Cnf.Tree(0);
  363. for (; Top != 0;)
  364. {
  365. // Print the config entry
  366. if (Top->Value.empty() == false)
  367. Out << Top->FullTag() + " \"" << Top->Value << "\";" << endl;
  368. if (Top->Child != 0)
  369. {
  370. Top = Top->Child;
  371. continue;
  372. }
  373. while (Top != 0 && Top->Next == 0)
  374. Top = Top->Parent;
  375. if (Top != 0)
  376. Top = Top->Next;
  377. }
  378. Out.close();
  379. link(DFile.c_str(),string(DFile + '~').c_str());
  380. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  381. return _error->Errno("rename","Failed to rename %s.new to %s",
  382. DFile.c_str(),DFile.c_str());
  383. return true;
  384. }
  385. /*}}}*/
  386. // WriteSourceList - Write an updated sourcelist /*{{{*/
  387. // ---------------------------------------------------------------------
  388. /* This reads the old source list and copies it into the new one. It
  389. appends the new CDROM entires just after the first block of comments.
  390. This places them first in the file. It also removes any old entries
  391. that were the same. */
  392. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  393. {
  394. if (List.empty() == true)
  395. return true;
  396. string File = _config->FindFile("Dir::Etc::sourcelist");
  397. // Open the stream for reading
  398. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  399. ios::in );
  400. if (!F != 0)
  401. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  402. string NewFile = File + ".new";
  403. unlink(NewFile.c_str());
  404. ofstream Out(NewFile.c_str());
  405. if (!Out)
  406. return _error->Errno("ofstream::ofstream",
  407. "Failed to open %s.new",File.c_str());
  408. // Create a short uri without the path
  409. string ShortURI = "cdrom:[" + Name + "]/";
  410. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  411. string Type;
  412. if (Source == true)
  413. Type = "deb-src";
  414. else
  415. Type = "deb";
  416. char Buffer[300];
  417. int CurLine = 0;
  418. bool First = true;
  419. while (F.eof() == false)
  420. {
  421. F.getline(Buffer,sizeof(Buffer));
  422. CurLine++;
  423. if (F.fail() && !F.eof())
  424. return _error->Error(_("Line %u too long in source list %s."),
  425. CurLine,File.c_str());
  426. _strtabexpand(Buffer,sizeof(Buffer));
  427. _strstrip(Buffer);
  428. // Comment or blank
  429. if (Buffer[0] == '#' || Buffer[0] == 0)
  430. {
  431. Out << Buffer << endl;
  432. continue;
  433. }
  434. if (First == true)
  435. {
  436. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  437. {
  438. string::size_type Space = (*I).find(' ');
  439. if (Space == string::npos)
  440. return _error->Error("Internal error");
  441. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  442. " " << string(*I,Space+1) << endl;
  443. }
  444. }
  445. First = false;
  446. // Grok it
  447. string cType;
  448. string URI;
  449. const char *C = Buffer;
  450. if (ParseQuoteWord(C,cType) == false ||
  451. ParseQuoteWord(C,URI) == false)
  452. {
  453. Out << Buffer << endl;
  454. continue;
  455. }
  456. // Emit lines like this one
  457. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  458. string(URI,0,ShortURI.length()) != ShortURI2))
  459. {
  460. Out << Buffer << endl;
  461. continue;
  462. }
  463. }
  464. // Just in case the file was empty
  465. if (First == true)
  466. {
  467. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  468. {
  469. string::size_type Space = (*I).find(' ');
  470. if (Space == string::npos)
  471. return _error->Error("Internal error");
  472. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  473. " " << string(*I,Space+1) << endl;
  474. }
  475. }
  476. Out.close();
  477. rename(File.c_str(),string(File + '~').c_str());
  478. if (rename(NewFile.c_str(),File.c_str()) != 0)
  479. return _error->Errno("rename","Failed to rename %s.new to %s",
  480. File.c_str(),File.c_str());
  481. return true;
  482. }
  483. /*}}}*/
  484. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log) /*{{{*/
  485. {
  486. stringstream msg;
  487. // Startup
  488. string CDROM = _config->FindDir("Acquire::cdrom::mount");
  489. if (CDROM[0] == '.')
  490. CDROM= SafeGetCWD() + '/' + CDROM;
  491. if (log != NULL)
  492. {
  493. msg.str("");
  494. ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
  495. CDROM.c_str());
  496. log->Update(msg.str());
  497. }
  498. if (MountCdrom(CDROM) == false)
  499. return _error->Error("Failed to mount the cdrom.");
  500. // Hash the CD to get an ID
  501. if (log != NULL)
  502. log->Update(_("Identifying.. "));
  503. if (IdentCdrom(CDROM,ident) == false)
  504. {
  505. ident = "";
  506. return false;
  507. }
  508. if (log != NULL)
  509. {
  510. msg.str("");
  511. ioprintf(msg, "[%s]\n",ident.c_str());
  512. log->Update(msg.str());
  513. }
  514. // Read the database
  515. Configuration Database;
  516. string DFile = _config->FindFile("Dir::State::cdroms");
  517. if (FileExists(DFile) == true)
  518. {
  519. if (ReadConfigFile(Database,DFile) == false)
  520. return _error->Error("Unable to read the cdrom database %s",
  521. DFile.c_str());
  522. }
  523. if (log != NULL)
  524. {
  525. msg.str("");
  526. ioprintf(msg, _("Stored label: %s\n"),
  527. Database.Find("CD::"+ident).c_str());
  528. log->Update(msg.str());
  529. }
  530. // Unmount and finish
  531. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  532. {
  533. if (log != NULL)
  534. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  535. UnmountCdrom(CDROM);
  536. }
  537. return true;
  538. }
  539. /*}}}*/
  540. bool pkgCdrom::Add(pkgCdromStatus *log) /*{{{*/
  541. {
  542. stringstream msg;
  543. // Startup
  544. string CDROM = _config->FindDir("Acquire::cdrom::mount");
  545. if (CDROM[0] == '.')
  546. CDROM= SafeGetCWD() + '/' + CDROM;
  547. if(log != NULL)
  548. {
  549. log->SetTotal(STEP_LAST);
  550. msg.str("");
  551. ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  552. log->Update(msg.str(), STEP_PREPARE);
  553. }
  554. // Read the database
  555. Configuration Database;
  556. string DFile = _config->FindFile("Dir::State::cdroms");
  557. if (FileExists(DFile) == true)
  558. {
  559. if (ReadConfigFile(Database,DFile) == false)
  560. return _error->Error("Unable to read the cdrom database %s",
  561. DFile.c_str());
  562. }
  563. // Unmount the CD and get the user to put in the one they want
  564. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  565. {
  566. if(log != NULL)
  567. log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
  568. UnmountCdrom(CDROM);
  569. if(log != NULL)
  570. {
  571. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  572. if(!log->ChangeCdrom()) {
  573. // user aborted
  574. return false;
  575. }
  576. }
  577. // Mount the new CDROM
  578. if(log != NULL)
  579. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  580. if (MountCdrom(CDROM) == false)
  581. return _error->Error("Failed to mount the cdrom.");
  582. }
  583. // Hash the CD to get an ID
  584. if(log != NULL)
  585. log->Update(_("Identifying.. "), STEP_IDENT);
  586. string ID;
  587. if (IdentCdrom(CDROM,ID) == false)
  588. {
  589. if (log != NULL)
  590. log->Update("\n");
  591. return false;
  592. }
  593. if(log != NULL)
  594. {
  595. log->Update("["+ID+"]\n");
  596. log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
  597. }
  598. // Get the CD structure
  599. vector<string> List;
  600. vector<string> SourceList;
  601. vector<string> SigList;
  602. vector<string> TransList;
  603. string StartDir = SafeGetCWD();
  604. string InfoDir;
  605. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  606. {
  607. if (log != NULL)
  608. log->Update("\n");
  609. return false;
  610. }
  611. chdir(StartDir.c_str());
  612. if (_config->FindB("Debug::aptcdrom",false) == true)
  613. {
  614. cout << "I found (binary):" << endl;
  615. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  616. cout << *I << endl;
  617. cout << "I found (source):" << endl;
  618. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  619. cout << *I << endl;
  620. cout << "I found (Signatures):" << endl;
  621. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I)
  622. cout << *I << endl;
  623. }
  624. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  625. // Fix up the list
  626. DropBinaryArch(List);
  627. DropRepeats(List,"Packages");
  628. DropRepeats(SourceList,"Sources");
  629. // FIXME: We ignore stat() errors here as we usually have only one of those in use
  630. // This has little potencial to drop 'valid' stat() errors as we know that one of these
  631. // files need to exist, but it would be better if we would check it here
  632. _error->PushToStack();
  633. DropRepeats(SigList,"Release.gpg");
  634. DropRepeats(SigList,"InRelease");
  635. _error->RevertToStack();
  636. DropRepeats(TransList,"");
  637. if(log != NULL) {
  638. msg.str("");
  639. ioprintf(msg, _("Found %zu package indexes, %zu source indexes, "
  640. "%zu translation indexes and %zu signatures\n"),
  641. List.size(), SourceList.size(), TransList.size(),
  642. SigList.size());
  643. log->Update(msg.str(), STEP_SCAN);
  644. }
  645. if (List.empty() == true && SourceList.empty() == true)
  646. {
  647. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  648. UnmountCdrom(CDROM);
  649. return _error->Error(_("Unable to locate any package files, perhaps this is not a Debian Disc or the wrong architecture?"));
  650. }
  651. // Check if the CD is in the database
  652. string Name;
  653. if (Database.Exists("CD::" + ID) == false ||
  654. _config->FindB("APT::CDROM::Rename",false) == true)
  655. {
  656. // Try to use the CDs label if at all possible
  657. if (InfoDir.empty() == false &&
  658. FileExists(InfoDir + "/info") == true)
  659. {
  660. ifstream F(string(InfoDir + "/info").c_str());
  661. if (!F == 0)
  662. getline(F,Name);
  663. if (Name.empty() == false)
  664. {
  665. // Escape special characters
  666. string::iterator J = Name.begin();
  667. for (; J != Name.end(); ++J)
  668. if (*J == '"' || *J == ']' || *J == '[')
  669. *J = '_';
  670. if(log != NULL)
  671. {
  672. msg.str("");
  673. ioprintf(msg, _("Found label '%s'\n"), Name.c_str());
  674. log->Update(msg.str());
  675. }
  676. Database.Set("CD::" + ID + "::Label",Name);
  677. }
  678. }
  679. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  680. Name.empty() == true)
  681. {
  682. if(log == NULL)
  683. {
  684. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  685. UnmountCdrom(CDROM);
  686. return _error->Error("No disc name found and no way to ask for it");
  687. }
  688. while(true) {
  689. if(!log->AskCdromName(Name)) {
  690. // user canceld
  691. return false;
  692. }
  693. cout << "Name: '" << Name << "'" << endl;
  694. if (Name.empty() == false &&
  695. Name.find('"') == string::npos &&
  696. Name.find('[') == string::npos &&
  697. Name.find(']') == string::npos)
  698. break;
  699. log->Update(_("That is not a valid name, try again.\n"));
  700. }
  701. }
  702. }
  703. else
  704. Name = Database.Find("CD::" + ID);
  705. // Escape special characters
  706. string::iterator J = Name.begin();
  707. for (; J != Name.end(); ++J)
  708. if (*J == '"' || *J == ']' || *J == '[')
  709. *J = '_';
  710. Database.Set("CD::" + ID,Name);
  711. if(log != NULL)
  712. {
  713. msg.str("");
  714. ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  715. log->Update(msg.str());
  716. log->Update(_("Copying package lists..."), STEP_COPY);
  717. }
  718. // take care of the signatures and copy them if they are ok
  719. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  720. SigVerify SignVerify;
  721. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  722. // Copy the package files to the state directory
  723. PackageCopy Copy;
  724. SourceCopy SrcCopy;
  725. TranslationsCopy TransCopy;
  726. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  727. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  728. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  729. return false;
  730. // reduce the List so that it takes less space in sources.list
  731. ReduceSourcelist(CDROM,List);
  732. ReduceSourcelist(CDROM,SourceList);
  733. // Write the database and sourcelist
  734. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  735. {
  736. if (WriteDatabase(Database) == false)
  737. return false;
  738. if(log != NULL)
  739. log->Update(_("Writing new source list\n"), STEP_WRITE);
  740. if (WriteSourceList(Name,List,false) == false ||
  741. WriteSourceList(Name,SourceList,true) == false)
  742. return false;
  743. }
  744. // Print the sourcelist entries
  745. if(log != NULL)
  746. log->Update(_("Source list entries for this disc are:\n"));
  747. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  748. {
  749. string::size_type Space = (*I).find(' ');
  750. if (Space == string::npos)
  751. {
  752. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  753. UnmountCdrom(CDROM);
  754. return _error->Error("Internal error");
  755. }
  756. if(log != NULL)
  757. {
  758. msg.str("");
  759. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  760. " " << string(*I,Space+1) << endl;
  761. log->Update(msg.str());
  762. }
  763. }
  764. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  765. {
  766. string::size_type Space = (*I).find(' ');
  767. if (Space == string::npos)
  768. {
  769. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  770. UnmountCdrom(CDROM);
  771. return _error->Error("Internal error");
  772. }
  773. if(log != NULL) {
  774. msg.str("");
  775. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  776. " " << string(*I,Space+1) << endl;
  777. log->Update(msg.str());
  778. }
  779. }
  780. // Unmount and finish
  781. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  782. if (log != NULL)
  783. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  784. UnmountCdrom(CDROM);
  785. }
  786. return true;
  787. }
  788. /*}}}*/
  789. pkgUdevCdromDevices::pkgUdevCdromDevices() /*{{{*/
  790. : libudev_handle(NULL)
  791. {
  792. }
  793. /*}}}*/
  794. bool
  795. pkgUdevCdromDevices::Dlopen() /*{{{*/
  796. {
  797. // alread open
  798. if(libudev_handle != NULL)
  799. return true;
  800. // see if we can get libudev
  801. void *h = ::dlopen("libudev.so.0", RTLD_LAZY);
  802. if(h == NULL)
  803. return false;
  804. // get the pointers to the udev structs
  805. libudev_handle = h;
  806. udev_new = (udev* (*)(void)) dlsym(h, "udev_new");
  807. udev_enumerate_add_match_property = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_property");
  808. udev_enumerate_add_match_sysattr = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_sysattr");
  809. udev_enumerate_scan_devices = (int (*)(udev_enumerate*))dlsym(h, "udev_enumerate_scan_devices");
  810. udev_enumerate_get_list_entry = (udev_list_entry* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_list_entry");
  811. udev_device_new_from_syspath = (udev_device* (*)(udev*, const char*))dlsym(h, "udev_device_new_from_syspath");
  812. udev_enumerate_get_udev = (udev* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_udev");
  813. udev_list_entry_get_name = (const char* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_name");
  814. udev_device_get_devnode = (const char* (*)(udev_device*))dlsym(h, "udev_device_get_devnode");
  815. udev_enumerate_new = (udev_enumerate* (*)(udev*))dlsym(h, "udev_enumerate_new");
  816. udev_list_entry_get_next = (udev_list_entry* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_next");
  817. udev_device_get_property_value = (const char* (*)(udev_device *, const char *))dlsym(h, "udev_device_get_property_value");
  818. return true;
  819. }
  820. /*}}}*/
  821. /*{{{*/
  822. // convenience interface, this will just call ScanForRemovable
  823. vector<CdromDevice>
  824. pkgUdevCdromDevices::Scan()
  825. {
  826. bool CdromOnly = _config->FindB("APT::cdrom::CdromOnly", true);
  827. return ScanForRemovable(CdromOnly);
  828. };
  829. /*}}}*/
  830. /*{{{*/
  831. vector<CdromDevice>
  832. pkgUdevCdromDevices::ScanForRemovable(bool CdromOnly)
  833. {
  834. vector<CdromDevice> cdrom_devices;
  835. struct udev_enumerate *enumerate;
  836. struct udev_list_entry *l, *devices;
  837. struct udev *udev_ctx;
  838. if(libudev_handle == NULL)
  839. return cdrom_devices;
  840. udev_ctx = udev_new();
  841. enumerate = udev_enumerate_new (udev_ctx);
  842. if (CdromOnly)
  843. udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
  844. else {
  845. udev_enumerate_add_match_sysattr(enumerate, "removable", "1");
  846. }
  847. udev_enumerate_scan_devices (enumerate);
  848. devices = udev_enumerate_get_list_entry (enumerate);
  849. for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
  850. {
  851. CdromDevice cdrom;
  852. struct udev_device *udevice;
  853. udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate), udev_list_entry_get_name (l));
  854. if (udevice == NULL)
  855. continue;
  856. const char* devnode = udev_device_get_devnode(udevice);
  857. // try fstab_dir first
  858. string mountpath;
  859. const char* mp = udev_device_get_property_value(udevice, "FSTAB_DIR");
  860. if (mp)
  861. mountpath = string(mp);
  862. else
  863. mountpath = FindMountPointForDevice(devnode);
  864. // fill in the struct
  865. cdrom.DeviceName = string(devnode);
  866. if (mountpath != "") {
  867. cdrom.MountPath = mountpath;
  868. string s = string(mountpath);
  869. cdrom.Mounted = IsMounted(s);
  870. } else {
  871. cdrom.Mounted = false;
  872. cdrom.MountPath = "";
  873. }
  874. cdrom_devices.push_back(cdrom);
  875. }
  876. return cdrom_devices;
  877. }
  878. /*}}}*/
  879. pkgUdevCdromDevices::~pkgUdevCdromDevices() /*{{{*/
  880. {
  881. if (libudev_handle != NULL)
  882. dlclose(libudev_handle);
  883. }
  884. /*}}}*/