You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

266 lines
7.5 KiB

  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. // ShowHelp - Show the help screen /*{{{*/
  180. bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
  181. {
  182. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  183. if (_config->FindB("version") == true)
  184. return true;
  185. std::cout <<
  186. _("Usage: apt-cdrom [options] command\n"
  187. "\n"
  188. "apt-cdrom is a tool to add CDROM's to APT's source list. The\n"
  189. "CDROM mount point and device information is taken from apt.conf,\n"
  190. "udev and /etc/fstab.\n")
  191. << std::endl
  192. << _("Commands:") << std::endl;
  193. for (; Cmds->Handler != nullptr; ++Cmds)
  194. {
  195. if (Cmds->Help == nullptr)
  196. continue;
  197. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  198. }
  199. std::cout << std::endl <<
  200. _("Options:\n"
  201. " -h This help text\n"
  202. " -d CD-ROM mount point\n"
  203. " -r Rename a recognized CD-ROM\n"
  204. " -m No mounting\n"
  205. " -f Fast mode, don't check package files\n"
  206. " -a Thorough scan mode\n"
  207. " --no-auto-detect Do not try to auto detect drive and mount point\n"
  208. " -c=? Read this configuration file\n"
  209. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  210. "See fstab(5)\n");
  211. return true;
  212. }
  213. /*}}}*/
  214. std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
  215. {
  216. return {
  217. {"add", &DoAdd, "Add a CDROM"},
  218. {"ident", &DoIdent, "Report the identity of a CDROM"},
  219. {nullptr, nullptr, nullptr}
  220. };
  221. }
  222. /*}}}*/
  223. int main(int argc,const char *argv[]) /*{{{*/
  224. {
  225. InitLocale();
  226. // Parse the command line and initialize the package library
  227. CommandLine CmdL;
  228. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CDROM, &_config, &_system, argc, argv);
  229. InitOutput();
  230. return DispatchCommandLine(CmdL, Cmds);
  231. }
  232. /*}}}*/