apt-cdrom.cc 8.4 KB

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