File: rconfiguration.cc

package info (click to toggle)
synaptic 0.91.7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,676 kB
  • sloc: cpp: 19,830; xml: 10,562; ansic: 2,084; makefile: 498; sed: 93; python: 82; sh: 52
file content (356 lines) | stat: -rw-r--r-- 9,296 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* rconfiguration.cc
 *
 * Copyright (c) 2000-2003 Conectiva S/A
 *               2002 Michael Vogt <mvo@debian.org>
 *
 * Author: Alfredo K. Kojima <kojima@conectiva.com.br>
 *         Michael Vogt <mvo@debian.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

#include "config.h"

#include <pwd.h>
#include <sys/types.h>

#include <sys/stat.h>
#include <sys/types.h>

#include <unistd.h>

#include <apt-pkg/init.h>
#include <apt-pkg/error.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/fileutl.h>

#include "rconfiguration.h"

#include "i18n.h"

static string ConfigFilePath;
static string ConfigFileDir;


// #ifndef HAVE_RPM
// bool _ReadConfigFile(Configuration &Conf,string FName,bool AsSectional = false,
//                  unsigned Depth = 0);
// #endif

static void dumpToFile(const Configuration::Item *Top, ostream &out,
                       string pad)
{
   while (Top) {
      out << pad << Top->Tag << " \"" << Top->Value << "\"";

      if (Top->Child) {
         out << " {" << endl;
         dumpToFile(Top->Child, out, pad + "  ");
         out << pad << "};" << endl;

         if (Top->Next)
            out << endl;
      } else {
         out << ";" << endl;
      }

      if (pad.empty())
         break;                 // dump only synaptic section

      Top = Top->Next;
   }
}



bool RWriteConfigFile(Configuration &Conf)
{
   const Configuration::Item *Synaptic;

   // when running non-interactivly don't save any config (there should be no 
   // need)
   if(_config->FindB("Volatile::Non-Interactive", false) == true) 
      return true;

   // store option 'consider recommended packages as dependencies'
   // to config of apt if we run as root
   if (getuid() == 0) {
      string aptConfPath = _config->FindDir("Dir::Etc::parts", "/etc/apt/apt.conf.d/")
                         + "99synaptic";

      int old_umask = umask(0022);
      ofstream aptfile(aptConfPath.c_str(), ios::out);
      if (!aptfile != 0) {
         cerr << "cannot open " << aptConfPath.c_str() <<
                 " to write APT::Install-Recommends" << endl;
      } else {
         if (_config->FindB("APT::Install-Recommends", false))
            aptfile << "APT::Install-Recommends \"true\";" << endl;
         else
            aptfile << "APT::Install-Recommends \"false\";" << endl;
         aptfile.close();
      }
      umask(old_umask);
   }
   // and backup Install-Recommends to config of synaptic
   _config->Set("Synaptic::Install-Recommends",
                _config->FindB("APT::Install-Recommends",
                _config->FindB("Synaptic::Install-Recommends",
                false)));

   ofstream cfile(ConfigFilePath.c_str(), ios::out);
   if (!cfile != 0)
      return _error->Errno("ofstream",
                           _("ERROR: couldn't open %s for writing"),
                           ConfigFilePath.c_str());

   Synaptic = Conf.Tree(0);
   while (Synaptic) {
      if (Synaptic->Tag == "Synaptic")
         break;
      Synaptic = Synaptic->Next;
   }
   dumpToFile(Synaptic, cfile, "");

   cfile.close();

   return true;
}

static bool checkConfigDir(string &path)
{
   struct stat stbuf;
   struct passwd *pwd;
   char *buf;
   string xdg_data_dir;
   string old_path;
   string home_dir;
   int l;

   pwd = getpwuid(getuid());
   if (!pwd) {
      return _error->Errno("getpwuid",
                           _
                           ("ERROR: Could not get password entry for superuser"));
   }

   home_dir = string(pwd->pw_dir);
   xdg_data_dir = home_dir + "/.config";
   old_path = home_dir + "/.synaptic";
   buf = getenv("XDG_CONFIG_HOME");

   if (buf) {
     l = strlen(buf) - 1;
     if (buf[l] == '/') {
       buf[l] = '\0';
     }
     xdg_data_dir=buf;

     // Guarantee that XDG_CONFIG_HOME is inside home directory.
     if (xdg_data_dir.substr(0,home_dir.length()) != home_dir) {
       xdg_data_dir = home_dir + "/.config";
       cerr << "Warning: XDG_CONFIG_HOME=" << buf
	    << " is not in the home directory. Using " << xdg_data_dir
	    << " instead." << std::endl;
     }
   }

   //path = "/etc/synaptic";
   path = xdg_data_dir + "/synaptic";

   if (! CreateDirectory(home_dir, xdg_data_dir)) {
      return _error->Errno("mkdir",
			   _("ERROR: could not create XDG home directory %s"),
			   xdg_data_dir.c_str());
   }

   // Move old config dir (if any) to the new one
   // Consider to delete this code in the future
   if (stat(old_path.c_str(), &stbuf) == 0 &&
       rename(old_path.c_str(), path.c_str()) < 0) {
      return _error->Errno("rename",
			   _("ERROR: could not move old configuration "
			     "directory %s to the new configuration "
			     "directory %s"),
			   old_path.c_str(), path.c_str());
   }

   if (stat(path.c_str(), &stbuf) < 0 &&
       mkdir(path.c_str(), 0700) < 0) {
      return _error->Errno("mkdir",
			   _
			   ("ERROR: could not create configuration directory %s"),
			   path.c_str());
   }
   return true;
}


