diff options
author | Yunchih Chen <yunchih.cat@gmail.com> | 2017-12-01 10:12:45 +0800 |
---|---|---|
committer | Yunchih Chen <yunchih.cat@gmail.com> | 2017-12-01 10:12:45 +0800 |
commit | ec0e71f4c1eefce0ae650c9340522c377a6abff3 (patch) | |
tree | fda235af8b7fada9456b8dec29f006fc570c4400 /commit.c | |
download | nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.tar nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.tar.gz nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.tar.bz2 nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.tar.lz nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.tar.xz nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.tar.zst nfcollect-ec0e71f4c1eefce0ae650c9340522c377a6abff3.zip |
First commit
Diffstat (limited to 'commit.c')
-rw-r--r-- | commit.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/commit.c b/commit.c new file mode 100644 index 0000000..90cecfd --- /dev/null +++ b/commit.c @@ -0,0 +1,43 @@ +#include <errno.h> +#include <string.h> +#include "commit.h" + +extern char *storage_dir; +extern char *storage_prefix; +const uint32_t write_blk_size = 8196; +const uint32_t commit_file_open_flag = O_RDWR | O_CREAT | O_APPEND | O_DIRECT; + +void nfl_commit_init() { + +} + +void nfl_commit_worker(nflog_header_t* header, nflog_entry_t* store) { + FILE* f; + char filename[1024]; + uint32_t id = header->id; + + sprintf(filename, "%s/%s_%d", storage_dir, storage_prefix, id); + debug("Comm worker #%u: commit to file %s\n", header->id, filename); + fd = open + ERR((f = fopen(filename, "wb")) == NULL, strerror(errno)); + fwrite(header, sizeof(nflog_header_t), 1, f); + + uint32_t total_size = sizeof(nflog_entry_t) * header->max_n_entries; + uint32_t total_blk = total_size / write_blk_size; + uint32_t i, written = 0; + for(i = 0; i < total_blk; ++i) { + written = fwrite(store, 1, write_blk_size, f); + + while(written < write_blk_size) { + written += fwrite(store, 1, write_blk_size - written, f); + } + } + + int remain = total_size - total_blk*write_blk_size; + while(remain > 0) { + remain -= fwrite(store, 1, remain, f); + } + + fclose(f); +} + |