#pragma once
#include "common.hpp"
#include "config.hpp"
#include "hash.hpp"
#include "pool.hpp"
#include <sys/uio.h>


#define DISKCACHE_MAX_ID_LEN 127
#define DISKCACHE_VERSION '1'


/** storing files on disk using blocking i/o, without (fd-)caching etc. but it's better than cert generation anyhow. **/
class DiskCache: public ConfigInst<char*> {
    private:
        typedef struct {
            char version;
            char id[DISKCACHE_MAX_ID_LEN];
            time_t time;
        } diskcache_header_t;

        int dirfd;
        char buf[2][BUF_SIZE];
        void id2fn(const char* id, char*&, char*&); ///< temp-filename and filename from slab pool for given id

    public:
        DiskCache(const char*);
        ~DiskCache();
        bool is_valid() const { return dirfd != -1; }

        SlabPool pool;
        ssize_t get(const char*, char*&); ///< yields file content from #pool, if any
        bool set(const char*, const char*, size_t);
        bool set(const char*, const iovec*, int, size_t);
};