apt-cdrom.cc 21 KB

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