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); free(buf);
} }
fclose(ctx->f);
ctx->f = NULL; ctx->f = NULL;
for (idx = 0; idx < 256; idx += 1) { for (idx = 0; idx < 256; idx += 1) {

View File

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

View File

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