apt-cdrom.cc 22 KB

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