home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / cachedump.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.7 KB  |  138 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* this little program will sequentially dump out
  19.  * every record in the database
  20.  */
  21. #include "extcache.h"
  22.  
  23. #include <fcntl.h>
  24. #include <sys/stat.h>
  25. #include <assert.h>
  26.  
  27. #ifdef _sgi
  28. #include <sys/endian.h> 
  29. #endif /* _sgi */
  30.  
  31. /* URL methods
  32.  */
  33. #define URL_GET_METHOD  0
  34. #define URL_POST_METHOD 1
  35. #define URL_HEAD_METHOD 2
  36.  
  37. static DB *
  38. net_OpenExtCacheFatDB(char *filename)
  39. {
  40.     DB *rv;
  41.     HASHINFO hash_info = {
  42.         16*1024,  /* bucket size */
  43.         0,        /* fill factor */
  44.         0,        /* number of elements */
  45.         0,        /* bytes to cache */
  46.         0,        /* hash function */
  47.         0};       /* byte order */
  48.  
  49.  
  50.     rv = dbopen(filename,
  51.                                     O_RDWR | O_CREAT,
  52.                                     0644,
  53.                                     DB_HASH,
  54.                 &hash_info);
  55.  
  56.     if(!rv)
  57.       {
  58.         printf("Could not open cache database: %s\n", filename);
  59.         exit(1);
  60.       }
  61.     
  62.     return(rv);
  63. }
  64.  
  65. int main(int argc, char **argv)
  66. {
  67.     char url[4028];
  68.     struct stat stat_s;
  69.     net_CacheObject * cache_obj;
  70.     DB * ext_cache_database=0;
  71.     DBT key;
  72.     DBT data;
  73.     int len;
  74.     char *end;
  75.  
  76.     memset(&cache_obj, 0, sizeof(net_CacheObject));
  77.  
  78.     if(argc != 2)
  79.       {
  80.         printf("Usage:\n"
  81.                 "%s database\n"
  82.                 "\n"
  83.                 "database: path and name of the database\n", argv[0]);
  84.         exit(1);
  85.       }
  86.  
  87.     /* open the cache database */
  88.     ext_cache_database = net_OpenExtCacheFatDB(argv[1]);
  89.  
  90.     if(!ext_cache_database)
  91.       {
  92.         perror("Could not open cache database");
  93.         exit(1);
  94.       }
  95.  
  96.     while(!(ext_cache_database->seq)(ext_cache_database, &key, &data, 0))
  97.       {
  98.  
  99.         if(key.size == XP_STRLEN(EXT_CACHE_NAME_STRING)
  100.             && !XP_STRCMP(key.data, EXT_CACHE_NAME_STRING))
  101.           {
  102.             /* make sure it's a terminated string */
  103.             if(((char *)data.data)[data.size-1] == '\0')
  104.                 printf("\n\nDatabase Name: %s\n", (char*)data.data);
  105.             else
  106.                  printf("\n\nDatabase Name is corrupted!\n");
  107.             printf("\n--------------------------------------\n");
  108.             continue;
  109.           }
  110.  
  111.         /* try and convert the db struct to a cache struct */
  112.         cache_obj = net_DBDataToCacheStruct(&data);
  113.  
  114.         if(!cache_obj)
  115.           {
  116.             printf("Malformed database entry:\n");
  117.             printf("key: ");
  118.             fwrite(key.data, 1, key.size, stdout);
  119.             printf("\ndata: ");
  120.             fwrite(data.data, 1, data.size, stdout);
  121.             printf("\n");
  122.             printf("--------------------------------------\n");
  123.             continue;
  124.           }
  125.  
  126.         /* the URL is 8 bytes into the key struct
  127.          */
  128.         printf("URL: %s\n",(char*)key.data+8);
  129.         printf("file: %s\n", cache_obj->filename);
  130.         printf("is_relative_path: %s\n", cache_obj->is_relative_path ? "TRUE" : "FALSE");
  131.         printf("content_type: %s\n", cache_obj->content_type);
  132.         printf("content_length: %d\n", cache_obj->content_length);
  133.         printf("last_modified: %s\n", ctime(&cache_obj->last_modified));
  134.         printf("--------------------------------------\n");
  135.       }
  136. }
  137.  
  138.