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