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