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