cdrom.cc 7.2 KB

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