apt-cdrom.cc 22 KB

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