]>
Commit | Line | Data |
---|---|---|
f46e7681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: cdrom.cc,v 1.20.2.1 2004/01/16 18:58:50 mdz Exp $ |
f46e7681 AL |
4 | /* ###################################################################### |
5 | ||
6 | CDROM URI method for APT | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include Files /*{{{*/ | |
11 | #include <apt-pkg/acquire-method.h> | |
a6418a4b | 12 | #include <apt-pkg/cdrom.h> |
f46e7681 AL |
13 | #include <apt-pkg/cdromutl.h> |
14 | #include <apt-pkg/error.h> | |
15 | #include <apt-pkg/configuration.h> | |
16 | #include <apt-pkg/fileutl.h> | |
13e8426f | 17 | #include <apt-pkg/hashes.h> |
f46e7681 AL |
18 | |
19 | #include <sys/stat.h> | |
20 | #include <unistd.h> | |
8e372e79 | 21 | #include <dlfcn.h> |
076cc664 AL |
22 | |
23 | #include <iostream> | |
d77559ac | 24 | #include <apti18n.h> |
f46e7681 AL |
25 | /*}}}*/ |
26 | ||
076cc664 AL |
27 | using namespace std; |
28 | ||
f46e7681 AL |
29 | class CDROMMethod : public pkgAcqMethod |
30 | { | |
f631d1ba | 31 | bool DatabaseLoaded; |
76fe5db7 MV |
32 | bool Debug; |
33 | ||
5b76e7f2 | 34 | ::Configuration Database; |
f46e7681 | 35 | string CurrentID; |
8e5fc8f5 | 36 | string CDROM; |
70dbf5f8 | 37 | bool MountedByApt; |
76fe5db7 | 38 | pkgUdevCdromDevices UdevCdroms; |
8e372e79 | 39 | |
bf783d90 MV |
40 | bool IsCorrectCD(URI want, string MountPath, string& NewID); |
41 | bool AutoDetectAndMount(URI, string &NewID); | |
f46e7681 AL |
42 | virtual bool Fetch(FetchItem *Itm); |
43 | string GetID(string Name); | |
8e5fc8f5 | 44 | virtual void Exit(); |
8e372e79 | 45 | |
f46e7681 AL |
46 | public: |
47 | ||
48 | CDROMMethod(); | |
49 | }; | |
50 | ||
51 | // CDROMMethod::CDROMethod - Constructor /*{{{*/ | |
52 | // --------------------------------------------------------------------- | |
53 | /* */ | |
459681d3 AL |
54 | CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly | |
55 | SendConfig | NeedsCleanup | | |
56 | Removable), | |
8e5fc8f5 | 57 | DatabaseLoaded(false), |
70dbf5f8 | 58 | MountedByApt(false) |
f46e7681 | 59 | { |
76fe5db7 | 60 | UdevCdroms.Dlopen(); |
f46e7681 AL |
61 | }; |
62 | /*}}}*/ | |
8e5fc8f5 AL |
63 | // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/ |
64 | // --------------------------------------------------------------------- | |
65 | /* */ | |
66 | void CDROMMethod::Exit() | |
8e372e79 MV |
67 | { |
68 | if (MountedByApt == true) | |
8e5fc8f5 AL |
69 | UnmountCdrom(CDROM); |
70 | } | |
71 | /*}}}*/ | |
e42eb508 | 72 | // CDROMMethod::GetID - Search the database for a matching string /*{{{*/ |
f46e7681 | 73 | // --------------------------------------------------------------------- |
e42eb508 | 74 | /* */ |
f46e7681 AL |
75 | string CDROMMethod::GetID(string Name) |
76 | { | |
e42eb508 | 77 | // Search for an ID |
f631d1ba | 78 | const Configuration::Item *Top = Database.Tree("CD"); |
b7d9b68e AL |
79 | if (Top != 0) |
80 | Top = Top->Child; | |
e42eb508 | 81 | |
f46e7681 | 82 | for (; Top != 0;) |
e42eb508 | 83 | { |
f46e7681 AL |
84 | if (Top->Value == Name) |
85 | return Top->Tag; | |
e42eb508 | 86 | |
f46e7681 | 87 | Top = Top->Next; |
e42eb508 | 88 | } |
f46e7681 AL |
89 | return string(); |
90 | } | |
91 | /*}}}*/ | |
76fe5db7 MV |
92 | // CDROMMethod::AutoDetectAndMount /*{{{*/ |
93 | // --------------------------------------------------------------------- | |
94 | /* Modifies class varaiable CDROM to the mountpoint */ | |
c1f168f5 | 95 | bool CDROMMethod::AutoDetectAndMount(URI Get, string &NewID) |
76fe5db7 MV |
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; | |
bf783d90 | 106 | if (IsCorrectCD(Get, v[i].MountPath, NewID)) |
76fe5db7 MV |
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); | |
a6418a4b | 121 | |
76fe5db7 MV |
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 | { | |
bf783d90 | 129 | if (IsCorrectCD(Get, "/media/apt", NewID)) |
76fe5db7 MV |
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 | /*}}}*/ | |
a6418a4b MV |
144 | // CDROMMethod::IsCorrectCD /*{{{*/ |
145 | // --------------------------------------------------------------------- | |
146 | /* */ | |
bf783d90 | 147 | bool CDROMMethod::IsCorrectCD(URI want, string MountPath, string& NewID) |
a6418a4b | 148 | { |
a6418a4b MV |
149 | for (unsigned int Version = 2; Version != 0; Version--) |
150 | { | |
151 | if (IdentCdrom(MountPath,NewID,Version) == false) | |
152 | return false; | |
153 | ||
76fe5db7 | 154 | if (Debug) |
a6418a4b MV |
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 | /*}}}*/ | |
f46e7681 AL |
165 | // CDROMMethod::Fetch - Fetch a file /*{{{*/ |
166 | // --------------------------------------------------------------------- | |
167 | /* */ | |
168 | bool CDROMMethod::Fetch(FetchItem *Itm) | |
169 | { | |
76fe5db7 MV |
170 | FetchResult Res; |
171 | ||
f46e7681 AL |
172 | URI Get = Itm->Uri; |
173 | string File = Get.Path; | |
76fe5db7 | 174 | Debug = _config->FindB("Debug::Acquire::cdrom", false); |
34fc0421 | 175 | |
49cb36fc MV |
176 | if (Debug) |
177 | clog << "CDROMMethod::Fetch " << Itm->Uri << endl; | |
34fc0421 | 178 | |
f46e7681 AL |
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; | |
2aab5956 | 185 | Res.Filename = Itm->DestFile; |
f46e7681 AL |
186 | URIDone(Res); |
187 | return true; | |
188 | } | |
e42eb508 AL |
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) | |
dc738e7a | 198 | return _error->Error(_("Unable to read the cdrom database %s"), |
e42eb508 AL |
199 | DFile.c_str()); |
200 | } | |
201 | DatabaseLoaded = true; | |
202 | } | |
203 | ||
f46e7681 | 204 | // All non IMS queries for package files fail. |
e42eb508 | 205 | if (Itm->IndexFile == true || GetID(Get.Host).empty() == true) |
f46e7681 | 206 | { |
db0db9fe CP |
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")); | |
9e0349cc | 209 | return true; |
f46e7681 AL |
210 | } |
211 | ||
212 | // We already have a CD inserted, but it is the wrong one | |
49cb36fc MV |
213 | if (CurrentID.empty() == false && |
214 | CurrentID != "FAIL" && | |
215 | Database.Find("CD::" + CurrentID) != Get.Host) | |
f46e7681 | 216 | { |
db0db9fe | 217 | Fail(_("Wrong CD-ROM"),true); |
9e0349cc | 218 | return true; |
f46e7681 | 219 | } |
a6418a4b | 220 | |
ff1e4b06 | 221 | bool AutoDetect = _config->FindB("Acquire::cdrom::AutoDetect", true); |
8e5fc8f5 | 222 | CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/"); |
49cb36fc MV |
223 | if (Debug) |
224 | clog << "Looking for CDROM at " << CDROM << endl; | |
a6418a4b | 225 | |
49cb36fc MV |
226 | if (CDROM[0] == '.') |
227 | CDROM= SafeGetCWD() + '/' + CDROM; | |
49cb36fc | 228 | |
bf783d90 | 229 | string NewID; |
49cb36fc | 230 | while (CurrentID.empty() == true) |
a6418a4b | 231 | { |
ff1e4b06 | 232 | if (AutoDetect) |
bf783d90 | 233 | AutoDetectAndMount(Get, NewID); |
a6418a4b | 234 | |
70dbf5f8 MV |
235 | if(!IsMounted(CDROM)) |
236 | MountedByApt = MountCdrom(CDROM); | |
34fc0421 | 237 | |
bf783d90 | 238 | if (IsCorrectCD(Get, CDROM, NewID)) |
175f08ac AL |
239 | break; |
240 | ||
4df0b629 | 241 | // I suppose this should prompt somehow? |
50959877 MV |
242 | if (_config->FindB("APT::CDROM::NoMount",false) == false && |
243 | UnmountCdrom(CDROM) == false) | |
dc738e7a | 244 | return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."), |
4df0b629 | 245 | CDROM.c_str()); |
018f1533 AL |
246 | if (MediaFail(Get.Host,CDROM) == false) |
247 | { | |
76d97c26 | 248 | CurrentID = "FAIL"; |
2a749770 | 249 | return _error->Error(_("Disk not found.")); |
018f1533 | 250 | } |
f46e7681 AL |
251 | } |
252 | ||
e42eb508 AL |
253 | // Found a CD |
254 | Res.Filename = CDROM + File; | |
255 | struct stat Buf; | |
256 | if (stat(Res.Filename.c_str(),&Buf) != 0) | |
dc738e7a | 257 | return _error->Error(_("File not found")); |
f46e7681 | 258 | |
281daf46 AL |
259 | if (NewID.empty() == false) |
260 | CurrentID = NewID; | |
e42eb508 | 261 | Res.LastModified = Buf.st_mtime; |
e42eb508 | 262 | Res.Size = Buf.st_size; |
13e8426f MV |
263 | |
264 | Hashes Hash; | |
265 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
266 | Hash.AddFD(Fd.Fd(), Fd.Size()); | |
267 | Res.TakeHashes(Hash); | |
268 | ||
e42eb508 AL |
269 | URIDone(Res); |
270 | return true; | |
f46e7681 AL |
271 | } |
272 | /*}}}*/ | |
273 | ||
274 | int main() | |
275 | { | |
b25423f6 MZ |
276 | setlocale(LC_ALL, ""); |
277 | ||
f46e7681 AL |
278 | CDROMMethod Mth; |
279 | return Mth.Run(); | |
280 | } |