This commit is contained in:
Jean-Francois Dockes 2016-04-12 10:04:48 +02:00
parent d70b3cb4d2
commit c06eb296c1
2 changed files with 610 additions and 554 deletions

View File

@ -44,8 +44,9 @@ static ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
ssize_t tot = 0;
for (int i = 0; i < iovcnt; i++) {
ssize_t ret = ::write(fd, iov[i].iov_base, iov[i].iov_len);
if (ret > 0)
if (ret > 0) {
tot += ret;
}
if (ret != (ssize_t)iov[i].iov_len) {
return ret == -1 ? -1 : tot;
}
@ -157,8 +158,7 @@ class UdiH {
public:
UCHAR h[UDIHLEN];
UdiH(const string& udi)
{
UdiH(const string& udi) {
MD5_CTX ctx;
MD5Init(&ctx);
MD5Update(&ctx, (const UCHAR*)udi.c_str(), udi.length());
@ -176,21 +176,22 @@ public:
}
return out;
}
bool operator==(const UdiH& r) const
{
bool operator==(const UdiH& r) const {
for (int i = 0; i < UDIHLEN; i++)
if (h[i] != r.h[i])
if (h[i] != r.h[i]) {
return false;
}
return true;
}
bool operator<(const UdiH& r) const
{
bool operator<(const UdiH& r) const {
for (int i = 0; i < UDIHLEN; i++) {
if (h[i] < r.h[i])
if (h[i] < r.h[i]) {
return true;
if (h[i] > r.h[i])
}
if (h[i] > r.h[i]) {
return false;
}
}
return false;
}
};
@ -231,8 +232,7 @@ public:
bool m_ofskhcplt; // Has cache been fully read since open?
// Add udi->offset translation to map
bool khEnter(const string& udi, off_t ofs)
{
bool khEnter(const string& udi, off_t ofs) {
UdiH h(udi);
LOGDEB2(("Circache::khEnter: h %s offs %lu udi [%s]\n",
@ -256,8 +256,7 @@ public:
LOGDEB2(("Circache::khEnter: inserted\n"));
return true;
}
void khDump()
{
void khDump() {
for (kh_type::const_iterator it = m_ofskh.begin();
it != m_ofskh.end(); it++) {
LOGDEB(("Circache::KHDUMP: %s %d\n",
@ -268,8 +267,7 @@ public:
// Return vector of candidate offsets for udi (possibly several
// because there may be hash collisions, and also multiple
// instances).
bool khFind(const string& udi, vector<off_t>& ofss)
{
bool khFind(const string& udi, vector<off_t>& ofss) {
ofss.clear();
UdiH h(udi);
@ -280,16 +278,21 @@ public:
pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
#if 0
if (p.first == m_ofskh.end()) LOGDEB(("KHFIND: FIRST END()\n"));
if (p.second == m_ofskh.end()) LOGDEB(("KHFIND: SECOND END()\n"));
if (p.first == m_ofskh.end()) {
LOGDEB(("KHFIND: FIRST END()\n"));
}
if (p.second == m_ofskh.end()) {
LOGDEB(("KHFIND: SECOND END()\n"));
}
if (!(p.first->first == h))
LOGDEB(("KHFIND: NOKEY: %s %s\n",
p.first->first.asHexString().c_str(),
p.second->first.asHexString().c_str()));
#endif
if (p.first == m_ofskh.end() || !(p.first->first == h))
if (p.first == m_ofskh.end() || !(p.first->first == h)) {
return false;
}
for (kh_type::iterator it = p.first; it != p.second; it++) {
ofss.push_back(it->second);
@ -297,30 +300,29 @@ public:
return true;
}
// Clear entry for udi/offs
bool khClear(const pair<string, off_t>& ref)
{
bool khClear(const pair<string, off_t>& ref) {
UdiH h(ref.first);
pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
if (p.first != m_ofskh.end() && (p.first->first == h)) {
for (kh_type::iterator it = p.first; it != p.second;) {
kh_type::iterator tmp = it++;
if (tmp->second == ref.second)
if (tmp->second == ref.second) {
m_ofskh.erase(tmp);
}
}
}
return true;
}
// Clear entries for vector of udi/offs
bool khClear(const vector<pair<string, off_t> >& udis)
{
bool khClear(const vector<pair<string, off_t> >& udis) {
for (vector<pair<string, off_t> >::const_iterator it = udis.begin();
it != udis.end(); it++)
it != udis.end(); it++) {
khClear(*it);
}
return true;
}
// Clear all entries for udi
bool khClear(const string& udi)
{
bool khClear(const string& udi) {
UdiH h(udi);
pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
if (p.first != m_ofskh.end() && (p.first->first == h)) {
@ -334,21 +336,22 @@ public:
CirCacheInternal()
: m_fd(-1), m_maxsize(-1), m_oheadoffs(-1),
m_nheadoffs(0), m_npadsize(0), m_uniquentries(false),
m_buffer(0), m_bufsiz(0), m_ofskhcplt(false)
{}
~CirCacheInternal()
{
if (m_fd >= 0)
close(m_fd);
if (m_buffer)
free(m_buffer);
m_buffer(0), m_bufsiz(0), m_ofskhcplt(false) {
}
char *buf(size_t sz)
{
if (m_bufsiz >= sz)
~CirCacheInternal() {
if (m_fd >= 0) {
close(m_fd);
}
if (m_buffer) {
free(m_buffer);
}
}
char *buf(size_t sz) {
if (m_bufsiz >= sz) {
return m_buffer;
}
if ((m_buffer = (char *)realloc(m_buffer, sz))) {
m_bufsiz = sz;
} else {
@ -359,8 +362,7 @@ public:
}
// Name for the cache file
string datafn(const string& d)
{
string datafn(const string& d) {
return path_cat(d, "circache.crch");
}
@ -392,8 +394,7 @@ public:
return true;
}
bool readfirstblock()
{
bool readfirstblock() {
if (m_fd < 0) {
m_reason << "readfirstblock: not open ";
return false;
@ -438,8 +439,7 @@ public:
return true;
}
bool writeEntryHeader(off_t offset, const EntryHeaderData& d)
{
bool writeEntryHeader(off_t offset, const EntryHeaderData& d) {
if (m_fd < 0) {
m_reason << "writeEntryHeader: not open ";
return false;
@ -460,8 +460,7 @@ public:
return true;
}
CCScanHook::status readEntryHeader(off_t offset, EntryHeaderData& d)
{
CCScanHook::status readEntryHeader(off_t offset, EntryHeaderData& d) {
if (m_fd < 0) {
m_reason << "readEntryHeader: not open ";
return CCScanHook::Error;
@ -496,8 +495,7 @@ public:
}
CCScanHook::status scan(off_t startoffset, CCScanHook *user,
bool fold = false)
{
bool fold = false) {
if (m_fd < 0) {
m_reason << "scan: not open ";
return CCScanHook::Error;
@ -515,7 +513,8 @@ public:
EntryHeaderData d;
CCScanHook::status st;
switch ((st = readEntryHeader(startoffset, d))) {
case CCScanHook::Continue: break;
case CCScanHook::Continue:
break;
case CCScanHook::Eof:
if (fold && !already_folded) {
already_folded = true;
@ -564,13 +563,14 @@ public:
}
}
bool readHUdi(off_t hoffs, EntryHeaderData& d, string& udi)
{
if (readEntryHeader(hoffs, d) != CCScanHook::Continue)
bool readHUdi(off_t hoffs, EntryHeaderData& d, string& udi) {
if (readEntryHeader(hoffs, d) != CCScanHook::Continue) {
return false;
}
string dic;
if (!readDicData(hoffs, d, dic, 0))
if (!readDicData(hoffs, d, dic, 0)) {
return false;
}
if (d.dicsize == 0) {
// This is an erased entry
udi.erase();
@ -585,8 +585,7 @@ public:
}
bool readDicData(off_t hoffs, EntryHeaderData& hd, string& dic,
string* data)
{
string* data) {
off_t offs = hoffs + CIRCACHE_HEADER_SIZE;
// This syscall could be avoided in some cases if we saved the offset
// at each seek. In most cases, we just read the header and we are
@ -599,8 +598,9 @@ public:
char *bf = 0;
if (hd.dicsize) {
bf = buf(hd.dicsize);
if (bf == 0)
if (bf == 0) {
return false;
}
if (read(m_fd, bf, hd.dicsize) != int(hd.dicsize)) {
m_reason << "CirCache::get: read() failed: errno " << errno;
return false;
@ -609,13 +609,15 @@ public:
} else {
dic.erase();
}
if (data == 0)
if (data == 0) {
return true;
}
if (hd.datasize) {
bf = buf(hd.datasize);
if (bf == 0)
if (bf == 0) {
return false;
}
if (read(m_fd, bf, hd.datasize) != int(hd.datasize)) {
m_reason << "CirCache::get: read() failed: errno " << errno;
return false;
@ -669,12 +671,10 @@ public:
off_t headoffs;
off_t padsize;
CCScanHookRecord()
: headoffs(0), padsize(0)
{
: headoffs(0), padsize(0) {
}
virtual status takeone(off_t offs, const string& udi,
const EntryHeaderData& d)
{
const EntryHeaderData& d) {
headoffs = offs;
padsize = d.padsize;
LOGDEB2(("CCScanHookRecord::takeone: offs %s padsize %s\n",
@ -773,8 +773,9 @@ bool CirCache::open(OpMode mode)
return false;
}
if (m_d->m_fd >= 0)
if (m_d->m_fd >= 0) {
::close(m_d->m_fd);
}
if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(),
mode == CC_OPREAD ?
@ -789,8 +790,7 @@ bool CirCache::open(OpMode mode)
class CCScanHookDump : public CCScanHook {
public:
virtual status takeone(off_t offs, const string& udi,
const EntryHeaderData& d)
{
const EntryHeaderData& d) {
cout << "Scan: offs " << offs << " dicsize " << d.dicsize
<< " datasize " << d.datasize << " padsize " << d.padsize <<
" flags " << d.flags <<
@ -839,8 +839,7 @@ public:
: m_udi(udi), m_targinstance(ti), m_instance(0), m_offs(0) {}
virtual status takeone(off_t offs, const string& udi,
const EntryHeaderData& d)
{
const EntryHeaderData& d) {
LOGDEB2(("Circache:Scan: off %ld udi [%s] dcsz %u dtsz %u pdsz %u "
" flgs %hu\n",
long(offs), udi.c_str(), (UINT)d.dicsize,
@ -849,9 +848,10 @@ public:
m_instance++;
m_offs = offs;
m_hd = d;
if (m_instance == m_targinstance)
if (m_instance == m_targinstance) {
return Stop;
}
}
return Continue;
}
};
@ -882,8 +882,9 @@ bool CirCache::get(const string& udi, string& dic, string& data, int instance)
LOGDEB1(("Circache::get: trying offs %lu\n", (ULONG)*it));
EntryHeaderData d;
string fudi;
if (!m_d->readHUdi(*it, d, fudi))
if (!m_d->readHUdi(*it, d, fudi)) {
return false;
}
if (!fudi.compare(udi)) {
// Found one, memorize offset. Done if instance
// matches, else go on. If instance is -1 need to
@ -913,8 +914,9 @@ bool CirCache::get(const string& udi, string& dic, string& data, int instance)
CCScanHook::status ret = m_d->scan(start, &getter, true);
if (ret == CCScanHook::Eof) {
if (getter.m_instance == 0)
if (getter.m_instance == 0) {
return false;
}
} else if (ret != CCScanHook::Stop) {
return false;
}
@ -960,15 +962,17 @@ bool CirCache::erase(const string& udi)
LOGDEB(("CirCache::erase: reading at %lu\n", (unsigned long)*it));
EntryHeaderData d;
string fudi;
if (!m_d->readHUdi(*it, d, fudi))
if (!m_d->readHUdi(*it, d, fudi)) {
return false;
}
LOGDEB(("CirCache::erase: found fudi [%s]\n", fudi.c_str()));
if (!fudi.compare(udi)) {
EntryHeaderData nd;
nd.padsize = d.dicsize + d.datasize + d.padsize;
LOGDEB(("CirCache::erase: rewriting at %lu\n", (unsigned long)*it));
if (*it == m_d->m_nheadoffs)
if (*it == m_d->m_nheadoffs) {
m_d->m_npadsize = nd.padsize;
}
if (!m_d->writeEntryHeader(*it, nd)) {
LOGERR(("CirCache::erase: write header failed\n"));
return false;
@ -987,17 +991,19 @@ public:
off_t sizeseen;
vector<pair<string, off_t> > squashed_udis;
CCScanHookSpacer(off_t sz)
: sizewanted(sz), sizeseen(0) {assert(sz > 0);}
: sizewanted(sz), sizeseen(0) {
assert(sz > 0);
}
virtual status takeone(off_t offs, const string& udi,
const EntryHeaderData& d)
{
const EntryHeaderData& d) {
LOGDEB2(("Circache:ScanSpacer:off %u dcsz %u dtsz %u pdsz %u udi[%s]\n",
(UINT)offs, d.dicsize, d.datasize, d.padsize, udi.c_str()));
sizeseen += CIRCACHE_HEADER_SIZE + d.dicsize + d.datasize + d.padsize;
squashed_udis.push_back(make_pair(udi, offs));
if (sizeseen >= sizewanted)
if (sizeseen >= sizewanted) {
return Stop;
}
return Continue;
}
};
@ -1090,8 +1096,9 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf,
} else {
LOGDEB(("CirCache::put: recov. prev. padsize %d\n", pd.padsize));
pd.padsize = 0;
if (!m_d->writeEntryHeader(m_d->m_nheadoffs, pd))
if (!m_d->writeEntryHeader(m_d->m_nheadoffs, pd)) {
return false;
}
// If we fail between here and the end, the file is broken.
}
nwriteoffs = m_d->m_oheadoffs - recovpadsize;
@ -1239,8 +1246,9 @@ bool CirCache::next(bool& eof)
st = m_d->readEntryHeader(m_d->m_itoffs, m_d->m_ithd);
}
if (st == CCScanHook::Continue)
if (st == CCScanHook::Continue) {
return true;
}
return false;
}
@ -1251,8 +1259,9 @@ bool CirCache::getCurrentUdi(string& udi)
return false;
}
if (!m_d->readHUdi(m_d->m_itoffs, m_d->m_ithd, udi))
if (!m_d->readHUdi(m_d->m_itoffs, m_d->m_ithd, udi)) {
return false;
}
return true;
}
@ -1262,8 +1271,9 @@ bool CirCache::getCurrent(string& udi, string& dic, string& data)
LOGERR(("CirCache::getCurrent: null data\n"));
return false;
}
if (!m_d->readDicData(m_d->m_itoffs, m_d->m_ithd, dic, &data))
if (!m_d->readDicData(m_d->m_itoffs, m_d->m_ithd, dic, &data)) {
return false;
}
ConfSimple conf(dic, 1);
conf.get("udi", udi, cstr_null);
@ -1284,8 +1294,9 @@ static void *allocmem(
}
int inc = (*np > maxinc) ? maxinc : *np;
if ((cp = realloc(cp, (*np + inc) * sz)) != 0)
if ((cp = realloc(cp, (*np + inc) * sz)) != 0) {
*np += inc;
}
return cp;
}
@ -1335,7 +1346,9 @@ static bool inflateToDynBuf(void* inp, UINT inlen, void **outpp, UINT *outlenp)
d_stream.next_out = (Bytef*)(outp + d_stream.total_out);
}
err = inflate(&d_stream, Z_NO_FLUSH);
if (err == Z_STREAM_END) break;
if (err == Z_STREAM_END) {
break;
}
if (err != Z_OK) {
LOGERR(("Inflate: error %d msg %s\n", err, d_stream.msg));
inflateEnd(&d_stream);
@ -1564,40 +1577,72 @@ int main(int argc, char **argv)
int instance = -1;
thisprog = argv[0];
argc--; argv++;
argc--;
argv++;
while (argc > 0 && **argv == '-') {
(*argv)++;
if (!(**argv))
/* Cas du "adb - core" */
{
Usage();
}
while (**argv)
switch (*(*argv)++) {
case 'a': op_flags |= OPT_a; break;
case 'c': op_flags |= OPT_c; break;
case 'D': op_flags |= OPT_D; break;
case 'd': op_flags |= OPT_d; break;
case 'e': op_flags |= OPT_e; break;
case 'g': op_flags |= OPT_g; break;
case 'i': op_flags |= OPT_i; if (argc < 2) Usage();
if ((sscanf(*(++argv), "%d", &instance)) != 1)
case 'a':
op_flags |= OPT_a;
break;
case 'c':
op_flags |= OPT_c;
break;
case 'D':
op_flags |= OPT_D;
break;
case 'd':
op_flags |= OPT_d;
break;
case 'e':
op_flags |= OPT_e;
break;
case 'g':
op_flags |= OPT_g;
break;
case 'i':
op_flags |= OPT_i;
if (argc < 2) {
Usage();
}
if ((sscanf(*(++argv), "%d", &instance)) != 1) {
Usage();
}
argc--;
goto b1;
case 'p': op_flags |= OPT_p; break;
case 's': op_flags |= OPT_s; break;
case 'u': op_flags |= OPT_u; break;
default: Usage(); break;
case 'p':
op_flags |= OPT_p;
break;
case 's':
op_flags |= OPT_s;
break;
case 'u':
op_flags |= OPT_u;
break;
default:
Usage();
break;
}
b1: argc--; argv++;
b1:
argc--;
argv++;
}
DebugLog::getdbl()->setloglevel(DEBERR);
DebugLog::setfilename("stderr");
if (argc < 1)
if (argc < 1) {
Usage();
string dir = *argv++;argc--;
}
string dir = *argv++;
argc--;
CirCache cc(dir);
@ -1605,10 +1650,12 @@ int main(int argc, char **argv)
if (argc != 1) {
Usage();
}
off_t sizekb = atoi(*argv++);argc--;
off_t sizekb = atoi(*argv++);
argc--;
int flags = 0;
if (op_flags & OPT_u)
if (op_flags & OPT_u) {
flags |= CirCache::CC_CRUNIQUE;
}
if (!cc.create(sizekb * 1024, flags)) {
cerr << "Create failed:" << cc.getReason() << endl;
exit(1);
@ -1617,7 +1664,8 @@ int main(int argc, char **argv)
if (argc != 1) {
Usage();
}
int newmbs = atoi(*argv++);argc--;
int newmbs = atoi(*argv++);
argc--;
if (!resizecc(dir, newmbs)) {
exit(1);
}
@ -1632,14 +1680,16 @@ int main(int argc, char **argv)
argc--;
}
} else if (op_flags & OPT_p) {
if (argc < 1)
if (argc < 1) {
Usage();
}
if (!cc.open(CirCache::CC_OPWRITE)) {
cerr << "Open failed: " << cc.getReason() << endl;
exit(1);
}
while (argc) {
string fn = *argv++;argc--;
string fn = *argv++;
argc--;
char dic[1000];
string data, reason;
if (!file_to_string(fn, data, &reason)) {
@ -1656,7 +1706,9 @@ int main(int argc, char **argv)
if (!cc.put(udi, &conf, data, 0)) {
cerr << "Put failed: " << cc.getReason() << endl;
cerr << "conf: ["; conf.write(cerr); cerr << "]" << endl;
cerr << "conf: [";
conf.write(cerr);
cerr << "]" << endl;
exit(1);
}
}
@ -1667,23 +1719,26 @@ int main(int argc, char **argv)
exit(1);
}
while (argc) {
string udi = *argv++;argc--;
string udi = *argv++;
argc--;
string dic, data;
if (!cc.get(udi, dic, data, instance)) {
cerr << "Get failed: " << cc.getReason() << endl;
exit(1);
}
cout << "Dict: [" << dic << "]" << endl;
if (op_flags & OPT_D)
if (op_flags & OPT_D) {
cout << "Data: [" << data << "]" << endl;
}
}
} else if (op_flags & OPT_e) {
if (!cc.open(CirCache::CC_OPWRITE)) {
cerr << "Open failed: " << cc.getReason() << endl;
exit(1);
}
while (argc) {
string udi = *argv++;argc--;
string udi = *argv++;
argc--;
string dic, data;
if (!cc.erase(udi)) {
cerr << "Erase failed: " << cc.getReason() << endl;
@ -1696,8 +1751,9 @@ int main(int argc, char **argv)
exit(1);
}
cc.dump();
} else
} else {
Usage();
}
exit(0);
}

View File

@ -40,26 +40,23 @@
#include <string>
#ifndef NO_NAMESPACES
using std::string;
#endif
class ConfSimple;
class CirCacheInternal;
class CirCache {
public:
CirCache(const string& dir);
CirCache(const std::string& dir);
virtual ~CirCache();
virtual string getReason();
virtual std::string getReason();
enum CreateFlags {CC_CRNONE = 0,
// Unique entries: erase older instances when same udi
// is stored.
CC_CRUNIQUE = 1,
// Truncate file (restart from scratch).
CC_CRTRUNCATE = 2};
CC_CRTRUNCATE = 2
};
virtual bool create(off_t maxsize, int flags);
enum OpMode {CC_OPREAD, CC_OPWRITE};
@ -67,15 +64,15 @@ public:
virtual std::string getpath();
virtual bool get(const string& udi, string& dic, string& data,
int instance = -1);
virtual bool get(const std::string& udi, std::string& dic,
std::string& data, int instance = -1);
// Note: the dicp MUST have an udi entry
enum PutFlags {NoCompHint = 1};
virtual bool put(const string& udi, const ConfSimple *dicp,
const string& data, unsigned int flags = 0);
virtual bool put(const std::string& udi, const ConfSimple *dicp,
const std::string& data, unsigned int flags = 0);
virtual bool erase(const string& udi);
virtual bool erase(const std::string& udi);
/** Walk the archive.
*
@ -86,10 +83,11 @@ public:
/** Back to oldest */
virtual bool rewind(bool& eof);
/** Get entry under cursor */
virtual bool getCurrent(string& udi, string& dic, string& data);
virtual bool getCurrent(std::string& udi, std::string& dic,
std::string& data);
/** Get current entry udi only. Udi can be empty (erased empty), caller
* should call again */
virtual bool getCurrentUdi(string& udi);
virtual bool getCurrentUdi(std::string& udi);
/** Skip to next. (false && !eof) -> error, (false&&eof)->EOF. */
virtual bool next(bool& eof);
@ -98,10 +96,12 @@ public:
protected:
CirCacheInternal *m_d;
string m_dir;
std::string m_dir;
private:
CirCache(const CirCache&) {}
CirCache& operator=(const CirCache&) {return *this;}
CirCache& operator=(const CirCache&) {
return *this;
}
};
#endif /* _circache_h_included_ */