apt-cdrom.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cdrom.cc,v 1.45 2003/11/19 23:50:51 mdz 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 <apt-pkg/acquire.h>
  19. #include <apt-pkg/acquire-item.h>
  20. #include <apt-pkg/cdrom.h>
  21. #include <config.h>
  22. #include <apti18n.h>
  23. //#include "indexcopy.h"
  24. #include <locale.h>
  25. #include <iostream>
  26. #include <fstream>
  27. #include <vector>
  28. #include <algorithm>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include <dirent.h>
  32. #include <unistd.h>
  33. #include <stdio.h>
  34. /*}}}*/
  35. using namespace std;
  36. /*{{{*/
  37. class pkgCdromTextStatus : public pkgCdromStatus
  38. {
  39. protected:
  40. OpTextProgress Progress;
  41. void Prompt(const char *Text);
  42. string PromptLine(const char *Text);
  43. bool AskCdromName(string &name);
  44. public:
  45. virtual void Update(string text, int current);
  46. virtual bool ChangeCdrom();
  47. virtual OpProgress* GetOpProgress();
  48. };
  49. void pkgCdromTextStatus::Prompt(const char *Text)
  50. {
  51. char C;
  52. cout << Text << ' ' << flush;
  53. read(STDIN_FILENO,&C,1);
  54. if (C != '\n')
  55. cout << endl;
  56. }
  57. string pkgCdromTextStatus::PromptLine(const char *Text)
  58. {
  59. cout << Text << ':' << endl;
  60. string Res;
  61. getline(cin,Res);
  62. return Res;
  63. }
  64. bool pkgCdromTextStatus::AskCdromName(string &name)
  65. {
  66. cout << _("Please provide a name for this Disc, such as 'Debian 2.1r1 Disk 1'") << flush;
  67. name = PromptLine("");
  68. return true;
  69. }
  70. void pkgCdromTextStatus::Update(string text, int current)
  71. {
  72. if(text.size() > 0)
  73. cout << text << flush;
  74. }
  75. bool pkgCdromTextStatus::ChangeCdrom()
  76. {
  77. Prompt(_("Please insert a Disc in the drive and press enter"));
  78. return true;
  79. }
  80. OpProgress* pkgCdromTextStatus::GetOpProgress()
  81. {
  82. return &Progress;
  83. };
  84. /*}}}*/
  85. // DoAdd - Add a new CDROM /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* This does the main add bit.. We show some status and things. The
  88. sequence is to mount/umount the CD, Ident it then scan it for package
  89. files and reduce that list. Then we copy over the package files and
  90. verify them. Then rewrite the database files */
  91. bool DoAdd(CommandLine &)
  92. {
  93. bool res = false;
  94. pkgCdromTextStatus log;
  95. pkgCdrom cdrom;
  96. res = cdrom.Add(&log);
  97. if(res)
  98. cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
  99. return res;
  100. }
  101. /*}}}*/
  102. // DoIdent - Ident a CDROM /*{{{*/
  103. // ---------------------------------------------------------------------
  104. /* */
  105. bool DoIdent(CommandLine &)
  106. {
  107. string ident;
  108. pkgCdromTextStatus log;
  109. pkgCdrom cdrom;
  110. return cdrom.Ident(ident, &log);
  111. }
  112. /*}}}*/
  113. // ShowHelp - Show the help screen /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* */
  116. int ShowHelp()
  117. {
  118. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
  119. COMMON_ARCH,__DATE__,__TIME__);
  120. if (_config->FindB("version") == true)
  121. return 0;
  122. cout <<
  123. "Usage: apt-cdrom [options] command\n"
  124. "\n"
  125. "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
  126. "CDROM mount point and device information is taken from apt.conf\n"
  127. "and /etc/fstab.\n"
  128. "\n"
  129. "Commands:\n"
  130. " add - Add a CDROM\n"
  131. " ident - Report the identity of a CDROM\n"
  132. "\n"
  133. "Options:\n"
  134. " -h This help text\n"
  135. " -d CD-ROM mount point\n"
  136. " -r Rename a recognized CD-ROM\n"
  137. " -m No mounting\n"
  138. " -f Fast mode, don't check package files\n"
  139. " -a Thorough scan mode\n"
  140. " -c=? Read this configuration file\n"
  141. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  142. "See fstab(5)\n";
  143. return 0;
  144. }
  145. /*}}}*/
  146. int main(int argc,const char *argv[])
  147. {
  148. CommandLine::Args Args[] = {
  149. {'h',"help","help",0},
  150. {'v',"version","version",0},
  151. {'d',"cdrom","Acquire::cdrom::mount",CommandLine::HasArg},
  152. {'r',"rename","APT::CDROM::Rename",0},
  153. {'m',"no-mount","APT::CDROM::NoMount",0},
  154. {'f',"fast","APT::CDROM::Fast",0},
  155. {'n',"just-print","APT::CDROM::NoAct",0},
  156. {'n',"recon","APT::CDROM::NoAct",0},
  157. {'n',"no-act","APT::CDROM::NoAct",0},
  158. {'a',"thorough","APT::CDROM::Thorough",0},
  159. {'c',"config-file",0,CommandLine::ConfigFile},
  160. {'o',"option",0,CommandLine::ArbItem},
  161. {0,0,0,0}};
  162. CommandLine::Dispatch Cmds[] = {
  163. {"add",&DoAdd},
  164. {"ident",&DoIdent},
  165. {0,0}};
  166. // Set up gettext support
  167. setlocale(LC_ALL,"");
  168. textdomain(PACKAGE);
  169. // Parse the command line and initialize the package library
  170. CommandLine CmdL(Args,_config);
  171. if (pkgInitConfig(*_config) == false ||
  172. CmdL.Parse(argc,argv) == false ||
  173. pkgInitSystem(*_config,_system) == false)
  174. {
  175. _error->DumpErrors();
  176. return 100;
  177. }
  178. // See if the help should be shown
  179. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  180. CmdL.FileSize() == 0)
  181. return ShowHelp();
  182. // Deal with stdout not being a tty
  183. if (isatty(STDOUT_FILENO) && _config->FindI("quiet",0) < 1)
  184. _config->Set("quiet","1");
  185. // Match the operation
  186. CmdL.DispatchArg(Cmds);
  187. // Print any errors or warnings found during parsing
  188. if (_error->empty() == false)
  189. {
  190. bool Errors = _error->PendingError();
  191. _error->DumpErrors();
  192. return Errors == true?100:0;
  193. }
  194. return 0;
  195. }