cdrom.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. */
  3. #include<apt-pkg/init.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<sstream>
  9. #include<fstream>
  10. #include<config.h>
  11. #include<apti18n.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <dirent.h>
  15. #include <unistd.h>
  16. #include <stdio.h>
  17. #include <algorithm>
  18. #include "indexcopy.h"
  19. using namespace std;
  20. // FindPackages - Find the package files on the CDROM /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* We look over the cdrom for package files. This is a recursive
  23. search that short circuits when it his a package file in the dir.
  24. This speeds it up greatly as the majority of the size is in the
  25. binary-* sub dirs. */
  26. bool pkgCdrom::FindPackages(string CD,
  27. vector<string> &List,
  28. vector<string> &SList,
  29. vector<string> &SigList,
  30. vector<string> &TransList,
  31. string &InfoDir, pkgCdromStatus *log,
  32. unsigned int Depth)
  33. {
  34. static ino_t Inodes[9];
  35. DIR *D;
  36. // if we have a look we "pulse" now
  37. if(log)
  38. log->Update();
  39. if (Depth >= 7)
  40. return true;
  41. if (CD[CD.length()-1] != '/')
  42. CD += '/';
  43. if (chdir(CD.c_str()) != 0)
  44. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  45. // Look for a .disk subdirectory
  46. struct stat Buf;
  47. if (stat(".disk",&Buf) == 0)
  48. {
  49. if (InfoDir.empty() == true)
  50. InfoDir = CD + ".disk/";
  51. }
  52. // Don't look into directories that have been marked to ingore.
  53. if (stat(".aptignr",&Buf) == 0)
  54. return true;
  55. /* Check _first_ for a signature file as apt-cdrom assumes that all files
  56. under a Packages/Source file are in control of that file and stops
  57. the scanning
  58. */
  59. if (stat("Release.gpg",&Buf) == 0)
  60. {
  61. SigList.push_back(CD);
  62. }
  63. /* Aha! We found some package files. We assume that everything under
  64. this dir is controlled by those package files so we don't look down
  65. anymore */
  66. if (stat("Packages",&Buf) == 0 || stat("Packages.gz",&Buf) == 0)
  67. {
  68. List.push_back(CD);
  69. // Continue down if thorough is given
  70. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  71. return true;
  72. }
  73. if (stat("Sources.gz",&Buf) == 0 || stat("Sources",&Buf) == 0)
  74. {
  75. SList.push_back(CD);
  76. // Continue down if thorough is given
  77. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  78. return true;
  79. }
  80. // see if we find translatin indexes
  81. if (stat("i18n",&Buf) == 0)
  82. {
  83. D = opendir("i18n");
  84. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  85. {
  86. if(strstr(Dir->d_name,"Translation") != NULL)
  87. {
  88. if (_config->FindB("Debug::aptcdrom",false) == true)
  89. std::clog << "found translations: " << Dir->d_name << "\n";
  90. string file = Dir->d_name;
  91. if(file.substr(file.size()-3,file.size()) == ".gz")
  92. file = file.substr(0,file.size()-3);
  93. TransList.push_back(CD+"i18n/"+ file);
  94. }
  95. }
  96. closedir(D);
  97. }
  98. D = opendir(".");
  99. if (D == 0)
  100. return _error->Errno("opendir","Unable to read %s",CD.c_str());
  101. // Run over the directory
  102. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  103. {
  104. // Skip some files..
  105. if (strcmp(Dir->d_name,".") == 0 ||
  106. strcmp(Dir->d_name,"..") == 0 ||
  107. //strcmp(Dir->d_name,"source") == 0 ||
  108. strcmp(Dir->d_name,".disk") == 0 ||
  109. strcmp(Dir->d_name,"experimental") == 0 ||
  110. strcmp(Dir->d_name,"binary-all") == 0 ||
  111. strcmp(Dir->d_name,"debian-installer") == 0)
  112. continue;
  113. // See if the name is a sub directory
  114. struct stat Buf;
  115. if (stat(Dir->d_name,&Buf) != 0)
  116. continue;
  117. if (S_ISDIR(Buf.st_mode) == 0)
  118. continue;
  119. unsigned int I;
  120. for (I = 0; I != Depth; I++)
  121. if (Inodes[I] == Buf.st_ino)
  122. break;
  123. if (I != Depth)
  124. continue;
  125. // Store the inodes weve seen
  126. Inodes[Depth] = Buf.st_ino;
  127. // Descend
  128. if (FindPackages(CD + Dir->d_name,List,SList,SigList,TransList,InfoDir,log,Depth+1) == false)
  129. break;
  130. if (chdir(CD.c_str()) != 0)
  131. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  132. };
  133. closedir(D);
  134. return !_error->PendingError();
  135. }
  136. // Score - We compute a 'score' for a path /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* Paths are scored based on how close they come to what I consider
  139. normal. That is ones that have 'dist' 'stable' 'testing' will score
  140. higher than ones without. */
  141. int pkgCdrom::Score(string Path)
  142. {
  143. int Res = 0;
  144. if (Path.find("stable/") != string::npos)
  145. Res += 29;
  146. if (Path.find("/binary-") != string::npos)
  147. Res += 20;
  148. if (Path.find("testing/") != string::npos)
  149. Res += 28;
  150. if (Path.find("unstable/") != string::npos)
  151. Res += 27;
  152. if (Path.find("/dists/") != string::npos)
  153. Res += 40;
  154. if (Path.find("/main/") != string::npos)
  155. Res += 20;
  156. if (Path.find("/contrib/") != string::npos)
  157. Res += 20;
  158. if (Path.find("/non-free/") != string::npos)
  159. Res += 20;
  160. if (Path.find("/non-US/") != string::npos)
  161. Res += 20;
  162. if (Path.find("/source/") != string::npos)
  163. Res += 10;
  164. if (Path.find("/debian/") != string::npos)
  165. Res -= 10;
  166. // check for symlinks in the patch leading to the actual file
  167. // a symlink gets a big penalty
  168. struct stat Buf;
  169. string statPath = flNotFile(Path);
  170. string cdromPath = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  171. while(statPath != cdromPath && statPath != "./") {
  172. statPath.resize(statPath.size()-1); // remove the trailing '/'
  173. if (lstat(statPath.c_str(),&Buf) == 0) {
  174. if(S_ISLNK(Buf.st_mode)) {
  175. Res -= 60;
  176. break;
  177. }
  178. }
  179. statPath = flNotFile(statPath); // descent
  180. }
  181. return Res;
  182. }
  183. /*}}}*/
  184. // DropBinaryArch - Dump dirs with a string like /binary-<foo>/ /*{{{*/
  185. // ---------------------------------------------------------------------
  186. /* Here we drop everything that is not this machines arch */
  187. bool pkgCdrom::DropBinaryArch(vector<string> &List)
  188. {
  189. char S[300];
  190. snprintf(S,sizeof(S),"/binary-%s/",
  191. _config->Find("Apt::Architecture").c_str());
  192. for (unsigned int I = 0; I < List.size(); I++)
  193. {
  194. const char *Str = List[I].c_str();
  195. const char *Res;
  196. if ((Res = strstr(Str,"/binary-")) == 0)
  197. continue;
  198. // Weird, remove it.
  199. if (strlen(Res) < strlen(S))
  200. {
  201. List.erase(List.begin() + I);
  202. I--;
  203. continue;
  204. }
  205. // See if it is our arch
  206. if (stringcmp(Res,Res + strlen(S),S) == 0)
  207. continue;
  208. // Erase it
  209. List.erase(List.begin() + I);
  210. I--;
  211. }
  212. return true;
  213. }
  214. // DropRepeats - Drop repeated files resulting from symlinks /*{{{*/
  215. // ---------------------------------------------------------------------
  216. /* Here we go and stat every file that we found and strip dup inodes. */
  217. bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
  218. {
  219. // Get a list of all the inodes
  220. ino_t *Inodes = new ino_t[List.size()];
  221. for (unsigned int I = 0; I != List.size(); I++)
  222. {
  223. struct stat Buf;
  224. if (stat((List[I] + Name).c_str(),&Buf) != 0 &&
  225. stat((List[I] + Name + ".gz").c_str(),&Buf) != 0)
  226. _error->Errno("stat","Failed to stat %s%s",List[I].c_str(),
  227. Name);
  228. Inodes[I] = Buf.st_ino;
  229. }
  230. if (_error->PendingError() == true)
  231. return false;
  232. // Look for dups
  233. for (unsigned int I = 0; I != List.size(); I++)
  234. {
  235. for (unsigned int J = I+1; J < List.size(); J++)
  236. {
  237. // No match
  238. if (Inodes[J] != Inodes[I])
  239. continue;
  240. // We score the two paths.. and erase one
  241. int ScoreA = Score(List[I]);
  242. int ScoreB = Score(List[J]);
  243. if (ScoreA < ScoreB)
  244. {
  245. List[I] = string();
  246. break;
  247. }
  248. List[J] = string();
  249. }
  250. }
  251. // Wipe erased entries
  252. for (unsigned int I = 0; I < List.size();)
  253. {
  254. if (List[I].empty() == false)
  255. I++;
  256. else
  257. List.erase(List.begin()+I);
  258. }
  259. return true;
  260. }
  261. /*}}}*/
  262. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* This takes the list of source list expressed entires and collects
  265. similar ones to form a single entry for each dist */
  266. void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
  267. {
  268. sort(List.begin(),List.end());
  269. // Collect similar entries
  270. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  271. {
  272. // Find a space..
  273. string::size_type Space = (*I).find(' ');
  274. if (Space == string::npos)
  275. continue;
  276. string::size_type SSpace = (*I).find(' ',Space + 1);
  277. if (SSpace == string::npos)
  278. continue;
  279. string Word1 = string(*I,Space,SSpace-Space);
  280. string Prefix = string(*I,0,Space);
  281. for (vector<string>::iterator J = List.begin(); J != I; J++)
  282. {
  283. // Find a space..
  284. string::size_type Space2 = (*J).find(' ');
  285. if (Space2 == string::npos)
  286. continue;
  287. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  288. if (SSpace2 == string::npos)
  289. continue;
  290. if (string(*J,0,Space2) != Prefix)
  291. continue;
  292. if (string(*J,Space2,SSpace2-Space2) != Word1)
  293. continue;
  294. *J += string(*I,SSpace);
  295. *I = string();
  296. }
  297. }
  298. // Wipe erased entries
  299. for (unsigned int I = 0; I < List.size();)
  300. {
  301. if (List[I].empty() == false)
  302. I++;
  303. else
  304. List.erase(List.begin()+I);
  305. }
  306. }
  307. /*}}}*/
  308. // WriteDatabase - Write the CDROM Database file /*{{{*/
  309. // ---------------------------------------------------------------------
  310. /* We rewrite the configuration class associated with the cdrom database. */
  311. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  312. {
  313. string DFile = _config->FindFile("Dir::State::cdroms");
  314. string NewFile = DFile + ".new";
  315. unlink(NewFile.c_str());
  316. ofstream Out(NewFile.c_str());
  317. if (!Out)
  318. return _error->Errno("ofstream::ofstream",
  319. "Failed to open %s.new",DFile.c_str());
  320. /* Write out all of the configuration directives by walking the
  321. configuration tree */
  322. const Configuration::Item *Top = Cnf.Tree(0);
  323. for (; Top != 0;)
  324. {
  325. // Print the config entry
  326. if (Top->Value.empty() == false)
  327. Out << Top->FullTag() + " \"" << Top->Value << "\";" << endl;
  328. if (Top->Child != 0)
  329. {
  330. Top = Top->Child;
  331. continue;
  332. }
  333. while (Top != 0 && Top->Next == 0)
  334. Top = Top->Parent;
  335. if (Top != 0)
  336. Top = Top->Next;
  337. }
  338. Out.close();
  339. rename(DFile.c_str(),string(DFile + '~').c_str());
  340. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  341. return _error->Errno("rename","Failed to rename %s.new to %s",
  342. DFile.c_str(),DFile.c_str());
  343. return true;
  344. }
  345. /*}}}*/
  346. // WriteSourceList - Write an updated sourcelist /*{{{*/
  347. // ---------------------------------------------------------------------
  348. /* This reads the old source list and copies it into the new one. It
  349. appends the new CDROM entires just after the first block of comments.
  350. This places them first in the file. It also removes any old entries
  351. that were the same. */
  352. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  353. {
  354. if (List.size() == 0)
  355. return true;
  356. string File = _config->FindFile("Dir::Etc::sourcelist");
  357. // Open the stream for reading
  358. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  359. ios::in );
  360. if (!F != 0)
  361. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  362. string NewFile = File + ".new";
  363. unlink(NewFile.c_str());
  364. ofstream Out(NewFile.c_str());
  365. if (!Out)
  366. return _error->Errno("ofstream::ofstream",
  367. "Failed to open %s.new",File.c_str());
  368. // Create a short uri without the path
  369. string ShortURI = "cdrom:[" + Name + "]/";
  370. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  371. string Type;
  372. if (Source == true)
  373. Type = "deb-src";
  374. else
  375. Type = "deb";
  376. char Buffer[300];
  377. int CurLine = 0;
  378. bool First = true;
  379. while (F.eof() == false)
  380. {
  381. F.getline(Buffer,sizeof(Buffer));
  382. CurLine++;
  383. if (F.fail() && !F.eof())
  384. return _error->Error(_("Line %u too long in source list %s."),
  385. CurLine,File.c_str());
  386. _strtabexpand(Buffer,sizeof(Buffer));
  387. _strstrip(Buffer);
  388. // Comment or blank
  389. if (Buffer[0] == '#' || Buffer[0] == 0)
  390. {
  391. Out << Buffer << endl;
  392. continue;
  393. }
  394. if (First == true)
  395. {
  396. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  397. {
  398. string::size_type Space = (*I).find(' ');
  399. if (Space == string::npos)
  400. return _error->Error("Internal error");
  401. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  402. " " << string(*I,Space+1) << endl;
  403. }
  404. }
  405. First = false;
  406. // Grok it
  407. string cType;
  408. string URI;
  409. const char *C = Buffer;
  410. if (ParseQuoteWord(C,cType) == false ||
  411. ParseQuoteWord(C,URI) == false)
  412. {
  413. Out << Buffer << endl;
  414. continue;
  415. }
  416. // Emit lines like this one
  417. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  418. string(URI,0,ShortURI.length()) != ShortURI2))
  419. {
  420. Out << Buffer << endl;
  421. continue;
  422. }
  423. }
  424. // Just in case the file was empty
  425. if (First == true)
  426. {
  427. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  428. {
  429. string::size_type Space = (*I).find(' ');
  430. if (Space == string::npos)
  431. return _error->Error("Internal error");
  432. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  433. " " << string(*I,Space+1) << endl;
  434. }
  435. }
  436. Out.close();
  437. rename(File.c_str(),string(File + '~').c_str());
  438. if (rename(NewFile.c_str(),File.c_str()) != 0)
  439. return _error->Errno("rename","Failed to rename %s.new to %s",
  440. File.c_str(),File.c_str());
  441. return true;
  442. }
  443. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log)
  444. {
  445. stringstream msg;
  446. // Startup
  447. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  448. if (CDROM[0] == '.')
  449. CDROM= SafeGetCWD() + '/' + CDROM;
  450. if(log) {
  451. msg.str("");
  452. ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
  453. CDROM.c_str());
  454. log->Update(msg.str());
  455. }
  456. if (MountCdrom(CDROM) == false)
  457. return _error->Error("Failed to mount the cdrom.");
  458. // Hash the CD to get an ID
  459. if(log)
  460. log->Update(_("Identifying.. "));
  461. if (IdentCdrom(CDROM,ident) == false)
  462. {
  463. ident = "";
  464. return false;
  465. }
  466. msg.str("");
  467. ioprintf(msg, "[%s]\n",ident.c_str());
  468. log->Update(msg.str());
  469. // Read the database
  470. Configuration Database;
  471. string DFile = _config->FindFile("Dir::State::cdroms");
  472. if (FileExists(DFile) == true)
  473. {
  474. if (ReadConfigFile(Database,DFile) == false)
  475. return _error->Error("Unable to read the cdrom database %s",
  476. DFile.c_str());
  477. }
  478. if(log) {
  479. msg.str("");
  480. ioprintf(msg, _("Stored label: %s \n"),
  481. Database.Find("CD::"+ident).c_str());
  482. log->Update(msg.str());
  483. }
  484. return true;
  485. }
  486. bool pkgCdrom::Add(pkgCdromStatus *log)
  487. {
  488. stringstream msg;
  489. // Startup
  490. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  491. if (CDROM[0] == '.')
  492. CDROM= SafeGetCWD() + '/' + CDROM;
  493. if(log) {
  494. log->SetTotal(STEP_LAST);
  495. msg.str("");
  496. ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  497. log->Update(msg.str(), STEP_PREPARE);
  498. }
  499. // Read the database
  500. Configuration Database;
  501. string DFile = _config->FindFile("Dir::State::cdroms");
  502. if (FileExists(DFile) == true)
  503. {
  504. if (ReadConfigFile(Database,DFile) == false)
  505. return _error->Error("Unable to read the cdrom database %s",
  506. DFile.c_str());
  507. }
  508. // Unmount the CD and get the user to put in the one they want
  509. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  510. {
  511. if(log)
  512. log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
  513. UnmountCdrom(CDROM);
  514. if(log) {
  515. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  516. if(!log->ChangeCdrom()) {
  517. // user aborted
  518. return false;
  519. }
  520. }
  521. // Mount the new CDROM
  522. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  523. if (MountCdrom(CDROM) == false)
  524. return _error->Error("Failed to mount the cdrom.");
  525. }
  526. // Hash the CD to get an ID
  527. if(log)
  528. log->Update(_("Identifying.. "), STEP_IDENT);
  529. string ID;
  530. if (IdentCdrom(CDROM,ID) == false)
  531. {
  532. log->Update("\n");
  533. return false;
  534. }
  535. if(log)
  536. log->Update("["+ID+"]\n");
  537. if(log)
  538. log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
  539. // Get the CD structure
  540. vector<string> List;
  541. vector<string> SourceList;
  542. vector<string> SigList;
  543. vector<string> TransList;
  544. string StartDir = SafeGetCWD();
  545. string InfoDir;
  546. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  547. {
  548. log->Update("\n");
  549. return false;
  550. }
  551. chdir(StartDir.c_str());
  552. if (_config->FindB("Debug::aptcdrom",false) == true)
  553. {
  554. cout << "I found (binary):" << endl;
  555. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  556. cout << *I << endl;
  557. cout << "I found (source):" << endl;
  558. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  559. cout << *I << endl;
  560. cout << "I found (Signatures):" << endl;
  561. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); I++)
  562. cout << *I << endl;
  563. }
  564. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  565. // Fix up the list
  566. DropBinaryArch(List);
  567. DropRepeats(List,"Packages");
  568. DropRepeats(SourceList,"Sources");
  569. DropRepeats(SigList,"Release.gpg");
  570. DropRepeats(TransList,"");
  571. if(log) {
  572. msg.str("");
  573. ioprintf(msg, _("Found %i package indexes, %i source indexes, "
  574. "%i translation indexes and %i signatures\n"),
  575. List.size(), SourceList.size(), TransList.size(),
  576. SigList.size());
  577. log->Update(msg.str(), STEP_SCAN);
  578. }
  579. if (List.size() == 0 && SourceList.size() == 0)
  580. {
  581. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  582. UnmountCdrom(CDROM);
  583. return _error->Error("Unable to locate any package files, perhaps this is not a Debian Disc");
  584. }
  585. // Check if the CD is in the database
  586. string Name;
  587. if (Database.Exists("CD::" + ID) == false ||
  588. _config->FindB("APT::CDROM::Rename",false) == true)
  589. {
  590. // Try to use the CDs label if at all possible
  591. if (InfoDir.empty() == false &&
  592. FileExists(InfoDir + "/info") == true)
  593. {
  594. ifstream F(string(InfoDir + "/info").c_str());
  595. if (!F == 0)
  596. getline(F,Name);
  597. if (Name.empty() == false)
  598. {
  599. // Escape special characters
  600. string::iterator J = Name.begin();
  601. for (; J != Name.end(); J++)
  602. if (*J == '"' || *J == ']' || *J == '[')
  603. *J = '_';
  604. if(log) {
  605. msg.str("");
  606. ioprintf(msg, _("Found label '%s'\n"), Name.c_str());
  607. log->Update(msg.str());
  608. }
  609. Database.Set("CD::" + ID + "::Label",Name);
  610. }
  611. }
  612. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  613. Name.empty() == true)
  614. {
  615. if(!log)
  616. {
  617. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  618. UnmountCdrom(CDROM);
  619. return _error->Error("No disc name found and no way to ask for it");
  620. }
  621. while(true) {
  622. if(!log->AskCdromName(Name)) {
  623. // user canceld
  624. return false;
  625. }
  626. cout << "Name: '" << Name << "'" << endl;
  627. if (Name.empty() == false &&
  628. Name.find('"') == string::npos &&
  629. Name.find('[') == string::npos &&
  630. Name.find(']') == string::npos)
  631. break;
  632. log->Update(_("That is not a valid name, try again.\n"));
  633. }
  634. }
  635. }
  636. else
  637. Name = Database.Find("CD::" + ID);
  638. // Escape special characters
  639. string::iterator J = Name.begin();
  640. for (; J != Name.end(); J++)
  641. if (*J == '"' || *J == ']' || *J == '[')
  642. *J = '_';
  643. Database.Set("CD::" + ID,Name);
  644. if(log) {
  645. msg.str("");
  646. ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  647. log->Update(msg.str());
  648. }
  649. log->Update(_("Copying package lists..."), STEP_COPY);
  650. // take care of the signatures and copy them if they are ok
  651. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  652. SigVerify SignVerify;
  653. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  654. // Copy the package files to the state directory
  655. PackageCopy Copy;
  656. SourceCopy SrcCopy;
  657. TranslationsCopy TransCopy;
  658. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  659. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  660. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  661. return false;
  662. // reduce the List so that it takes less space in sources.list
  663. ReduceSourcelist(CDROM,List);
  664. ReduceSourcelist(CDROM,SourceList);
  665. // Write the database and sourcelist
  666. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  667. {
  668. if (WriteDatabase(Database) == false)
  669. return false;
  670. if(log) {
  671. log->Update(_("Writing new source list\n"), STEP_WRITE);
  672. }
  673. if (WriteSourceList(Name,List,false) == false ||
  674. WriteSourceList(Name,SourceList,true) == false)
  675. return false;
  676. }
  677. // Print the sourcelist entries
  678. if(log)
  679. log->Update(_("Source list entries for this disc are:\n"));
  680. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  681. {
  682. string::size_type Space = (*I).find(' ');
  683. if (Space == string::npos)
  684. {
  685. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  686. UnmountCdrom(CDROM);
  687. return _error->Error("Internal error");
  688. }
  689. if(log) {
  690. msg.str("");
  691. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  692. " " << string(*I,Space+1) << endl;
  693. log->Update(msg.str());
  694. }
  695. }
  696. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  697. {
  698. string::size_type Space = (*I).find(' ');
  699. if (Space == string::npos)
  700. {
  701. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  702. UnmountCdrom(CDROM);
  703. return _error->Error("Internal error");
  704. }
  705. if(log) {
  706. msg.str("");
  707. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  708. " " << string(*I,Space+1) << endl;
  709. log->Update(msg.str());
  710. }
  711. }
  712. // Unmount and finish
  713. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  714. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  715. UnmountCdrom(CDROM);
  716. }
  717. return true;
  718. }