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