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),
64 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
65 // ---------------------------------------------------------------------
67 void CDROMMethod::Exit()
69 if (MountedByApt
== true)
73 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
74 // ---------------------------------------------------------------------
76 string
CDROMMethod::GetID(string Name
)
79 const Configuration::Item
*Top
= Database
.Tree("CD");
85 if (Top
->Value
== Name
)
93 // CDROMMethod::AutoDetectAndMount /*{{{*/
94 // ---------------------------------------------------------------------
95 /* Modifies class varaiable CDROM to the mountpoint */
96 bool CDROMMethod::AutoDetectAndMount(const URI Get
, string
&NewID
)
98 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
100 // first check if its mounted somewhere already
101 for (unsigned int i
=0; i
< v
.size(); i
++)
106 clog
<< "Checking mounted cdrom device " << v
[i
].DeviceName
<< endl
;
107 if (IsCorrectCD(Get
, v
[i
].MountPath
, NewID
))
109 CDROM
= v
[i
].MountPath
;
115 // we are not supposed to mount, exit
116 if (_config
->FindB("APT::CDROM::NoMount",false) == true)
119 // check if we have the mount point
120 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
121 if (!FileExists(AptMountPoint
))
122 mkdir(AptMountPoint
.c_str(), 0750);
125 for (unsigned int i
=0; i
< v
.size(); i
++)
129 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
))
131 if (IsCorrectCD(Get
, AptMountPoint
, NewID
))
134 CDROM
= AptMountPoint
;
137 UnmountCdrom(AptMountPoint
);
146 // CDROMMethod::IsCorrectCD /*{{{*/
147 // ---------------------------------------------------------------------
149 bool CDROMMethod::IsCorrectCD(URI want
, string MountPath
, string
& NewID
)
151 for (unsigned int Version
= 2; Version
!= 0; Version
--)
153 if (IdentCdrom(MountPath
,NewID
,Version
) == false)
157 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
160 if (Database
.Find("CD::" + NewID
) == want
.Host
)
167 // CDROMMethod::Fetch - Fetch a file /*{{{*/
168 // ---------------------------------------------------------------------
170 bool CDROMMethod::Fetch(FetchItem
*Itm
)
175 string File
= Get
.Path
;
176 Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
179 clog
<< "CDROMMethod::Fetch " << Itm
->Uri
<< endl
;
181 /* All IMS queries are returned as a hit, CDROMs are readonly so
182 time stamps never change */
183 if (Itm
->LastModified
!= 0)
185 Res
.LastModified
= Itm
->LastModified
;
187 Res
.Filename
= Itm
->DestFile
;
193 if (DatabaseLoaded
== false)
196 string DFile
= _config
->FindFile("Dir::State::cdroms");
197 if (FileExists(DFile
) == true)
199 if (ReadConfigFile(Database
,DFile
) == false)
200 return _error
->Error(_("Unable to read the cdrom database %s"),
203 DatabaseLoaded
= true;
206 // All non IMS queries for package files fail.
207 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
209 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
210 " apt-get update cannot be used to add new CD-ROMs"));
214 // We already have a CD inserted, but it is the wrong one
215 if (CurrentID
.empty() == false &&
216 CurrentID
!= "FAIL" &&
217 Database
.Find("CD::" + CurrentID
) != Get
.Host
)
219 Fail(_("Wrong CD-ROM"),true);
223 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
224 CDROM
= _config
->FindDir("Acquire::cdrom::mount");
226 clog
<< "Looking for CDROM at " << CDROM
<< endl
;
229 CDROM
= SafeGetCWD() + '/' + CDROM
;
232 while (CurrentID
.empty() == true)
235 AutoDetectAndMount(Get
, NewID
);
237 if(!IsMounted(CDROM
))
238 MountedByApt
= MountCdrom(CDROM
);
240 if (IsCorrectCD(Get
, CDROM
, NewID
))
243 // I suppose this should prompt somehow?
244 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
245 UnmountCdrom(CDROM
) == false)
246 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
248 if (MediaFail(Get
.Host
,CDROM
) == false)
251 return _error
->Error(_("Disk not found."));
256 Res
.Filename
= CDROM
+ File
;
258 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
259 return _error
->Error(_("File not found"));
261 if (NewID
.empty() == false)
263 Res
.LastModified
= Buf
.st_mtime
;
264 Res
.Size
= Buf
.st_size
;
267 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
268 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
269 Res
.TakeHashes(Hash
);
278 setlocale(LC_ALL
, "");