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 /*{{{*/
11 #include <apt-pkg/acquire-method.h>
12 #include <apt-pkg/cdrom.h>
13 #include <apt-pkg/cdromutl.h>
14 #include <apt-pkg/error.h>
15 #include <apt-pkg/configuration.h>
16 #include <apt-pkg/fileutl.h>
17 #include <apt-pkg/hashes.h>
29 class CDROMMethod
: public pkgAcqMethod
34 ::Configuration Database
;
38 pkgUdevCdromDevices UdevCdroms
;
40 bool IsCorrectCD(URI want
, string MountPath
, string
& NewID
);
41 bool AutoDetectAndMount(const URI
, string
&NewID
);
42 virtual bool Fetch(FetchItem
*Itm
);
43 string
GetID(string Name
);
51 // CDROMMethod::CDROMethod - Constructor /*{{{*/
52 // ---------------------------------------------------------------------
54 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance
| LocalOnly
|
55 SendConfig
| NeedsCleanup
|
57 DatabaseLoaded(false),
63 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
64 // ---------------------------------------------------------------------
66 void CDROMMethod::Exit()
68 if (MountedByApt
== true)
72 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
73 // ---------------------------------------------------------------------
75 string
CDROMMethod::GetID(string Name
)
78 const Configuration::Item
*Top
= Database
.Tree("CD");
84 if (Top
->Value
== Name
)
92 // CDROMMethod::AutoDetectAndMount /*{{{*/
93 // ---------------------------------------------------------------------
94 /* Modifies class varaiable CDROM to the mountpoint */
95 bool CDROMMethod::AutoDetectAndMount(const URI Get
, string
&NewID
)
97 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
99 // first check if its mounted somewhere already
100 for (unsigned int i
=0; i
< v
.size(); i
++)
105 clog
<< "Checking mounted cdrom device " << v
[i
].DeviceName
<< endl
;
106 if (IsCorrectCD(Get
, v
[i
].MountPath
, NewID
))
108 CDROM
= v
[i
].MountPath
;
114 // we are not supposed to mount, exit
115 if (_config
->FindB("APT::CDROM::NoMount",false) == true)
118 // check if we have the mount point
119 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
120 if (!FileExists(AptMountPoint
))
121 mkdir(AptMountPoint
.c_str(), 0750);
124 for (unsigned int i
=0; i
< v
.size(); i
++)
128 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
))
130 if (IsCorrectCD(Get
, AptMountPoint
, NewID
))
133 CDROM
= AptMountPoint
;
136 UnmountCdrom(AptMountPoint
);
145 // CDROMMethod::IsCorrectCD /*{{{*/
146 // ---------------------------------------------------------------------
148 bool CDROMMethod::IsCorrectCD(URI want
, string MountPath
, string
& NewID
)
150 for (unsigned int Version
= 2; Version
!= 0; Version
--)
152 if (IdentCdrom(MountPath
,NewID
,Version
) == false)
156 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
159 if (Database
.Find("CD::" + NewID
) == want
.Host
)
166 // CDROMMethod::Fetch - Fetch a file /*{{{*/
167 // ---------------------------------------------------------------------
169 bool CDROMMethod::Fetch(FetchItem
*Itm
)
174 string File
= Get
.Path
;
175 Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
178 clog
<< "CDROMMethod::Fetch " << Itm
->Uri
<< endl
;
180 /* All IMS queries are returned as a hit, CDROMs are readonly so
181 time stamps never change */
182 if (Itm
->LastModified
!= 0)
184 Res
.LastModified
= Itm
->LastModified
;
186 Res
.Filename
= Itm
->DestFile
;
192 if (DatabaseLoaded
== false)
195 string DFile
= _config
->FindFile("Dir::State::cdroms");
196 if (FileExists(DFile
) == true)
198 if (ReadConfigFile(Database
,DFile
) == false)
199 return _error
->Error(_("Unable to read the cdrom database %s"),
202 DatabaseLoaded
= true;
205 // All non IMS queries for package files fail.
206 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
208 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
209 " apt-get update cannot be used to add new CD-ROMs"));
213 // We already have a CD inserted, but it is the wrong one
214 if (CurrentID
.empty() == false &&
215 CurrentID
!= "FAIL" &&
216 Database
.Find("CD::" + CurrentID
) != Get
.Host
)
218 Fail(_("Wrong CD-ROM"),true);
222 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
223 CDROM
= _config
->FindDir("Acquire::cdrom::mount","/cdrom/");
225 clog
<< "Looking for CDROM at " << CDROM
<< endl
;
228 CDROM
= SafeGetCWD() + '/' + CDROM
;
231 while (CurrentID
.empty() == true)
234 AutoDetectAndMount(Get
, NewID
);
236 if(!IsMounted(CDROM
))
237 MountedByApt
= MountCdrom(CDROM
);
239 if (IsCorrectCD(Get
, CDROM
, NewID
))
242 // I suppose this should prompt somehow?
243 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
244 UnmountCdrom(CDROM
) == false)
245 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
247 if (MediaFail(Get
.Host
,CDROM
) == false)
250 return _error
->Error(_("Disk not found."));
255 Res
.Filename
= CDROM
+ File
;
257 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
258 return _error
->Error(_("File not found"));
260 if (NewID
.empty() == false)
262 Res
.LastModified
= Buf
.st_mtime
;
263 Res
.Size
= Buf
.st_size
;
266 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
267 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
268 Res
.TakeHashes(Hash
);
277 setlocale(LC_ALL
, "");