apt-cdrom.cc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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<config.h>
  12. #include <apt-pkg/cmndline.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/init.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/progress.h>
  17. #include <apt-pkg/cdromutl.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/cdrom.h>
  20. #include <apt-pkg/configuration.h>
  21. #include <apt-pkg/pkgsystem.h>
  22. #include <iostream>
  23. #include <vector>
  24. #include <string>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <apt-private/private-cmndline.h>
  28. #include <apti18n.h>
  29. /*}}}*/
  30. static const char *W_NO_CDROM_FOUND = \
  31. N_("No CD-ROM could be auto-detected or found using "
  32. "the default mount point.\n"
  33. "You may try the --cdrom option to set the CD-ROM mount point. "
  34. "See 'man apt-cdrom' for more "
  35. "information about the CD-ROM auto-detection and mount point.");
  36. using namespace std;
  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. if (read(STDIN_FILENO,&C,1) < 0)
  54. _error->Errno("pkgCdromTextStatus::Prompt",
  55. "Failed to read from standard input (not a terminal?)");
  56. if (C != '\n')
  57. cout << endl;
  58. }
  59. string pkgCdromTextStatus::PromptLine(const char *Text)
  60. {
  61. cout << Text << ':' << endl;
  62. string Res;
  63. getline(cin,Res);
  64. return Res;
  65. }
  66. bool pkgCdromTextStatus::AskCdromName(string &name)
  67. {
  68. cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
  69. name = PromptLine("");
  70. return true;
  71. }
  72. void pkgCdromTextStatus::Update(string text, int /*current*/)
  73. {
  74. if(text.size() > 0)
  75. cout << text << flush;
  76. }
  77. bool pkgCdromTextStatus::ChangeCdrom()
  78. {
  79. Prompt(_("Please insert a Disc in the drive and press enter"));
  80. return true;
  81. }
  82. APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress()
  83. {
  84. return &Progress;
  85. }
  86. /*}}}*/
  87. // SetupAutoDetect /*{{{*/
  88. static bool AutoDetectCdrom(pkgUdevCdromDevices &UdevCdroms, unsigned int &i, bool &automounted)
  89. {
  90. bool Debug = _config->FindB("Debug::Acquire::cdrom", false);
  91. automounted = false;
  92. vector<struct CdromDevice> v = UdevCdroms.Scan();
  93. if (i >= v.size())
  94. return false;
  95. if (Debug)
  96. clog << "Looking at devce " << i
  97. << " DeviveName: " << v[i].DeviceName
  98. << " IsMounted: '" << v[i].Mounted << "'"
  99. << " MountPoint: '" << v[i].MountPath << "'"
  100. << endl;
  101. if (v[i].Mounted)
  102. {
  103. // set the right options
  104. _config->Set("Acquire::cdrom::mount", v[i].MountPath);
  105. _config->Set("APT::CDROM::NoMount", true);
  106. } else {
  107. string AptMountPoint = _config->FindDir("Dir::Media::MountPath");
  108. if (!FileExists(AptMountPoint))
  109. mkdir(AptMountPoint.c_str(), 0750);
  110. if(MountCdrom(AptMountPoint, v[i].DeviceName) == false)
  111. _error->Warning(_("Failed to mount '%s' to '%s'"), v[i].DeviceName.c_str(), AptMountPoint.c_str());
  112. else
  113. automounted = true;
  114. _config->Set("Acquire::cdrom::mount", AptMountPoint);
  115. _config->Set("APT::CDROM::NoMount", true);
  116. }
  117. i++;
  118. return true;
  119. }
  120. /*}}}*/
  121. // DoAdd - Add a new CDROM /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* This does the main add bit.. We show some status and things. The
  124. sequence is to mount/umount the CD, Ident it then scan it for package
  125. files and reduce that list. Then we copy over the package files and
  126. verify them. Then rewrite the database files */
  127. static bool DoAdd(CommandLine &)
  128. {
  129. pkgUdevCdromDevices UdevCdroms;
  130. pkgCdromTextStatus log;
  131. pkgCdrom cdrom;
  132. bool res = true;
  133. bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
  134. unsigned int count = 0;
  135. string AptMountPoint = _config->FindDir("Dir::Media::MountPath");
  136. bool automounted = false;
  137. if (AutoDetect && UdevCdroms.Dlopen())
  138. while (AutoDetectCdrom(UdevCdroms, count, automounted)) {
  139. if (count == 1) {
  140. // begin loop with res false to detect any success using OR
  141. res = false;
  142. }
  143. // dump any warnings/errors from autodetect
  144. if (_error->empty() == false)
  145. _error->DumpErrors();
  146. res |= cdrom.Add(&log);
  147. if (automounted)
  148. UnmountCdrom(AptMountPoint);
  149. // dump any warnings/errors from add/unmount
  150. if (_error->empty() == false)
  151. _error->DumpErrors();
  152. }
  153. if (count == 0)
  154. res = cdrom.Add(&log);
  155. if (res == false)
  156. _error->Error("%s", _(W_NO_CDROM_FOUND));
  157. else
  158. cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
  159. return res;
  160. }
  161. /*}}}*/
  162. // DoIdent - Ident a CDROM /*{{{*/
  163. // ---------------------------------------------------------------------
  164. /* */
  165. static bool DoIdent(CommandLine &)
  166. {
  167. pkgUdevCdromDevices UdevCdroms;
  168. string ident;
  169. pkgCdromTextStatus log;
  170. pkgCdrom cdrom;
  171. bool res = true;
  172. bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
  173. unsigned int count = 0;
  174. string AptMountPoint = _config->FindDir("Dir::Media::MountPath");
  175. bool automounted = false;
  176. if (AutoDetect && UdevCdroms.Dlopen())
  177. while (AutoDetectCdrom(UdevCdroms, count, automounted)) {
  178. if (count == 1) {
  179. // begin loop with res false to detect any success using OR
  180. res = false;
  181. }
  182. // dump any warnings/errors from autodetect
  183. if (_error->empty() == false)
  184. _error->DumpErrors();
  185. res |= cdrom.Ident(ident, &log);
  186. if (automounted)
  187. UnmountCdrom(AptMountPoint);
  188. // dump any warnings/errors from add/unmount
  189. if (_error->empty() == false)
  190. _error->DumpErrors();
  191. }
  192. if (count == 0)
  193. res = cdrom.Ident(ident, &log);
  194. if (res == false)
  195. _error->Error("%s", _(W_NO_CDROM_FOUND));
  196. return res;
  197. }
  198. /*}}}*/
  199. // ShowHelp - Show the help screen /*{{{*/
  200. // ---------------------------------------------------------------------
  201. /* */
  202. static bool ShowHelp(CommandLine &)
  203. {
  204. ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  205. COMMON_ARCH,__DATE__,__TIME__);
  206. if (_config->FindB("version") == true)
  207. return true;
  208. cout <<
  209. "Usage: apt-cdrom [options] command\n"
  210. "\n"
  211. "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
  212. "CDROM mount point and device information is taken from apt.conf\n"
  213. "and /etc/fstab.\n"
  214. "\n"
  215. "Commands:\n"
  216. " add - Add a CDROM\n"
  217. " ident - Report the identity of a CDROM\n"
  218. "\n"
  219. "Options:\n"
  220. " -h This help text\n"
  221. " -d CD-ROM mount point\n"
  222. " -r Rename a recognized CD-ROM\n"
  223. " -m No mounting\n"
  224. " -f Fast mode, don't check package files\n"
  225. " -a Thorough scan mode\n"
  226. " --no-auto-detect Do not try to auto detect drive and mount point\n"
  227. " -c=? Read this configuration file\n"
  228. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  229. "See fstab(5)\n";
  230. return true;
  231. }
  232. /*}}}*/
  233. int main(int argc,const char *argv[]) /*{{{*/
  234. {
  235. CommandLine::Dispatch Cmds[] = {
  236. {"add",&DoAdd},
  237. {"ident",&DoIdent},
  238. {"help",&ShowHelp},
  239. {0,0}};
  240. std::vector<CommandLine::Args> Args = getCommandArgs("apt-cdrom", CommandLine::GetCommand(Cmds, argc, argv));
  241. // Set up gettext support
  242. setlocale(LC_ALL,"");
  243. textdomain(PACKAGE);
  244. // Parse the command line and initialize the package library
  245. CommandLine CmdL(Args.data(),_config);
  246. if (pkgInitConfig(*_config) == false ||
  247. CmdL.Parse(argc,argv) == false ||
  248. pkgInitSystem(*_config,_system) == false)
  249. {
  250. _error->DumpErrors();
  251. return 100;
  252. }
  253. // See if the help should be shown
  254. if (_config->FindB("help") == true || _config->FindB("version") == true ||
  255. CmdL.FileSize() == 0)
  256. return ShowHelp(CmdL);
  257. // Deal with stdout not being a tty
  258. if (isatty(STDOUT_FILENO) && _config->FindI("quiet", -1) == -1)
  259. _config->Set("quiet","1");
  260. // Match the operation
  261. CmdL.DispatchArg(Cmds);
  262. // Print any errors or warnings found during parsing
  263. bool const Errors = _error->PendingError();
  264. if (_config->FindI("quiet",0) > 0)
  265. _error->DumpErrors();
  266. else
  267. _error->DumpErrors(GlobalError::DEBUG);
  268. return Errors == true ? 100 : 0;
  269. }
  270. /*}}}*/