cdrom.cc 7.2 KB

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