apt-cdrom.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <apt-private/private-output.h>
  29. #include <apti18n.h>
  30. /*}}}*/
  31. using namespace std;
  32. class pkgCdromTextStatus : public pkgCdromStatus /*{{{*/
  33. {
  34. protected:
  35. OpTextProgress Progress;
  36. void Prompt(const char *Text);
  37. string PromptLine(const char *Text);
  38. bool AskCdromName(string &name);
  39. public:
  40. virtual void Update(string text, int current);
  41. virtual bool ChangeCdrom();
  42. virtual OpProgress* GetOpProgress();
  43. };
  44. void pkgCdromTextStatus::Prompt(const char *Text)
  45. {
  46. char C;
  47. cout << Text << ' ' << flush;
  48. if (read(STDIN_FILENO,&C,1) < 0)
  49. _error->Errno("pkgCdromTextStatus::Prompt",
  50. "Failed to read from standard input (not a terminal?)");
  51. if (C != '\n')
  52. cout << endl;
  53. }
  54. string pkgCdromTextStatus::PromptLine(const char *Text)
  55. {
  56. cout << Text << ':' << endl;
  57. string Res;
  58. getline(cin,Res);
  59. return Res;
  60. }
  61. bool pkgCdromTextStatus::AskCdromName(string &name)
  62. {
  63. cout << _("Please provide a name for this Disc, such as 'Debian 5.0.3 Disk 1'") << flush;
  64. name = PromptLine("");
  65. return true;
  66. }
  67. void pkgCdromTextStatus::Update(string text, int /*current*/)
  68. {
  69. if(text.size() > 0)
  70. cout << text << flush;
  71. }
  72. bool pkgCdromTextStatus::ChangeCdrom()
  73. {
  74. Prompt(_("Please insert a Disc in the drive and press enter"));
  75. return true;
  76. }
  77. APT_CONST OpProgress* pkgCdromTextStatus::GetOpProgress()
  78. {
  79. return &Progress;
  80. }
  81. /*}}}*/
  82. // AddOrIdent - Add or Ident a CDROM /*{{{*/
  83. static bool AddOrIdent(bool const Add)
  84. {
  85. pkgUdevCdromDevices UdevCdroms;
  86. pkgCdromTextStatus log;
  87. pkgCdrom cdrom;
  88. bool oneSuccessful = false;
  89. bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
  90. if (AutoDetect == true && UdevCdroms.Dlopen() == true)
  91. {
  92. bool const Debug = _config->FindB("Debug::Acquire::cdrom", false);
  93. std::string const CDMount = _config->Find("Acquire::cdrom::mount");
  94. bool const NoMount = _config->FindB("APT::CDROM::NoMount", false);
  95. if (NoMount == false)
  96. _config->Set("APT::CDROM::NoMount", true);
  97. vector<struct CdromDevice> const v = UdevCdroms.Scan();
  98. for (std::vector<struct CdromDevice>::const_iterator cd = v.begin(); cd != v.end(); ++cd)
  99. {
  100. if (Debug)
  101. clog << "Looking at device:"
  102. << "\tDeviveName: '" << cd->DeviceName << "'"
  103. << "\tIsMounted: '" << cd->Mounted << "'"
  104. << "\tMountPoint: '" << cd->MountPath << "'"
  105. << endl;
  106. std::string AptMountPoint;
  107. if (cd->Mounted)
  108. _config->Set("Acquire::cdrom::mount", cd->MountPath);
  109. else if (NoMount == true)
  110. continue;
  111. else
  112. {
  113. AptMountPoint = _config->FindDir("Dir::Media::MountPath");
  114. if (FileExists(AptMountPoint) == false)
  115. mkdir(AptMountPoint.c_str(), 0750);
  116. if(MountCdrom(AptMountPoint, cd->DeviceName) == false)
  117. {
  118. _error->Warning(_("Failed to mount '%s' to '%s'"), cd->DeviceName.c_str(), AptMountPoint.c_str());
  119. continue;
  120. }
  121. _config->Set("Acquire::cdrom::mount", AptMountPoint);
  122. }
  123. _error->PushToStack();
  124. if (Add == true)
  125. oneSuccessful = cdrom.Add(&log);
  126. else
  127. {
  128. std::string id;
  129. oneSuccessful = cdrom.Ident(id, &log);
  130. }
  131. _error->MergeWithStack();
  132. if (AptMountPoint.empty() == false)
  133. UnmountCdrom(AptMountPoint);
  134. }
  135. if (NoMount == false)
  136. _config->Set("APT::CDROM::NoMount", NoMount);
  137. _config->Set("Acquire::cdrom::mount", CDMount);
  138. }
  139. // fallback if auto-detect didn't work
  140. if (oneSuccessful == false)
  141. {
  142. _error->PushToStack();
  143. if (Add == true)
  144. oneSuccessful = cdrom.Add(&log);
  145. else
  146. {
  147. std::string id;
  148. oneSuccessful = cdrom.Ident(id, &log);
  149. }
  150. _error->MergeWithStack();
  151. }
  152. if (oneSuccessful == false)
  153. _error->Error("%s", _("No CD-ROM could be auto-detected or found using the default mount point.\n"
  154. "You may try the --cdrom option to set the CD-ROM mount point.\n"
  155. "See 'man apt-cdrom' for more information about the CD-ROM auto-detection and mount point."));
  156. else if (Add == true)
  157. cout << _("Repeat this process for the rest of the CDs in your set.") << endl;
  158. return oneSuccessful;
  159. }
  160. /*}}}*/
  161. // DoAdd - Add a new CDROM /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* This does the main add bit.. We show some status and things. The
  164. sequence is to mount/umount the CD, Ident it then scan it for package
  165. files and reduce that list. Then we copy over the package files and
  166. verify them. Then rewrite the database files */
  167. static bool DoAdd(CommandLine &)
  168. {
  169. return AddOrIdent(true);
  170. }
  171. /*}}}*/
  172. // DoIdent - Ident a CDROM /*{{{*/
  173. static bool DoIdent(CommandLine &)
  174. {
  175. return AddOrIdent(false);
  176. }
  177. /*}}}*/
  178. // ShowHelp - Show the help screen /*{{{*/
  179. static bool ShowHelp(CommandLine &)
  180. {
  181. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  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;
  222. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  223. InitOutput();
  224. // Match the operation
  225. bool returned = CmdL.DispatchArg(Cmds);
  226. if (_config->FindI("quiet",0) > 0)
  227. _error->DumpErrors();
  228. else
  229. _error->DumpErrors(GlobalError::DEBUG);
  230. return returned == true ? 0 : 100;
  231. }
  232. /*}}}*/