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