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),
66 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
67 // ---------------------------------------------------------------------
69 void CDROMMethod::Exit()
71 if (MountedByApt
== true)
75 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
76 // ---------------------------------------------------------------------
78 string
CDROMMethod::GetID(string Name
)
81 const Configuration::Item
*Top
= Database
.Tree("CD");
87 if (Top
->Value
== Name
)
95 // CDROMMethod::AutoDetectAndMount /*{{{*/
96 // ---------------------------------------------------------------------
97 /* Modifies class varaiable CDROM to the mountpoint */
98 bool CDROMMethod::AutoDetectAndMount(const URI Get
, string
&NewID
)
100 vector
<struct CdromDevice
> v
= UdevCdroms
.Scan();
102 // first check if its mounted somewhere already
103 for (unsigned int i
=0; i
< v
.size(); i
++)
108 clog
<< "Checking mounted cdrom device " << v
[i
].DeviceName
<< endl
;
109 if (IsCorrectCD(Get
, v
[i
].MountPath
, NewID
))
111 CDROM
= v
[i
].MountPath
;
117 // we are not supposed to mount, exit
118 if (_config
->FindB("APT::CDROM::NoMount",false) == true)
121 // check if we have the mount point
122 string AptMountPoint
= _config
->FindDir("Dir::Media::MountPath");
123 if (!FileExists(AptMountPoint
))
124 mkdir(AptMountPoint
.c_str(), 0750);
127 for (unsigned int i
=0; i
< v
.size(); i
++)
131 if(MountCdrom(AptMountPoint
, v
[i
].DeviceName
))
133 if (IsCorrectCD(Get
, AptMountPoint
, NewID
))
136 CDROM
= AptMountPoint
;
139 UnmountCdrom(AptMountPoint
);
148 // CDROMMethod::IsCorrectCD /*{{{*/
149 // ---------------------------------------------------------------------
151 bool CDROMMethod::IsCorrectCD(URI want
, string MountPath
, string
& NewID
)
153 for (unsigned int Version
= 2; Version
!= 0; Version
--)
155 if (IdentCdrom(MountPath
,NewID
,Version
) == false)
159 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
162 if (Database
.Find("CD::" + NewID
) == want
.Host
)
169 // CDROMMethod::Fetch - Fetch a file /*{{{*/
170 // ---------------------------------------------------------------------
172 bool CDROMMethod::Fetch(FetchItem
*Itm
)
177 string File
= Get
.Path
;
178 Debug
= _config
->FindB("Debug::Acquire::cdrom", false);
181 clog
<< "CDROMMethod::Fetch " << Itm
->Uri
<< endl
;
183 /* All IMS queries are returned as a hit, CDROMs are readonly so
184 time stamps never change */
185 if (Itm
->LastModified
!= 0)
187 Res
.LastModified
= Itm
->LastModified
;
189 Res
.Filename
= Itm
->DestFile
;
195 if (DatabaseLoaded
== false)
198 string DFile
= _config
->FindFile("Dir::State::cdroms");
199 if (FileExists(DFile
) == true)
201 if (ReadConfigFile(Database
,DFile
) == false)
202 return _error
->Error(_("Unable to read the cdrom database %s"),
205 DatabaseLoaded
= true;
208 // All non IMS queries for package files fail.
209 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
211 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
212 " apt-get update cannot be used to add new CD-ROMs"));
216 // We already have a CD inserted, but it is the wrong one
217 if (CurrentID
.empty() == false &&
218 CurrentID
!= "FAIL" &&
219 Database
.Find("CD::" + CurrentID
) != Get
.Host
)
221 Fail(_("Wrong CD-ROM"),true);
225 bool AutoDetect
= _config
->FindB("Acquire::cdrom::AutoDetect", true);
226 CDROM
= _config
->FindDir("Acquire::cdrom::mount");
228 clog
<< "Looking for CDROM at " << CDROM
<< endl
;
231 CDROM
= SafeGetCWD() + '/' + CDROM
;
234 while (CurrentID
.empty() == true)
237 AutoDetectAndMount(Get
, NewID
);
239 if(!IsMounted(CDROM
))
240 MountedByApt
= MountCdrom(CDROM
);
242 if (IsCorrectCD(Get
, CDROM
, NewID
))
245 // I suppose this should prompt somehow?
246 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
247 UnmountCdrom(CDROM
) == false)
248 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
250 if (MediaFail(Get
.Host
,CDROM
) == false)
253 return _error
->Error(_("Disk not found."));
258 Res
.Filename
= CDROM
+ File
;
260 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
261 return _error
->Error(_("File not found"));
263 if (NewID
.empty() == false)
265 Res
.LastModified
= Buf
.st_mtime
;
266 Res
.Size
= Buf
.st_size
;
269 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
270 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
271 Res
.TakeHashes(Hash
);
280 setlocale(LC_ALL
, "");