cdrom.cc 22 KB

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