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