cdrom.cc 22 KB

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