cdrom.cc 3.6 KB

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