File indexing completed on 2024-05-05 03:54:29

0001 /* vi: ts=8 sts=4 sw=4
0002 
0003     This file is part of the KDE project, module kdesu.
0004     SPDX-FileCopyrightText: 1999, 2000 Geert Jansen <g.t.jansen@stud.tue.nl>
0005 */
0006 
0007 #include "repo.h"
0008 
0009 #include <ksud_debug.h>
0010 
0011 #include <assert.h>
0012 #include <time.h>
0013 
0014 #include <QStack>
0015 
0016 Repository::Repository()
0017 {
0018     head_time = (unsigned)-1;
0019 }
0020 
0021 Repository::~Repository()
0022 {
0023 }
0024 
0025 void Repository::add(const QByteArray &key, Data_entry &data)
0026 {
0027     RepoIterator it = repo.find(key);
0028     if (it != repo.end()) {
0029         remove(key);
0030     }
0031     if (data.timeout == 0) {
0032         data.timeout = (unsigned)-1;
0033     } else {
0034         data.timeout += time(nullptr);
0035     }
0036     head_time = qMin(head_time, data.timeout);
0037     repo.insert(key, data);
0038 }
0039 
0040 int Repository::remove(const QByteArray &key)
0041 {
0042     if (key.isEmpty()) {
0043         return -1;
0044     }
0045 
0046     RepoIterator it = repo.find(key);
0047     if (it == repo.end()) {
0048         return -1;
0049     }
0050     it.value().value.fill('x');
0051     it.value().group.fill('x');
0052     repo.erase(it);
0053     return 0;
0054 }
0055 
0056 int Repository::removeSpecialKey(const QByteArray &key)
0057 {
0058     int found = -1;
0059     if (!key.isEmpty()) {
0060         QStack<QByteArray> rm_keys;
0061         for (RepoCIterator it = repo.constBegin(); it != repo.constEnd(); ++it) {
0062             if (key.indexOf(it.value().group) == 0 && it.key().indexOf(key) >= 0) {
0063                 rm_keys.push(it.key());
0064                 found = 0;
0065             }
0066         }
0067         while (!rm_keys.isEmpty()) {
0068             qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
0069             remove(rm_keys.pop());
0070         }
0071     }
0072     return found;
0073 }
0074 
0075 int Repository::removeGroup(const QByteArray &group)
0076 {
0077     int found = -1;
0078     if (!group.isEmpty()) {
0079         QStack<QByteArray> rm_keys;
0080         for (RepoCIterator it = repo.constBegin(); it != repo.constEnd(); ++it) {
0081             if (it.value().group == group) {
0082                 rm_keys.push(it.key());
0083                 found = 0;
0084             }
0085         }
0086         while (!rm_keys.isEmpty()) {
0087             qCDebug(KSUD_LOG) << "Removed key: " << rm_keys.top();
0088             remove(rm_keys.pop());
0089         }
0090     }
0091     return found;
0092 }
0093 
0094 int Repository::hasGroup(const QByteArray &group) const
0095 {
0096     if (!group.isEmpty()) {
0097         RepoCIterator it;
0098         for (it = repo.begin(); it != repo.end(); ++it) {
0099             if (it.value().group == group) {
0100                 return 0;
0101             }
0102         }
0103     }
0104     return -1;
0105 }
0106 
0107 QByteArray Repository::findKeys(const QByteArray &group, const char *sep) const
0108 {
0109     QByteArray list = "";
0110     if (!group.isEmpty()) {
0111         qCDebug(KSUD_LOG) << "Looking for matching key with group key: " << group;
0112         int pos;
0113         QByteArray key;
0114         RepoCIterator it;
0115         for (it = repo.begin(); it != repo.end(); ++it) {
0116             if (it.value().group == group) {
0117                 key = it.key();
0118                 qCDebug(KSUD_LOG) << "Matching key found: " << key;
0119                 pos = key.lastIndexOf(sep);
0120                 key.truncate(pos);
0121                 key.remove(0, 2);
0122                 if (!list.isEmpty()) {
0123                     // Add the same keys only once please :)
0124                     if (!list.contains(key)) {
0125                         qCDebug(KSUD_LOG) << "Key added to list: " << key;
0126                         list += '\007'; // I do not know
0127                         list.append(key);
0128                     }
0129                 } else {
0130                     list = key;
0131                 }
0132             }
0133         }
0134     }
0135     return list;
0136 }
0137 
0138 QByteArray Repository::find(const QByteArray &key) const
0139 {
0140     if (key.isEmpty()) {
0141         return nullptr;
0142     }
0143 
0144     RepoCIterator it = repo.find(key);
0145     if (it == repo.end()) {
0146         return nullptr;
0147     }
0148     return it.value().value;
0149 }
0150 
0151 int Repository::expire()
0152 {
0153     unsigned current = time(nullptr);
0154     if (current < head_time) {
0155         return 0;
0156     }
0157 
0158     unsigned t;
0159     QStack<QByteArray> keys;
0160     head_time = (unsigned)-1;
0161     RepoIterator it;
0162     for (it = repo.begin(); it != repo.end(); ++it) {
0163         t = it.value().timeout;
0164         if (t <= current) {
0165             keys.push(it.key());
0166         } else {
0167             head_time = qMin(head_time, t);
0168         }
0169     }
0170 
0171     int n = keys.count();
0172     while (!keys.isEmpty()) {
0173         remove(keys.pop());
0174     }
0175     return n;
0176 }