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