From 03443b4e9e5142019b46242ab6a3c77ec18e956c Mon Sep 17 00:00:00 2001 From: Carsten Haitzler Date: Mon, 5 Jun 2000 18:19:37 +0000 Subject: [PATCH] shoudl in theory handle locks better.... SVN revision: 2754 --- loaders/loader_db.c | 74 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/loaders/loader_db.c b/loaders/loader_db.c index bf133fa..1358ad1 100644 --- a/loaders/loader_db.c +++ b/loaders/loader_db.c @@ -25,7 +25,44 @@ void formats (ImlibLoader *l); #include #include #include +#include +#include +static int +permissions(char *file) +{ + struct stat st; + + if (!stat(file, &st) < 0) + return 0; + return st.st_mode; +} + +static int +exists(char *file) +{ + struct stat st; + + if (!stat(file, &st) < 0) + return 0; + return 1; +} + +static int +can_read(char *file) +{ + if (!(permissions(file) & (S_IRUSR | S_IRGRP | S_IROTH))) + return 0; + return (1 + access(file, R_OK)); +} + +static int +can_write(char *file) +{ + if (!(permissions(file) & (S_IWUSR | S_IWGRP | S_IWOTH))) + return 0; + return (1 + access(file, W_OK)); +} char load (ImlibImage *im, ImlibProgressFunction progress, @@ -48,6 +85,8 @@ load (ImlibImage *im, ImlibProgressFunction progress, int flen; *ptr = 0; + if (!can_read(file)) + return 0; flen = strlen(file); strcpy(key, &(ptr[1])); if ((flen > 3) && @@ -60,7 +99,19 @@ load (ImlibImage *im, ImlibProgressFunction progress, return 0; db = dbm_open(file, O_RDONLY, 0664); if (!db) - return 0; + { + int i; + + for (i = 0; i < 32; i++) + { + usleep((rand() % 0xff) * 1000); + db = dbm_open(file, O_RDONLY, 0664); + if (db) + break; + } + if (!db) + return 0; + } dkey.dptr = key; dkey.dsize = strlen(key); @@ -195,6 +246,13 @@ save (ImlibImage *im, ImlibProgressFunction progress, int flen; *cp = 0; + if (exists(file)) + { + if (!can_write(file)) + return 0; + if (!can_read(file)) + return 0; + } flen = strlen(file); strcpy(key, &(cp[1])); if ((flen > 3) && @@ -207,7 +265,19 @@ save (ImlibImage *im, ImlibProgressFunction progress, return 0; db = dbm_open(file, O_RDWR | O_CREAT, 0664); if (!db) - return 0; + { + int i; + + for (i = 0; i < 32; i++) + { + usleep((rand() % 0xff) * 1000); + db = dbm_open(file, O_RDWR | O_CREAT, 0664); + if (db) + break; + } + if (!db) + return 0; + } dkey.dptr = key; dkey.dsize = strlen(key);