#include "cache.h" #include "config.h" #include #include #include #include #include EncodedCache::EncodedCache(fs::path path, Decoder::p decoder, Encoder::p encoder) { this->decoder = decoder; this->encoder = encoder; // TODO: Differentiate between encoders? SHA_CTX context; unsigned char md[SHA_DIGEST_LENGTH]; SHA_Init(&context); SHA_Update(&context, (unsigned char*)path.string().c_str(), path.string().length()); SHA_Final(md, &context); for(int i = 0; i < SHA_DIGEST_LENGTH; i++) { hash = hash + (boost::format("%02x") % (int)md[i]).str(); } } fs::path EncodedCache::get_path() { return fs::path(config::vm["audist.cache_dir"].as()) / hash; } void EncodedCache::create_cache() { fs::path path = get_path(); // TODO: Locking? fs::ofstream os(path, std::ios::out | std::ios::binary); // TODO: Make an encoder-to-istream adapter to get rid of this: if(!os.is_open()) throw std::runtime_error("failed to create cache object"); char data[0x10000]; std::streamsize size = 1; while(size) { size = encoder->read(data, 0x10000); if(size > 0) os.write(data, size); } os.close(); }