1 | /* rconfiguration.cc |
---|
2 | * |
---|
3 | * Copyright (c) 2000-2003 Conectiva S/A |
---|
4 | * 2002 Michael Vogt <mvo@debian.org> |
---|
5 | * |
---|
6 | * Author: Alfredo K. Kojima <kojima@conectiva.com.br> |
---|
7 | * Michael Vogt <mvo@debian.org> |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License as |
---|
11 | * published by the Free Software Foundation; either version 2 of the |
---|
12 | * License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
22 | * USA |
---|
23 | */ |
---|
24 | |
---|
25 | #include "config.h" |
---|
26 | |
---|
27 | #include <pwd.h> |
---|
28 | #include <sys/types.h> |
---|
29 | |
---|
30 | #include <sys/stat.h> |
---|
31 | #include <sys/types.h> |
---|
32 | |
---|
33 | #include <apt-pkg/init.h> |
---|
34 | #include <apt-pkg/error.h> |
---|
35 | #include <apt-pkg/configuration.h> |
---|
36 | |
---|
37 | #include "rconfiguration.h" |
---|
38 | |
---|
39 | #include "i18n.h" |
---|
40 | |
---|
41 | static string ConfigFilePath; |
---|
42 | static string ConfigFileDir; |
---|
43 | |
---|
44 | |
---|
45 | // #ifndef HAVE_RPM |
---|
46 | // bool _ReadConfigFile(Configuration &Conf,string FName,bool AsSectional = false, |
---|
47 | // unsigned Depth = 0); |
---|
48 | // #endif |
---|
49 | |
---|
50 | static void dumpToFile(const Configuration::Item *Top, ostream &out, |
---|
51 | string pad) |
---|
52 | { |
---|
53 | while (Top) { |
---|
54 | out << pad << Top->Tag << " \"" << Top->Value << "\""; |
---|
55 | |
---|
56 | if (Top->Child) { |
---|
57 | out << " {" << endl; |
---|
58 | dumpToFile(Top->Child, out, pad + " "); |
---|
59 | out << pad << "};" << endl; |
---|
60 | |
---|
61 | if (Top->Next) |
---|
62 | out << endl; |
---|
63 | } else { |
---|
64 | out << ";" << endl; |
---|
65 | } |
---|
66 | |
---|
67 | if (pad.empty()) |
---|
68 | break; // dump only synaptic section |
---|
69 | |
---|
70 | Top = Top->Next; |
---|
71 | } |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | bool RWriteConfigFile(Configuration &Conf) |
---|
77 | { |
---|
78 | const Configuration::Item *Synaptic; |
---|
79 | |
---|
80 | // when running non-interactivly don't save any config (there should be no |
---|
81 | // need) |
---|
82 | if(_config->FindB("Volatile::Non-Interactive", false) == true) |
---|
83 | return true; |
---|
84 | |
---|
85 | ofstream cfile(ConfigFilePath.c_str(), ios::out); |
---|
86 | if (!cfile != 0) |
---|
87 | return _error->Errno("ofstream", |
---|
88 | _("ERROR: couldn't open %s for writing"), |
---|
89 | ConfigFilePath.c_str()); |
---|
90 | |
---|
91 | Synaptic = Conf.Tree(0); |
---|
92 | while (Synaptic) { |
---|
93 | if (Synaptic->Tag == "Synaptic") |
---|
94 | break; |
---|
95 | Synaptic = Synaptic->Next; |
---|
96 | } |
---|
97 | dumpToFile(Synaptic, cfile, ""); |
---|
98 | |
---|
99 | cfile.close(); |
---|
100 | |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | static bool checkConfigDir(string &path) |
---|
106 | { |
---|
107 | struct stat stbuf; |
---|
108 | struct passwd *pwd; |
---|
109 | |
---|
110 | pwd = getpwuid(0); |
---|
111 | if (!pwd) { |
---|
112 | return _error->Errno("getpwuid", |
---|
113 | _ |
---|
114 | ("ERROR: Could not get password entry for superuser")); |
---|
115 | } |
---|
116 | path = string(pwd->pw_dir) + "/.synaptic"; |
---|
117 | //path = "/etc/synaptic"; |
---|
118 | |
---|
119 | if (stat(path.c_str(), &stbuf) < 0) { |
---|
120 | if (mkdir(path.c_str(), 0700) < 0) { |
---|
121 | return _error->Errno("mkdir", |
---|
122 | _ |
---|
123 | ("ERROR: could not create configuration directory %s"), |
---|
124 | path.c_str()); |
---|
125 | } |
---|
126 | } |
---|
127 | return true; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | string RConfDir() |
---|
132 | { |
---|
133 | static string confDir; |
---|
134 | if (!checkConfigDir(confDir)) |
---|
135 | cerr << "checkConfigDir() failed! please report to mvo@debian.org" << |
---|
136 | endl; |
---|
137 | return confDir; |
---|
138 | } |
---|
139 | |
---|
140 | string RStateDir() |
---|
141 | { |
---|
142 | struct stat stbuf; |
---|
143 | static string stateDir = string(SYNAPTICSTATEDIR); |
---|
144 | if (stat(stateDir.c_str(), &stbuf) < 0) { |
---|
145 | if (mkdir(stateDir.c_str(), 0755) < 0) { |
---|
146 | _error->Errno("mkdir", |
---|
147 | _("ERROR: could not create state directory %s"), |
---|
148 | stateDir.c_str()); |
---|
149 | return ""; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return stateDir; |
---|
154 | } |
---|
155 | |
---|
156 | // we use the ConfDir for now to store very small tmpfiles |
---|
157 | string RTmpDir() |
---|
158 | { |
---|
159 | struct stat stbuf; |
---|
160 | static string tmpDir = RConfDir() + string("/tmp/"); |
---|
161 | if (stat(tmpDir.c_str(), &stbuf) < 0) { |
---|
162 | if (mkdir(tmpDir.c_str(), 0700) < 0) { |
---|
163 | _error->Errno("mkdir", |
---|
164 | _("ERROR: could not create tmp directory %s"), |
---|
165 | tmpDir.c_str()); |
---|
166 | return ""; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | return tmpDir; |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | string RLogDir() |
---|
175 | { |
---|
176 | struct stat stbuf; |
---|
177 | static string logDir = RConfDir() + string("/log/"); |
---|
178 | |
---|
179 | if (stat(logDir.c_str(), &stbuf) < 0) { |
---|
180 | if (mkdir(logDir.c_str(), 0700) < 0) { |
---|
181 | _error->Errno("mkdir", |
---|
182 | _("ERROR: could not create log directory %s"), |
---|
183 | logDir.c_str()); |
---|
184 | return ""; |
---|
185 | } |
---|
186 | } |
---|
187 | |
---|
188 | return logDir; |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | bool RInitConfiguration(string confFileName) |
---|
193 | { |
---|
194 | string configDir; |
---|
195 | |
---|
196 | if (!pkgInitConfig(*_config)) |
---|
197 | return false; |
---|
198 | |
---|
199 | _config->Set("Program", "synaptic"); |
---|
200 | |
---|
201 | if (!pkgInitSystem(*_config, _system)) |
---|
202 | return false; |
---|
203 | |
---|
204 | if (!checkConfigDir(configDir)) |
---|
205 | return false; |
---|
206 | |
---|
207 | ConfigFilePath = configDir + "/" + confFileName; |
---|
208 | ConfigFileDir = configDir; |
---|
209 | |
---|
210 | if (!ReadConfigFile(*_config, ConfigFilePath)) { |
---|
211 | _error->Discard(); |
---|
212 | } |
---|
213 | |
---|
214 | return true; |
---|
215 | } |
---|
216 | |
---|
217 | |
---|
218 | bool RReadFilterData(Configuration &config) |
---|
219 | { |
---|
220 | string defaultPath = ConfigFileDir + "/filters"; |
---|
221 | string path = _config->Find("Volatile::filterFile", defaultPath.c_str()); |
---|
222 | |
---|
223 | if (!FileExists(path)) { |
---|
224 | return false; |
---|
225 | } |
---|
226 | |
---|
227 | if (!ReadConfigFile(config, path, true)) { |
---|
228 | _error->DumpErrors(); |
---|
229 | _error->Discard(); |
---|
230 | return false; |
---|
231 | } |
---|
232 | return true; |
---|
233 | } |
---|
234 | |
---|
235 | bool RPackageOptionsFile(ofstream &out) |
---|
236 | { |
---|
237 | string path = ConfigFileDir + "/options"; |
---|
238 | out.open(path.c_str()); |
---|
239 | if (!out != 0) |
---|
240 | return _error->Errno("ofstream", |
---|
241 | _("ERROR: couldn't open %s for writing"), |
---|
242 | path.c_str()); |
---|
243 | return true; |
---|
244 | } |
---|
245 | |
---|
246 | bool RPackageOptionsFile(ifstream &in) |
---|
247 | { |
---|
248 | string path = ConfigFileDir + "/options"; |
---|
249 | in.open(path.c_str()); |
---|
250 | if (!in != 0) |
---|
251 | return false; |
---|
252 | // return _error->Errno("ifstream", _("ERROR: couldn't open %s for reading"), |
---|
253 | // path.c_str()); |
---|
254 | return true; |
---|
255 | } |
---|
256 | |
---|
257 | |
---|
258 | bool RFilterDataOutFile(ofstream &out) |
---|
259 | { |
---|
260 | string defaultPath = ConfigFileDir + "/filters"; |
---|
261 | string path = _config->Find("Volatile::filterFile", defaultPath.c_str()); |
---|
262 | |
---|
263 | out.open(path.c_str(), ios::out); |
---|
264 | |
---|
265 | if (!out != 0) |
---|
266 | return _error->Errno("ofstream", _("couldn't open %s for writing"), |
---|
267 | path.c_str()); |
---|
268 | |
---|
269 | return true; |
---|
270 | } |
---|