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
32 ::Configuration Database
;
37 bool IsCorrectCD(URI want
, string MountPath
);
38 virtual bool Fetch(FetchItem
*Itm
);
39 string
GetID(string Name
);
47 // CDROMMethod::CDROMethod - Constructor /*{{{*/
48 // ---------------------------------------------------------------------
50 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance
| LocalOnly
|
51 SendConfig
| NeedsCleanup
|
53 DatabaseLoaded(false),
60 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
61 // ---------------------------------------------------------------------
63 void CDROMMethod::Exit()
65 if (MountedByApt
== true)
69 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
70 // ---------------------------------------------------------------------
72 string
CDROMMethod::GetID(string Name
)
75 const Configuration::Item
*Top
= Database
.Tree("CD");
81 if (Top
->Value
== Name
)
90 // CDROMMethod::IsCorrectCD /*{{{*/
91 // ---------------------------------------------------------------------
93 bool CDROMMethod::IsCorrectCD(URI want
, string MountPath
)
95 bool Debug
= _config
->FindB("Debug::Acquire::cdrom",false);
98 for (unsigned int Version
= 2; Version
!= 0; Version
--)
100 if (IdentCdrom(MountPath
,NewID
,Version
) == false)
104 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
107 if (Database
.Find("CD::" + NewID
) == want
.Host
)
114 // CDROMMethod::Fetch - Fetch a file /*{{{*/
115 // ---------------------------------------------------------------------
117 bool CDROMMethod::Fetch(FetchItem
*Itm
)
120 string File
= Get
.Path
;
123 bool Debug
= _config
->FindB("Debug::Acquire::cdrom",false);
125 /* All IMS queries are returned as a hit, CDROMs are readonly so
126 time stamps never change */
127 if (Itm
->LastModified
!= 0)
129 Res
.LastModified
= Itm
->LastModified
;
131 Res
.Filename
= Itm
->DestFile
;
137 if (DatabaseLoaded
== false)
140 string DFile
= _config
->FindFile("Dir::State::cdroms");
141 if (FileExists(DFile
) == true)
143 if (ReadConfigFile(Database
,DFile
) == false)
144 return _error
->Error(_("Unable to read the cdrom database %s"),
147 DatabaseLoaded
= true;
150 // All non IMS queries for package files fail.
151 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
153 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
154 " apt-get update cannot be used to add new CD-ROMs"));
158 // We already have a CD inserted, but it is the wrong one
159 if (CurrentID
.empty() == false && Database
.Find("CD::" + CurrentID
) != Get
.Host
)
161 Fail(_("Wrong CD-ROM"),true);
165 CDROM
= _config
->FindDir("Acquire::cdrom::mount","/cdrom/");
168 if (CDROM
== "apt-udev-auto/")
170 pkgUdevCdromDevices udev
;
173 vector
<struct CdromDevice
> v
= udev
.Scan();
174 for (unsigned int i
=0; i
< v
.size(); i
++)
178 if (!FileExists("/media/apt"))
179 mkdir("/media/apt", 0755);
180 if(MountCdrom("/media/apt", v
[i
].DeviceName
))
182 if (IsCorrectCD(Get
, "/media/apt"))
185 CDROM
= "/media/apt";
188 UnmountCdrom("/media/apt");
192 if (IsCorrectCD(Get
, v
[i
].MountPath
))
194 CDROM
= v
[i
].MountPath
;
203 CDROM
= SafeGetCWD() + '/' + CDROM
;
205 while (CurrentID
.empty() == true)
208 if(!IsMounted(CDROM
))
209 MountedByApt
= MountCdrom(CDROM
);
211 if (IsCorrectCD(Get
, CDROM
))
214 // I suppose this should prompt somehow?
215 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
216 UnmountCdrom(CDROM
) == false)
217 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
219 if (MediaFail(Get
.Host
,CDROM
) == false)
222 return _error
->Error(_("Disk not found."));
227 Res
.Filename
= CDROM
+ File
;
229 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
230 return _error
->Error(_("File not found"));
232 if (NewID
.empty() == false)
234 Res
.LastModified
= Buf
.st_mtime
;
235 Res
.Size
= Buf
.st_size
;
238 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
239 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
240 Res
.TakeHashes(Hash
);
249 setlocale(LC_ALL
, "");