cdrom.cc 4.7 KB

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