cdrom.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cdrom.cc,v 1.2 1998/12/04 22:56:55 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. string CurrentID;
  21. virtual bool Fetch(FetchItem *Itm);
  22. string GetID(string Name);
  23. public:
  24. CDROMMethod();
  25. };
  26. // CDROMMethod::CDROMethod - Constructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* */
  29. CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly)
  30. {
  31. // Read the database
  32. string DFile = _config->FindFile("Dir::State::cdroms");
  33. if (FileExists(DFile) == true)
  34. {
  35. if (ReadConfigFile(Database,DFile) == false)
  36. {
  37. _error->Error("Unable to read the cdrom database %s",
  38. DFile.c_str());
  39. Fail();
  40. }
  41. }
  42. };
  43. /*}}}*/
  44. // CDROMMethod::GetID - Get the ID hash for /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* We search the configuration space for the name and then return the ID
  47. tag associated with it. */
  48. string CDROMMethod::GetID(string Name)
  49. {
  50. const Configuration::Item *Top = Database.Tree(0);
  51. for (; Top != 0;)
  52. {
  53. if (Top->Value == Name)
  54. return Top->Tag;
  55. Top = Top->Next;
  56. }
  57. return string();
  58. }
  59. /*}}}*/
  60. // CDROMMethod::Fetch - Fetch a file /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* */
  63. bool CDROMMethod::Fetch(FetchItem *Itm)
  64. {
  65. URI Get = Itm->Uri;
  66. string File = Get.Path;
  67. FetchResult Res;
  68. /* All IMS queries are returned as a hit, CDROMs are readonly so
  69. time stamps never change */
  70. if (Itm->LastModified != 0)
  71. {
  72. Res.LastModified = Itm->LastModified;
  73. Res.IMSHit = true;
  74. URIDone(Res);
  75. return true;
  76. }
  77. string ID = GetID(Get.Host);
  78. // All non IMS queries for package files fail.
  79. if (Itm->IndexFile == true || ID.empty() == false)
  80. {
  81. Fail("Please use apt-cdrom to make this CD recognized by APT."
  82. " apt-get update cannot be used to add new CDs");
  83. return true;
  84. }
  85. // We already have a CD inserted, but it is the wrong one
  86. if (CurrentID.empty() == false && ID != CurrentID)
  87. {
  88. Fail("Wrong CD",true);
  89. return true;
  90. }
  91. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  92. string NewID;
  93. while (1)
  94. {
  95. if (IdentCdrom(CDROM,NewID) == false)
  96. return false;
  97. // A hit
  98. if (NewID == ID)
  99. break;
  100. UnmountCdrom(CDROM);
  101. if (MediaFail(Get.Host,CDROM) == false)
  102. {
  103. ID = "FAIL";
  104. break;
  105. }
  106. MountCdrom(CDROM);
  107. }
  108. // ID matches
  109. if (NewID == ID)
  110. {
  111. Res.Filename = CDROM + File;
  112. if (FileExists(Res.Filename) == false)
  113. return _error->Error("File not found");
  114. CurrentID = ID;
  115. Res.LastModified = Itm->LastModified;
  116. Res.IMSHit = true;
  117. URIDone(Res);
  118. return true;
  119. }
  120. return _error->Error("CDROM not found");
  121. }
  122. /*}}}*/
  123. int main()
  124. {
  125. CDROMMethod Mth;
  126. return Mth.Run();
  127. }