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