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