Parent: [2ae704] (diff)

Download this file

ohmetacache.cxx    203 lines (181 with data), 5.7 kB

  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
/* Copyright (C) 2014 J.F.Dockes
* 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 "ohmetacache.hxx"
#include <errno.h> // for errno
#include <stdio.h> // for rename
#include <string.h> // for strchr
#include <unistd.h>
#include <iostream> // for basic_ostream, operator<<, etc
#include <utility> // for pair
#include "libupnpp/log.h"
#include "libupnpp/workqueue.h"
using namespace std;
static unsigned int slptimesecs;
void dmcacheSetOpts(unsigned int slpsecs)
{
slptimesecs = slpsecs;
}
class SaveCacheTask {
public:
SaveCacheTask(const string& fn, const mcache_type& cache)
: m_fn(fn), m_cache(cache)
{}
string m_fn;
mcache_type m_cache;
};
static WorkQueue<SaveCacheTask*> saveQueue("SaveQueue");
// Encode uris and values so that they can be decoded (escape %, =, and eol)
static string encode(const string& in)
{
string out;
const char *cp = in.c_str();
for (string::size_type i = 0; i < in.size(); i++) {
unsigned int c;
const char *h = "0123456789ABCDEF";
c = cp[i];
if (c == '%' || c == '=' || c == '\n' || c == '\r') {
out += '%';
out += h[(c >> 4) & 0xf];
out += h[c & 0xf];
} else {
out += char(c);
}
}
return out;
}
static int h2d(int c)
{
if ('0' <= c && c <= '9')
return c - '0';
else if ('A' <= c && c <= 'F')
return 10 + c - 'A';
else
return -1;
}
static string decode(const string &in)
{
string out;
const char *cp = in.c_str();
if (in.size() <= 2)
return in;
string::size_type i = 0;
for (; i < in.size() - 2; i++) {
if (cp[i] == '%') {
int d1 = h2d(cp[++i]);
int d2 = h2d(cp[++i]);
if (d1 != -1 && d2 != -1)
out += (d1 << 4) + d2;
} else {
out += cp[i];
}
}
while (i < in.size()) {
out += cp[i++];
}
return out;
}
bool dmcacheSave(const string& fn, const mcache_type& cache)
{
SaveCacheTask *tsk = new SaveCacheTask(fn, cache);
// Use the flush option to put() so that only the latest version
// stays on the queue, possibly saving writes.
if (!saveQueue.put(tsk, true)) {
LOGERR("dmcacheSave: can't queue save task" << endl);
return false;
}
return true;
}
static void *dmcacheSaveWorker(void *)
{
for (;;) {
SaveCacheTask *tsk = 0;
size_t qsz;
if (!saveQueue.take(&tsk, &qsz)) {
LOGERR("dmcacheSaveWorker: can't get task from queue" << endl);
saveQueue.workerExit();
return (void*)1;
}
LOGDEB("dmcacheSave: got save task: " << tsk->m_cache.size() <<
" entries to " << tsk->m_fn << endl);
string tfn = tsk->m_fn + "-";
ofstream output(tfn, ios::out | ios::trunc);
if (!output.is_open()) {
LOGERR("dmcacheSave: could not open " << tfn
<< " for writing" << endl);
delete tsk;
continue;
}
for (mcache_type::const_iterator it = tsk->m_cache.begin();
it != tsk->m_cache.end(); it++) {
output << encode(it->first) << '=' << encode(it->second) << '\n';
if (!output.good()) {
LOGERR("dmcacheSave: write error while saving to " <<
tfn << endl);
break;
}
}
output.flush();
if (!output.good()) {
LOGERR("dmcacheSave: flush error while saving to " <<
tfn << endl);
}
if (rename(tfn.c_str(), tsk->m_fn.c_str()) != 0) {
LOGERR("dmcacheSave: rename(" << tfn << ", " << tsk->m_fn << ")" <<
" failed: errno: " << errno << endl);
}
delete tsk;
if (slptimesecs) {
LOGDEB1("dmcacheSave: sleeping " << slptimesecs << endl);
sleep(slptimesecs);
}
}
}
// Max size of metadata element ??
#define LL 10*1024
bool dmcacheRestore(const string& fn, mcache_type& cache)
{
// Restore is called once at startup, so seize the opportunity to start the
// save thread
if (!saveQueue.start(1, dmcacheSaveWorker, 0)) {
LOGERR("dmcacheRestore: could not start save thread" << endl);
return false;
}
ifstream input;
input.open(fn, ios::in);
if (!input.is_open()) {
LOGERR("dmcacheRestore: could not open " << fn << endl);
return false;
}
char cline[LL];
for (;;) {
input.getline(cline, LL-1, '\n');
if (input.eof())
break;
if (!input.good()) {
LOGERR("dmcacheRestore: read error on " << fn << endl);
return false;
}
char *cp = strchr(cline, '=');
if (cp == 0) {
LOGERR("dmcacheRestore: no = in line !" << endl);
return false;
}
*cp = 0;
cache[decode(cline)] = decode(cp+1);
}
return true;
}