1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cdrom.cc,v 1.20.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 CDROM URI method for APT
8 ##################################################################### */
10 // Include Files /*{{{*/
13 #include <apt-pkg/acquire-method.h>
14 #include <apt-pkg/cdrom.h>
15 #include <apt-pkg/cdromutl.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/fileutl.h>
19 #include <apt-pkg/hashes.h>
31 class CDROMMethod
: public pkgAcqMethod
36 ::Configuration Database
;
40 pkgUdevCdromDevices UdevCdroms
;
42 bool IsCorrectCD(URI want
, string MountPath
, string
& NewID
);
43 bool AutoDetectAndMount(const URI
, string
&NewID
);
44 virtual bool Fetch(FetchItem
*Itm
);
45 string
GetID(string Name
);
53 // CDROMMethod::CDROMethod - Constructor /*{{{*/
54 // ---------------------------------------------------------------------
56 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance
| LocalOnly
|
57 SendConfig
| NeedsCleanup
|
59 DatabaseLoaded(false),
65 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
66 // ---------------------------------------------------------------------
68 void CDROMMethod::Exit()
70 if (MountedByApt
== true)
74 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
75 // ---------------------------------------------------------------------
77 string
CDROMMethod::GetID(string Name
)
80 const Configuration::Item
*Top
= Database
.Tree("CD");
86 if (Top
->Value
== Name
)
94 // CDROMMethod::AutoDetectAndMount /*{{{*/
95 // ---------------------------------------------------------------------
96 /* Modifies class varaiable CDROM to the mountpoint */
97 bool CDROMMethod::AutoDetectAndMount(const URI Get
, string
&NewID
)
99 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
101 // first check if its mounted somewhere already
102 for (unsigned int i
=0; i
< v
.size(); i
++)
107 clog
<< "Checking mounted cdrom device " << v
[i
].DeviceName
<< endl
;
108 if (IsCorrectCD(Get
, v
[i
].MountPath
, NewID
))
110 CDROM
= v
[i
].MountPath
;
116 // we are not supposed to mount, exit
117 if (_config
->FindB("APT::CDROM::NoMount",false) == true)
120 // check if we have the mount point
121 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
122 if (!FileExists(AptMountPoint
))
123 mkdir(AptMountPoint
.c_str(), 0750);
126 for (unsigned int i
=0; i
< v
.size(); i
++)
130 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
))
132 if (IsCorrectCD(Get
, AptMountPoint
, NewID
))
135 CDROM
= AptMountPoint
;
138 UnmountCdrom(AptMountPoint
);
147 // CDROMMethod::IsCorrectCD /*{{{*/
148 // ---------------------------------------------------------------------
150 bool CDROMMethod::IsCorrectCD(URI want
, string MountPath
, string
& NewID
)
152 for (unsigned int Version
= 2; Version
!= 0; Version
--)
154 if (IdentCdrom(MountPath
,NewID
,Version
) == false)
158 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
161 if (Database
.Find("CD::" + NewID
) == want
.Host
)
168 // CDROMMethod::Fetch - Fetch a file /*{{{*/
169 // ---------------------------------------------------------------------
171 bool CDROMMethod::Fetch(FetchItem
*Itm
)
176 string File
= Get
.Path
;
177 Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
180 clog
<< "CDROMMethod::Fetch " << Itm
->Uri
<< endl
;
182 /* All IMS queries are returned as a hit, CDROMs are readonly so
183 time stamps never change */
184 if (Itm
->LastModified
!= 0)
186 Res
.LastModified
= Itm
->LastModified
;
188 Res
.Filename
= Itm
->DestFile
;
194 if (DatabaseLoaded
== false)
197 string DFile
= _config
->FindFile("Dir::State::cdroms");
198 if (FileExists(DFile
) == true)
200 if (ReadConfigFile(Database
,DFile
) == false)
201 return _error
->Error(_("Unable to read the cdrom database %s"),
204 DatabaseLoaded
= true;
207 // All non IMS queries for package files fail.
208 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
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"));
215 // We already have a CD inserted, but it is the wrong one
216 if (CurrentID
.empty() == false &&
217 CurrentID
!= "FAIL" &&
218 Database
.Find("CD::" + CurrentID
) != Get
.Host
)
220 Fail(_("Wrong CD-ROM"),true);
224 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
225 CDROM
= _config
->FindDir("Acquire::cdrom::mount");
227 clog
<< "Looking for CDROM at " << CDROM
<< endl
;
230 CDROM
= SafeGetCWD() + '/' + CDROM
;
233 while (CurrentID
.empty() == true)
236 AutoDetectAndMount(Get
, NewID
);
238 if(!IsMounted(CDROM
))
239 MountedByApt
= MountCdrom(CDROM
);
241 if (IsCorrectCD(Get
, CDROM
, NewID
))
244 // I suppose this should prompt somehow?
245 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
246 UnmountCdrom(CDROM
) == false)
247 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
249 if (MediaFail(Get
.Host
,CDROM
) == false)
252 return _error
->Error(_("Disk not found."));
257 Res
.Filename
= CDROM
+ File
;
259 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
260 return _error
->Error(_("File not found"));
262 if (NewID
.empty() == false)
264 Res
.LastModified
= Buf
.st_mtime
;
265 Res
.Size
= Buf
.st_size
;
268 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
269 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
270 Res
.TakeHashes(Hash
);
279 setlocale(LC_ALL
, "");