apt-cdrom.cc 7.7 KB

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