apt-cdrom.cc 22 KB

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