cdrom.cc 29 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. 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. string Component = string(*I,SSpace);
  322. for (vector<string>::iterator J = List.begin(); J != I; ++J)
  323. {
  324. // Find a space..
  325. string::size_type Space2 = (*J).find(' ');
  326. if (Space2 == string::npos)
  327. continue;
  328. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  329. if (SSpace2 == string::npos)
  330. continue;
  331. if (string(*J,0,Space2) != Prefix)
  332. continue;
  333. if (string(*J,Space2,SSpace2-Space2) != Word1)
  334. continue;
  335. string Component2 = string(*J, SSpace2) + " ";
  336. if (Component2.find(Component + " ") == std::string::npos)
  337. *J += Component;
  338. I->clear();
  339. }
  340. }
  341. // Wipe erased entries
  342. for (unsigned int I = 0; I < List.size();)
  343. {
  344. if (List[I].empty() == false)
  345. I++;
  346. else
  347. List.erase(List.begin()+I);
  348. }
  349. }
  350. /*}}}*/
  351. // WriteDatabase - Write the CDROM Database file /*{{{*/
  352. // ---------------------------------------------------------------------
  353. /* We rewrite the configuration class associated with the cdrom database. */
  354. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  355. {
  356. string DFile = _config->FindFile("Dir::State::cdroms");
  357. string NewFile = DFile + ".new";
  358. unlink(NewFile.c_str());
  359. ofstream Out(NewFile.c_str());
  360. if (!Out)
  361. return _error->Errno("ofstream::ofstream",
  362. "Failed to open %s.new",DFile.c_str());
  363. /* Write out all of the configuration directives by walking the
  364. configuration tree */
  365. Cnf.Dump(Out, NULL, "%f \"%v\";\n", false);
  366. Out.close();
  367. if (FileExists(DFile) == true)
  368. rename(DFile.c_str(), string(DFile + '~').c_str());
  369. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  370. return _error->Errno("rename","Failed to rename %s.new to %s",
  371. DFile.c_str(),DFile.c_str());
  372. return true;
  373. }
  374. /*}}}*/
  375. // WriteSourceList - Write an updated sourcelist /*{{{*/
  376. // ---------------------------------------------------------------------
  377. /* This reads the old source list and copies it into the new one. It
  378. appends the new CDROM entires just after the first block of comments.
  379. This places them first in the file. It also removes any old entries
  380. that were the same. */
  381. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  382. {
  383. if (List.empty() == true)
  384. return true;
  385. string File = _config->FindFile("Dir::Etc::sourcelist");
  386. // Open the stream for reading
  387. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  388. ios::in );
  389. if (!F != 0)
  390. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  391. string NewFile = File + ".new";
  392. unlink(NewFile.c_str());
  393. ofstream Out(NewFile.c_str());
  394. if (!Out)
  395. return _error->Errno("ofstream::ofstream",
  396. "Failed to open %s.new",File.c_str());
  397. // Create a short uri without the path
  398. string ShortURI = "cdrom:[" + Name + "]/";
  399. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  400. string Type;
  401. if (Source == true)
  402. Type = "deb-src";
  403. else
  404. Type = "deb";
  405. char Buffer[300];
  406. int CurLine = 0;
  407. bool First = true;
  408. while (F.eof() == false)
  409. {
  410. F.getline(Buffer,sizeof(Buffer));
  411. CurLine++;
  412. if (F.fail() && !F.eof())
  413. return _error->Error(_("Line %u too long in source list %s."),
  414. CurLine,File.c_str());
  415. _strtabexpand(Buffer,sizeof(Buffer));
  416. _strstrip(Buffer);
  417. // Comment or blank
  418. if (Buffer[0] == '#' || Buffer[0] == 0)
  419. {
  420. Out << Buffer << endl;
  421. continue;
  422. }
  423. if (First == true)
  424. {
  425. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  426. {
  427. string::size_type Space = (*I).find(' ');
  428. if (Space == string::npos)
  429. return _error->Error("Internal error");
  430. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  431. " " << string(*I,Space+1) << endl;
  432. }
  433. }
  434. First = false;
  435. // Grok it
  436. string cType;
  437. string URI;
  438. const char *C = Buffer;
  439. if (ParseQuoteWord(C,cType) == false ||
  440. ParseQuoteWord(C,URI) == false)
  441. {
  442. Out << Buffer << endl;
  443. continue;
  444. }
  445. // Emit lines like this one
  446. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  447. string(URI,0,ShortURI.length()) != ShortURI2))
  448. {
  449. Out << Buffer << endl;
  450. continue;
  451. }
  452. }
  453. // Just in case the file was empty
  454. if (First == true)
  455. {
  456. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  457. {
  458. string::size_type Space = (*I).find(' ');
  459. if (Space == string::npos)
  460. return _error->Error("Internal error");
  461. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  462. " " << string(*I,Space+1) << endl;
  463. }
  464. }
  465. Out.close();
  466. rename(File.c_str(),string(File + '~').c_str());
  467. if (rename(NewFile.c_str(),File.c_str()) != 0)
  468. return _error->Errno("rename","Failed to rename %s.new to %s",
  469. File.c_str(),File.c_str());
  470. return true;
  471. }
  472. /*}}}*/
  473. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log) /*{{{*/
  474. {
  475. stringstream msg;
  476. // Startup
  477. string CDROM = _config->FindDir("Acquire::cdrom::mount");
  478. if (CDROM[0] == '.')
  479. CDROM= SafeGetCWD() + '/' + CDROM;
  480. if (log != NULL)
  481. {
  482. msg.str("");
  483. ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
  484. CDROM.c_str());
  485. log->Update(msg.str());
  486. }
  487. if (MountCdrom(CDROM) == false)
  488. return _error->Error("Failed to mount the cdrom.");
  489. // Hash the CD to get an ID
  490. if (log != NULL)
  491. log->Update(_("Identifying.. "));
  492. if (IdentCdrom(CDROM,ident) == false)
  493. {
  494. ident = "";
  495. return false;
  496. }
  497. if (log != NULL)
  498. {
  499. msg.str("");
  500. ioprintf(msg, "[%s]\n",ident.c_str());
  501. log->Update(msg.str());
  502. }
  503. // Read the database
  504. Configuration Database;
  505. string DFile = _config->FindFile("Dir::State::cdroms");
  506. if (FileExists(DFile) == true)
  507. {
  508. if (ReadConfigFile(Database,DFile) == false)
  509. return _error->Error("Unable to read the cdrom database %s",
  510. DFile.c_str());
  511. }
  512. if (log != NULL)
  513. {
  514. msg.str("");
  515. ioprintf(msg, _("Stored label: %s\n"),
  516. Database.Find("CD::"+ident).c_str());
  517. log->Update(msg.str());
  518. }
  519. // Unmount and finish
  520. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  521. {
  522. if (log != NULL)
  523. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  524. UnmountCdrom(CDROM);
  525. }
  526. return true;
  527. }
  528. /*}}}*/
  529. bool pkgCdrom::Add(pkgCdromStatus *log) /*{{{*/
  530. {
  531. stringstream msg;
  532. // Startup
  533. string CDROM = _config->FindDir("Acquire::cdrom::mount");
  534. if (CDROM[0] == '.')
  535. CDROM= SafeGetCWD() + '/' + CDROM;
  536. if(log != NULL)
  537. {
  538. log->SetTotal(STEP_LAST);
  539. msg.str("");
  540. ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  541. log->Update(msg.str(), STEP_PREPARE);
  542. }
  543. // Read the database
  544. Configuration Database;
  545. string DFile = _config->FindFile("Dir::State::cdroms");
  546. if (FileExists(DFile) == true)
  547. {
  548. if (ReadConfigFile(Database,DFile) == false)
  549. return _error->Error("Unable to read the cdrom database %s",
  550. DFile.c_str());
  551. }
  552. // Unmount the CD and get the user to put in the one they want
  553. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  554. {
  555. if(log != NULL)
  556. log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
  557. UnmountCdrom(CDROM);
  558. if(log != NULL)
  559. {
  560. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  561. if(!log->ChangeCdrom()) {
  562. // user aborted
  563. return false;
  564. }
  565. }
  566. // Mount the new CDROM
  567. if(log != NULL)
  568. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  569. if (MountCdrom(CDROM) == false)
  570. return _error->Error("Failed to mount the cdrom.");
  571. }
  572. // Hash the CD to get an ID
  573. if(log != NULL)
  574. log->Update(_("Identifying.. "), STEP_IDENT);
  575. string ID;
  576. if (IdentCdrom(CDROM,ID) == false)
  577. {
  578. if (log != NULL)
  579. log->Update("\n");
  580. return false;
  581. }
  582. if(log != NULL)
  583. {
  584. log->Update("["+ID+"]\n");
  585. log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
  586. }
  587. // Get the CD structure
  588. vector<string> List;
  589. vector<string> SourceList;
  590. vector<string> SigList;
  591. vector<string> TransList;
  592. string StartDir = SafeGetCWD();
  593. string InfoDir;
  594. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  595. {
  596. if (log != NULL)
  597. log->Update("\n");
  598. return false;
  599. }
  600. if (chdir(StartDir.c_str()) != 0)
  601. return _error->Errno("chdir","Unable to change to %s", StartDir.c_str());
  602. if (_config->FindB("Debug::aptcdrom",false) == true)
  603. {
  604. cout << "I found (binary):" << endl;
  605. for (vector<string>::iterator I = List.begin(); I != List.end(); ++I)
  606. cout << *I << endl;
  607. cout << "I found (source):" << endl;
  608. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); ++I)
  609. cout << *I << endl;
  610. cout << "I found (Signatures):" << endl;
  611. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); ++I)
  612. cout << *I << endl;
  613. }
  614. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  615. // Fix up the list
  616. DropBinaryArch(List);
  617. DropRepeats(List,"Packages");
  618. DropRepeats(SourceList,"Sources");
  619. // FIXME: We ignore stat() errors here as we usually have only one of those in use
  620. // This has little potencial to drop 'valid' stat() errors as we know that one of these
  621. // files need to exist, but it would be better if we would check it here
  622. _error->PushToStack();
  623. DropRepeats(SigList,"Release.gpg");
  624. DropRepeats(SigList,"InRelease");
  625. _error->RevertToStack();
  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. /*}}}*/