Merge branch 'tom_dev'
[exim.git] / src / src / exim_dbutil.c
index dd401741d8fef4086294cf0624bd4bdf0fbae8eb..1e94f33e60610eb79ff016bc3d031aac23b8372b 100644 (file)
@@ -1,10 +1,10 @@
-/* $Cambridge: exim/src/src/exim_dbutil.c,v 1.1 2004/10/07 10:39:01 ph10 Exp $ */
+/* $Cambridge: exim/src/src/exim_dbutil.c,v 1.12 2009/11/16 19:50:36 nm4 Exp $ */
 
 /*************************************************
 *     Exim - an Internet mail transport agent    *
 *************************************************/
 
-/* Copyright (c) University of Cambridge 1995 - 2004 */
+/* Copyright (c) University of Cambridge 1995 - 2009 */
 /* See the file NOTICE for conditions of use and distribution. */
 
 
@@ -63,10 +63,11 @@ not too much extra baggage. */
 
 /* Identifiers for the different database types. */
 
-#define type_retry   1
-#define type_wait    2
-#define type_misc    3
-#define type_callout 4
+#define type_retry     1
+#define type_wait      2
+#define type_misc      3
+#define type_callout   4
+#define type_ratelimit 5
 
 
 
@@ -77,12 +78,18 @@ not too much extra baggage. */
 
 /* For Berkeley DB >= 2, we can define a function to be called in case of DB
 errors. This should help with debugging strange DB problems, e.g. getting "File
-exists" when you try to open a db file. */
+exists" when you try to open a db file. The API changed at release 4.3. */
 
 #if defined(USE_DB) && defined(DB_VERSION_STRING)
 void
+#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
+dbfn_bdb_error_callback(const DB_ENV *dbenv, const char *pfx, const char *msg)
+{
+dbenv = dbenv;
+#else
 dbfn_bdb_error_callback(const char *pfx, char *msg)
 {
+#endif
 pfx = pfx;
 printf("Berkeley DB error: %s\n", msg);
 }
@@ -113,7 +120,7 @@ static void
 usage(uschar *name, uschar *options)
 {
 printf("Usage: exim_%s%s  <spool-directory> <database-name>\n", name, options);
-printf("       <database-name> = retry | misc | wait-<transport-name> | callout\n");
+printf("  <database-name> = retry | misc | wait-<transport-name> | callout | ratelimit\n");
 exit(1);
 }
 
@@ -135,6 +142,7 @@ if (argc == 3)
   if (Ustrcmp(argv[2], "misc") == 0) return type_misc;
   if (Ustrncmp(argv[2], "wait-", 5) == 0) return type_wait;
   if (Ustrcmp(argv[2], "callout") == 0) return type_callout;
+  if (Ustrcmp(argv[2], "ratelimit") == 0) return type_ratelimit;
   }
 usage(name, options);
 return -1;              /* Never obeyed */
@@ -308,7 +316,7 @@ if (rc < 0)
   printf("** Failed to get %s lock for %s: %s",
     ((flags & O_RDONLY) != 0)? "read" : "write", buffer,
     (errno == ETIMEDOUT)? "timed out" : strerror(errno));
-  close(dbblock->lockfd);
+  (void)close(dbblock->lockfd);
   return NULL;
   }
 
@@ -328,7 +336,7 @@ if (dbblock->dbptr == NULL)
     ""
     #endif
     );
-  close(dbblock->lockfd);
+  (void)close(dbblock->lockfd);
   return NULL;
   }
 
@@ -353,7 +361,7 @@ static void
 dbfn_close(open_db *dbblock)
 {
 EXIM_DBCLOSE(dbblock->dbptr);
-close(dbblock->lockfd);
+(void)close(dbblock->lockfd);
 }
 
 
@@ -536,6 +544,7 @@ while (key != NULL)
   dbdata_retry *retry;
   dbdata_wait *wait;
   dbdata_callout_cache *callout;
+  dbdata_ratelimit *ratelimit;
   int count_bad = 0;
   int i, length;
   uschar *t;
@@ -661,6 +670,15 @@ while (key != NULL)
           print_cache(callout->random_result));
         }
 
+      break;
+
+      case type_ratelimit:
+      ratelimit = (dbdata_ratelimit *)value;
+
+      printf("%s.%06d rate: %10.3f key: %s\n",
+        print_time(ratelimit->time_stamp), ratelimit->time_usec,
+        ratelimit->rate, keybuffer);
+
       break;
       }
     store_reset(value);
@@ -733,6 +751,7 @@ for(;;)
   dbdata_retry *retry;
   dbdata_wait *wait;
   dbdata_callout_cache *callout;
+  dbdata_ratelimit *ratelimit;
   int i, oldlength;
   uschar *t;
   uschar field[256], value[256];
@@ -748,20 +767,20 @@ for(;;)
   /* If the buffer contains just one digit, or just consists of "d", use the
   previous name for an update. */
 
-  if ((isdigit((uschar)buffer[0]) && !isdigit((uschar)buffer[1])) ||
-       Ustrcmp(buffer, "d") == 0)
+  if ((isdigit((uschar)buffer[0]) && (buffer[1] == ' ' || buffer[1] == '\0'))
+       || Ustrcmp(buffer, "d") == 0)
     {
     if (name[0] == 0)
       {
       printf("No previous record name is set\n");
       continue;
       }
-    sscanf(CS buffer, "%s %s", field, value);
+    (void)sscanf(CS buffer, "%s %s", field, value);
     }
   else
     {
     name[0] = 0;
-    sscanf(CS buffer, "%s %s %s", name, field, value);
+    (void)sscanf(CS buffer, "%s %s %s", name, field, value);
     }
 
   /* Handle an update request */
@@ -873,6 +892,31 @@ for(;;)
               break;
               }
             break;
+
+            case type_ratelimit:
+            ratelimit = (dbdata_ratelimit *)record;
+            length = sizeof(dbdata_ratelimit);
+            switch(fieldno)
+              {
+              case 0:
+              if ((tt = read_time(value)) > 0) ratelimit->time_stamp = tt;
+                else printf("bad time value\n");
+              break;
+
+              case 1:
+              ratelimit->time_usec = Uatoi(value);
+              break;
+
+              case 2:
+              ratelimit->rate = Ustrtod(value, NULL);
+              break;
+
+              default:
+              printf("unknown field number\n");
+              verify = 0;
+              break;
+              }
+            break;
             }
 
           dbfn_write(dbm, name, record, length);
@@ -970,6 +1014,13 @@ for(;;)
             callout->random_result);
         }
       break;
+
+      case type_ratelimit:
+      ratelimit = (dbdata_ratelimit *)record;
+      printf("0 time stamp:  %s\n", print_time(ratelimit->time_stamp));
+      printf("1 fract. time: .%06d\n", ratelimit->time_usec);
+      printf("2 sender rate: % .3f\n", ratelimit->rate);
+      break;
       }
     }
 
@@ -1246,8 +1297,12 @@ while (keychain != NULL)
 
     if (Ustat(buffer, &statbuf) != 0)
       {
-      dbfn_delete(dbm, key);
-      printf("deleted %s (no message)\n", key);
+      sprintf(CS(buffer + path_len), "%c/%s-D", id[5], id);
+      if (Ustat(buffer, &statbuf) != 0)
+        {
+        dbfn_delete(dbm, key);
+        printf("deleted %s (no message)\n", key);
+        }
       }
     }
   }