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