You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

175 lines
4.2 KiB

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