apt-cdrom.cc 7.9 KB

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