append action

This commit is contained in:
Neale Pickett 2012-11-20 20:02:04 -07:00
parent 6fbd1feb67
commit b993629d88
3 changed files with 10 additions and 7 deletions

View File

@ -108,7 +108,6 @@ cdbmake_finalize(struct cdbmake_ctx *ctx)
free(buf);
}
fclose(ctx->f);
ctx->f = NULL;
for (idx = 0; idx < 256; idx += 1) {

View File

@ -6,6 +6,7 @@
#include <string.h>
struct cdbmake_record {
FILE *f;
uint32_t hashval;
uint32_t offset;
};

View File

@ -122,19 +122,18 @@ add(char *filename, char *key, char *val)
struct cdbmake_ctx outc;
FILE *inf;
FILE *outf;
char tmpfn[8192];
// Open input file
inf = fopen(filename, "rb");
if (! inf) {
perror("Opening database");
return EX_NOINPUT;
}
{
char fn[4096];
snprintf(fn, sizeof(fn), "%s.%d", filename, getpid());
outf = fopen(fn, "wb");
}
// Create temporary output file
snprintf(tmpfn, sizeof(tmpfn), "%s.%d", filename, getpid());
outf = fopen(tmpfn, "wb");
if (! outf) {
perror("Creating temporary database");
return EX_CANTCREAT;
@ -157,6 +156,10 @@ add(char *filename, char *key, char *val)
cdbmake_add(&outc, key, strlen(key), val, strlen(val));
cdbmake_finalize(&outc);
fclose(outf);
fclose(inf);
rename(tmpfn, filename);
return 0;
}