1
/* raptoptions.cc - configuration handling
3
* Copyright (c) 2000, 2001 Conectiva S/A
5
* Author: Alfredo K. Kojima <kojima@conectiva.com.br>
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30
#include <apt-pkg/error.h>
31
#include <apt-pkg/configuration.h>
32
#include <apt-pkg/tagfile.h>
33
#include <apt-pkg/policy.h>
34
#include <apt-pkg/sptr.h>
35
#include <apt-pkg/strutl.h>
38
#include "rconfiguration.h"
39
#include "raptoptions.h"
42
RAPTOptions *_roptions = new RAPTOptions;
46
ostream &operator<<(ostream &os, const RAPTOptions::packageOptions &o)
52
istream &operator>>(istream &is, RAPTOptions::packageOptions &o)
59
bool RAPTOptions::store()
62
if(!RPackageOptionsFile(out))
65
for(packageOptionsIter it = _roptions->_packageOptions.begin();
66
it != _roptions->_packageOptions.end();
69
// we only write out if it's new and the pkgname is not empty
70
if( (*it).second.isNew && !(*it).first.empty() )
71
out << (*it).first << " " << (*it).second << endl;
77
bool RAPTOptions::restore()
83
if(!RPackageOptionsFile(in))
89
istrstream strstr(line.c_str());
90
strstr >> pkg >> o >> ws;
91
_packageOptions[pkg] = o;
95
string File = _config->FindFile("Dir::Etc::Preferences");
97
if( !FileExists(File) )
100
FileFd Fd(File,FileFd::ReadOnly);
102
if (_error->PendingError() == true) {
106
while (TF.Step(Tags) == true) {
107
string Name = Tags.FindS("Package");
108
if (Name.empty() == true)
109
return _error->Error(_("Invalid record in the preferences file, no Package header"));
115
if (Tags.Find("Pin",Start,End) == false)
118
const char *Word = Start;
119
for (; Word != End && isspace(*Word) == 0; Word++);
121
// Parse the type, we are only interesseted in "version" for now
122
pkgVersionMatch::MatchType Type;
123
if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
124
Type = pkgVersionMatch::Version;
127
for (; Word != End && isspace(*Word) != 0; Word++);
129
setPackageLock(Name.c_str(), true);
138
void RAPTOptions::rereadOrphaned() {
139
// forget about any previously orphaned packages
140
for(packageOptionsIter it = _roptions->_packageOptions.begin();
141
it != _roptions->_packageOptions.end();
144
(*it).second.isOrphaned = false;
147
//mvo: call deborphan and read package list from it
148
// TODO: make deborphan a library to make this cleaner
151
char cmd[] = "/usr/bin/deborphan";
152
//FIXME: fail silently if deborphan is not installed - change this?
155
fp = popen(cmd, "r");
157
//cerr << "deborphan failed" << endl;
160
while(fgets(buf, 255, fp) != NULL) {
161
//mvo: FIXME this sucks (remove newline at end)
162
buf[strlen(buf)-1] = 0;
163
//cout << "buf: " << buf << endl;
164
setPackageOrphaned(buf, true);
170
bool RAPTOptions::getPackageOrphaned(const char *package)
172
string tmp = string(package);
174
if (_packageOptions.find(tmp) == _packageOptions.end())
177
//cout << "getPackageOrphaned("<<package<<") called"<<endl;
178
return _packageOptions[tmp].isOrphaned;
182
void RAPTOptions::setPackageOrphaned(const char *package, bool flag)
184
//cout << "orphaned called pkg: " << package << endl;
185
_packageOptions[string(package)].isOrphaned = flag;
189
bool RAPTOptions::getPackageLock(const char *package)
191
string tmp = string(package);
193
if (_packageOptions.find(tmp) == _packageOptions.end())
196
return _packageOptions[tmp].isLocked;
200
void RAPTOptions::setPackageLock(const char *package, bool lock)
202
_packageOptions[string(package)].isLocked = lock;
205
bool RAPTOptions::getPackageNew(const char *package)
207
string tmp = string(package);
209
if (_packageOptions.find(tmp) == _packageOptions.end())
212
return _packageOptions[tmp].isNew;
215
void RAPTOptions::setPackageNew(const char *package, bool lock)
217
_packageOptions[string(package)].isNew = lock;
220
void RAPTOptions::forgetNewPackages()
222
for(packageOptionsIter it = _roptions->_packageOptions.begin();
223
it != _roptions->_packageOptions.end();
226
(*it).second.isNew = false;
230
bool RAPTOptions::getFlag(const char *key)
232
if (_options.find(key) != _options.end())
233
return _options[string(key)] == "true";
239
string RAPTOptions::getString(const char *key)
241
if (_options.find(key) != _options.end())
242
return _options[string(key)];
248
void RAPTOptions::setFlag(const char *key, bool value)
250
_options[string(key)] = string(value ? "true" : "false");
254
void RAPTOptions::setString(const char *key, string value)
256
_options[string(key)] = value;