string RConfDir()
{
   static string confDir;
   if (!checkConfigDir(confDir))
      cerr << "checkConfigDir() failed! please report to mvo@debian.org" <<
         endl;
   return confDir;
}

string RStateDir()
{
   struct stat stbuf;
   static string stateDir = string(SYNAPTICSTATEDIR);
   if (stat(stateDir.c_str(), &stbuf) < 0) {
      if (mkdir(stateDir.c_str(), 0755) < 0) {
	 _error->Errno("mkdir",
		       _("ERROR: could not create state directory %s"),
		       stateDir.c_str());
	 return "";
      }
   }

   return stateDir;
}

// we use the ConfDir for now to store very small tmpfiles
string RTmpDir()
{
   struct stat stbuf;
   static string tmpDir = RConfDir() + string("/tmp/");
   if (stat(tmpDir.c_str(), &stbuf) < 0) {
      if (mkdir(tmpDir.c_str(), 0700) < 0) {
	 _error->Errno("mkdir",
		       _("ERROR: could not create tmp directory %s"),
		       tmpDir.c_str());
	 return "";
      }
   }

   return tmpDir;
}


string RLogDir()
{
   struct stat stbuf;
   static string logDir = RConfDir() + string("/log/");

   if (stat(logDir.c_str(), &stbuf) < 0) {
      if (mkdir(logDir.c_str(), 0700) < 0) {
	 _error->Errno("mkdir",
		       _("ERROR: could not create log directory %s"),
		       logDir.c_str());
	 return "";
      }
   }

   return logDir;
}


bool RInitConfiguration(string confFileName)
{
   string configDir;

   if (!pkgInitConfig(*_config))
      return false;

   _config->Set("Program", "synaptic");

   if (!pkgInitSystem(*_config, _system))
      return false;

   if (!checkConfigDir(configDir))
      return false;

   ConfigFilePath = configDir + "/" + confFileName;
   ConfigFileDir = configDir;

   if (!ReadConfigFile(*_config, ConfigFilePath)) {
      _error->Discard();
   }

   // read Install-Recommends, preferably from APT:: if we run as root
   // or from Synaptic:: otherwise
   if(getuid() == 0) {
      _config->Set("APT::Install-Recommends",
                   _config->FindB("APT::Install-Recommends",
                   _config->FindB("Synaptic::Install-Recommends",
                   false)));
   } else {
      _config->Set("APT::Install-Recommends",
                   _config->FindB("Synaptic::Install-Recommends",
                   _config->FindB("APT::Install-Recommends",
                   false)));
   }

   return true;
}


bool RReadFilterData(Configuration &config)
{
   string defaultPath = ConfigFileDir + "/filters";
   string path = _config->Find("Volatile::filterFile", defaultPath.c_str());

   if (!FileExists(path)) {
      return false;
   }

   if (!ReadConfigFile(config, path, true)) {
      _error->DumpErrors();
      _error->Discard();
      return false;
   }
   return true;
}

bool RPackageOptionsFile(ofstream &out)
{
   string path = ConfigFileDir + "/options";
   out.open(path.c_str());
   if (!out != 0)
      return _error->Errno("ofstream",
                           _("ERROR: couldn't open %s for writing"),
                           path.c_str());
   return true;
}

bool RPackageOptionsFile(ifstream &in)
{
   string path = ConfigFileDir + "/options";
   in.open(path.c_str());
   if (!in != 0)
      return false;
//      return _error->Errno("ifstream", _("ERROR: couldn't open %s for reading"),
//                           path.c_str());
   return true;
}


bool RFilterDataOutFile(ofstream &out)
{
   string defaultPath = ConfigFileDir + "/filters";
   string path = _config->Find("Volatile::filterFile", defaultPath.c_str());

   out.open(path.c_str(), ios::out);

   if (!out != 0)
      return _error->Errno("ofstream", _("couldn't open %s for writing"),
                           path.c_str());

   return true;
}