cdrom.cc 7.2 KB

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