cdrom.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cdrom.cc,v 1.9 1999/05/23 05:45:13 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. /* All IMS queries are returned as a hit, CDROMs are readonly so
  62. time stamps never change */
  63. if (Itm->LastModified != 0)
  64. {
  65. Res.LastModified = Itm->LastModified;
  66. Res.IMSHit = true;
  67. URIDone(Res);
  68. return true;
  69. }
  70. // Load the database
  71. if (DatabaseLoaded == false)
  72. {
  73. // Read the database
  74. string DFile = _config->FindFile("Dir::State::cdroms");
  75. if (FileExists(DFile) == true)
  76. {
  77. if (ReadConfigFile(Database,DFile) == false)
  78. return _error->Error("Unable to read the cdrom database %s",
  79. DFile.c_str());
  80. }
  81. DatabaseLoaded = true;
  82. }
  83. // All non IMS queries for package files fail.
  84. if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
  85. {
  86. Fail("Please use apt-cdrom to make this CD recognized by APT."
  87. " apt-get update cannot be used to add new CDs");
  88. return true;
  89. }
  90. // We already have a CD inserted, but it is the wrong one
  91. if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host)
  92. {
  93. Fail("Wrong CD",true);
  94. return true;
  95. }
  96. string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  97. if (CDROM[0] == '.')
  98. CDROM= SafeGetCWD() + '/' + CDROM;
  99. string NewID;
  100. while (1)
  101. {
  102. if (IdentCdrom(CDROM,NewID) == false)
  103. return false;
  104. // A hit
  105. if (Database.Find("CD::" + NewID) == Get.Host)
  106. break;
  107. // I suppose this should prompt somehow?
  108. if (UnmountCdrom(CDROM) == false)
  109. return _error->Error("Unable to unmount the CD-ROM in %s, it may still be in use.",
  110. CDROM.c_str());
  111. if (MediaFail(Get.Host,CDROM) == false)
  112. {
  113. CurrentID = "FAIL";
  114. Fail("Wrong CD",true);
  115. return true;
  116. }
  117. MountCdrom(CDROM);
  118. }
  119. // Found a CD
  120. Res.Filename = CDROM + File;
  121. struct stat Buf;
  122. if (stat(Res.Filename.c_str(),&Buf) != 0)
  123. return _error->Error("File not found");
  124. CurrentID = NewID;
  125. Res.LastModified = Buf.st_mtime;
  126. Res.IMSHit = true;
  127. Res.Size = Buf.st_size;
  128. URIDone(Res);
  129. return true;
  130. }
  131. /*}}}*/
  132. int main()
  133. {
  134. CDROMMethod Mth;
  135. return Mth.Run();
  136. }