cdrom.cc 4.7 KB

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