cdrom.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cdrom.cc,v 1.20.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. CDROM URI method for APT
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <apt-pkg/acquire-method.h>
  10. #include <apt-pkg/cdrom.h>
  11. #include <apt-pkg/cdromutl.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/hashes.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <dlfcn.h>
  19. #include <iostream>
  20. #include <apti18n.h>
  21. /*}}}*/
  22. using namespace std;
  23. class CDROMMethod : public pkgAcqMethod
  24. {
  25. bool DatabaseLoaded;
  26. ::Configuration Database;
  27. string CurrentID;
  28. string CDROM;
  29. bool MountedByApt;
  30. bool IsCorrectCD(URI want, string MountPath);
  31. virtual bool Fetch(FetchItem *Itm);
  32. string GetID(string Name);
  33. virtual void Exit();
  34. public:
  35. CDROMMethod();
  36. };
  37. // CDROMMethod::CDROMethod - Constructor /*{{{*/
  38. // ---------------------------------------------------------------------
  39. /* */
  40. CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
  41. SendConfig | NeedsCleanup |
  42. Removable),
  43. DatabaseLoaded(false),
  44. MountedByApt(false)
  45. {
  46. };
  47. /*}}}*/
  48. // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* */
  51. void CDROMMethod::Exit()
  52. {
  53. if (MountedByApt == true)
  54. UnmountCdrom(CDROM);
  55. }
  56. /*}}}*/
  57. // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. string CDROMMethod::GetID(string Name)
  61. {
  62. // Search for an ID
  63. const Configuration::Item *Top = Database.Tree("CD");
  64. if (Top != 0)
  65. Top = Top->Child;
  66. for (; Top != 0;)
  67. {
  68. if (Top->Value == Name)
  69. return Top->Tag;
  70. Top = Top->Next;
  71. }
  72. return string();
  73. }
  74. /*}}}*/
  75. // CDROMMethod::IsCorrectCD /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* */
  78. bool CDROMMethod::IsCorrectCD(URI want, string MountPath)
  79. {
  80. bool Debug = _config->FindB("Debug::Acquire::cdrom",false);
  81. string NewID;
  82. for (unsigned int Version = 2; Version != 0; Version--)
  83. {
  84. if (IdentCdrom(MountPath,NewID,Version) == false)
  85. return false;
  86. if (Debug == true)
  87. clog << "ID " << Version << " " << NewID << endl;
  88. // A hit
  89. if (Database.Find("CD::" + NewID) == want.Host)
  90. return true;
  91. }
  92. return false;
  93. }
  94. /*}}}*/
  95. // CDROMMethod::Fetch - Fetch a file /*{{{*/
  96. // ---------------------------------------------------------------------
  97. /* */
  98. bool CDROMMethod::Fetch(FetchItem *Itm)
  99. {
  100. URI Get = Itm->Uri;
  101. string File = Get.Path;
  102. FetchResult Res;
  103. bool Debug = _config->FindB("Debug::Acquire::cdrom",false);
  104. /* All IMS queries are returned as a hit, CDROMs are readonly so
  105. time stamps never change */
  106. if (Itm->LastModified != 0)
  107. {
  108. Res.LastModified = Itm->LastModified;
  109. Res.IMSHit = true;
  110. Res.Filename = Itm->DestFile;
  111. URIDone(Res);
  112. return true;
  113. }
  114. // Load the database
  115. if (DatabaseLoaded == false)
  116. {
  117. // Read the database
  118. string DFile = _config->FindFile("Dir::State::cdroms");
  119. if (FileExists(DFile) == true)
  120. {
  121. if (ReadConfigFile(Database,DFile) == false)
  122. return _error->Error(_("Unable to read the cdrom database %s"),
  123. DFile.c_str());
  124. }
  125. DatabaseLoaded = true;
  126. }
  127. // All non IMS queries for package files fail.
  128. if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
  129. {
  130. Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
  131. " apt-get update cannot be used to add new CD-ROMs"));
  132. return true;
  133. }
  134. // We already have a CD inserted, but it is the wrong one
  135. if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host)
  136. {
  137. Fail(_("Wrong CD-ROM"),true);
  138. return true;
  139. }
  140. CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  141. // auto-detect mode
  142. if (CDROM == "apt-udev-auto/")
  143. {
  144. pkgUdevCdromDevices udev;
  145. if(udev.Dlopen())
  146. {
  147. vector<struct CdromDevice> v = udev.Scan();
  148. for (unsigned int i=0; i < v.size(); i++)
  149. {
  150. if (!v[i].Mounted)
  151. {
  152. if (!FileExists("/media/apt"))
  153. mkdir("/media/apt", 0755);
  154. if(MountCdrom("/media/apt", v[i].DeviceName))
  155. {
  156. if (IsCorrectCD(Get, "/media/apt"))
  157. {
  158. MountedByApt = true;
  159. CDROM = "/media/apt";
  160. break;
  161. } else {
  162. UnmountCdrom("/media/apt");
  163. }
  164. }
  165. } else {
  166. if (IsCorrectCD(Get, v[i].MountPath))
  167. {
  168. CDROM = v[i].MountPath;
  169. break;
  170. }
  171. }
  172. }
  173. }
  174. }
  175. if (CDROM[0] == '.')
  176. CDROM= SafeGetCWD() + '/' + CDROM;
  177. string NewID;
  178. while (CurrentID.empty() == true)
  179. {
  180. bool Hit = false;
  181. if(!IsMounted(CDROM))
  182. MountedByApt = MountCdrom(CDROM);
  183. if (IsCorrectCD(Get, CDROM))
  184. break;
  185. // I suppose this should prompt somehow?
  186. if (_config->FindB("APT::CDROM::NoMount",false) == false &&
  187. UnmountCdrom(CDROM) == false)
  188. return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
  189. CDROM.c_str());
  190. if (MediaFail(Get.Host,CDROM) == false)
  191. {
  192. CurrentID = "FAIL";
  193. return _error->Error(_("Disk not found."));
  194. }
  195. }
  196. // Found a CD
  197. Res.Filename = CDROM + File;
  198. struct stat Buf;
  199. if (stat(Res.Filename.c_str(),&Buf) != 0)
  200. return _error->Error(_("File not found"));
  201. if (NewID.empty() == false)
  202. CurrentID = NewID;
  203. Res.LastModified = Buf.st_mtime;
  204. Res.Size = Buf.st_size;
  205. Hashes Hash;
  206. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  207. Hash.AddFD(Fd.Fd(), Fd.Size());
  208. Res.TakeHashes(Hash);
  209. URIDone(Res);
  210. return true;
  211. }
  212. /*}}}*/
  213. int main()
  214. {
  215. setlocale(LC_ALL, "");
  216. CDROMMethod Mth;
  217. return Mth.Run();
  218. }