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