|
a/src/ohmetacache.cxx |
|
b/src/ohmetacache.cxx |
1 |
/* Copyright (C) 2014 J.F.Dockes
|
1 |
/* Copyright (C) 2014 J.F.Dockes
|
2 |
* This program is free software; you can redistribute it and/or modify
|
2 |
* This program is free software; you can redistribute it and/or modify
|
3 |
* it under the terms of the GNU Lesser General Public License as published by
|
3 |
* it under the terms of the GNU Lesser General Public License as published by
|
4 |
* the Free Software Foundation; either version 2.1 of the License, or
|
4 |
* the Free Software Foundation; either version 2.1 of the License, or
|
5 |
* (at your option) any later version.
|
5 |
* (at your option) any later version.
|
6 |
*
|
6 |
*
|
7 |
* This program is distributed in the hope that it will be useful,
|
7 |
* This program is distributed in the hope that it will be useful,
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10 |
* GNU Lesser General Public License for more details.
|
10 |
* GNU Lesser General Public License for more details.
|
11 |
*
|
11 |
*
|
12 |
* You should have received a copy of the GNU Lesser General Public License
|
12 |
* You should have received a copy of the GNU Lesser General Public License
|
13 |
* along with this program; if not, write to the
|
13 |
* along with this program; if not, write to the
|
14 |
* Free Software Foundation, Inc.,
|
14 |
* Free Software Foundation, Inc.,
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
15 |
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
16 |
*/
|
16 |
*/
|
17 |
|
17 |
|
18 |
#include "ohmetacache.hxx"
|
18 |
#include "ohmetacache.hxx"
|
19 |
|
19 |
|
20 |
#include <errno.h> // for errno
|
20 |
#include <errno.h>
|
21 |
#include <stdio.h> // for rename
|
21 |
#include <stdio.h>
|
22 |
#include <string.h> // for strchr
|
22 |
#include <string.h>
|
23 |
#include <unistd.h>
|
23 |
#include <unistd.h>
|
24 |
|
24 |
|
25 |
#include <iostream> // for basic_ostream, operator<<, etc
|
25 |
#include <iostream>
|
26 |
#include <utility> // for pair
|
26 |
#include <utility>
|
27 |
|
27 |
|
28 |
#include "libupnpp/log.h"
|
28 |
#include "libupnpp/log.h"
|
29 |
#include "libupnpp/workqueue.h"
|
29 |
#include "libupnpp/workqueue.h"
|
30 |
|
30 |
|
31 |
using namespace std;
|
31 |
using namespace std;
|
|
... |
|
... |
51 |
static string encode(const string& in)
|
51 |
static string encode(const string& in)
|
52 |
{
|
52 |
{
|
53 |
string out;
|
53 |
string out;
|
54 |
const char *cp = in.c_str();
|
54 |
const char *cp = in.c_str();
|
55 |
for (string::size_type i = 0; i < in.size(); i++) {
|
55 |
for (string::size_type i = 0; i < in.size(); i++) {
|
56 |
unsigned int c;
|
56 |
unsigned int c;
|
57 |
const char *h = "0123456789ABCDEF";
|
57 |
const char *h = "0123456789ABCDEF";
|
58 |
c = cp[i];
|
58 |
c = cp[i];
|
59 |
if (c == '%' || c == '=' || c == '\n' || c == '\r') {
|
59 |
if (c == '%' || c == '=' || c == '\n' || c == '\r') {
|
60 |
out += '%';
|
60 |
out += '%';
|
61 |
out += h[(c >> 4) & 0xf];
|
61 |
out += h[(c >> 4) & 0xf];
|
62 |
out += h[c & 0xf];
|
62 |
out += h[c & 0xf];
|
63 |
} else {
|
63 |
} else {
|
64 |
out += char(c);
|
64 |
out += char(c);
|
65 |
}
|
65 |
}
|
66 |
}
|
66 |
}
|
67 |
return out;
|
67 |
return out;
|
68 |
}
|
68 |
}
|
69 |
|
69 |
|
70 |
static int h2d(int c)
|
70 |
static int h2d(int c)
|
|
... |
|
... |
83 |
const char *cp = in.c_str();
|
83 |
const char *cp = in.c_str();
|
84 |
if (in.size() <= 2)
|
84 |
if (in.size() <= 2)
|
85 |
return in;
|
85 |
return in;
|
86 |
string::size_type i = 0;
|
86 |
string::size_type i = 0;
|
87 |
for (; i < in.size() - 2; i++) {
|
87 |
for (; i < in.size() - 2; i++) {
|
88 |
if (cp[i] == '%') {
|
88 |
if (cp[i] == '%') {
|
89 |
int d1 = h2d(cp[++i]);
|
89 |
int d1 = h2d(cp[++i]);
|
90 |
int d2 = h2d(cp[++i]);
|
90 |
int d2 = h2d(cp[++i]);
|
91 |
if (d1 != -1 && d2 != -1)
|
91 |
if (d1 != -1 && d2 != -1)
|
92 |
out += (d1 << 4) + d2;
|
92 |
out += (d1 << 4) + d2;
|
93 |
} else {
|
93 |
} else {
|
94 |
out += cp[i];
|
94 |
out += cp[i];
|
95 |
}
|
95 |
}
|
96 |
}
|
96 |
}
|
97 |
while (i < in.size()) {
|
97 |
while (i < in.size()) {
|
98 |
out += cp[i++];
|
98 |
out += cp[i++];
|
|
... |
|
... |
125 |
}
|
125 |
}
|
126 |
LOGDEB("dmcacheSave: got save task: " << tsk->m_cache.size() <<
|
126 |
LOGDEB("dmcacheSave: got save task: " << tsk->m_cache.size() <<
|
127 |
" entries to " << tsk->m_fn << endl);
|
127 |
" entries to " << tsk->m_fn << endl);
|
128 |
|
128 |
|
129 |
string tfn = tsk->m_fn + "-";
|
129 |
string tfn = tsk->m_fn + "-";
|
130 |
ofstream output(tfn, ios::out | ios::trunc);
|
130 |
ofstream output(tfn, ios::out | ios::trunc);
|
131 |
if (!output.is_open()) {
|
131 |
if (!output.is_open()) {
|
132 |
LOGERR("dmcacheSave: could not open " << tfn
|
132 |
LOGERR("dmcacheSave: could not open " << tfn
|
133 |
<< " for writing" << endl);
|
133 |
<< " for writing" << endl);
|
134 |
delete tsk;
|
134 |
delete tsk;
|
135 |
continue;
|
135 |
continue;
|
136 |
}
|
136 |
}
|
137 |
|
137 |
|
138 |
for (mcache_type::const_iterator it = tsk->m_cache.begin();
|
138 |
for (mcache_type::const_iterator it = tsk->m_cache.begin();
|
139 |
it != tsk->m_cache.end(); it++) {
|
139 |
it != tsk->m_cache.end(); it++) {
|
140 |
output << encode(it->first) << '=' << encode(it->second) << '\n';
|
140 |
output << encode(it->first) << '=' << encode(it->second) << '\n';
|
141 |
if (!output.good()) {
|
141 |
if (!output.good()) {
|
142 |
LOGERR("dmcacheSave: write error while saving to " <<
|
142 |
LOGERR("dmcacheSave: write error while saving to " <<
|
143 |
tfn << endl);
|
143 |
tfn << endl);
|
144 |
break;
|
144 |
break;
|
145 |
}
|
145 |
}
|
146 |
}
|
146 |
}
|
|
... |
|
... |
181 |
return false;
|
181 |
return false;
|
182 |
}
|
182 |
}
|
183 |
|
183 |
|
184 |
char cline[LL];
|
184 |
char cline[LL];
|
185 |
for (;;) {
|
185 |
for (;;) {
|
186 |
input.getline(cline, LL-1, '\n');
|
186 |
input.getline(cline, LL-1, '\n');
|
187 |
if (input.eof())
|
187 |
if (input.eof())
|
188 |
break;
|
188 |
break;
|
189 |
if (!input.good()) {
|
189 |
if (!input.good()) {
|
190 |
LOGERR("dmcacheRestore: read error on " << fn << endl);
|
190 |
LOGERR("dmcacheRestore: read error on " << fn << endl);
|
191 |
return false;
|
191 |
return false;
|