apt-cdrom.cc 9.0 KB

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