cdrom.cc 4.7 KB

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