cdrom.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cdrom.cc,v 1.7 1998/12/22 08:20: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. 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 - Get the ID hash for /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* We search the configuration space for the name and then return the ID
  38. tag associated with it. */
  39. string CDROMMethod::GetID(string Name)
  40. {
  41. if (DatabaseLoaded == false)
  42. {
  43. // Read the database
  44. string DFile = _config->FindFile("Dir::State::cdroms");
  45. if (FileExists(DFile) == true)
  46. {
  47. if (ReadConfigFile(Database,DFile) == false)
  48. {
  49. _error->Error("Unable to read the cdrom database %s",
  50. DFile.c_str());
  51. return string();
  52. }
  53. }
  54. DatabaseLoaded = true;
  55. }
  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. /* 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. URIDone(Res);
  83. return true;
  84. }
  85. string ID = GetID(Get.Host);
  86. if (_error->PendingError() == true)
  87. return false;
  88. // All non IMS queries for package files fail.
  89. if (Itm->IndexFile == true || ID.empty() == true)
  90. {
  91. Fail("Please use apt-cdrom to make this CD recognized by APT."
  92. " apt-get update cannot be used to add new CDs");
  93. return true;
  94. }
  95. // We already have a CD inserted, but it is the wrong one
  96. if (CurrentID.empty() == false && ID != CurrentID)
  97. {
  98. Fail("Wrong CD",true);
  99. return true;
  100. }
  101. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  102. if (CDROM[0] == '.')
  103. CDROM= SafeGetCWD() + '/' + CDROM;
  104. string NewID;
  105. while (1)
  106. {
  107. if (IdentCdrom(CDROM,NewID) == false)
  108. return false;
  109. // A hit
  110. if (NewID == ID)
  111. break;
  112. UnmountCdrom(CDROM);
  113. if (MediaFail(Get.Host,CDROM) == false)
  114. {
  115. CurrentID = "FAIL";
  116. Fail("Wrong CD",true);
  117. return true;
  118. }
  119. MountCdrom(CDROM);
  120. }
  121. // ID matches
  122. if (NewID == ID)
  123. {
  124. Res.Filename = CDROM + File;
  125. struct stat Buf;
  126. if (stat(Res.Filename.c_str(),&Buf) != 0)
  127. return _error->Error("File not found");
  128. CurrentID = ID;
  129. Res.LastModified = Buf.st_mtime;
  130. Res.IMSHit = true;
  131. Res.Size = Buf.st_size;
  132. URIDone(Res);
  133. return true;
  134. }
  135. return _error->Error("CDROM not found");
  136. }
  137. /*}}}*/
  138. int main()
  139. {
  140. CDROMMethod Mth;
  141. return Mth.Run();
  142. }