cdrom.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. /*}}}*/
  137. // Score - We compute a 'score' for a path /*{{{*/
  138. // ---------------------------------------------------------------------
  139. /* Paths are scored based on how close they come to what I consider
  140. normal. That is ones that have 'dist' 'stable' 'testing' will score
  141. higher than ones without. */
  142. int pkgCdrom::Score(string Path)
  143. {
  144. int Res = 0;
  145. if (Path.find("stable/") != string::npos)
  146. Res += 29;
  147. if (Path.find("/binary-") != string::npos)
  148. Res += 20;
  149. if (Path.find("testing/") != string::npos)
  150. Res += 28;
  151. if (Path.find("unstable/") != string::npos)
  152. Res += 27;
  153. if (Path.find("/dists/") != string::npos)
  154. Res += 40;
  155. if (Path.find("/main/") != string::npos)
  156. Res += 20;
  157. if (Path.find("/contrib/") != string::npos)
  158. Res += 20;
  159. if (Path.find("/non-free/") != string::npos)
  160. Res += 20;
  161. if (Path.find("/non-US/") != string::npos)
  162. Res += 20;
  163. if (Path.find("/source/") != string::npos)
  164. Res += 10;
  165. if (Path.find("/debian/") != string::npos)
  166. Res -= 10;
  167. // check for symlinks in the patch leading to the actual file
  168. // a symlink gets a big penalty
  169. struct stat Buf;
  170. string statPath = flNotFile(Path);
  171. string cdromPath = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  172. while(statPath != cdromPath && statPath != "./") {
  173. statPath.resize(statPath.size()-1); // remove the trailing '/'
  174. if (lstat(statPath.c_str(),&Buf) == 0) {
  175. if(S_ISLNK(Buf.st_mode)) {
  176. Res -= 60;
  177. break;
  178. }
  179. }
  180. statPath = flNotFile(statPath); // descent
  181. }
  182. return Res;
  183. }
  184. /*}}}*/
  185. // DropBinaryArch - Dump dirs with a string like /binary-<foo>/ /*{{{*/
  186. // ---------------------------------------------------------------------
  187. /* Here we drop everything that is not this machines arch */
  188. bool pkgCdrom::DropBinaryArch(vector<string> &List)
  189. {
  190. char S[300];
  191. snprintf(S,sizeof(S),"/binary-%s/",
  192. _config->Find("Apt::Architecture").c_str());
  193. for (unsigned int I = 0; I < List.size(); I++)
  194. {
  195. const char *Str = List[I].c_str();
  196. const char *Res;
  197. if ((Res = strstr(Str,"/binary-")) == 0)
  198. continue;
  199. // Weird, remove it.
  200. if (strlen(Res) < strlen(S))
  201. {
  202. List.erase(List.begin() + I);
  203. I--;
  204. continue;
  205. }
  206. // See if it is our arch
  207. if (stringcmp(Res,Res + strlen(S),S) == 0)
  208. continue;
  209. // Erase it
  210. List.erase(List.begin() + I);
  211. I--;
  212. }
  213. return true;
  214. }
  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. if (F.fail() && !F.eof())
  386. return _error->Error(_("Line %u too long in source list %s."),
  387. CurLine,File.c_str());
  388. _strtabexpand(Buffer,sizeof(Buffer));
  389. _strstrip(Buffer);
  390. // Comment or blank
  391. if (Buffer[0] == '#' || Buffer[0] == 0)
  392. {
  393. Out << Buffer << endl;
  394. continue;
  395. }
  396. if (First == true)
  397. {
  398. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  399. {
  400. string::size_type Space = (*I).find(' ');
  401. if (Space == string::npos)
  402. return _error->Error("Internal error");
  403. Out << Type << " cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  404. " " << string(*I,Space+1) << endl;
  405. }
  406. }
  407. First = false;
  408. // Grok it
  409. string cType;
  410. string URI;
  411. const char *C = Buffer;
  412. if (ParseQuoteWord(C,cType) == false ||
  413. ParseQuoteWord(C,URI) == false)
  414. {
  415. Out << Buffer << endl;
  416. continue;
  417. }
  418. // Emit lines like this one
  419. if (cType != Type || (string(URI,0,ShortURI.length()) != ShortURI &&
  420. string(URI,0,ShortURI.length()) != ShortURI2))
  421. {
  422. Out << Buffer << endl;
  423. continue;
  424. }
  425. }
  426. // Just in case the file was empty
  427. if (First == true)
  428. {
  429. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  430. {
  431. string::size_type Space = (*I).find(' ');
  432. if (Space == string::npos)
  433. return _error->Error("Internal error");
  434. Out << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  435. " " << string(*I,Space+1) << endl;
  436. }
  437. }
  438. Out.close();
  439. rename(File.c_str(),string(File + '~').c_str());
  440. if (rename(NewFile.c_str(),File.c_str()) != 0)
  441. return _error->Errno("rename","Failed to rename %s.new to %s",
  442. File.c_str(),File.c_str());
  443. return true;
  444. }
  445. /*}}}*/
  446. bool pkgCdrom::Ident(string &ident, pkgCdromStatus *log) /*{{{*/
  447. {
  448. stringstream msg;
  449. // Startup
  450. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  451. if (CDROM[0] == '.')
  452. CDROM= SafeGetCWD() + '/' + CDROM;
  453. if(log) {
  454. msg.str("");
  455. ioprintf(msg, _("Using CD-ROM mount point %s\nMounting CD-ROM\n"),
  456. CDROM.c_str());
  457. log->Update(msg.str());
  458. }
  459. if (MountCdrom(CDROM) == false)
  460. return _error->Error("Failed to mount the cdrom.");
  461. // Hash the CD to get an ID
  462. if(log)
  463. log->Update(_("Identifying.. "));
  464. if (IdentCdrom(CDROM,ident) == false)
  465. {
  466. ident = "";
  467. return false;
  468. }
  469. msg.str("");
  470. ioprintf(msg, "[%s]\n",ident.c_str());
  471. log->Update(msg.str());
  472. // Read the database
  473. Configuration Database;
  474. string DFile = _config->FindFile("Dir::State::cdroms");
  475. if (FileExists(DFile) == true)
  476. {
  477. if (ReadConfigFile(Database,DFile) == false)
  478. return _error->Error("Unable to read the cdrom database %s",
  479. DFile.c_str());
  480. }
  481. if(log) {
  482. msg.str("");
  483. ioprintf(msg, _("Stored label: %s\n"),
  484. Database.Find("CD::"+ident).c_str());
  485. log->Update(msg.str());
  486. }
  487. // Unmount and finish
  488. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  489. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  490. UnmountCdrom(CDROM);
  491. }
  492. return true;
  493. }
  494. /*}}}*/
  495. bool pkgCdrom::Add(pkgCdromStatus *log) /*{{{*/
  496. {
  497. stringstream msg;
  498. // Startup
  499. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  500. if (CDROM[0] == '.')
  501. CDROM= SafeGetCWD() + '/' + CDROM;
  502. if(log) {
  503. log->SetTotal(STEP_LAST);
  504. msg.str("");
  505. ioprintf(msg, _("Using CD-ROM mount point %s\n"), CDROM.c_str());
  506. log->Update(msg.str(), STEP_PREPARE);
  507. }
  508. // Read the database
  509. Configuration Database;
  510. string DFile = _config->FindFile("Dir::State::cdroms");
  511. if (FileExists(DFile) == true)
  512. {
  513. if (ReadConfigFile(Database,DFile) == false)
  514. return _error->Error("Unable to read the cdrom database %s",
  515. DFile.c_str());
  516. }
  517. // Unmount the CD and get the user to put in the one they want
  518. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  519. {
  520. if(log)
  521. log->Update(_("Unmounting CD-ROM\n"), STEP_UNMOUNT);
  522. UnmountCdrom(CDROM);
  523. if(log) {
  524. log->Update(_("Waiting for disc...\n"), STEP_WAIT);
  525. if(!log->ChangeCdrom()) {
  526. // user aborted
  527. return false;
  528. }
  529. }
  530. // Mount the new CDROM
  531. log->Update(_("Mounting CD-ROM...\n"), STEP_MOUNT);
  532. if (MountCdrom(CDROM) == false)
  533. return _error->Error("Failed to mount the cdrom.");
  534. }
  535. // Hash the CD to get an ID
  536. if(log)
  537. log->Update(_("Identifying.. "), STEP_IDENT);
  538. string ID;
  539. if (IdentCdrom(CDROM,ID) == false)
  540. {
  541. log->Update("\n");
  542. return false;
  543. }
  544. if(log)
  545. log->Update("["+ID+"]\n");
  546. if(log)
  547. log->Update(_("Scanning disc for index files..\n"),STEP_SCAN);
  548. // Get the CD structure
  549. vector<string> List;
  550. vector<string> SourceList;
  551. vector<string> SigList;
  552. vector<string> TransList;
  553. string StartDir = SafeGetCWD();
  554. string InfoDir;
  555. if (FindPackages(CDROM,List,SourceList, SigList,TransList,InfoDir,log) == false)
  556. {
  557. log->Update("\n");
  558. return false;
  559. }
  560. chdir(StartDir.c_str());
  561. if (_config->FindB("Debug::aptcdrom",false) == true)
  562. {
  563. cout << "I found (binary):" << endl;
  564. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  565. cout << *I << endl;
  566. cout << "I found (source):" << endl;
  567. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  568. cout << *I << endl;
  569. cout << "I found (Signatures):" << endl;
  570. for (vector<string>::iterator I = SigList.begin(); I != SigList.end(); I++)
  571. cout << *I << endl;
  572. }
  573. //log->Update(_("Cleaning package lists..."), STEP_CLEAN);
  574. // Fix up the list
  575. DropBinaryArch(List);
  576. DropRepeats(List,"Packages");
  577. DropRepeats(SourceList,"Sources");
  578. DropRepeats(SigList,"Release.gpg");
  579. DropRepeats(TransList,"");
  580. if(log) {
  581. msg.str("");
  582. ioprintf(msg, _("Found %zu package indexes, %zu source indexes, "
  583. "%zu translation indexes and %zu signatures\n"),
  584. List.size(), SourceList.size(), TransList.size(),
  585. SigList.size());
  586. log->Update(msg.str(), STEP_SCAN);
  587. }
  588. if (List.size() == 0 && SourceList.size() == 0)
  589. {
  590. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  591. UnmountCdrom(CDROM);
  592. return _error->Error("Unable to locate any package files, perhaps this is not a Debian Disc");
  593. }
  594. // Check if the CD is in the database
  595. string Name;
  596. if (Database.Exists("CD::" + ID) == false ||
  597. _config->FindB("APT::CDROM::Rename",false) == true)
  598. {
  599. // Try to use the CDs label if at all possible
  600. if (InfoDir.empty() == false &&
  601. FileExists(InfoDir + "/info") == true)
  602. {
  603. ifstream F(string(InfoDir + "/info").c_str());
  604. if (!F == 0)
  605. getline(F,Name);
  606. if (Name.empty() == false)
  607. {
  608. // Escape special characters
  609. string::iterator J = Name.begin();
  610. for (; J != Name.end(); J++)
  611. if (*J == '"' || *J == ']' || *J == '[')
  612. *J = '_';
  613. if(log) {
  614. msg.str("");
  615. ioprintf(msg, _("Found label '%s'\n"), Name.c_str());
  616. log->Update(msg.str());
  617. }
  618. Database.Set("CD::" + ID + "::Label",Name);
  619. }
  620. }
  621. if (_config->FindB("APT::CDROM::Rename",false) == true ||
  622. Name.empty() == true)
  623. {
  624. if(!log)
  625. {
  626. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  627. UnmountCdrom(CDROM);
  628. return _error->Error("No disc name found and no way to ask for it");
  629. }
  630. while(true) {
  631. if(!log->AskCdromName(Name)) {
  632. // user canceld
  633. return false;
  634. }
  635. cout << "Name: '" << Name << "'" << endl;
  636. if (Name.empty() == false &&
  637. Name.find('"') == string::npos &&
  638. Name.find('[') == string::npos &&
  639. Name.find(']') == string::npos)
  640. break;
  641. log->Update(_("That is not a valid name, try again.\n"));
  642. }
  643. }
  644. }
  645. else
  646. Name = Database.Find("CD::" + ID);
  647. // Escape special characters
  648. string::iterator J = Name.begin();
  649. for (; J != Name.end(); J++)
  650. if (*J == '"' || *J == ']' || *J == '[')
  651. *J = '_';
  652. Database.Set("CD::" + ID,Name);
  653. if(log) {
  654. msg.str("");
  655. ioprintf(msg, _("This disc is called: \n'%s'\n"), Name.c_str());
  656. log->Update(msg.str());
  657. }
  658. log->Update(_("Copying package lists..."), STEP_COPY);
  659. // take care of the signatures and copy them if they are ok
  660. // (we do this before PackageCopy as it modifies "List" and "SourceList")
  661. SigVerify SignVerify;
  662. SignVerify.CopyAndVerify(CDROM, Name, SigList, List, SourceList);
  663. // Copy the package files to the state directory
  664. PackageCopy Copy;
  665. SourceCopy SrcCopy;
  666. TranslationsCopy TransCopy;
  667. if (Copy.CopyPackages(CDROM,Name,List, log) == false ||
  668. SrcCopy.CopyPackages(CDROM,Name,SourceList, log) == false ||
  669. TransCopy.CopyTranslations(CDROM,Name,TransList, log) == false)
  670. return false;
  671. // reduce the List so that it takes less space in sources.list
  672. ReduceSourcelist(CDROM,List);
  673. ReduceSourcelist(CDROM,SourceList);
  674. // Write the database and sourcelist
  675. if (_config->FindB("APT::cdrom::NoAct",false) == false)
  676. {
  677. if (WriteDatabase(Database) == false)
  678. return false;
  679. if(log) {
  680. log->Update(_("Writing new source list\n"), STEP_WRITE);
  681. }
  682. if (WriteSourceList(Name,List,false) == false ||
  683. WriteSourceList(Name,SourceList,true) == false)
  684. return false;
  685. }
  686. // Print the sourcelist entries
  687. if(log)
  688. log->Update(_("Source list entries for this disc are:\n"));
  689. for (vector<string>::iterator I = List.begin(); I != List.end(); I++)
  690. {
  691. string::size_type Space = (*I).find(' ');
  692. if (Space == string::npos)
  693. {
  694. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  695. UnmountCdrom(CDROM);
  696. return _error->Error("Internal error");
  697. }
  698. if(log) {
  699. msg.str("");
  700. msg << "deb cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  701. " " << string(*I,Space+1) << endl;
  702. log->Update(msg.str());
  703. }
  704. }
  705. for (vector<string>::iterator I = SourceList.begin(); I != SourceList.end(); I++)
  706. {
  707. string::size_type Space = (*I).find(' ');
  708. if (Space == string::npos)
  709. {
  710. if (_config->FindB("APT::CDROM::NoMount",false) == false)
  711. UnmountCdrom(CDROM);
  712. return _error->Error("Internal error");
  713. }
  714. if(log) {
  715. msg.str("");
  716. msg << "deb-src cdrom:[" << Name << "]/" << string(*I,0,Space) <<
  717. " " << string(*I,Space+1) << endl;
  718. log->Update(msg.str());
  719. }
  720. }
  721. // Unmount and finish
  722. if (_config->FindB("APT::CDROM::NoMount",false) == false) {
  723. log->Update(_("Unmounting CD-ROM...\n"), STEP_LAST);
  724. UnmountCdrom(CDROM);
  725. }
  726. return true;
  727. }
  728. /*}}}*/