KDESu

repo.cpp
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 <[email protected]>
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 
16 Repository::Repository()
17 {
18  head_time = (unsigned)-1;
19 }
20 
21 Repository::~Repository()
22 {
23 }
24 
25 void 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(nullptr);
35  }
36  head_time = qMin(head_time, data.timeout);
37  repo.insert(key, data);
38 }
39 
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('x');
51  it.value().group.fill('x');
52  repo.erase(it);
53  return 0;
54 }
55 
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(it.value().group) == 0 && it.key().indexOf(key) >= 0) {
63  rm_keys.push(it.key());
64  found = 0;
65  }
66  }
67  while (!rm_keys.isEmpty()) {
68  qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
69  remove(rm_keys.pop());
70  }
71  }
72  return found;
73 }
74 
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(it.key());
83  found = 0;
84  }
85  }
86  while (!rm_keys.isEmpty()) {
87  qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
88  remove(rm_keys.pop());
89  }
90  }
91  return found;
92 }
93 
94 int 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 
107 QByteArray 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(sep);
120  key.truncate(pos);
121  key.remove(0, 2);
122  if (!list.isEmpty()) {
123  // Add the same keys only once please :)
124  if (!list.contains(key)) {
125  qCDebug(KSUD_LOG) << "Key added to list: " << key;
126  list += '\007'; // I do not know
127  list.append(key);
128  }
129  } else {
130  list = key;
131  }
132  }
133  }
134  }
135  return list;
136 }
137 
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 
152 {
153  unsigned current = time(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(it.key());
166  } else {
167  head_time = qMin(head_time, t);
168  }
169  }
170 
171  int n = keys.count();
172  while (!keys.isEmpty()) {
173  remove(keys.pop());
174  }
175  return n;
176 }
int remove(const QByteArray &key)
Delete a data element.
Definition: repo.cpp:40
QMap::iterator erase(QMap::iterator pos)
int lastIndexOf(char ch, int from) const const
void push(const T &t)
int removeGroup(const QByteArray &group)
Delete all data entries having the given group.
Definition: repo.cpp:75
QMap::const_iterator constBegin() const const
bool isEmpty() const const
Used internally.
Definition: repo.h:16
int indexOf(char ch, int from) const const
QByteArray findKeys(const QByteArray &group, const char *sep="-") const
Returns the key values for the given group.
Definition: repo.cpp:107
QMap::const_iterator constEnd() const const
int removeSpecialKey(const QByteArray &key)
Delete all data entries based on key.
Definition: repo.cpp:56
void truncate(int pos)
QMap::iterator end()
QMap::iterator begin()
QByteArray & append(char ch)
bool isEmpty() const const
int count(const T &value) const const
bool contains(char ch) const const
int expire()
Remove data elements which are expired.
Definition: repo.cpp:151
QMap::iterator insert(const Key &key, const T &value)
int hasGroup(const QByteArray &group) const
Checks for the existence of the specified group.
Definition: repo.cpp:94
void add(const QByteArray &key, Data_entry &data)
Add a data element.
Definition: repo.cpp:25
QByteArray find(const QByteArray &key) const
Return a data value.
Definition: repo.cpp:138
QByteArray & remove(int pos, int len)
QMap::iterator find(const Key &key)
T & top()
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 22:40:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.