cdrom.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cdrom.cc,v 1.20.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. CDROM URI method for APT
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <apt-pkg/acquire-method.h>
  10. #include <apt-pkg/cdromutl.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/hashes.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #include <iostream>
  18. #include <apti18n.h>
  19. /*}}}*/
  20. using namespace std;
  21. class CDROMMethod : public pkgAcqMethod
  22. {
  23. bool DatabaseLoaded;
  24. ::Configuration Database;
  25. string CurrentID;
  26. string CDROM;
  27. bool Mounted;
  28. virtual bool Fetch(FetchItem *Itm);
  29. string GetID(string Name);
  30. virtual void Exit();
  31. public:
  32. CDROMMethod();
  33. };
  34. // CDROMMethod::CDROMethod - Constructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
  38. SendConfig | NeedsCleanup |
  39. Removable),
  40. DatabaseLoaded(false),
  41. Mounted(false)
  42. {
  43. };
  44. /*}}}*/
  45. // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* */
  48. void CDROMMethod::Exit()
  49. {
  50. if (Mounted == true && _config->FindB("APT::CDROM::NoMount",false) == false)
  51. UnmountCdrom(CDROM);
  52. }
  53. /*}}}*/
  54. // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* */
  57. string CDROMMethod::GetID(string Name)
  58. {
  59. // Search for an ID
  60. const Configuration::Item *Top = Database.Tree("CD");
  61. if (Top != 0)
  62. Top = Top->Child;
  63. for (; Top != 0;)
  64. {
  65. if (Top->Value == Name)
  66. return Top->Tag;
  67. Top = Top->Next;
  68. }
  69. return string();
  70. }
  71. /*}}}*/
  72. // CDROMMethod::Fetch - Fetch a file /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* */
  75. bool CDROMMethod::Fetch(FetchItem *Itm)
  76. {
  77. URI Get = Itm->Uri;
  78. string File = Get.Path;
  79. FetchResult Res;
  80. bool Debug = _config->FindB("Debug::Acquire::cdrom",false);
  81. /* All IMS queries are returned as a hit, CDROMs are readonly so
  82. time stamps never change */
  83. if (Itm->LastModified != 0)
  84. {
  85. Res.LastModified = Itm->LastModified;
  86. Res.IMSHit = true;
  87. Res.Filename = Itm->DestFile;
  88. URIDone(Res);
  89. return true;
  90. }
  91. // Load the database
  92. if (DatabaseLoaded == false)
  93. {
  94. // Read the database
  95. string DFile = _config->FindFile("Dir::State::cdroms");
  96. if (FileExists(DFile) == true)
  97. {
  98. if (ReadConfigFile(Database,DFile) == false)
  99. return _error->Error(_("Unable to read the cdrom database %s"),
  100. DFile.c_str());
  101. }
  102. DatabaseLoaded = true;
  103. }
  104. // All non IMS queries for package files fail.
  105. if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
  106. {
  107. Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
  108. " apt-get update cannot be used to add new CD-ROMs"));
  109. return true;
  110. }
  111. // We already have a CD inserted, but it is the wrong one
  112. if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host)
  113. {
  114. Fail(_("Wrong CD-ROM"),true);
  115. return true;
  116. }
  117. CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  118. if (CDROM[0] == '.')
  119. CDROM= SafeGetCWD() + '/' + CDROM;
  120. string NewID;
  121. while (CurrentID.empty() == true)
  122. {
  123. bool Hit = false;
  124. Mounted = MountCdrom(CDROM);
  125. for (unsigned int Version = 2; Version != 0; Version--)
  126. {
  127. if (IdentCdrom(CDROM,NewID,Version) == false)
  128. return false;
  129. if (Debug == true)
  130. clog << "ID " << Version << " " << NewID << endl;
  131. // A hit
  132. if (Database.Find("CD::" + NewID) == Get.Host)
  133. {
  134. Hit = true;
  135. break;
  136. }
  137. }
  138. if (Hit == true)
  139. break;
  140. // I suppose this should prompt somehow?
  141. if (_config->FindB("APT::CDROM::NoMount",false) == false &&
  142. UnmountCdrom(CDROM) == false)
  143. return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
  144. CDROM.c_str());
  145. if (MediaFail(Get.Host,CDROM) == false)
  146. {
  147. CurrentID = "FAIL";
  148. return _error->Error(_("Disk not found."));
  149. }
  150. }
  151. // Found a CD
  152. Res.Filename = CDROM + File;
  153. struct stat Buf;
  154. if (stat(Res.Filename.c_str(),&Buf) != 0)
  155. return _error->Error(_("File not found"));
  156. if (NewID.empty() == false)
  157. CurrentID = NewID;
  158. Res.LastModified = Buf.st_mtime;
  159. Res.Size = Buf.st_size;
  160. Hashes Hash;
  161. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  162. Hash.AddFD(Fd.Fd(), Fd.Size());
  163. Res.TakeHashes(Hash);
  164. URIDone(Res);
  165. return true;
  166. }
  167. /*}}}*/
  168. int main()
  169. {
  170. setlocale(LC_ALL, "");
  171. CDROMMethod Mth;
  172. return Mth.Run();
  173. }