methods/cdrom.cc: fix compile error
[ntk/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 if (!FileExists("/media/apt"))
120 mkdir("/media/apt", 0755);
121
122 // now try mounting
123 for (unsigned int i=0; i < v.size(); i++)
124 {
125 if (!v[i].Mounted)
126 {
127 if(MountCdrom("/media/apt", v[i].DeviceName))
128 {
129 if (IsCorrectCD(Get, "/media/apt", NewID))
130 {
131 MountedByApt = true;
132 CDROM = "/media/apt";
133 return true;
134 } else {
135 UnmountCdrom("/media/apt");
136 }
137 }
138 }
139 }
140
141 return false;
142 }
143 /*}}}*/
144 // CDROMMethod::IsCorrectCD /*{{{*/
145 // ---------------------------------------------------------------------
146 /* */
147 bool CDROMMethod::IsCorrectCD(URI want, string MountPath, string& NewID)
148 {
149 for (unsigned int Version = 2; Version != 0; Version--)
150 {
151 if (IdentCdrom(MountPath,NewID,Version) == false)
152 return false;
153
154 if (Debug)
155 clog << "ID " << Version << " " << NewID << endl;
156
157 // A hit
158 if (Database.Find("CD::" + NewID) == want.Host)
159 return true;
160 }
161
162 return false;
163 }
164 /*}}}*/
165 // CDROMMethod::Fetch - Fetch a file /*{{{*/
166 // ---------------------------------------------------------------------
167 /* */
168 bool CDROMMethod::Fetch(FetchItem *Itm)
169 {
170 FetchResult Res;
171
172 URI Get = Itm->Uri;
173 string File = Get.Path;
174 Debug = _config->FindB("Debug::Acquire::cdrom", false);
175
176 if (Debug)
177 clog << "CDROMMethod::Fetch " << Itm->Uri << endl;
178
179 /* All IMS queries are returned as a hit, CDROMs are readonly so
180 time stamps never change */
181 if (Itm->LastModified != 0)
182 {
183 Res.LastModified = Itm->LastModified;
184 Res.IMSHit = true;
185 Res.Filename = Itm->DestFile;
186 URIDone(Res);
187 return true;
188 }
189
190 // Load the database
191 if (DatabaseLoaded == false)
192 {
193 // Read the database
194 string DFile = _config->FindFile("Dir::State::cdroms");
195 if (FileExists(DFile) == true)
196 {
197 if (ReadConfigFile(Database,DFile) == false)
198 return _error->Error(_("Unable to read the cdrom database %s"),
199 DFile.c_str());
200 }
201 DatabaseLoaded = true;
202 }
203
204 // All non IMS queries for package files fail.
205 if (Itm->IndexFile == true || GetID(Get.Host).empty() == true)
206 {
207 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
208 " apt-get update cannot be used to add new CD-ROMs"));
209 return true;
210 }
211
212 // We already have a CD inserted, but it is the wrong one
213 if (CurrentID.empty() == false &&
214 CurrentID != "FAIL" &&
215 Database.Find("CD::" + CurrentID) != Get.Host)
216 {
217 Fail(_("Wrong CD-ROM"),true);
218 return true;
219 }
220
221 CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/");
222 if (Debug)
223 clog << "Looking for CDROM at " << CDROM << endl;
224
225 if (CDROM[0] == '.')
226 CDROM= SafeGetCWD() + '/' + CDROM;
227
228 string NewID;
229 while (CurrentID.empty() == true)
230 {
231 if (CDROM == "apt-udev-auto/")
232 AutoDetectAndMount(Get, NewID);
233
234 if(!IsMounted(CDROM))
235 MountedByApt = MountCdrom(CDROM);
236
237 if (IsCorrectCD(Get, CDROM, NewID))
238 break;
239
240 // I suppose this should prompt somehow?
241 if (_config->FindB("APT::CDROM::NoMount",false) == false &&
242 UnmountCdrom(CDROM) == false)
243 return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
244 CDROM.c_str());
245 if (MediaFail(Get.Host,CDROM) == false)
246 {
247 CurrentID = "FAIL";
248 return _error->Error(_("Disk not found."));
249 }
250 }
251
252 // Found a CD
253 Res.Filename = CDROM + File;
254 struct stat Buf;
255 if (stat(Res.Filename.c_str(),&Buf) != 0)
256 return _error->Error(_("File not found"));
257
258 if (NewID.empty() == false)
259 CurrentID = NewID;
260 Res.LastModified = Buf.st_mtime;
261 Res.Size = Buf.st_size;
262
263 Hashes Hash;
264 FileFd Fd(Res.Filename, FileFd::ReadOnly);
265 Hash.AddFD(Fd.Fd(), Fd.Size());
266 Res.TakeHashes(Hash);
267
268 URIDone(Res);
269 return true;
270 }
271 /*}}}*/
272
273 int main()
274 {
275 setlocale(LC_ALL, "");
276
277 CDROMMethod Mth;
278 return Mth.Run();
279 }