]>
git.saurik.com Git - apt.git/blob - methods/cdrom.cc
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 clog
<< "CDROMMethod::Fetch " << Itm
->Uri
<< endl
;
127 /* All IMS queries are returned as a hit, CDROMs are readonly so
128 time stamps never change */
129 if (Itm
->LastModified
!= 0)
131 Res
.LastModified
= Itm
->LastModified
;
133 Res
.Filename
= Itm
->DestFile
;
139 if (DatabaseLoaded
== false)
142 string DFile
= _config
->FindFile("Dir::State::cdroms");
143 if (FileExists(DFile
) == true)
145 if (ReadConfigFile(Database
,DFile
) == false)
146 return _error
->Error(_("Unable to read the cdrom database %s"),
149 DatabaseLoaded
= true;
152 // All non IMS queries for package files fail.
153 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
155 Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT."
156 " apt-get update cannot be used to add new CD-ROMs"));
160 // We already have a CD inserted, but it is the wrong one
161 if (CurrentID
.empty() == false &&
162 CurrentID
!= "FAIL" &&
163 Database
.Find("CD::" + CurrentID
) != Get
.Host
)
165 Fail(_("Wrong CD-ROM"),true);
169 CDROM
= _config
->FindDir("Acquire::cdrom::mount","/cdrom/");
171 clog
<< "Looking for CDROM at " << CDROM
<< endl
;
174 CDROM
= SafeGetCWD() + '/' + CDROM
;
176 pkgUdevCdromDevices udev
;
178 while (CurrentID
.empty() == true)
180 // hrm, ugly the loop here
181 if (CDROM
== "apt-udev-auto/")
185 vector
<struct CdromDevice
> v
= udev
.Scan();
186 for (unsigned int i
=0; i
< v
.size(); i
++)
189 clog
<< "Have cdrom device " << v
[i
].DeviceName
<< endl
;
192 if (!FileExists("/media/apt"))
193 mkdir("/media/apt", 0755);
194 if(MountCdrom("/media/apt", v
[i
].DeviceName
))
196 if (IsCorrectCD(Get
, "/media/apt"))
199 CDROM
= "/media/apt";
202 UnmountCdrom("/media/apt");
206 if (IsCorrectCD(Get
, v
[i
].MountPath
))
208 CDROM
= v
[i
].MountPath
;
214 _error
->WarningE("udev.Dlopen() failed","foo");
219 if(!IsMounted(CDROM
))
220 MountedByApt
= MountCdrom(CDROM
);
222 if (IsCorrectCD(Get
, CDROM
))
225 // I suppose this should prompt somehow?
226 if (_config
->FindB("APT::CDROM::NoMount",false) == false &&
227 UnmountCdrom(CDROM
) == false)
228 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
230 if (MediaFail(Get
.Host
,CDROM
) == false)
233 return _error
->Error(_("Disk not found."));
238 Res
.Filename
= CDROM
+ File
;
240 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
241 return _error
->Error(_("File not found"));
243 if (NewID
.empty() == false)
245 Res
.LastModified
= Buf
.st_mtime
;
246 Res
.Size
= Buf
.st_size
;
249 FileFd
Fd(Res
.Filename
, FileFd::ReadOnly
);
250 Hash
.AddFD(Fd
.Fd(), Fd
.Size());
251 Res
.TakeHashes(Hash
);
260 setlocale(LC_ALL
, "");