apt-cdrom.cc 6.6 KB

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