1/* vi: ts=8 sts=4 sw=4
2
3 This file is part of the KDE project, module kdesu.
4 SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <g.t.jansen@stud.tue.nl>
5*/
6
7#include "repo.h"
8
9#include <ksud_debug.h>
10
11#include <assert.h>
12#include <time.h>
13
14#include <QStack>
15
16Repository::Repository()
17{
18 head_time = (unsigned)-1;
19}
20
21Repository::~Repository()
22{
23}
24
25void Repository::add(const QByteArray &key, Data_entry &data)
26{
27 RepoIterator it = repo.find(key);
28 if (it != repo.end()) {
29 remove(key);
30 }
31 if (data.timeout == 0) {
32 data.timeout = (unsigned)-1;
33 } else {
34 data.timeout += time(timer: nullptr);
35 }
36 head_time = qMin(a: head_time, b: data.timeout);
37 repo.insert(key, value: data);
38}
39
40int Repository::remove(const QByteArray &key)
41{
42 if (key.isEmpty()) {
43 return -1;
44 }
45
46 RepoIterator it = repo.find(key);
47 if (it == repo.end()) {
48 return -1;
49 }
50 it.value().value.fill(c: 'x');
51 it.value().group.fill(c: 'x');
52 repo.erase(it);
53 return 0;
54}
55
56int Repository::removeSpecialKey(const QByteArray &key)
57{
58 int found = -1;
59 if (!key.isEmpty()) {
60 QStack<QByteArray> rm_keys;
61 for (RepoCIterator it = repo.constBegin(); it != repo.constEnd(); ++it) {
62 if (key.indexOf(bv: it.value().group) == 0 && it.key().indexOf(bv: key) >= 0) {
63 rm_keys.push(t: it.key());
64 found = 0;
65 }
66 }
67 while (!rm_keys.isEmpty()) {
68 qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
69 remove(key: rm_keys.pop());
70 }
71 }
72 return found;
73}
74
75int Repository::removeGroup(const QByteArray &group)
76{
77 int found = -1;
78 if (!group.isEmpty()) {
79 QStack<QByteArray> rm_keys;
80 for (RepoCIterator it = repo.constBegin(); it != repo.constEnd(); ++it) {
81 if (it.value().group == group) {
82 rm_keys.push(t: it.key());
83 found = 0;
84 }
85 }
86 while (!rm_keys.isEmpty()) {
87 qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
88 remove(key: rm_keys.pop());
89 }
90 }
91 return found;
92}
93
94int Repository::hasGroup(const QByteArray &group) const
95{
96 if (!group.isEmpty()) {
97 RepoCIterator it;
98 for (it = repo.begin(); it != repo.end(); ++it) {
99 if (it.value().group == group) {
100 return 0;
101 }
102 }
103 }
104 return -1;
105}
106
107QByteArray Repository::findKeys(const QByteArray &group, const char *sep) const
108{
109 QByteArray list = "";
110 if (!group.isEmpty()) {
111 qCDebug(KSUD_LOG) << "Looking for matching key with group key: " << group;
112 int pos;
113 QByteArray key;
114 RepoCIterator it;
115 for (it = repo.begin(); it != repo.end(); ++it) {
116 if (it.value().group == group) {
117 key = it.key();
118 qCDebug(KSUD_LOG) << "Matching key found: " << key;
119 pos = key.lastIndexOf(bv: sep);
120 key.truncate(pos);
121 key.remove(index: 0, len: 2);
122 if (!list.isEmpty()) {
123 // Add the same keys only once please :)
124 if (!list.contains(bv: key)) {
125 qCDebug(KSUD_LOG) << "Key added to list: " << key;
126 list += '\007'; // I do not know
127 list.append(a: key);
128 }
129 } else {
130 list = key;
131 }
132 }
133 }
134 }
135 return list;
136}
137
138QByteArray Repository::find(const QByteArray &key) const
139{
140 if (key.isEmpty()) {
141 return nullptr;
142 }
143
144 RepoCIterator it = repo.find(key);
145 if (it == repo.end()) {
146 return nullptr;
147 }
148 return it.value().value;
149}
150
151int Repository::expire()
152{
153 unsigned current = time(timer: nullptr);
154 if (current < head_time) {
155 return 0;
156 }
157
158 unsigned t;
159 QStack<QByteArray> keys;
160 head_time = (unsigned)-1;
161 RepoIterator it;
162 for (it = repo.begin(); it != repo.end(); ++it) {
163 t = it.value().timeout;
164 if (t <= current) {
165 keys.push(t: it.key());
166 } else {
167 head_time = qMin(a: head_time, b: t);
168 }
169 }
170
171 int n = keys.count();
172 while (!keys.isEmpty()) {
173 remove(key: keys.pop());
174 }
175 return n;
176}
177

source code of kdesu/src/kdesud/repo.cpp