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