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/strutl.h>
20 #include <apt-pkg/hashes.h>
32 class CDROMMethod
: public pkgAcqMethod
37 ::Configuration Database
;
41 pkgUdevCdromDevices UdevCdroms
;
43 bool IsCorrectCD(URI want
, string MountPath
, string
& NewID
);
44 bool AutoDetectAndMount(const URI
, string
&NewID
);
45 virtual bool Fetch(FetchItem
*Itm
);
46 string
GetID(string Name
);
54 // CDROMMethod::CDROMethod - Constructor /*{{{*/
55 // ---------------------------------------------------------------------
57 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance
| LocalOnly
|
58 SendConfig
| NeedsCleanup
|
60 DatabaseLoaded(false),
67 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
68 // ---------------------------------------------------------------------
70 void CDROMMethod::Exit()
72 if (MountedByApt
== true)
76 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
77 // ---------------------------------------------------------------------
79 string
CDROMMethod::GetID(string Name
)
82 const Configuration::Item
*Top
= Database
.Tree("CD");
88 if (Top
->Value
== Name
)
96 // CDROMMethod::AutoDetectAndMount /*{{{*/
97 // ---------------------------------------------------------------------
98 /* Modifies class varaiable CDROM to the mountpoint */
99 bool CDROMMethod::AutoDetectAndMount(const URI Get
, string
&NewID
)
101 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
103 // first check if its mounted somewhere already
104 for (unsigned int i
=0; i
< v
.size(); i
++)
109 clog
<< "Checking mounted cdrom device " << v
[i
].DeviceName
<< endl
;
110 if (IsCorrectCD(Get
, v
[i
].MountPath
, NewID
))
112 CDROM
= v
[i
].MountPath
;
118 // we are not supposed to mount, exit
119 if (_config
->FindB("APT::CDROM::NoMount",false) == true)
122 // check if we have the mount point
123 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
124 if (!FileExists(AptMountPoint
))
125 mkdir(AptMountPoint
.c_str(), 0750);
128 for (unsigned int i
=0; i
< v
.size(); i
++)
132 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
))
134 if (IsCorrectCD(Get
, AptMountPoint
, NewID
))
137 CDROM
= AptMountPoint
;
140 UnmountCdrom(AptMountPoint
);
149 // CDROMMethod::IsCorrectCD /*{{{*/
150 // ---------------------------------------------------------------------
152 bool CDROMMethod::IsCorrectCD(URI want
, string MountPath
, string
& NewID
)
154 for (unsigned int Version
= 2; Version
!= 0; Version
--)
156 if (IdentCdrom(MountPath
,NewID
,Version
) == false)
160 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
163 if (Database
.Find("CD::" + NewID
) == want
.Host
)
170 // CDROMMethod::Fetch - Fetch a file /*{{{*/
171 // ---------------------------------------------------------------------
173 bool CDROMMethod::Fetch(FetchItem
*Itm
)
178 string File
= Get
.Path
;
179 Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
182 clog
<< "CDROMMethod::Fetch " << Itm
->Uri
<< endl
;
184 /* All IMS queries are returned as a hit, CDROMs are readonly so
185 time stamps never change */
186 if (Itm
->LastModified
!= 0)
188 Res
.LastModified
= Itm
->LastModified
;
190 Res
.Filename
= Itm
->DestFile
;
196 if (DatabaseLoaded
== false)
199 string DFile
= _config
->FindFile("Dir::State::cdroms");
200 if (FileExists(DFile
) == true)
202 if (ReadConfigFile(Database
,DFile
) == false)
203 return _error
->Error(_("Unable to read the cdrom database %s"),
206 DatabaseLoaded
= true;
209 // All non IMS queries for package files fail.
210 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
212 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
213 " apt-get update cannot be used to add new CD-ROMs"));
217 // We already have a CD inserted, but it is the wrong one
218 if (CurrentID
.empty() == false &&
219 CurrentID
!= "FAIL" &&
220 Database
.Find("CD::" + CurrentID
) != Get
.Host
)
222 Fail(_("Wrong CD-ROM"),true);
226 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
227 CDROM
= _config
->FindDir("Acquire::cdrom::mount");
229 clog
<< "Looking for CDROM at " << CDROM
<< endl
;
232 CDROM
= SafeGetCWD() + '/' + CDROM
;
235 while (CurrentID
.empty() == true)
238 AutoDetectAndMount(Get
, NewID
);
240 if(!IsMounted(CDROM
))
241 MountedByApt
= MountCdrom(CDROM
);
243 if (IsCorrectCD(Get
, CDROM
, NewID
))
246 // I suppose this should prompt somehow?
247 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
248 UnmountCdrom(CDROM
) == false)
249 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
251 if (MediaFail(Get
.Host
,CDROM
) == false)
254 return _error
->Error(_("Disk not found."));
259 Res
.Filename
= CDROM
+ File
;
261 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
262 return _error
->Error(_("File not found"));
264 if (NewID
.empty() == false)
266 Res
.LastModified
= Buf
.st_mtime
;
267 Res
.Size
= Buf
.st_size
;
269 Hashes
Hash(Itm
->ExpectedHashes
);
270 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
272 Res
.TakeHashes(Hash
);
281 setlocale(LC_ALL
, "");