]>
Commit | Line | Data |
---|---|---|
f46e7681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
b3d44315 | 3 | // $Id: cdrom.cc,v 1.20.2.1 2004/01/16 18:58:50 mdz Exp $ |
f46e7681 AL |
4 | /* ###################################################################### |
5 | ||
6 | CDROM URI method for APT | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
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> | |
13e8426f | 16 | #include <apt-pkg/hashes.h> |
f46e7681 AL |
17 | |
18 | #include <sys/stat.h> | |
19 | #include <unistd.h> | |
8e372e79 | 20 | #include <dlfcn.h> |
076cc664 AL |
21 | |
22 | #include <iostream> | |
d77559ac | 23 | #include <apti18n.h> |
f46e7681 AL |
24 | /*}}}*/ |
25 | ||
076cc664 AL |
26 | using namespace std; |
27 | ||
f46e7681 AL |
28 | class CDROMMethod : public pkgAcqMethod |
29 | { | |
f631d1ba | 30 | bool DatabaseLoaded; |
5b76e7f2 | 31 | ::Configuration Database; |
f46e7681 | 32 | string CurrentID; |
8e5fc8f5 | 33 | string CDROM; |
70dbf5f8 | 34 | bool MountedByApt; |
8e372e79 | 35 | |
f46e7681 AL |
36 | virtual bool Fetch(FetchItem *Itm); |
37 | string GetID(string Name); | |
8e5fc8f5 | 38 | virtual void Exit(); |
8e372e79 | 39 | |
f46e7681 AL |
40 | public: |
41 | ||
42 | CDROMMethod(); | |
43 | }; | |
44 | ||
45 | // CDROMMethod::CDROMethod - Constructor /*{{{*/ | |
46 | // --------------------------------------------------------------------- | |
47 | /* */ | |
459681d3 AL |
48 | CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly | |
49 | SendConfig | NeedsCleanup | | |
50 | Removable), | |
8e5fc8f5 | 51 | DatabaseLoaded(false), |
70dbf5f8 | 52 | MountedByApt(false) |
f46e7681 | 53 | { |
8e372e79 MV |
54 | |
55 | ||
f46e7681 AL |
56 | }; |
57 | /*}}}*/ | |
8e5fc8f5 AL |
58 | // CDROMMethod::Exit - Unmount the disc if necessary /*{{{*/ |
59 | // --------------------------------------------------------------------- | |
60 | /* */ | |
61 | void CDROMMethod::Exit() | |
8e372e79 MV |
62 | { |
63 | if (MountedByApt == true) | |
8e5fc8f5 AL |
64 | UnmountCdrom(CDROM); |
65 | } | |
66 | /*}}}*/ | |
e42eb508 | 67 | // CDROMMethod::GetID - Search the database for a matching string /*{{{*/ |
f46e7681 | 68 | // --------------------------------------------------------------------- |
e42eb508 | 69 | /* */ |
f46e7681 AL |
70 | string CDROMMethod::GetID(string Name) |
71 | { | |
e42eb508 | 72 | // Search for an ID |
f631d1ba | 73 | const Configuration::Item *Top = Database.Tree("CD"); |
b7d9b68e AL |
74 | if (Top != 0) |
75 | Top = Top->Child; | |
e42eb508 | 76 | |
f46e7681 | 77 | for (; Top != 0;) |
e42eb508 | 78 | { |
f46e7681 AL |
79 | if (Top->Value == Name) |
80 | return Top->Tag; | |
e42eb508 | 81 | |
f46e7681 | 82 | Top = Top->Next; |
e42eb508 | 83 | } |
f46e7681 AL |
84 | return string(); |
85 | } | |
86 | /*}}}*/ | |
87 | // CDROMMethod::Fetch - Fetch a file /*{{{*/ | |
88 | // --------------------------------------------------------------------- | |
89 | /* */ | |
90 | bool CDROMMethod::Fetch(FetchItem *Itm) | |
91 | { | |
92 | URI Get = Itm->Uri; | |
93 | string File = Get.Path; | |
94 | FetchResult Res; | |
34fc0421 AL |
95 | |
96 | bool Debug = _config->FindB("Debug::Acquire::cdrom",false); | |
97 | ||
f46e7681 AL |
98 | /* All IMS queries are returned as a hit, CDROMs are readonly so |
99 | time stamps never change */ | |
100 | if (Itm->LastModified != 0) | |
101 | { | |
102 | Res.LastModified = Itm->LastModified; | |
103 | Res.IMSHit = true; | |
2aab5956 | 104 | Res.Filename = Itm->DestFile; |
f46e7681 AL |
105 | URIDone(Res); |
106 | return true; | |
107 | } | |
e42eb508 AL |
108 | |
109 | // Load the database | |
110 | if (DatabaseLoaded == false) | |
111 | { | |
112 | // Read the database | |
113 | string DFile = _config->FindFile("Dir::State::cdroms"); | |
114 | if (FileExists(DFile) == true) | |
115 | { | |
116 | if (ReadConfigFile(Database,DFile) == false) | |
dc738e7a | 117 | return _error->Error(_("Unable to read the cdrom database %s"), |
e42eb508 AL |
118 | DFile.c_str()); |
119 | } | |
120 | DatabaseLoaded = true; | |
121 | } | |
122 | ||
f46e7681 | 123 | // All non IMS queries for package files fail. |
e42eb508 | 124 | if (Itm->IndexFile == true || GetID(Get.Host).empty() == true) |
f46e7681 | 125 | { |
db0db9fe CP |
126 | Fail(_("Please use apt-cdrom to make this CD-ROM recognized by APT." |
127 | " apt-get update cannot be used to add new CD-ROMs")); | |
9e0349cc | 128 | return true; |
f46e7681 AL |
129 | } |
130 | ||
131 | // We already have a CD inserted, but it is the wrong one | |
e42eb508 | 132 | if (CurrentID.empty() == false && Database.Find("CD::" + CurrentID) != Get.Host) |
f46e7681 | 133 | { |
db0db9fe | 134 | Fail(_("Wrong CD-ROM"),true); |
9e0349cc | 135 | return true; |
f46e7681 AL |
136 | } |
137 | ||
8e5fc8f5 | 138 | CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/"); |
76d97c26 AL |
139 | if (CDROM[0] == '.') |
140 | CDROM= SafeGetCWD() + '/' + CDROM; | |
f46e7681 | 141 | string NewID; |
281daf46 | 142 | while (CurrentID.empty() == true) |
f46e7681 | 143 | { |
34fc0421 | 144 | bool Hit = false; |
70dbf5f8 MV |
145 | if(!IsMounted(CDROM)) |
146 | MountedByApt = MountCdrom(CDROM); | |
34fc0421 AL |
147 | for (unsigned int Version = 2; Version != 0; Version--) |
148 | { | |
149 | if (IdentCdrom(CDROM,NewID,Version) == false) | |
150 | return false; | |
151 | ||
152 | if (Debug == true) | |
153 | clog << "ID " << Version << " " << NewID << endl; | |
154 | ||
155 | // A hit | |
156 | if (Database.Find("CD::" + NewID) == Get.Host) | |
157 | { | |
158 | Hit = true; | |
159 | break; | |
160 | } | |
161 | } | |
175f08ac AL |
162 | |
163 | if (Hit == true) | |
164 | break; | |
165 | ||
4df0b629 | 166 | // I suppose this should prompt somehow? |
50959877 MV |
167 | if (_config->FindB("APT::CDROM::NoMount",false) == false && |
168 | UnmountCdrom(CDROM) == false) | |
dc738e7a | 169 | return _error->Error(_("Unable to unmount the CD-ROM in %s, it may still be in use."), |
4df0b629 | 170 | CDROM.c_str()); |
018f1533 AL |
171 | if (MediaFail(Get.Host,CDROM) == false) |
172 | { | |
76d97c26 | 173 | CurrentID = "FAIL"; |
2a749770 | 174 | return _error->Error(_("Disk not found.")); |
018f1533 | 175 | } |
f46e7681 AL |
176 | } |
177 | ||
e42eb508 AL |
178 | // Found a CD |
179 | Res.Filename = CDROM + File; | |
180 | struct stat Buf; | |
181 | if (stat(Res.Filename.c_str(),&Buf) != 0) | |
dc738e7a | 182 | return _error->Error(_("File not found")); |
f46e7681 | 183 | |
281daf46 AL |
184 | if (NewID.empty() == false) |
185 | CurrentID = NewID; | |
e42eb508 | 186 | Res.LastModified = Buf.st_mtime; |
e42eb508 | 187 | Res.Size = Buf.st_size; |
13e8426f MV |
188 | |
189 | Hashes Hash; | |
190 | FileFd Fd(Res.Filename, FileFd::ReadOnly); | |
191 | Hash.AddFD(Fd.Fd(), Fd.Size()); | |
192 | Res.TakeHashes(Hash); | |
193 | ||
e42eb508 AL |
194 | URIDone(Res); |
195 | return true; | |
f46e7681 AL |
196 | } |
197 | /*}}}*/ | |
198 | ||
199 | int main() | |
200 | { | |
b25423f6 MZ |
201 | setlocale(LC_ALL, ""); |
202 | ||
f46e7681 AL |
203 | CDROMMethod Mth; |
204 | return Mth.Run(); | |
205 | } |