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