svn commit: samba r18310 - in branches/SAMBA_3_0/source: lib torture utils

vlendec at samba.org vlendec at samba.org
Sat Sep 9 21:05:51 GMT 2006


Author: vlendec
Date: 2006-09-09 21:05:51 +0000 (Sat, 09 Sep 2006)
New Revision: 18310

WebSVN: http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=18310

Log:
Add a little test for some gencache routines
Remove unused gencache_set_only
Use CONST_DISCARD instead of SMB_STRDUP

Volker

Modified:
   branches/SAMBA_3_0/source/lib/gencache.c
   branches/SAMBA_3_0/source/torture/torture.c
   branches/SAMBA_3_0/source/utils/net_cache.c


Changeset:
Modified: branches/SAMBA_3_0/source/lib/gencache.c
===================================================================
--- branches/SAMBA_3_0/source/lib/gencache.c	2006-09-09 12:57:45 UTC (rev 18309)
+++ branches/SAMBA_3_0/source/lib/gencache.c	2006-09-09 21:05:51 UTC (rev 18310)
@@ -56,13 +56,13 @@
 	if (cache) return True;
 
 	asprintf(&cache_fname, "%s/%s", lp_lockdir(), "gencache.tdb");
-	if (cache_fname)
-		DEBUG(5, ("Opening cache file at %s\n", cache_fname));
-	else {
+	if (cache_fname == NULL) {
 		DEBUG(0, ("Filename allocation failed.\n"));
 		return False;
 	}
 
+	DEBUG(5, ("Opening cache file at %s\n", cache_fname));
+
 	cache = tdb_open_log(cache_fname, 0, TDB_DEFAULT,
 	                     O_RDWR|O_CREAT, 0644);
 
@@ -121,9 +121,9 @@
 	if (!valstr)
 		return False;
 
-	keybuf.dptr = SMB_STRDUP(keystr);
+	keybuf.dptr = CONST_DISCARD(char *, keystr);
 	keybuf.dsize = strlen(keystr)+1;
-	databuf.dptr = SMB_STRDUP(valstr);
+	databuf.dptr = valstr;
 	databuf.dsize = strlen(valstr)+1;
 	DEBUG(10, ("Adding cache entry with key = %s; value = %s and timeout ="
 	           " %s (%d seconds %s)\n", keybuf.dptr, value,ctime(&timeout),
@@ -132,69 +132,11 @@
 
 	ret = tdb_store(cache, keybuf, databuf, 0);
 	SAFE_FREE(valstr);
-	SAFE_FREE(keybuf.dptr);
-	SAFE_FREE(databuf.dptr);
 	
 	return ret == 0;
 }
 
-
 /**
- * Set existing entry to the cache file.
- *
- * @param keystr string that represents a key of this entry
- * @param valstr text representation value being cached
- * @param timeout time when the value is expired
- *
- * @retval true when entry is successfuly set
- * @retval false on failure
- **/
-
-BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
-{
-	int ret = -1;
-	TDB_DATA keybuf, databuf;
-	char *old_valstr, *datastr;
-	time_t old_timeout;
-	
-	/* fail completely if get null pointers passed */
-	SMB_ASSERT(keystr && valstr);
-
-	if (!gencache_init()) return False;
-			
-	/* 
-	 * Check whether entry exists in the cache
-	 * Don't verify gencache_get exit code, since the entry may be expired
-	 */	
-	gencache_get(keystr, &old_valstr, &old_timeout);
-	
-	if (!(old_valstr && old_timeout)) return False;
-		
-	DEBUG(10, ("Setting cache entry with key = %s; old value = %s and old timeout \
-	           = %s\n", keystr, old_valstr, ctime(&old_timeout)));
-
-	asprintf(&datastr, CACHE_DATA_FMT, (int)timeout, valstr);
-	keybuf.dptr = SMB_STRDUP(keystr);
-	keybuf.dsize = strlen(keystr)+1;
-	databuf.dptr = SMB_STRDUP(datastr);
-	databuf.dsize = strlen(datastr)+1;
-	DEBUGADD(10, ("New value = %s, new timeout = %s (%d seconds %s)", valstr,
-	              ctime(&timeout), (int)(timeout - time(NULL)),
-	              timeout > time(NULL) ? "ahead" : "in the past"));
-
-		
-	ret = tdb_store(cache, keybuf, databuf, TDB_REPLACE);
-
-	SAFE_FREE(datastr);
-	SAFE_FREE(old_valstr);
-	SAFE_FREE(keybuf.dptr);
-	SAFE_FREE(databuf.dptr);
-	
-	return ret == 0;
-}
- 
-
-/**
  * Delete one entry from the cache file.
  *
  * @param keystr string that represents a key of this entry
@@ -213,12 +155,11 @@
 
 	if (!gencache_init()) return False;	
 	
-	keybuf.dptr = SMB_STRDUP(keystr);
+	keybuf.dptr = CONST_DISCARD(char *, keystr);
 	keybuf.dsize = strlen(keystr)+1;
 	DEBUG(10, ("Deleting cache entry (key = %s)\n", keystr));
 	ret = tdb_delete(cache, keybuf);
 	
-	SAFE_FREE(keybuf.dptr);
 	return ret == 0;
 }
 
@@ -247,10 +188,9 @@
 		return False;
 	}
 	
-	keybuf.dptr = SMB_STRDUP(keystr);
+	keybuf.dptr = CONST_DISCARD(char *, keystr);
 	keybuf.dsize = strlen(keystr)+1;
 	databuf = tdb_fetch(cache, keybuf);
-	SAFE_FREE(keybuf.dptr);
 	
 	if (databuf.dptr && databuf.dsize > TIMEOUT_LEN) {
 		char* entry_buf = SMB_STRNDUP(databuf.dptr, databuf.dsize);

Modified: branches/SAMBA_3_0/source/torture/torture.c
===================================================================
--- branches/SAMBA_3_0/source/torture/torture.c	2006-09-09 12:57:45 UTC (rev 18309)
+++ branches/SAMBA_3_0/source/torture/torture.c	2006-09-09 21:05:51 UTC (rev 18310)
@@ -4805,6 +4805,65 @@
 	return (diff == 0);
 }
 
+static BOOL run_local_gencache(int dummy)
+{
+	char *val;
+	time_t tm;
+
+	if (!gencache_init()) {
+		d_printf("%s: gencache_init() failed\n", __location__);
+		return False;
+	}
+
+	if (!gencache_set("foo", "bar", time(NULL) + 1000)) {
+		d_printf("%s: gencache_set() failed\n", __location__);
+		return False;
+	}
+
+	if (!gencache_get("foo", &val, &tm)) {
+		d_printf("%s: gencache_get() failed\n", __location__);
+		return False;
+	}
+
+	if (strcmp(val, "bar") != 0) {
+		d_printf("%s: gencache_get() returned %s, expected %s\n",
+			 __location__, val, "bar");
+		SAFE_FREE(val);
+		return False;
+	}
+
+	SAFE_FREE(val);
+
+	if (!gencache_del("foo")) {
+		d_printf("%s: gencache_del() failed\n", __location__);
+		return False;
+	}
+	if (gencache_del("foo")) {
+		d_printf("%s: second gencache_del() succeeded\n",
+			 __location__);
+		return False;
+	}
+			
+	if (gencache_get("foo", &val, &tm)) {
+		d_printf("%s: gencache_get() on deleted entry "
+			 "succeeded\n", __location__);
+		return False;
+	}
+
+	if (!gencache_shutdown()) {
+		d_printf("%s: gencache_shutdown() failed\n", __location__);
+		return False;
+	}
+
+	if (gencache_shutdown()) {
+		d_printf("%s: second gencache_shutdown() succeeded\n",
+			 __location__);
+		return False;
+	}
+
+	return True;
+}
+
 static double create_procs(BOOL (*fn)(int), BOOL *result)
 {
 	int i, status;
@@ -4956,6 +5015,7 @@
 	{"FDSESS", run_fdsesstest, 0},
 	{ "EATEST", run_eatest, 0},
 	{ "LOCAL-SUBSTITUTE", run_local_substitute, 0},
+	{ "LOCAL-GENCACHE", run_local_gencache, 0},
 	{NULL, NULL, 0}};
 
 

Modified: branches/SAMBA_3_0/source/utils/net_cache.c
===================================================================
--- branches/SAMBA_3_0/source/utils/net_cache.c	2006-09-09 12:57:45 UTC (rev 18309)
+++ branches/SAMBA_3_0/source/utils/net_cache.c	2006-09-09 21:05:51 UTC (rev 18310)
@@ -181,48 +181,7 @@
 	return -1;
 }
 
-
 /**
- * Set new value of an existing entry in the cache. Fail If the entry doesn't
- * exist.
- * 
- * @param argv key being searched and new value and timeout to set in the entry
- * @return 0 on success, otherwise failure
- **/
-static int net_cache_set(int argc, const char **argv)
-{
-	const char *keystr, *datastr, *timeout_str;
-	time_t timeout;
-	
-	if (argc < 3) {
-		d_printf("\nUsage: net cache set <key string> <data string> <timeout>\n");
-		return -1;
-	}
-	
-	keystr = argv[0];
-	datastr = argv[1];
-	timeout_str = argv[2];
-	
-	/* parse timeout given in command line */
-	timeout = parse_timeout(timeout_str);
-	if (!timeout) {
-		d_fprintf(stderr, "Invalid timeout argument.\n");
-		return -1;
-	}
-	
-	if (gencache_set_only(keystr, datastr, timeout)) {
-		d_printf("Cache entry set successfully.\n");
-		gencache_shutdown();
-		return 0;
-	}
-
-	d_fprintf(stderr, "Entry couldn't be set. Perhaps there's no such a key.\n");
-	gencache_shutdown();
-	return -1;
-}
-
-
-/**
  * Delete an entry in the cache
  * 
  * @param argv key to delete an entry of
@@ -334,7 +293,6 @@
 static int net_cache_usage(int argc, const char **argv)
 {
 	d_printf("  net cache add \t add add new cache entry\n");
-	d_printf("  net cache set \t set new value for existing cache entry\n");
 	d_printf("  net cache del \t delete existing cache entry by key\n");
 	d_printf("  net cache flush \t delete all entries existing in the cache\n");
 	d_printf("  net cache get \t get cache entry by key\n");
@@ -354,7 +312,6 @@
 {
 	struct functable func[] = {
 		{"add", net_cache_add},
-		{"set", net_cache_set},
 		{"del", net_cache_del},
 		{"get", net_cache_get},
 		{"search", net_cache_search},



More information about the samba-cvs mailing list