From: Daniel Carl Date: Wed, 21 Oct 2015 17:46:50 +0000 (+0200) Subject: Use flock instead of fcntl for file locking (#235). X-Git-Url: https://git.owens.tech/wrapped.html/wrapped.html/git?a=commitdiff_plain;h=d546d7d1e39db8bac27cac250c3a40f854c1a9ef;p=vimb.git Use flock instead of fcntl for file locking (#235). --- diff --git a/src/cookiejar.c b/src/cookiejar.c index 23ea525..a16392f 100644 --- a/src/cookiejar.c +++ b/src/cookiejar.c @@ -20,6 +20,8 @@ #include "config.h" #ifdef FEATURE_COOKIE +#include +#include #include "main.h" #include "cookiejar.h" @@ -47,7 +49,7 @@ SoupCookieJar *cookiejar_new(const char *file, gboolean ro) static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupCookie *new_cookie) { - FLOCK(COOKIEJAR(self)->lock, F_WRLCK); + flock(COOKIEJAR(self)->lock, LOCK_EX); SoupDate *expire; if (new_cookie) { /* session-expire-time handling */ @@ -70,7 +72,7 @@ static void cookiejar_changed(SoupCookieJar *self, SoupCookie *old_cookie, SoupC } } SOUP_COOKIE_JAR_CLASS(cookiejar_parent_class)->changed(self, old_cookie, new_cookie); - FLOCK(COOKIEJAR(self)->lock, F_UNLCK); + flock(COOKIEJAR(self)->lock, LOCK_UN); } static void cookiejar_class_init(CookieJarClass *klass) @@ -96,8 +98,8 @@ static void cookiejar_init(CookieJar *self) static void cookiejar_set_property(GObject *self, guint prop_id, const GValue *value, GParamSpec *pspec) { - FLOCK(COOKIEJAR(self)->lock, F_RDLCK); + flock(COOKIEJAR(self)->lock, LOCK_SH); G_OBJECT_CLASS(cookiejar_parent_class)->set_property(self, prop_id, value, pspec); - FLOCK(COOKIEJAR(self)->lock, F_UNLCK); + flock(COOKIEJAR(self)->lock, LOCK_UN); } #endif diff --git a/src/history.c b/src/history.c index 0c4eef0..ceeb5e0 100644 --- a/src/history.c +++ b/src/history.c @@ -18,6 +18,8 @@ */ #include "config.h" +#include +#include #include "main.h" #include "history.h" #include "util.h" @@ -216,7 +218,7 @@ static void write_to_file(GList *list, const char *file) { FILE *f; if ((f = fopen(file, "w"))) { - FLOCK(fileno(f), F_WRLCK); + flock(fileno(f), LOCK_EX); /* overwrite the history file with new unique history items */ for (GList *link = list; link; link = link->next) { @@ -228,7 +230,7 @@ static void write_to_file(GList *list, const char *file) } } - FLOCK(fileno(f), F_UNLCK); + flock(fileno(f), LOCK_UN); fclose(f); } } diff --git a/src/hsts.c b/src/hsts.c index 75fad95..00b25a1 100644 --- a/src/hsts.c +++ b/src/hsts.c @@ -22,6 +22,8 @@ #include "hsts.h" #include "util.h" #include "main.h" +#include +#include #include #include #include @@ -440,7 +442,7 @@ static void save_entries(HSTSProvider *provider, const char *file) HSTSProviderPrivate *priv = HSTS_PROVIDER_GET_PRIVATE(provider); if ((f = fopen(file, "w"))) { - FLOCK(fileno(f), F_WRLCK); + flock(fileno(f), LOCK_EX); g_hash_table_iter_init(&iter, priv->whitelist); while (g_hash_table_iter_next (&iter, (gpointer)&host, (gpointer)&entry)) { @@ -449,7 +451,7 @@ static void save_entries(HSTSProvider *provider, const char *file) g_free(date); } - FLOCK(fileno(f), F_UNLCK); + flock(fileno(f), LOCK_UN); fclose(f); } } diff --git a/src/main.h b/src/main.h index 7e4c035..6179928 100644 --- a/src/main.h +++ b/src/main.h @@ -41,10 +41,6 @@ #define LENGTH(x) (sizeof x / sizeof x[0]) -#define FLOCK(fd, cmd) { \ - struct flock lock = {.l_type=cmd,.l_start=0,.l_whence=SEEK_SET,.l_len=0}; \ - fcntl(fd, F_SETLKW, &lock); \ -} #ifdef DEBUG #define PRINT_DEBUG(...) { \ diff --git a/src/util.c b/src/util.c index dcf2694..21a5709 100644 --- a/src/util.c +++ b/src/util.c @@ -18,6 +18,8 @@ */ #include "config.h" +#include +#include #include #include #include @@ -222,13 +224,13 @@ gboolean util_file_append(const char *file, const char *format, ...) FILE *f; if ((f = fopen(file, "a+"))) { - FLOCK(fileno(f), F_WRLCK); + flock(fileno(f), LOCK_EX); va_start(args, format); vfprintf(f, format, args); va_end(args); - FLOCK(fileno(f), F_UNLCK); + flock(fileno(f), LOCK_UN); fclose(f); return true; @@ -251,7 +253,7 @@ gboolean util_file_prepend(const char *file, const char *format, ...) content = util_get_file_contents(file, NULL); if ((f = fopen(file, "w"))) { - FLOCK(fileno(f), F_WRLCK); + flock(fileno(f), LOCK_EX); va_start(args, format); /* write new content to the file */ @@ -261,7 +263,7 @@ gboolean util_file_prepend(const char *file, const char *format, ...) /* append previous file content */ fputs(content, f); - FLOCK(fileno(f), F_UNLCK); + flock(fileno(f), LOCK_UN); fclose(f); res = true;