apt-cdrom.cc 21 KB

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