cdrom.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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 "indexcopy.h"
  21. using namespace std;
  22. // FindPackages - Find the package files on the CDROM /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* We look over the cdrom for package files. This is a recursive
  25. search that short circuits when it his a package file in the dir.
  26. This speeds it up greatly as the majority of the size is in the
  27. binary-* sub dirs. */
  28. bool pkgCdrom::FindPackages(string CD,
  29. vector<string> &List,
  30. vector<string> &SList,
  31. vector<string> &SigList,
  32. vector<string> &TransList,
  33. string &InfoDir, pkgCdromStatus *log,
  34. unsigned int Depth)
  35. {
  36. static ino_t Inodes[9];
  37. DIR *D;
  38. // if we have a look we "pulse" now
  39. if(log)
  40. log->Update();
  41. if (Depth >= 7)
  42. return true;
  43. if (CD[CD.length()-1] != '/')
  44. CD += '/';
  45. if (chdir(CD.c_str()) != 0)
  46. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  47. // Look for a .disk subdirectory
  48. struct stat Buf;
  49. if (stat(".disk",&Buf) == 0)
  50. {
  51. if (InfoDir.empty() == true)
  52. InfoDir = CD + ".disk/";
  53. }
  54. // Don't look into directories that have been marked to ingore.
  55. if (stat(".aptignr",&Buf) == 0)
  56. return true;
  57. /* Check _first_ for a signature file as apt-cdrom assumes that all files
  58. under a Packages/Source file are in control of that file and stops
  59. the scanning
  60. */
  61. if (stat("Release.gpg",&Buf) == 0)
  62. {
  63. SigList.push_back(CD);
  64. }
  65. /* Aha! We found some package files. We assume that everything under
  66. this dir is controlled by those package files so we don't look down
  67. anymore */
  68. if (stat("Packages",&Buf) == 0 || stat("Packages.gz",&Buf) == 0)
  69. {
  70. List.push_back(CD);
  71. // Continue down if thorough is given
  72. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  73. return true;
  74. }
  75. if (stat("Sources.gz",&Buf) == 0 || stat("Sources",&Buf) == 0)
  76. {
  77. SList.push_back(CD);
  78. // Continue down if thorough is given
  79. if (_config->FindB("APT::CDROM::Thorough",false) == false)
  80. return true;
  81. }
  82. // see if we find translatin indexes
  83. if (stat("i18n",&Buf) == 0)
  84. {
  85. D = opendir("i18n");
  86. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  87. {
  88. if(strstr(Dir->d_name,"Translation") != NULL)
  89. {
  90. if (_config->FindB("Debug::aptcdrom",false) == true)
  91. std::clog << "found translations: " << Dir->d_name << "\n";
  92. string file = Dir->d_name;
  93. if(file.substr(file.size()-3,file.size()) == ".gz")
  94. file = file.substr(0,file.size()-3);
  95. TransList.push_back(CD+"i18n/"+ file);
  96. }
  97. }
  98. closedir(D);
  99. }
  100. D = opendir(".");
  101. if (D == 0)
  102. return _error->Errno("opendir","Unable to read %s",CD.c_str());
  103. // Run over the directory
  104. for (struct dirent *Dir = readdir(D); Dir != 0; Dir = readdir(D))
  105. {
  106. // Skip some files..
  107. if (strcmp(Dir->d_name,".") == 0 ||
  108. strcmp(Dir->d_name,"..") == 0 ||
  109. //strcmp(Dir->d_name,"source") == 0 ||
  110. strcmp(Dir->d_name,".disk") == 0 ||
  111. strcmp(Dir->d_name,"experimental") == 0 ||
  112. strcmp(Dir->d_name,"binary-all") == 0 ||
  113. strcmp(Dir->d_name,"debian-installer") == 0)
  114. continue;
  115. // See if the name is a sub directory
  116. struct stat Buf;
  117. if (stat(Dir->d_name,&Buf) != 0)
  118. continue;
  119. if (S_ISDIR(Buf.st_mode) == 0)
  120. continue;
  121. unsigned int I;
  122. for (I = 0; I != Depth; I++)
  123. if (Inodes[I] == Buf.st_ino)
  124. break;
  125. if (I != Depth)
  126. continue;
  127. // Store the inodes weve seen
  128. Inodes[Depth] = Buf.st_ino;
  129. // Descend
  130. if (FindPackages(CD + Dir->d_name,List,SList,SigList,TransList,InfoDir,log,Depth+1) == false)
  131. break;
  132. if (chdir(CD.c_str()) != 0)
  133. return _error->Errno("chdir","Unable to change to %s",CD.c_str());
  134. };
  135. closedir(D);
  136. return !_error->PendingError();
  137. }
  138. // Score - We compute a 'score' for a path /*{{{*/
  139. // ---------------------------------------------------------------------
  140. /* Paths are scored based on how close they come to what I consider
  141. normal. That is ones that have 'dist' 'stable' 'testing' will score
  142. higher than ones without. */
  143. int pkgCdrom::Score(string Path)
  144. {
  145. int Res = 0;
  146. if (Path.find("stable/") != string::npos)
  147. Res += 29;
  148. if (Path.find("/binary-") != string::npos)
  149. Res += 20;
  150. if (Path.find("testing/") != string::npos)
  151. Res += 28;
  152. if (Path.find("unstable/") != string::npos)
  153. Res += 27;
  154. if (Path.find("/dists/") != string::npos)
  155. Res += 40;
  156. if (Path.find("/main/") != string::npos)
  157. Res += 20;
  158. if (Path.find("/contrib/") != string::npos)
  159. Res += 20;
  160. if (Path.find("/non-free/") != string::npos)
  161. Res += 20;
  162. if (Path.find("/non-US/") != string::npos)
  163. Res += 20;
  164. if (Path.find("/source/") != string::npos)
  165. Res += 10;
  166. if (Path.find("/debian/") != string::npos)
  167. Res -= 10;
  168. // check for symlinks in the patch leading to the actual file
  169. // a symlink gets a big penalty
  170. struct stat Buf;
  171. string statPath = flNotFile(Path);
  172. string cdromPath = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  173. while(statPath != cdromPath && statPath != "./") {
  174. statPath.resize(statPath.size()-1); // remove the trailing '/'
  175. if (lstat(statPath.c_str(),&Buf) == 0) {
  176. if(S_ISLNK(Buf.st_mode)) {
  177. Res -= 60;
  178. break;
  179. }
  180. }
  181. statPath = flNotFile(statPath); // descent
  182. }
  183. return Res;
  184. }
  185. /*}}}*/
  186. // DropBinaryArch - Dump dirs with a string like /binary-<foo>/ /*{{{*/
  187. // ---------------------------------------------------------------------
  188. /* Here we drop everything that is not this machines arch */
  189. bool pkgCdrom::DropBinaryArch(vector<string> &List)
  190. {
  191. char S[300];
  192. snprintf(S,sizeof(S),"/binary-%s/",
  193. _config->Find("Apt::Architecture").c_str());
  194. for (unsigned int I = 0; I < List.size(); I++)
  195. {
  196. const char *Str = List[I].c_str();
  197. const char *Res;
  198. if ((Res = strstr(Str,"/binary-")) == 0)
  199. continue;
  200. // Weird, remove it.
  201. if (strlen(Res) < strlen(S))
  202. {
  203. List.erase(List.begin() + I);
  204. I--;
  205. continue;
  206. }
  207. // See if it is our arch
  208. if (stringcmp(Res,Res + strlen(S),S) == 0)
  209. continue;
  210. // Erase it
  211. List.erase(List.begin() + I);
  212. I--;
  213. }
  214. return true;
  215. }
  216. // DropRepeats - Drop repeated files resulting from symlinks /*{{{*/
  217. // ---------------------------------------------------------------------
  218. /* Here we go and stat every file that we found and strip dup inodes. */
  219. bool pkgCdrom::DropRepeats(vector<string> &List,const char *Name)
  220. {
  221. // Get a list of all the inodes
  222. ino_t *Inodes = new ino_t[List.size()];
  223. for (unsigned int I = 0; I != List.size(); I++)
  224. {
  225. struct stat Buf;
  226. if (stat((List[I] + Name).c_str(),&Buf) != 0 &&
  227. stat((List[I] + Name + ".gz").c_str(),&Buf) != 0)
  228. _error->Errno("stat","Failed to stat %s%s",List[I].c_str(),
  229. Name);
  230. Inodes[I] = Buf.st_ino;
  231. }
  232. if (_error->PendingError() == true)
  233. return false;
  234. // Look for dups
  235. for (unsigned int I = 0; I != List.size(); I++)
  236. {
  237. for (unsigned int J = I+1; J < List.size(); J++)
  238. {
  239. // No match
  240. if (Inodes[J] != Inodes[I])
  241. continue;
  242. // We score the two paths.. and erase one
  243. int ScoreA = Score(List[I]);
  244. int ScoreB = Score(List[J]);
  245. if (ScoreA < ScoreB)
  246. {
  247. List[I] = string();
  248. break;
  249. }
  250. List[J] = string();
  251. }
  252. }
  253. // Wipe erased entries
  254. for (unsigned int I = 0; I < List.size();)
  255. {
  256. if (List[I].empty() == false)
  257. I++;
  258. else
  259. List.erase(List.begin()+I);
  260. }
  261. return true;
  262. }
  263. /*}}}*/
  264. // ReduceSourceList - Takes the path list and reduces it /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* This takes the list of source list expressed entires and collects
  267. similar ones to form a single entry for each dist */
  268. void pkgCdrom::ReduceSourcelist(string CD,vector<string> &List)
  269. {
  270. sort(List.begin(),List.end());
  271. // Collect similar entries
  272. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  273. {
  274. // Find a space..
  275. string::size_type Space = (*I).find(' ');
  276. if (Space == string::npos)
  277. continue;
  278. string::size_type SSpace = (*I).find(' ',Space + 1);
  279. if (SSpace == string::npos)
  280. continue;
  281. string Word1 = string(*I,Space,SSpace-Space);
  282. string Prefix = string(*I,0,Space);
  283. for (vector<string>::iterator J = List.begin(); J != I; J++)
  284. {
  285. // Find a space..
  286. string::size_type Space2 = (*J).find(' ');
  287. if (Space2 == string::npos)
  288. continue;
  289. string::size_type SSpace2 = (*J).find(' ',Space2 + 1);
  290. if (SSpace2 == string::npos)
  291. continue;
  292. if (string(*J,0,Space2) != Prefix)
  293. continue;
  294. if (string(*J,Space2,SSpace2-Space2) != Word1)
  295. continue;
  296. *J += string(*I,SSpace);
  297. *I = string();
  298. }
  299. }
  300. // Wipe erased entries
  301. for (unsigned int I = 0; I < List.size();)
  302. {
  303. if (List[I].empty() == false)
  304. I++;
  305. else
  306. List.erase(List.begin()+I);
  307. }
  308. }
  309. /*}}}*/
  310. // WriteDatabase - Write the CDROM Database file /*{{{*/
  311. // ---------------------------------------------------------------------
  312. /* We rewrite the configuration class associated with the cdrom database. */
  313. bool pkgCdrom::WriteDatabase(Configuration &Cnf)
  314. {
  315. string DFile = _config->FindFile("Dir::State::cdroms");
  316. string NewFile = DFile + ".new";
  317. unlink(NewFile.c_str());
  318. ofstream Out(NewFile.c_str());
  319. if (!Out)
  320. return _error->Errno("ofstream::ofstream",
  321. "Failed to open %s.new",DFile.c_str());
  322. /* Write out all of the configuration directives by walking the
  323. configuration tree */
  324. const Configuration::Item *Top = Cnf.Tree(0);
  325. for (; Top != 0;)
  326. {
  327. // Print the config entry
  328. if (Top->Value.empty() == false)
  329. Out << Top->FullTag() + " \"" << Top->Value << "\";" << endl;
  330. if (Top->Child != 0)
  331. {
  332. Top = Top->Child;
  333. continue;
  334. }
  335. while (Top != 0 && Top->Next == 0)
  336. Top = Top->Parent;
  337. if (Top != 0)
  338. Top = Top->Next;
  339. }
  340. Out.close();
  341. rename(DFile.c_str(),string(DFile + '~').c_str());
  342. if (rename(NewFile.c_str(),DFile.c_str()) != 0)
  343. return _error->Errno("rename","Failed to rename %s.new to %s",
  344. DFile.c_str(),DFile.c_str());
  345. return true;
  346. }
  347. /*}}}*/
  348. // WriteSourceList - Write an updated sourcelist /*{{{*/
  349. // ---------------------------------------------------------------------
  350. /* This reads the old source list and copies it into the new one. It
  351. appends the new CDROM entires just after the first block of comments.
  352. This places them first in the file. It also removes any old entries
  353. that were the same. */
  354. bool pkgCdrom::WriteSourceList(string Name,vector<string> &List,bool Source)
  355. {
  356. if (List.size() == 0)
  357. return true;
  358. string File = _config->FindFile("Dir::Etc::sourcelist");
  359. // Open the stream for reading
  360. ifstream F((FileExists(File)?File.c_str():"/dev/null"),
  361. ios::in );
  362. if (!F != 0)
  363. return _error->Errno("ifstream::ifstream","Opening %s",File.c_str());
  364. string NewFile = File + ".new";
  365. unlink(NewFile.c_str());
  366. ofstream Out(NewFile.c_str());
  367. if (!Out)
  368. return _error->Errno("ofstream::ofstream",
  369. "Failed to open %s.new",File.c_str());
  370. // Create a short uri without the path
  371. string ShortURI = "cdrom:[" + Name + "]/";
  372. string ShortURI2 = "cdrom:" + Name + "/"; // For Compatibility
  373. string Type;
  374. if (Source == true)
  375. Type = "deb-src";
  376. else
  377. Type = "deb";
  378. char Buffer[300];
  379. int CurLine = 0;
  380. bool First = true;
  381. while (F.eof() == false)
  382. {
  383. F.getline(Buffer,sizeof(Buffer));
  384. CurLine++;
  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. return _error->Error("Unable to locate any package files, perhaps this is not a Debian Disc");
  580. // Check if the CD is in the database
  581. string Name;
  582. if (Database.Exists("CD::" + ID) == false ||
  583. _config->FindB("APT::CDROM::Rename",false) == true)
  584. {
  585. // Try to use the CDs label if at all possible
  586. if (InfoDir.empty() == false &&
  587. FileExists(InfoDir + "/info") == true)
  588. {
  589. ifstream F(string(InfoDir + "/info").c_str());
  590. if (!F == 0)
  591. getline(F,Name);
  592. if (Name.empty() == false)
  593. {
  594. // Escape special characters
  595. string::iterator J = Name.begin();
  596. for (; J != Name.end(); J++)
  597. if (*J == '"' || *J == ']' || *J == '[')
  598. *J = '_';
  599. if(log) {
  600. msg.str("");
  601. ioprintf(msg, "Found label '%s'\n", Name.c_str());
  602. log->Update(msg.str());
  603. }
  604. Database.Set("CD::" + ID + "::Label",Name);
  605. }
  606. }
  607. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  608. Name.empty() == true)
  609. {
  610. if(!log)
  611. return _error->Error("No disc name found and no way to ask for it");
  612. while(true) {
  613. if(!log->AskCdromName(Name)) {
  614. // user canceld
  615. return false;
  616. }
  617. cout << "Name: '" << Name << "'" << endl;
  618. if (Name.empty() == false &&
  619. Name.find('"') == string::npos &&
  620. Name.find('[') == string::npos &&
  621. Name.find(']') == string::npos)
  622. break;
  623. log->Update(_("That is not a valid name, try again.\n"));
  624. }
  625. }
  626. }
  627. else
  628. Name = Database.Find("CD::" + ID);
  629. // Escape special characters
  630. string::iterator J = Name.begin();
  631. for (; J != Name.end(); J++)
  632. if (*J == '"' || *J == ']' || *J == '[')
  633. *J = '_';
  634. Database.Set("CD::" + ID,Name);
  635. if(log) {
  636. msg.str("");
  637. ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  638. log->Update(msg.str());
  639. }
  640. log->Update(_("Copying package lists..."), STEP_COPY);
  641. // take care of the signatures and copy them if they are ok
  642. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  643. SigVerify SignVerify;
  644. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  645. // Copy the package files to the state directory
  646. PackageCopy Copy;
  647. SourceCopy SrcCopy;
  648. TranslationsCopy TransCopy;
  649. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  650. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  651. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  652. return false;
  653. // reduce the List so that it takes less space in sources.list
  654. ReduceSourcelist(CDROM,List);
  655. ReduceSourcelist(CDROM,SourceList);
  656. // Write the database and sourcelist
  657. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  658. {
  659. if (WriteDatabase(Database) == false)
  660. return false;
  661. if(log) {
  662. log->Update(_("Writing new source list\n"), STEP_WRITE);
  663. }
  664. if (WriteSourceList(Name,List,false) == false ||
  665. WriteSourceList(Name,SourceList,true) == false)
  666. return false;
  667. }
  668. // Print the sourcelist entries
  669. if(log)
  670. log->Update(_("Source list entries for this disc are:\n"));
  671. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  672. {
  673. string::size_type Space = (*I).find(' ');
  674. if (Space == string::npos)
  675. return _error->Error("Internal error");
  676. if(log) {
  677. msg.str("");
  678. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  679. " " << string(*I,Space+1) << endl;
  680. log->Update(msg.str());
  681. }
  682. }
  683. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  684. {
  685. string::size_type Space = (*I).find(' ');
  686. if (Space == string::npos)
  687. return _error->Error("Internal error");
  688. if(log) {
  689. msg.str("");
  690. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  691. " " << string(*I,Space+1) << endl;
  692. log->Update(msg.str());
  693. }
  694. }
  695. // Unmount and finish
  696. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  697. log->Update(_("Unmounting CD-ROM..."), STEP_LAST);
  698. UnmountCdrom(CDROM);
  699. }
  700. return true;
  701. }