r19054: Callers of gencache may not have the rights to
authorJeremy Allison <jra@samba.org>
Mon, 2 Oct 2006 23:34:03 +0000 (23:34 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:15:06 +0000 (12:15 -0500)
open read/write. Allow them to fallback to read-only.
Jeremy
(This used to be commit ec526e1b882e3ade23f90c5e3d637c72b6839da5)

source3/lib/gencache.c

index dc5f32d66eaa331219d4a1ad96499e472ede6585..95ec47b697e756cba6e0357aad1b190267490d36 100644 (file)
@@ -31,6 +31,7 @@
 #define READ_CACHE_DATA_FMT_TEMPLATE "%%12u/%%%us"
 
 static TDB_CONTEXT *cache;
+static BOOL cache_readonly;
 
 /**
  * @file gencache.c
@@ -66,6 +67,14 @@ BOOL gencache_init(void)
        cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
                             O_RDWR|O_CREAT, 0644);
 
+       if (!cache && (errno == EACCES)) {
+               cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT, O_RDONLY, 0644);
+               if (cache) {
+                       cache_readonly = True;
+                       DEBUG(5, ("gencache_init: Opening cache file %s read-only.\n", cache_fname));
+               }
+       }
+
        SAFE_FREE(cache_fname);
        if (!cache) {
                DEBUG(5, ("Attempt to open gencache.tdb has failed.\n"));
@@ -90,6 +99,7 @@ BOOL gencache_shutdown(void)
        DEBUG(5, ("Closing cache file\n"));
        ret = tdb_close(cache);
        cache = NULL;
+       cache_readonly = False;
        return ret != -1;
 }
 
@@ -117,6 +127,10 @@ BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
 
        if (!gencache_init()) return False;
        
+       if (cache_readonly) {
+               return False;
+       }
+
        asprintf(&valstr, CACHE_DATA_FMT, (int)timeout, value);
        if (!valstr)
                return False;
@@ -155,6 +169,10 @@ BOOL gencache_del(const char *keystr)
 
        if (!gencache_init()) return False;     
        
+       if (cache_readonly) {
+               return False;
+       }
+
        keybuf.dptr = CONST_DISCARD(char *, keystr);
        keybuf.dsize = strlen(keystr)+1;
        DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));