]>
git.saurik.com Git - apt.git/blob - methods/cdrom.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cdrom.cc,v 1.21 2004/01/07 20:39:38 mdz Exp $
4 /* ######################################################################
6 CDROM URI method for APT
8 ##################################################################### */
10 // Include Files /*{{{*/
11 #include <apt-pkg/acquire-method.h>
12 #include <apt-pkg/cdromutl.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/configuration.h>
15 #include <apt-pkg/fileutl.h>
26 class CDROMMethod
: public pkgAcqMethod
29 ::Configuration Database
;
34 virtual bool Fetch(FetchItem
*Itm
);
35 string
GetID(string Name
);
43 // CDROMMethod::CDROMethod - Constructor /*{{{*/
44 // ---------------------------------------------------------------------
46 CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance
| LocalOnly
|
47 SendConfig
| NeedsCleanup
|
49 DatabaseLoaded(false),
54 // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/
55 // ---------------------------------------------------------------------
57 void CDROMMethod::Exit()
63 // CDROMMethod::GetID - Search the database for a matching string /*{{{*/
64 // ---------------------------------------------------------------------
66 string
CDROMMethod::GetID(string Name
)
69 const Configuration::Item
*Top
= Database
.Tree("CD");
75 if (Top
->Value
== Name
)
83 // CDROMMethod::Fetch - Fetch a file /*{{{*/
84 // ---------------------------------------------------------------------
86 bool CDROMMethod::Fetch(FetchItem
*Itm
)
89 string File
= Get
.Path
;
92 bool Debug
= _config
->FindB("Debug::Acquire::cdrom",false);
94 /* All IMS queries are returned as a hit, CDROMs are readonly so
95 time stamps never change */
96 if (Itm
->LastModified
!= 0)
98 Res
.LastModified
= Itm
->LastModified
;
106 if (DatabaseLoaded
== false)
109 string DFile
= _config
->FindFile("Dir::State::cdroms");
110 if (FileExists(DFile
) == true)
112 if (ReadConfigFile(Database
,DFile
) == false)
113 return _error
->Error(_("Unable to read the cdrom database %s"),
116 DatabaseLoaded
= true;
119 // All non IMS queries for package files fail.
120 if (Itm
->IndexFile
== true || GetID(Get
.Host
).empty() == true)
122 Fail(_("Please use apt-cdrom to make this CD recognized by APT."
123 " apt-get update cannot be used to add new CDs"));
127 // We already have a CD inserted, but it is the wrong one
128 if (CurrentID
.empty() == false && Database
.Find("CD::" + CurrentID
) != Get
.Host
)
130 Fail(_("Wrong CD"),true);
134 CDROM
= _config
->FindDir("Acquire::cdrom::mount","/cdrom/");
136 CDROM
= SafeGetCWD() + '/' + CDROM
;
138 while (CurrentID
.empty() == true)
141 Mounted
= MountCdrom(CDROM
);
142 for (unsigned int Version
= 2; Version
!= 0; Version
--)
144 if (IdentCdrom(CDROM
,NewID
,Version
) == false)
148 clog
<< "ID " << Version
<< " " << NewID
<< endl
;
151 if (Database
.Find("CD::" + NewID
) == Get
.Host
)
161 // I suppose this should prompt somehow?
162 if (UnmountCdrom(CDROM
) == false)
163 return _error
->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."),
165 if (MediaFail(Get
.Host
,CDROM
) == false)
168 Fail(_("Wrong CD"),true);
174 Res
.Filename
= CDROM
+ File
;
176 if (stat(Res
.Filename
.c_str(),&Buf
) != 0)
177 return _error
->Error(_("File not found"));
179 if (NewID
.empty() == false)
181 Res
.LastModified
= Buf
.st_mtime
;
182 Res
.Size
= Buf
.st_size
;