apt-cdrom.cc 23 KB

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