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