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