cdrom.cc 4.7 KB

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