Archived
1
0
Fork 0

dlhist.c: use lockfile

This commit is contained in:
Henrik Hautakoski 2011-06-13 19:40:12 +02:00
parent 5a5a3d33e4
commit 06dca6c150
2 changed files with 18 additions and 15 deletions

View file

@ -10,7 +10,7 @@ all : $(PROGRAMS)
install : $(PROGRAMS) install : $(PROGRAMS)
cp $^ $(HOME)/bin/ cp $^ $(HOME)/bin/
dlight : dlight.o env.o http.o rss.o filter.o cconf.o dlhist.o dlight : dlight.o env.o http.o rss.o lockfile.o filter.o cconf.o dlhist.o
dlight-compile : compile.o env.o lockfile.o filter.o cconf.o dlight-compile : compile.o env.o lockfile.o filter.o cconf.o
dlight-read-config : read-config.o env.o cconf.o dlight-read-config : read-config.o env.o cconf.o

View file

@ -9,6 +9,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <time.h> #include <time.h>
#include "env.h" #include "env.h"
#include "lockfile.h"
#include "dlhist.h" #include "dlhist.h"
/* /*
@ -37,7 +38,7 @@ struct hash_entry {
#define he_empty(x) (!(x) || (x)->key == NULL) #define he_empty(x) (!(x) || (x)->key == NULL)
static int fd = -1; static struct lockfile lock = LOCKFILE_INIT;
static struct hash_entry *table; static struct hash_entry *table;
static unsigned int table_size; static unsigned int table_size;
@ -161,7 +162,7 @@ static void build_table(const char *buf, size_t len) {
int dlhist_open() { int dlhist_open() {
char filename[4096], *buf = NULL; char filename[4096], *buf = NULL;
unsigned offset = 0; int ret = -1, fd = -1, offset = 0;
struct stat st; struct stat st;
struct header *hdr; struct header *hdr;
@ -169,7 +170,11 @@ int dlhist_open() {
snprintf(filename, sizeof(filename), snprintf(filename, sizeof(filename),
"%s/%s", env_get_dir(), STORAGE_FILE); "%s/%s", env_get_dir(), STORAGE_FILE);
fd = open(filename, O_CREAT | O_RDWR, 0600); /* try lockin the file */
if (hold_lock(&lock, filename, 0) < 0)
goto error;
fd = open(filename, O_CREAT | O_RDONLY, 0600);
if (fd < 0 || fstat(fd, &st) < 0) { if (fd < 0 || fstat(fd, &st) < 0) {
perror("dlhist_open"); perror("dlhist_open");
goto error; goto error;
@ -202,16 +207,15 @@ int dlhist_open() {
build_table(buf + offset, st.st_size - offset); build_table(buf + offset, st.st_size - offset);
if (buf) ret = 0;
free(buf);
return 0;
error: error:
if (ret)
release_lock(&lock);
if (buf) if (buf)
free(buf); free(buf);
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
fd = -1; return ret;
return -1;
} }
int dlhist_lookup(const char *url) { int dlhist_lookup(const char *url) {
@ -262,8 +266,9 @@ void dlhist_flush() {
int i; int i;
struct header hdr; struct header hdr;
int fd = lock.fd;
if (fd < 0) if (table_size < 1)
return; return;
ftruncate(fd, 0); ftruncate(fd, 0);
@ -292,8 +297,8 @@ void dlhist_flush() {
write(fd, entry->key, strlen(entry->key) + 1); write(fd, entry->key, strlen(entry->key) + 1);
} }
/* Make sure we flush to disk */ /* Flush it to the real file */
fsync(fd); commit_lock(&lock);
} }
void dlhist_close() { void dlhist_close() {
@ -302,9 +307,7 @@ void dlhist_close() {
dlhist_flush(); dlhist_flush();
if (fd >= 0) release_lock(&lock);
close(fd);
fd = -1;
if (table) { if (table) {
for(i=0; i < table_size; i++) { for(i=0; i < table_size; i++) {