cdrom.cc 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. bool Debug;
  27. ::Configuration Database;
  28. string CurrentID;
  29. string CDROM;
  30. bool MountedByApt;
  31. pkgUdevCdromDevices UdevCdroms;
  32. bool IsCorrectCD(URI want, string MountPath, string& NewID);
  33. bool AutoDetectAndMount(URI, string &NewID);
  34. virtual bool Fetch(FetchItem *Itm);
  35. string GetID(string Name);
  36. virtual void Exit();
  37. public:
  38. CDROMMethod();
  39. };
  40. // CDROMMethod::CDROMethod - Constructor /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* */
  43. CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
  44. SendConfig | NeedsCleanup |
  45. Removable),
  46. DatabaseLoaded(false),
  47. MountedByApt(false)
  48. {
  49. UdevCdroms.Dlopen();
  50. };
  51. /*}}}*/
  52. // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* */
  55. void CDROMMethod::Exit()
  56. {
  57. if (MountedByApt == true)
  58. UnmountCdrom(CDROM);
  59. }
  60. /*}}}*/
  61. // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* */
  64. string CDROMMethod::GetID(string Name)
  65. {
  66. // Search for an ID
  67. const Configuration::Item *Top = Database.Tree("CD");
  68. if (Top != 0)
  69. Top = Top->Child;
  70. for (; Top != 0;)
  71. {
  72. if (Top->Value == Name)
  73. return Top->Tag;
  74. Top = Top->Next;
  75. }
  76. return string();
  77. }
  78. /*}}}*/
  79. // CDROMMethod::AutoDetectAndMount /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* Modifies class varaiable CDROM to the mountpoint */
  82. bool CDROMMethod::AutoDetectAndMount(URI Get, string NewID)
  83. {
  84. vector<struct CdromDevice> v = UdevCdroms.Scan();
  85. // first check if its mounted somewhere already
  86. for (unsigned int i=0; i < v.size(); i++)
  87. {
  88. if (v[i].Mounted)
  89. {
  90. if (Debug)
  91. clog << "Checking mounted cdrom device " << v[i].DeviceName << endl;
  92. if (IsCorrectCD(Get, v[i].MountPath, NewID))
  93. {
  94. CDROM = v[i].MountPath;
  95. return true;
  96. }
  97. }
  98. }
  99. // we are not supposed to mount, exit
  100. if (_config->FindB("APT::CDROM::NoMount",false) == true)
  101. return false;
  102. // check if we have the mount point
  103. if (!FileExists("/media/apt"))
  104. mkdir("/media/apt", 0755);
  105. // now try mounting
  106. for (unsigned int i=0; i < v.size(); i++)
  107. {
  108. if (!v[i].Mounted)
  109. {
  110. if(MountCdrom("/media/apt", v[i].DeviceName))
  111. {
  112. if (IsCorrectCD(Get, "/media/apt", NewID))
  113. {
  114. MountedByApt = true;
  115. CDROM = "/media/apt";
  116. return true;
  117. } else {
  118. UnmountCdrom("/media/apt");
  119. }
  120. }
  121. }
  122. }
  123. return false;
  124. }
  125. /*}}}*/
  126. // CDROMMethod::IsCorrectCD /*{{{*/
  127. // ---------------------------------------------------------------------
  128. /* */
  129. bool CDROMMethod::IsCorrectCD(URI want, string MountPath, string& NewID)
  130. {
  131. for (unsigned int Version = 2; Version != 0; Version--)
  132. {
  133. if (IdentCdrom(MountPath,NewID,Version) == false)
  134. return false;
  135. if (Debug)
  136. clog << "ID " << Version << " " << NewID << endl;
  137. // A hit
  138. if (Database.Find("CD::" + NewID) == want.Host)
  139. return true;
  140. }
  141. return false;
  142. }
  143. /*}}}*/
  144. // CDROMMethod::Fetch - Fetch a file /*{{{*/
  145. // ---------------------------------------------------------------------
  146. /* */
  147. bool CDROMMethod::Fetch(FetchItem *Itm)
  148. {
  149. FetchResult Res;
  150. URI Get = Itm->Uri;
  151. string File = Get.Path;
  152. Debug = _config->FindB("Debug::Acquire::cdrom", false);
  153. if (Debug)
  154. clog << "CDROMMethod::Fetch " << Itm->Uri << endl;
  155. /* All IMS queries are returned as a hit, CDROMs are readonly so
  156. time stamps never change */
  157. if (Itm->LastModified != 0)
  158. {
  159. Res.LastModified = Itm->LastModified;
  160. Res.IMSHit = true;
  161. Res.Filename = Itm->DestFile;
  162. URIDone(Res);
  163. return true;
  164. }
  165. // Load the database
  166. if (DatabaseLoaded == false)
  167. {
  168. // Read the database
  169. string DFile = _config->FindFile("Dir::State::cdroms");
  170. if (FileExists(DFile) == true)
  171. {
  172. if (ReadConfigFile(Database,DFile) == false)
  173. return _error->Error(_("Unable to read the cdrom database %s"),
  174. DFile.c_str());
  175. }
  176. DatabaseLoaded = true;
  177. }
  178. // All non IMS queries for package files fail.
  179. if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
  180. {
  181. Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
  182. " apt-get update cannot be used to add new CD-ROMs"));
  183. return true;
  184. }
  185. // We already have a CD inserted, but it is the wrong one
  186. if (CurrentID.empty() == false &&
  187. CurrentID != "FAIL" &&
  188. Database.Find("CD::" + CurrentID) != Get.Host)
  189. {
  190. Fail(_("Wrong CD-ROM"),true);
  191. return true;
  192. }
  193. CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
  194. if (Debug)
  195. clog << "Looking for CDROM at " << CDROM << endl;
  196. if (CDROM[0] == '.')
  197. CDROM= SafeGetCWD() + '/' + CDROM;
  198. string NewID;
  199. while (CurrentID.empty() == true)
  200. {
  201. if (CDROM == "apt-udev-auto/")
  202. AutoDetectAndMount(Get, NewID);
  203. if(!IsMounted(CDROM))
  204. MountedByApt = MountCdrom(CDROM);
  205. if (IsCorrectCD(Get, CDROM, NewID))
  206. break;
  207. // I suppose this should prompt somehow?
  208. if (_config->FindB("APT::CDROM::NoMount",false) == false &&
  209. UnmountCdrom(CDROM) == false)
  210. return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
  211. CDROM.c_str());
  212. if (MediaFail(Get.Host,CDROM) == false)
  213. {
  214. CurrentID = "FAIL";
  215. return _error->Error(_("Disk not found."));
  216. }
  217. }
  218. // Found a CD
  219. Res.Filename = CDROM + File;
  220. struct stat Buf;
  221. if (stat(Res.Filename.c_str(),&Buf) != 0)
  222. return _error->Error(_("File not found"));
  223. if (NewID.empty() == false)
  224. CurrentID = NewID;
  225. Res.LastModified = Buf.st_mtime;
  226. Res.Size = Buf.st_size;
  227. Hashes Hash;
  228. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  229. Hash.AddFD(Fd.Fd(), Fd.Size());
  230. Res.TakeHashes(Hash);
  231. URIDone(Res);
  232. return true;
  233. }
  234. /*}}}*/
  235. int main()
  236. {
  237. setlocale(LC_ALL, "");
  238. CDROMMethod Mth;
  239. return Mth.Run();
  240. }