cdrom.cc 30 KB

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