cdrom.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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(const 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(const 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. string AptMountPoint = _config->FindDir("Dir::Media::MountPath");
  104. if (!FileExists(AptMountPoint))
  105. mkdir(AptMountPoint.c_str(), 0750);
  106. // now try mounting
  107. for (unsigned int i=0; i < v.size(); i++)
  108. {
  109. if (!v[i].Mounted)
  110. {
  111. if(MountCdrom(AptMountPoint, v[i].DeviceName))
  112. {
  113. if (IsCorrectCD(Get, AptMountPoint, NewID))
  114. {
  115. MountedByApt = true;
  116. CDROM = AptMountPoint;
  117. return true;
  118. } else {
  119. UnmountCdrom(AptMountPoint);
  120. }
  121. }
  122. }
  123. }
  124. return false;
  125. }
  126. /*}}}*/
  127. // CDROMMethod::IsCorrectCD /*{{{*/
  128. // ---------------------------------------------------------------------
  129. /* */
  130. bool CDROMMethod::IsCorrectCD(URI want, string MountPath, string& NewID)
  131. {
  132. for (unsigned int Version = 2; Version != 0; Version--)
  133. {
  134. if (IdentCdrom(MountPath,NewID,Version) == false)
  135. return false;
  136. if (Debug)
  137. clog << "ID " << Version << " " << NewID << endl;
  138. // A hit
  139. if (Database.Find("CD::" + NewID) == want.Host)
  140. return true;
  141. }
  142. return false;
  143. }
  144. /*}}}*/
  145. // CDROMMethod::Fetch - Fetch a file /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* */
  148. bool CDROMMethod::Fetch(FetchItem *Itm)
  149. {
  150. FetchResult Res;
  151. URI Get = Itm->Uri;
  152. string File = Get.Path;
  153. Debug = _config->FindB("Debug::Acquire::cdrom", false);
  154. if (Debug)
  155. clog << "CDROMMethod::Fetch " << Itm->Uri << endl;
  156. /* All IMS queries are returned as a hit, CDROMs are readonly so
  157. time stamps never change */
  158. if (Itm->LastModified != 0)
  159. {
  160. Res.LastModified = Itm->LastModified;
  161. Res.IMSHit = true;
  162. Res.Filename = Itm->DestFile;
  163. URIDone(Res);
  164. return true;
  165. }
  166. // Load the database
  167. if (DatabaseLoaded == false)
  168. {
  169. // Read the database
  170. string DFile = _config->FindFile("Dir::State::cdroms");
  171. if (FileExists(DFile) == true)
  172. {
  173. if (ReadConfigFile(Database,DFile) == false)
  174. return _error->Error(_("Unable to read the cdrom database %s"),
  175. DFile.c_str());
  176. }
  177. DatabaseLoaded = true;
  178. }
  179. // All non IMS queries for package files fail.
  180. if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
  181. {
  182. Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
  183. " apt-get update cannot be used to add new CD-ROMs"));
  184. return true;
  185. }
  186. // We already have a CD inserted, but it is the wrong one
  187. if (CurrentID.empty() == false &&
  188. CurrentID != "FAIL" &&
  189. Database.Find("CD::" + CurrentID) != Get.Host)
  190. {
  191. Fail(_("Wrong CD-ROM"),true);
  192. return true;
  193. }
  194. bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true);
  195. CDROM = _config->FindDir("Acquire::cdrom::mount");
  196. if (Debug)
  197. clog << "Looking for CDROM at " << CDROM << endl;
  198. if (CDROM[0] == '.')
  199. CDROM= SafeGetCWD() + '/' + CDROM;
  200. string NewID;
  201. while (CurrentID.empty() == true)
  202. {
  203. if (AutoDetect)
  204. AutoDetectAndMount(Get, NewID);
  205. if(!IsMounted(CDROM))
  206. MountedByApt = MountCdrom(CDROM);
  207. if (IsCorrectCD(Get, CDROM, NewID))
  208. break;
  209. // I suppose this should prompt somehow?
  210. if (_config->FindB("APT::CDROM::NoMount",false) == false &&
  211. UnmountCdrom(CDROM) == false)
  212. return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
  213. CDROM.c_str());
  214. if (MediaFail(Get.Host,CDROM) == false)
  215. {
  216. CurrentID = "FAIL";
  217. return _error->Error(_("Disk not found."));
  218. }
  219. }
  220. // Found a CD
  221. Res.Filename = CDROM + File;
  222. struct stat Buf;
  223. if (stat(Res.Filename.c_str(),&Buf) != 0)
  224. return _error->Error(_("File not found"));
  225. if (NewID.empty() == false)
  226. CurrentID = NewID;
  227. Res.LastModified = Buf.st_mtime;
  228. Res.Size = Buf.st_size;
  229. Hashes Hash;
  230. FileFd Fd(Res.Filename, FileFd::ReadOnly);
  231. Hash.AddFD(Fd.Fd(), Fd.Size());
  232. Res.TakeHashes(Hash);
  233. URIDone(Res);
  234. return true;
  235. }
  236. /*}}}*/
  237. int main()
  238. {
  239. setlocale(LC_ALL, "");
  240. CDROMMethod Mth;
  241. return Mth.Run();
  242. }