diff --git a/src/utils/circache.cpp b/src/utils/circache.cpp index 5a54038e..a67bd742 100644 --- a/src/utils/circache.cpp +++ b/src/utils/circache.cpp @@ -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; } @@ -99,23 +100,23 @@ static bool inflateToDynBuf(void *inp, UINT inlen, void **outpp, UINT *outlenp); * File structure: * - Starts with a 1-KB header block, with a param dictionary. * - Stored items follow. Each item has a header and 2 segments for - * the metadata and the data. + * the metadata and the data. * The segment sizes are stored in the ascii header/marker: * circacheSizes = xxx yyy zzz * xxx bytes of metadata * yyy bytes of data * zzz bytes of padding up to next object (only one entry has non zero) * - * There is a write position, which can be at eof while + * There is a write position, which can be at eof while * the file is growing, or inside the file if we are recycling. This is stored * in the header (oheadoffs), together with the maximum size * - * If we are recycling, we have to take care to compute the size of the - * possible remaining area from the last object invalidated by the write, + * If we are recycling, we have to take care to compute the size of the + * possible remaining area from the last object invalidated by the write, * pad it with neutral data and store the size in the new header. To help with * this, the address for the last object written is also kept in the header * (nheadoffs, npadsize) - * + * */ // First block size @@ -142,13 +143,13 @@ class CCScanHook { public: virtual ~CCScanHook() {} enum status {Stop, Continue, Error, Eof}; - virtual status takeone(off_t offs, const string& udi, + virtual status takeone(off_t offs, const string& udi, const EntryHeaderData& d) = 0; }; // We have an auxiliary in-memory multimap of hashed-udi -> offset to // speed things up. This is created the first time the file is scanned -// (on the first get), and not saved to disk. +// (on the first get), and not saved to disk. // The map key: hashed udi. As a very short hash seems sufficient, // maybe we could find something faster/simpler than md5? @@ -157,18 +158,17 @@ class UdiH { public: UCHAR h[UDIHLEN]; - UdiH(const string& udi) - { - MD5_CTX ctx; - MD5Init(&ctx); - MD5Update(&ctx, (const UCHAR*)udi.c_str(), udi.length()); - UCHAR md[16]; - MD5Final(md, &ctx); - memcpy(h, md, UDIHLEN); - } + UdiH(const string& udi) { + MD5_CTX ctx; + MD5Init(&ctx); + MD5Update(&ctx, (const UCHAR*)udi.c_str(), udi.length()); + UCHAR md[16]; + MD5Final(md, &ctx); + memcpy(h, md, UDIHLEN); + } string asHexString() const { - static const char hex[]="0123456789abcdef"; + static const char hex[] = "0123456789abcdef"; string out; for (int i = 0; i < UDIHLEN; i++) { out.append(1, hex[h[i] >> 4]); @@ -176,23 +176,24 @@ public: } return out; } - bool operator==(const UdiH& r) const - { - for (int i = 0; i < UDIHLEN; i++) - if (h[i] != r.h[i]) - return false; - return true; - } - bool operator<(const UdiH& r) const - { - for (int i = 0; i < UDIHLEN; i++) { - if (h[i] < r.h[i]) - return true; - if (h[i] > r.h[i]) - return false; + bool operator==(const UdiH& r) const { + for (int i = 0; i < UDIHLEN; i++) + if (h[i] != r.h[i]) { + return false; + } + return true; + } + bool operator<(const UdiH& r) const { + for (int i = 0; i < UDIHLEN; i++) { + if (h[i] < r.h[i]) { + return true; + } + if (h[i] > r.h[i]) { + return false; } - return false; } + return false; + } }; typedef multimap kh_type; typedef multimap::value_type kh_value_type; @@ -202,16 +203,16 @@ public: int m_fd; ////// These are cache persistent state and written to the first block: // Maximum file size, after which we begin reusing old space - off_t m_maxsize; + off_t m_maxsize; // Offset of the oldest header, or max file offset (file size) // while the file is growing. This is the next write position. off_t m_oheadoffs; // Offset of last write (newest header) off_t m_nheadoffs; - // Pad size for newest entry. + // Pad size for newest entry. off_t m_npadsize; // Keep history or only last entry - bool m_uniquentries; + bool m_uniquentries; ///////////////////// End header entries // A place to hold data when reading @@ -231,138 +232,139 @@ 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) - { - UdiH h(udi); + bool khEnter(const string& udi, off_t ofs) { + UdiH h(udi); - LOGDEB2(("Circache::khEnter: h %s offs %lu udi [%s]\n", - h.asHexString().c_str(), (ULONG)ofs, udi.c_str())); + LOGDEB2(("Circache::khEnter: h %s offs %lu udi [%s]\n", + h.asHexString().c_str(), (ULONG)ofs, udi.c_str())); - pair p = m_ofskh.equal_range(h); + pair 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; it++) { - LOGDEB2(("Circache::khEnter: col h %s, ofs %lu\n", - it->first.asHexString().c_str(), - (ULONG)it->second)); - if (it->second == ofs) { - // (h,offs) already there. Happens - LOGDEB2(("Circache::khEnter: already there\n")); - return true; - } + if (p.first != m_ofskh.end() && p.first->first == h) { + for (kh_type::iterator it = p.first; it != p.second; it++) { + LOGDEB2(("Circache::khEnter: col h %s, ofs %lu\n", + it->first.asHexString().c_str(), + (ULONG)it->second)); + if (it->second == ofs) { + // (h,offs) already there. Happens + LOGDEB2(("Circache::khEnter: already there\n")); + return true; } } - m_ofskh.insert(kh_value_type(h, ofs)); - LOGDEB2(("Circache::khEnter: inserted\n")); - return true; } - void khDump() - { - for (kh_type::const_iterator it = m_ofskh.begin(); - it != m_ofskh.end(); it++) { - LOGDEB(("Circache::KHDUMP: %s %d\n", - it->first.asHexString().c_str(), (ULONG)it->second)); - } + m_ofskh.insert(kh_value_type(h, ofs)); + LOGDEB2(("Circache::khEnter: inserted\n")); + return true; + } + void khDump() { + for (kh_type::const_iterator it = m_ofskh.begin(); + it != m_ofskh.end(); it++) { + LOGDEB(("Circache::KHDUMP: %s %d\n", + it->first.asHexString().c_str(), (ULONG)it->second)); } + } // 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& ofss) - { - ofss.clear(); + bool khFind(const string& udi, vector& ofss) { + ofss.clear(); - UdiH h(udi); + UdiH h(udi); - LOGDEB2(("Circache::khFind: h %s udi [%s]\n", - h.asHexString().c_str(), udi.c_str())); + LOGDEB2(("Circache::khFind: h %s udi [%s]\n", + h.asHexString().c_str(), udi.c_str())); - pair p = m_ofskh.equal_range(h); + pair 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->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)) - return false; - - for (kh_type::iterator it = p.first; it != p.second; it++) { - ofss.push_back(it->second); - } - return true; + 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)) { + return false; + } + + for (kh_type::iterator it = p.first; it != p.second; it++) { + ofss.push_back(it->second); + } + return true; + } // Clear entry for udi/offs - bool khClear(const pair& ref) - { - UdiH h(ref.first); - pair 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) - m_ofskh.erase(tmp); - } - } - return true; - } - // Clear entries for vector of udi/offs - bool khClear(const vector >& udis) - { - for (vector >::const_iterator it = udis.begin(); - it != udis.end(); it++) - khClear(*it); - return true; - } - // Clear all entries for udi - bool khClear(const string& udi) - { - UdiH h(udi); - pair 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++; + bool khClear(const pair& ref) { + UdiH h(ref.first); + pair 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) { m_ofskh.erase(tmp); } } - return true; } - 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); + return true; + } + // Clear entries for vector of udi/offs + bool khClear(const vector >& udis) { + for (vector >::const_iterator it = udis.begin(); + it != udis.end(); it++) { + khClear(*it); } - - char *buf(size_t sz) - { - if (m_bufsiz >= sz) - return m_buffer; - if ((m_buffer = (char *)realloc(m_buffer, sz))) { - m_bufsiz = sz; - } else { - m_reason << "CirCache:: realloc(" << sz << ") failed"; - m_bufsiz = 0; + return true; + } + // Clear all entries for udi + bool khClear(const string& udi) { + UdiH h(udi); + pair 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++; + m_ofskh.erase(tmp); } + } + return true; + } + 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); + } + } + + char *buf(size_t sz) { + if (m_bufsiz >= sz) { return m_buffer; } + if ((m_buffer = (char *)realloc(m_buffer, sz))) { + m_bufsiz = sz; + } else { + m_reason << "CirCache:: realloc(" << sz << ") failed"; + m_bufsiz = 0; + } + return m_buffer; + } // Name for the cache file - string datafn(const string& d) - { - return path_cat(d, "circache.crch"); - } + string datafn(const string& d) { + return path_cat(d, "circache.crch"); + } bool writefirstblock() { if (m_fd < 0) { @@ -371,16 +373,16 @@ public: } ostringstream s; - s << - "maxsize = " << m_maxsize << "\n" << - "oheadoffs = " << m_oheadoffs << "\n" << - "nheadoffs = " << m_nheadoffs << "\n" << - "npadsize = " << m_npadsize << "\n" << - "unient = " << m_uniquentries << "\n" << - " " << - " " << - " " << - "\0"; + s << + "maxsize = " << m_maxsize << "\n" << + "oheadoffs = " << m_oheadoffs << "\n" << + "nheadoffs = " << m_nheadoffs << "\n" << + "npadsize = " << m_npadsize << "\n" << + "unient = " << m_uniquentries << "\n" << + " " << + " " << + " " << + "\0"; int sz = int(s.str().size()); assert(sz < CIRCACHE_FIRSTBLOCK_SIZE); @@ -392,254 +394,254 @@ public: return true; } - bool readfirstblock() - { - if (m_fd < 0) { - m_reason << "readfirstblock: not open "; - return false; - } - - char bf[CIRCACHE_FIRSTBLOCK_SIZE]; - - lseek(m_fd, 0, 0); - if (read(m_fd, bf, CIRCACHE_FIRSTBLOCK_SIZE) != - CIRCACHE_FIRSTBLOCK_SIZE) { - m_reason << "readfirstblock: read() failed: errno " << errno; - return false; - } - string s(bf, CIRCACHE_FIRSTBLOCK_SIZE); - ConfSimple conf(s, 1); - string value; - if (!conf.get("maxsize", value, cstr_null)) { - m_reason << "readfirstblock: conf get maxsize failed"; - return false; - } - m_maxsize = atoll(value.c_str()); - if (!conf.get("oheadoffs", value, cstr_null)) { - m_reason << "readfirstblock: conf get oheadoffs failed"; - return false; - } - m_oheadoffs = atoll(value.c_str()); - if (!conf.get("nheadoffs", value, cstr_null)) { - m_reason << "readfirstblock: conf get nheadoffs failed"; - return false; - } - m_nheadoffs = atoll(value.c_str()); - if (!conf.get("npadsize", value, cstr_null)) { - m_reason << "readfirstblock: conf get npadsize failed"; - return false; - } - m_npadsize = atoll(value.c_str()); - if (!conf.get("unient", value, cstr_null)) { - m_uniquentries = false; - } else { - m_uniquentries = stringToBool(value); - } - return true; - } - - bool writeEntryHeader(off_t offset, const EntryHeaderData& d) - { - if (m_fd < 0) { - m_reason << "writeEntryHeader: not open "; - return false; - } - char bf[CIRCACHE_HEADER_SIZE]; - memset(bf, 0, CIRCACHE_HEADER_SIZE); - snprintf(bf, CIRCACHE_HEADER_SIZE, - headerformat, d.dicsize, d.datasize, d.padsize, d.flags); - if (lseek(m_fd, offset, 0) != offset) { - m_reason << "CirCache::weh: lseek(" << offset << - ") failed: errno " << errno; - return false; - } - if (write(m_fd, bf, CIRCACHE_HEADER_SIZE) != CIRCACHE_HEADER_SIZE) { - m_reason << "CirCache::weh: write failed. errno " << errno; - return false; - } - return true; + bool readfirstblock() { + if (m_fd < 0) { + m_reason << "readfirstblock: not open "; + return false; } - CCScanHook::status readEntryHeader(off_t offset, EntryHeaderData& d) - { - if (m_fd < 0) { - m_reason << "readEntryHeader: not open "; - return CCScanHook::Error; - } + char bf[CIRCACHE_FIRSTBLOCK_SIZE]; - if (lseek(m_fd, offset, 0) != offset) { - m_reason << "readEntryHeader: lseek(" << offset << - ") failed: errno " << errno; - return CCScanHook::Error; - } - char bf[CIRCACHE_HEADER_SIZE]; + lseek(m_fd, 0, 0); + if (read(m_fd, bf, CIRCACHE_FIRSTBLOCK_SIZE) != + CIRCACHE_FIRSTBLOCK_SIZE) { + m_reason << "readfirstblock: read() failed: errno " << errno; + return false; + } + string s(bf, CIRCACHE_FIRSTBLOCK_SIZE); + ConfSimple conf(s, 1); + string value; + if (!conf.get("maxsize", value, cstr_null)) { + m_reason << "readfirstblock: conf get maxsize failed"; + return false; + } + m_maxsize = atoll(value.c_str()); + if (!conf.get("oheadoffs", value, cstr_null)) { + m_reason << "readfirstblock: conf get oheadoffs failed"; + return false; + } + m_oheadoffs = atoll(value.c_str()); + if (!conf.get("nheadoffs", value, cstr_null)) { + m_reason << "readfirstblock: conf get nheadoffs failed"; + return false; + } + m_nheadoffs = atoll(value.c_str()); + if (!conf.get("npadsize", value, cstr_null)) { + m_reason << "readfirstblock: conf get npadsize failed"; + return false; + } + m_npadsize = atoll(value.c_str()); + if (!conf.get("unient", value, cstr_null)) { + m_uniquentries = false; + } else { + m_uniquentries = stringToBool(value); + } + return true; + } - int ret = read(m_fd, bf, CIRCACHE_HEADER_SIZE); - if (ret == 0) { - // Eof - m_reason << " Eof "; + bool writeEntryHeader(off_t offset, const EntryHeaderData& d) { + if (m_fd < 0) { + m_reason << "writeEntryHeader: not open "; + return false; + } + char bf[CIRCACHE_HEADER_SIZE]; + memset(bf, 0, CIRCACHE_HEADER_SIZE); + snprintf(bf, CIRCACHE_HEADER_SIZE, + headerformat, d.dicsize, d.datasize, d.padsize, d.flags); + if (lseek(m_fd, offset, 0) != offset) { + m_reason << "CirCache::weh: lseek(" << offset << + ") failed: errno " << errno; + return false; + } + if (write(m_fd, bf, CIRCACHE_HEADER_SIZE) != CIRCACHE_HEADER_SIZE) { + m_reason << "CirCache::weh: write failed. errno " << errno; + return false; + } + return true; + } + + CCScanHook::status readEntryHeader(off_t offset, EntryHeaderData& d) { + if (m_fd < 0) { + m_reason << "readEntryHeader: not open "; + return CCScanHook::Error; + } + + if (lseek(m_fd, offset, 0) != offset) { + m_reason << "readEntryHeader: lseek(" << offset << + ") failed: errno " << errno; + return CCScanHook::Error; + } + char bf[CIRCACHE_HEADER_SIZE]; + + int ret = read(m_fd, bf, CIRCACHE_HEADER_SIZE); + if (ret == 0) { + // Eof + m_reason << " Eof "; + return CCScanHook::Eof; + } + if (ret != CIRCACHE_HEADER_SIZE) { + m_reason << " readheader: read failed errno " << errno; + return CCScanHook::Error; + } + if (sscanf(bf, headerformat, &d.dicsize, &d.datasize, + &d.padsize, &d.flags) != 4) { + m_reason << " readEntryHeader: bad header at " << + offset << " [" << bf << "]"; + return CCScanHook::Error; + } + LOGDEB2(("Circache:readEntryHeader: dcsz %u dtsz %u pdsz %u flgs %hu\n", + d.dicsize, d.datasize, d.padsize, d.flags)); + return CCScanHook::Continue; + } + + CCScanHook::status scan(off_t startoffset, CCScanHook *user, + bool fold = false) { + if (m_fd < 0) { + m_reason << "scan: not open "; + return CCScanHook::Error; + } + + off_t so0 = startoffset; + bool already_folded = false; + + while (true) { + if (already_folded && startoffset == so0) { + m_ofskhcplt = true; return CCScanHook::Eof; } - if (ret != CIRCACHE_HEADER_SIZE) { - m_reason << " readheader: read failed errno " << errno; - return CCScanHook::Error; + + EntryHeaderData d; + CCScanHook::status st; + switch ((st = readEntryHeader(startoffset, d))) { + case CCScanHook::Continue: + break; + case CCScanHook::Eof: + if (fold && !already_folded) { + already_folded = true; + startoffset = CIRCACHE_FIRSTBLOCK_SIZE; + continue; + } + /* FALLTHROUGH */ + default: + return st; } - if (sscanf(bf, headerformat, &d.dicsize, &d.datasize, - &d.padsize, &d.flags) != 4) { - m_reason << " readEntryHeader: bad header at " << - offset << " [" << bf << "]"; - return CCScanHook::Error; + + string udi; + if (d.dicsize) { + // d.dicsize is 0 for erased entries + char *bf; + if ((bf = buf(d.dicsize + 1)) == 0) { + return CCScanHook::Error; + } + bf[d.dicsize] = 0; + if (read(m_fd, bf, d.dicsize) != int(d.dicsize)) { + m_reason << "scan: read failed errno " << errno; + return CCScanHook::Error; + } + string b(bf, d.dicsize); + ConfSimple conf(b, 1); + + if (!conf.get("udi", udi, cstr_null)) { + m_reason << "scan: no udi in dic"; + return CCScanHook::Error; + } + khEnter(udi, startoffset); } - LOGDEB2(("Circache:readEntryHeader: dcsz %u dtsz %u pdsz %u flgs %hu\n", - d.dicsize, d.datasize, d.padsize, d.flags)); - return CCScanHook::Continue; + + // Call callback + CCScanHook::status a = + user->takeone(startoffset, udi, d); + switch (a) { + case CCScanHook::Continue: + break; + default: + return a; + } + + startoffset += CIRCACHE_HEADER_SIZE + d.dicsize + + d.datasize + d.padsize; } + } - CCScanHook::status scan(off_t startoffset, CCScanHook *user, - bool fold = false) - { - if (m_fd < 0) { - m_reason << "scan: not open "; - return CCScanHook::Error; - } - - off_t so0 = startoffset; - bool already_folded = false; - - while (true) { - if (already_folded && startoffset == so0) { - m_ofskhcplt = true; - return CCScanHook::Eof; - } - - EntryHeaderData d; - CCScanHook::status st; - switch ((st = readEntryHeader(startoffset, d))) { - case CCScanHook::Continue: break; - case CCScanHook::Eof: - if (fold && !already_folded) { - already_folded = true; - startoffset = CIRCACHE_FIRSTBLOCK_SIZE; - continue; - } - /* FALLTHROUGH */ - default: - return st; - } - - string udi; - if (d.dicsize) { - // d.dicsize is 0 for erased entries - char *bf; - if ((bf = buf(d.dicsize+1)) == 0) { - return CCScanHook::Error; - } - bf[d.dicsize] = 0; - if (read(m_fd, bf, d.dicsize) != int(d.dicsize)) { - m_reason << "scan: read failed errno " << errno; - return CCScanHook::Error; - } - string b(bf, d.dicsize); - ConfSimple conf(b, 1); - - if (!conf.get("udi", udi, cstr_null)) { - m_reason << "scan: no udi in dic"; - return CCScanHook::Error; - } - khEnter(udi, startoffset); - } - - // Call callback - CCScanHook::status a = - user->takeone(startoffset, udi, d); - switch (a) { - case CCScanHook::Continue: - break; - default: - return a; - } - - startoffset += CIRCACHE_HEADER_SIZE + d.dicsize + - d.datasize + d.padsize; - } + 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)) { + return false; + } + if (d.dicsize == 0) { + // This is an erased entry + udi.erase(); + return true; + } + ConfSimple conf(dic); + if (!conf.get("udi", udi)) { + m_reason << "Bad file: no udi in dic"; + return false; + } + return true; + } - 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)) - return false; - if (d.dicsize == 0) { - // This is an erased entry - udi.erase(); - return true; - } - ConfSimple conf(dic); - if (!conf.get("udi", udi)) { - m_reason << "Bad file: no udi in dic"; + bool readDicData(off_t hoffs, EntryHeaderData& hd, string& dic, + 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 + // at the right position + if (lseek(m_fd, offs, 0) != offs) { + m_reason << "CirCache::get: lseek(" << offs << ") failed: " << + errno; + return false; + } + char *bf = 0; + if (hd.dicsize) { + bf = buf(hd.dicsize); + 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; + } + dic.assign(bf, hd.dicsize); + } else { + dic.erase(); + } + if (data == 0) { return true; } - bool readDicData(off_t hoffs, EntryHeaderData& hd, string& dic, - 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 - // at the right position - if (lseek(m_fd, offs, 0) != offs) { - m_reason << "CirCache::get: lseek(" << offs << ") failed: " << - errno; + if (hd.datasize) { + bf = buf(hd.datasize); + if (bf == 0) { return false; } - char *bf = 0; - if (hd.dicsize) { - bf = buf(hd.dicsize); - 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; - } - dic.assign(bf, hd.dicsize); - } else { - dic.erase(); + if (read(m_fd, bf, hd.datasize) != int(hd.datasize)) { + m_reason << "CirCache::get: read() failed: errno " << errno; + return false; } - if (data == 0) - return true; - if (hd.datasize) { - bf = buf(hd.datasize); - if (bf == 0) - return false; - if (read(m_fd, bf, hd.datasize) != int(hd.datasize)){ - m_reason << "CirCache::get: read() failed: errno " << errno; + if (hd.flags & EFDataCompressed) { + LOGDEB1(("Circache:readdicdata: data compressed\n")); + void *uncomp; + unsigned int uncompsize; + if (!inflateToDynBuf(bf, hd.datasize, &uncomp, &uncompsize)) { + m_reason << "CirCache: decompression failed "; return false; } - - if (hd.flags & EFDataCompressed) { - LOGDEB1(("Circache:readdicdata: data compressed\n")); - void *uncomp; - unsigned int uncompsize; - if (!inflateToDynBuf(bf, hd.datasize, &uncomp, &uncompsize)) { - m_reason << "CirCache: decompression failed "; - return false; - } - data->assign((char *)uncomp, uncompsize); - free(uncomp); - } else { - LOGDEB1(("Circache:readdicdata: data NOT compressed\n")); - data->assign(bf, hd.datasize); - } + data->assign((char *)uncomp, uncompsize); + free(uncomp); } else { - data->erase(); + LOGDEB1(("Circache:readdicdata: data NOT compressed\n")); + data->assign(bf, hd.datasize); } - return true; + } else { + data->erase(); } + return true; + } }; @@ -669,18 +671,16 @@ public: off_t headoffs; off_t padsize; CCScanHookRecord() - : headoffs(0), padsize(0) - { - } - virtual status takeone(off_t offs, const string& udi, - const EntryHeaderData& d) - { - headoffs = offs; - padsize = d.padsize; - LOGDEB2(("CCScanHookRecord::takeone: offs %s padsize %s\n", - lltodecstr(headoffs).c_str(), lltodecstr(padsize).c_str())); - return Continue; - } + : headoffs(0), padsize(0) { + } + virtual status takeone(off_t offs, const string& udi, + const EntryHeaderData& d) { + headoffs = offs; + padsize = d.padsize; + LOGDEB2(("CCScanHookRecord::takeone: offs %s padsize %s\n", + lltodecstr(headoffs).c_str(), lltodecstr(padsize).c_str())); + return Continue; + } }; string CirCache::getpath() @@ -690,7 +690,7 @@ string CirCache::getpath() bool CirCache::create(off_t maxsize, int flags) { - LOGDEB(("CirCache::create: [%s] maxsz %s flags 0x%x\n", + LOGDEB(("CirCache::create: [%s] maxsz %s flags 0x%x\n", m_dir.c_str(), lltodecstr((long long)maxsize).c_str(), flags)); if (m_d == 0) { LOGERR(("CirCache::create: null data\n")); @@ -701,20 +701,20 @@ bool CirCache::create(off_t maxsize, int flags) if (stat(m_dir.c_str(), &st) < 0) { // Directory does not exist, create it if (mkdir(m_dir.c_str(), 0777) < 0) { - m_d->m_reason << "CirCache::create: mkdir(" << m_dir << - ") failed" << " errno " << errno; + m_d->m_reason << "CirCache::create: mkdir(" << m_dir << + ") failed" << " errno " << errno; return false; } } else { // If the file exists too, and truncate is not set, switch // to open-mode. Still may need to update header params. if (access(m_d->datafn(m_dir).c_str(), 0) >= 0 && - !(flags & CC_CRTRUNCATE)) { + !(flags & CC_CRTRUNCATE)) { if (!open(CC_OPWRITE)) { return false; } if (maxsize == m_d->m_maxsize && - ((flags & CC_CRUNIQUE) != 0) == m_d->m_uniquentries) { + ((flags & CC_CRUNIQUE) != 0) == m_d->m_uniquentries) { LOGDEB(("Header unchanged, no rewrite\n")); return true; } @@ -735,8 +735,8 @@ bool CirCache::create(off_t maxsize, int flags) LOGDEB2(("CirCache::create: rewriting header with " "maxsize %s oheadoffs %s nheadoffs %s " "npadsize %d unient %d\n", - lltodecstr(m_d->m_maxsize).c_str(), - lltodecstr(m_d->m_oheadoffs).c_str(), + lltodecstr(m_d->m_maxsize).c_str(), + lltodecstr(m_d->m_oheadoffs).c_str(), lltodecstr(m_d->m_nheadoffs).c_str(), m_d->m_npadsize, int(m_d->m_uniquentries))); return m_d->writefirstblock(); @@ -744,10 +744,10 @@ bool CirCache::create(off_t maxsize, int flags) // Else fallthrough to create file } - if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), - O_CREAT|O_RDWR|O_TRUNC|O_BINARY, 0666)) < 0) { - m_d->m_reason << "CirCache::create: open/creat(" << - m_d->datafn(m_dir) << ") failed " << "errno " << errno; + if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), + O_CREAT | O_RDWR | O_TRUNC | O_BINARY, 0666)) < 0) { + m_d->m_reason << "CirCache::create: open/creat(" << + m_d->datafn(m_dir) << ") failed " << "errno " << errno; return false; } @@ -757,9 +757,9 @@ bool CirCache::create(off_t maxsize, int flags) char buf[CIRCACHE_FIRSTBLOCK_SIZE]; memset(buf, 0, CIRCACHE_FIRSTBLOCK_SIZE); - if (::write(m_d->m_fd, buf, CIRCACHE_FIRSTBLOCK_SIZE) != - CIRCACHE_FIRSTBLOCK_SIZE) { - m_d->m_reason << "CirCache::create: write header failed, errno " + if (::write(m_d->m_fd, buf, CIRCACHE_FIRSTBLOCK_SIZE) != + CIRCACHE_FIRSTBLOCK_SIZE) { + m_d->m_reason << "CirCache::create: write header failed, errno " << errno; return false; } @@ -773,14 +773,15 @@ 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(), + if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), mode == CC_OPREAD ? - O_RDONLY|O_BINARY : O_RDWR|O_BINARY)) < 0) { - m_d->m_reason << "CirCache::open: open(" << m_d->datafn(m_dir) << - ") failed " << "errno " << errno; + O_RDONLY | O_BINARY : O_RDWR | O_BINARY)) < 0) { + m_d->m_reason << "CirCache::open: open(" << m_d->datafn(m_dir) << + ") failed " << "errno " << errno; return false; } return m_d->readfirstblock(); @@ -788,15 +789,14 @@ bool CirCache::open(OpMode mode) class CCScanHookDump : public CCScanHook { public: - virtual status takeone(off_t offs, const string& udi, - const EntryHeaderData& d) - { - cout << "Scan: offs " << offs << " dicsize " << d.dicsize - << " datasize " << d.datasize << " padsize " << d.padsize << - " flags " << d.flags << - " udi [" << udi << "]" << endl; - return Continue; - } + virtual status takeone(off_t offs, const string& udi, + const EntryHeaderData& d) { + cout << "Scan: offs " << offs << " dicsize " << d.dicsize + << " datasize " << d.datasize << " padsize " << d.padsize << + " flags " << d.flags << + " udi [" << udi << "]" << endl; + return Continue; + } }; bool CirCache::dump() @@ -808,14 +808,14 @@ bool CirCache::dump() off_t start = m_d->m_oheadoffs; switch (m_d->scan(start, &dumper, true)) { - case CCScanHook::Stop: + case CCScanHook::Stop: cout << "Scan returns Stop??" << endl; return false; - case CCScanHook::Continue: + case CCScanHook::Continue: cout << "Scan returns Continue ?? " << CCScanHook::Continue << " " << - getReason() << endl; + getReason() << endl; return false; - case CCScanHook::Error: + case CCScanHook::Error: cout << "Scan returns Error: " << getReason() << endl; return false; case CCScanHook::Eof: @@ -835,25 +835,25 @@ public: off_t m_offs; EntryHeaderData m_hd; - CCScanHookGetter(const string &udi, int ti) - : m_udi(udi), m_targinstance(ti), m_instance(0), m_offs(0){} + CCScanHookGetter(const string& udi, int ti) + : m_udi(udi), m_targinstance(ti), m_instance(0), m_offs(0) {} - virtual status takeone(off_t offs, const string& udi, - 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, - (UINT)d.datasize, (UINT)d.padsize, d.flags)); - if (!m_udi.compare(udi)) { - m_instance++; - m_offs = offs; - m_hd = d; - if (m_instance == m_targinstance) - return Stop; + virtual status takeone(off_t offs, const string& udi, + 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, + (UINT)d.datasize, (UINT)d.padsize, d.flags)); + if (!m_udi.compare(udi)) { + m_instance++; + m_offs = offs; + m_hd = d; + if (m_instance == m_targinstance) { + return Stop; } - return Continue; } + return Continue; + } }; // instance == -1 means get latest. Otherwise specify from 1+ @@ -878,12 +878,13 @@ bool CirCache::get(const string& udi, string& dic, string& data, int instance) EntryHeaderData d_good; off_t o_good = 0; for (vector::iterator it = ofss.begin(); - it != ofss.end(); it++) { + it != ofss.end(); it++) { 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 @@ -900,7 +901,7 @@ bool CirCache::get(const string& udi, string& dic, string& data, int instance) // Did we read an appropriate entry ? if (o_good != 0 && (instance == -1 || instance == finst)) { bool ret = m_d->readDicData(o_good, d_good, dic, &data); - LOGDEB0(("Circache::get: hfound, %d mS\n", + LOGDEB0(("Circache::get: hfound, %d mS\n", chron.millis())); return ret; } @@ -913,12 +914,13 @@ 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; } - bool bret = + bool bret = m_d->readDicData(getter.m_offs, getter.m_hd, dic, &data); LOGDEB0(("Circache::get: scanfound, %d mS\n", chron.millis())); @@ -960,16 +962,18 @@ 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)) { + } + if (!m_d->writeEntryHeader(*it, nd)) { LOGERR(("CirCache::erase: write header failed\n")); return false; } @@ -980,29 +984,31 @@ bool CirCache::erase(const string& udi) } // Used to scan the file ahead until we accumulated enough space for the new -// entry. +// entry. class CCScanHookSpacer : public CCScanHook { public: off_t sizewanted; off_t sizeseen; vector > 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) - { - 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) - return Stop; - return Continue; + virtual status takeone(off_t offs, const string& udi, + 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) { + return Stop; } + return Continue; + } }; -bool CirCache::put(const string& udi, const ConfSimple *iconf, +bool CirCache::put(const string& udi, const ConfSimple *iconf, const string& data, unsigned int iflags) { if (m_d == 0) { @@ -1018,7 +1024,7 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, string dic; if (!iconf || !iconf->get("udi", dic) || dic.empty() || dic.compare(udi)) { m_d->m_reason << "No/bad 'udi' entry in input dic"; - LOGERR(("Circache::put: no/bad udi: DIC:[%s] UDI [%s]\n", + LOGERR(("Circache::put: no/bad udi: DIC:[%s] UDI [%s]\n", dic.c_str(), udi.c_str())); return false; } @@ -1043,8 +1049,8 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, uLong len = compressBound(static_cast(data.size())); char *bf = compbuf.setsize(len); if (bf != 0 && - compress((Bytef*)bf, &len, (Bytef*)data.c_str(), static_cast(data.size())) - == Z_OK) { + compress((Bytef*)bf, &len, (Bytef*)data.c_str(), static_cast(data.size())) + == Z_OK) { if (float(len) < 0.9 * float(data.size())) { // bf is local but it's our static buffer address datap = bf; @@ -1071,12 +1077,12 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, // Check if we can recover some pad space from the (physically) previous // entry. off_t recovpadsize = m_d->m_oheadoffs == CIRCACHE_FIRSTBLOCK_SIZE ? - 0 : m_d->m_npadsize; + 0 : m_d->m_npadsize; if (recovpadsize != 0) { - // Need to read the latest entry's header, to rewrite it with a + // Need to read the latest entry's header, to rewrite it with a // zero pad size EntryHeaderData pd; - if (m_d->readEntryHeader(m_d->m_nheadoffs, pd) != CCScanHook::Continue){ + if (m_d->readEntryHeader(m_d->m_nheadoffs, pd) != CCScanHook::Continue) { return false; } if (int(pd.padsize) != m_d->m_npadsize) { @@ -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; @@ -1103,7 +1110,7 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, LOGDEB(("CirCache::put: new fits in old padsize %d\n", recovpadsize)); npadsize = recovpadsize - nsize; } else if (st.st_size < m_d->m_maxsize) { - // Still growing the file. + // Still growing the file. npadsize = 0; extending = true; } else { @@ -1114,8 +1121,8 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, scansize, (UINT)m_d->m_oheadoffs)); CCScanHookSpacer spacer(scansize); switch (m_d->scan(m_d->m_oheadoffs, &spacer)) { - case CCScanHook::Stop: - LOGDEB(("CirCache::put: Scan ok, sizeseen %d\n", + case CCScanHook::Stop: + LOGDEB(("CirCache::put: Scan ok, sizeseen %d\n", spacer.sizeseen)); npadsize = spacer.sizeseen - scansize; break; @@ -1123,15 +1130,15 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, npadsize = 0; extending = true; break; - case CCScanHook::Continue: - case CCScanHook::Error: + case CCScanHook::Continue: + case CCScanHook::Error: return false; } // Take the recycled entries off the multimap m_d->khClear(spacer.squashed_udis); } - - LOGDEB(("CirCache::put: writing %d at %d padsize %d\n", + + LOGDEB(("CirCache::put: writing %d at %d padsize %d\n", nsize, nwriteoffs, npadsize)); if (lseek(m_d->m_fd, nwriteoffs, 0) != nwriteoffs) { @@ -1141,7 +1148,7 @@ bool CirCache::put(const string& udi, const ConfSimple *iconf, char head[CIRCACHE_HEADER_SIZE]; memset(head, 0, CIRCACHE_HEADER_SIZE); - snprintf(head, CIRCACHE_HEADER_SIZE, + snprintf(head, CIRCACHE_HEADER_SIZE, headerformat, dic.size(), datalen, npadsize, flags); struct iovec vecs[3]; vecs[0].iov_base = head; @@ -1183,7 +1190,7 @@ bool CirCache::rewind(bool& eof) eof = false; off_t fsize = lseek(m_d->m_fd, 0, SEEK_END); - if (fsize == (off_t)-1) { + if (fsize == (off_t) - 1) { LOGERR(("CirCache::rewind: seek to EOF failed\n")); return false; } @@ -1197,7 +1204,7 @@ bool CirCache::rewind(bool& eof) } CCScanHook::status st = m_d->readEntryHeader(m_d->m_itoffs, m_d->m_ithd); - switch(st) { + switch (st) { case CCScanHook::Eof: eof = true; return false; @@ -1218,8 +1225,8 @@ bool CirCache::next(bool& eof) eof = false; // Skip to next header, using values stored from previous one - m_d->m_itoffs += CIRCACHE_HEADER_SIZE + m_d->m_ithd.dicsize + - m_d->m_ithd.datasize + m_d->m_ithd.padsize; + m_d->m_itoffs += CIRCACHE_HEADER_SIZE + m_d->m_ithd.dicsize + + m_d->m_ithd.datasize + m_d->m_ithd.padsize; // Looped back ? if (m_d->m_itoffs == m_d->m_oheadoffs) { @@ -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; } @@ -1250,9 +1258,10 @@ bool CirCache::getCurrentUdi(string& udi) LOGERR(("CirCache::getCurrentUdi: null data\n")); 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; } @@ -1322,10 +1333,10 @@ static bool inflateToDynBuf(void* inp, UINT inlen, void **outpp, UINT *outlenp) "total_out %d\n", d_stream.avail_in, d_stream.total_in, d_stream.avail_out, d_stream.total_out)); if (d_stream.avail_out == 0) { - if ((outp = (char*)allocmem(outp, inlen, &alloc, + if ((outp = (char*)allocmem(outp, inlen, &alloc, imul, mxinc)) == 0) { - LOGERR(("Inflate: out of memory, current alloc %d\n", - alloc*inlen)); + LOGERR(("Inflate: out of memory, current alloc %d\n", + alloc * inlen)); inflateEnd(&d_stream); return false; } else { @@ -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); @@ -1408,7 +1421,7 @@ bool copyall(STD_SHARED_PTR occ, //cerr << "UDI: " << udi << endl; if (!ncc->put(udi, &dic, data)) { cerr << "put failed: " << ncc->getReason() << " sdic [" << sdic << - "]" << endl; + "]" << endl; return false; } nentries++; @@ -1437,8 +1450,8 @@ bool resizecc(const string& dir, int newmbs) // Check for previous backup string backupfn = ofn + ".orig"; if (access(backupfn.c_str(), 0) >= 0) { - cerr << "Backup file " << backupfn << - " exists, please move it out of the way" << endl; + cerr << "Backup file " << backupfn << + " exists, please move it out of the way" << endl; return false; } @@ -1458,10 +1471,10 @@ bool resizecc(const string& dir, int newmbs) } STD_SHARED_PTR ncc(new CirCache(tmpdir)); string nfn = ncc->getpath(); - if (!ncc->create(off_t(newmbs) * 1000 * 1024, + if (!ncc->create(off_t(newmbs) * 1000 * 1024, CirCache::CC_CRUNIQUE | CirCache::CC_CRTRUNCATE)) { - cerr << "Cant create new file in " << tmpdir << " : " << - ncc->getReason() << endl; + cerr << "Cant create new file in " << tmpdir << " : " << + ncc->getReason() << endl; return false; } @@ -1487,7 +1500,7 @@ bool resizecc(const string& dir, int newmbs) // Move the new file in place. if (rename(nfn.c_str(), ofn.c_str()) < 0) { cerr << "Could not rename new file from " << nfn << " to " << - ofn << " : "; + ofn << " : "; perror("rename"); return false; } @@ -1527,17 +1540,17 @@ bool appendcc(const string ddir, const string& sdir) static char *thisprog; static char usage [] = -" -c [-u] : create\n" -" -p [apath ...] : put files\n" -" -d : dump\n" -" -g [-i instance] [-D] : get\n" -" -D: also dump data\n" -" -e : erase\n" -" -s : resize\n" -" -a [ ...]: append old content to target\n" -" The target should be first resized to hold all the data, else only\n" -" as many entries as capacity permit will be retained\n" -; + " -c [-u] : create\n" + " -p [apath ...] : put files\n" + " -d : dump\n" + " -g [-i instance] [-D] : get\n" + " -D: also dump data\n" + " -e : erase\n" + " -s : resize\n" + " -a [ ...]: append old content to target\n" + " The target should be first resized to hold all the data, else only\n" + " as many entries as capacity permit will be retained\n" + ; static void Usage(FILE *fp = stderr) @@ -1548,7 +1561,7 @@ Usage(FILE *fp = stderr) static int op_flags; #define OPT_MOINS 0x1 -#define OPT_c 0x2 +#define OPT_c 0x2 #define OPT_p 0x8 #define OPT_g 0x10 #define OPT_d 0x20 @@ -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) - Usage(); - argc--; + 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,11 +1650,13 @@ 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)) { + } + 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)) { @@ -1648,15 +1698,17 @@ int main(int argc, char **argv) } string udi; make_udi(fn, "", udi); - sprintf(dic, "#whatever...\nmimetype = text/plain\nudi=%s\n", + sprintf(dic, "#whatever...\nmimetype = text/plain\nudi=%s\n", udi.c_str()); string sdic; sdic.assign(dic, strlen(dic)); ConfSimple conf(sdic); - + 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,15 +1719,17 @@ 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)) { @@ -1683,7 +1737,8 @@ int main(int argc, char **argv) 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); } diff --git a/src/utils/circache.h b/src/utils/circache.h index d8303680..2022af1b 100644 --- a/src/utils/circache.h +++ b/src/utils/circache.h @@ -24,14 +24,14 @@ * specified maximum size, then is rewritten from the start, * overwriting older entries. * - * Data objects inside the cache each have two parts: a data segment and an + * Data objects inside the cache each have two parts: a data segment and an * attribute (metadata) dictionary. * They are named using the same identifiers that are used inside the Recoll * index (the UDI). * * Inside the file. the UDIs are stored inside the entry dictionary * under the key "udi". - * + * * It is assumed that the dictionary are small (they are routinely read/parsed) * */ @@ -40,26 +40,23 @@ #include -#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}; + 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 + }; 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_ */