apt-cdrom.cc 23 KB

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