]>
Commit | Line | Data |
---|---|---|
f46e7681 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
f631d1ba | 3 | // $Id: cdrom.cc,v 1.4 1998/12/22 07:36:49 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> | |
19 | /*}}}*/ | |
20 | ||
21 | class CDROMMethod : public pkgAcqMethod | |
22 | { | |
23 | Configuration Database; | |
f631d1ba | 24 | bool DatabaseLoaded; |
f46e7681 AL |
25 | string CurrentID; |
26 | ||
27 | virtual bool Fetch(FetchItem *Itm); | |
28 | string GetID(string Name); | |
29 | ||
30 | public: | |
31 | ||
32 | CDROMMethod(); | |
33 | }; | |
34 | ||
35 | // CDROMMethod::CDROMethod - Constructor /*{{{*/ | |
36 | // --------------------------------------------------------------------- | |
37 | /* */ | |
76d97c26 | 38 | CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly | |
f631d1ba | 39 | SendConfig), DatabaseLoaded(false) |
f46e7681 | 40 | { |
f46e7681 AL |
41 | }; |
42 | /*}}}*/ | |
43 | // CDROMMethod::GetID - Get the ID hash for /*{{{*/ | |
44 | // --------------------------------------------------------------------- | |
45 | /* We search the configuration space for the name and then return the ID | |
46 | tag associated with it. */ | |
47 | string CDROMMethod::GetID(string Name) | |
48 | { | |
f631d1ba AL |
49 | if (DatabaseLoaded == false) |
50 | { | |
51 | // Read the database | |
52 | string DFile = _config->FindFile("Dir::State::cdroms"); | |
53 | if (FileExists(DFile) == true) | |
54 | { | |
55 | if (ReadConfigFile(Database,DFile) == false) | |
56 | { | |
57 | _error->Error("Unable to read the cdrom database %s", | |
58 | DFile.c_str()); | |
59 | Fail(); | |
60 | } | |
61 | } | |
62 | DatabaseLoaded = true; | |
63 | } | |
64 | ||
65 | const Configuration::Item *Top = Database.Tree("CD"); | |
f46e7681 AL |
66 | for (; Top != 0;) |
67 | { | |
68 | if (Top->Value == Name) | |
69 | return Top->Tag; | |
70 | ||
71 | Top = Top->Next; | |
72 | } | |
73 | ||
74 | return string(); | |
75 | } | |
76 | /*}}}*/ | |
77 | // CDROMMethod::Fetch - Fetch a file /*{{{*/ | |
78 | // --------------------------------------------------------------------- | |
79 | /* */ | |
80 | bool CDROMMethod::Fetch(FetchItem *Itm) | |
81 | { | |
82 | URI Get = Itm->Uri; | |
83 | string File = Get.Path; | |
84 | FetchResult Res; | |
85 | ||
86 | /* All IMS queries are returned as a hit, CDROMs are readonly so | |
87 | time stamps never change */ | |
88 | if (Itm->LastModified != 0) | |
89 | { | |
90 | Res.LastModified = Itm->LastModified; | |
91 | Res.IMSHit = true; | |
92 | URIDone(Res); | |
93 | return true; | |
94 | } | |
95 | ||
96 | string ID = GetID(Get.Host); | |
97 | ||
98 | // All non IMS queries for package files fail. | |
f631d1ba | 99 | if (Itm->IndexFile == true || ID.empty() == true) |
f46e7681 AL |
100 | { |
101 | Fail("Please use apt-cdrom to make this CD recognized by APT." | |
102 | " apt-get update cannot be used to add new CDs"); | |
103 | return true; | |
104 | } | |
105 | ||
106 | // We already have a CD inserted, but it is the wrong one | |
107 | if (CurrentID.empty() == false && ID != CurrentID) | |
108 | { | |
109 | Fail("Wrong CD",true); | |
110 | return true; | |
111 | } | |
112 | ||
113 | string CDROM = _config->FindDir("Acquire::cdrom::mount","/cdrom/"); | |
76d97c26 AL |
114 | if (CDROM[0] == '.') |
115 | CDROM= SafeGetCWD() + '/' + CDROM; | |
f46e7681 AL |
116 | string NewID; |
117 | while (1) | |
118 | { | |
119 | if (IdentCdrom(CDROM,NewID) == false) | |
120 | return false; | |
121 | ||
122 | // A hit | |
123 | if (NewID == ID) | |
124 | break; | |
125 | ||
126 | UnmountCdrom(CDROM); | |
018f1533 AL |
127 | if (MediaFail(Get.Host,CDROM) == false) |
128 | { | |
76d97c26 AL |
129 | CurrentID = "FAIL"; |
130 | Fail("Wrong CD",true); | |
131 | return true; | |
018f1533 AL |
132 | } |
133 | ||
f46e7681 AL |
134 | MountCdrom(CDROM); |
135 | } | |
136 | ||
137 | // ID matches | |
138 | if (NewID == ID) | |
139 | { | |
140 | Res.Filename = CDROM + File; | |
141 | if (FileExists(Res.Filename) == false) | |
142 | return _error->Error("File not found"); | |
143 | ||
144 | CurrentID = ID; | |
145 | Res.LastModified = Itm->LastModified; | |
146 | Res.IMSHit = true; | |
147 | URIDone(Res); | |
148 | return true; | |
149 | } | |
150 | ||
151 | return _error->Error("CDROM not found"); | |
152 | } | |
153 | /*}}}*/ | |
154 | ||
155 | int main() | |
156 | { | |
157 | CDROMMethod Mth; | |
158 | return Mth.Run(); | |
159 | } |