Changeset 5117:ddd38b4cf15c

Show
Ignore:
Timestamp:
2007-04-10 19:19:24 (21 months ago)
Author:
Brendan Cully <brendan@…>
Branch:
HEAD
Message:

Refactor mutt_hcache_open to share more code

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • hcache.c

    r5116 r5117  
    6868  DB_ENV *env; 
    6969  DB *db; 
     70  char *folder; 
    7071  unsigned int crc; 
    7172  int fd; 
     
    779780 
    780781#if HAVE_QDBM 
    781 header_cache_t * 
    782 mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer) 
    783 { 
    784   struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE)); 
     782static int 
     783hcache_open_qdbm (struct header_cache* h, const char* path) 
     784{ 
    785785  int    flags = VL_OWRITER | VL_OCREAT; 
    786  
    787   h->db = NULL; 
    788   h->folder = get_foldername(folder); 
    789   h->crc = HCACHEVER; 
    790  
    791   if (!path || path[0] == '\0') 
    792   { 
    793     FREE(&h->folder); 
    794     FREE(&h); 
    795     return NULL; 
    796   } 
    797  
    798   path = mutt_hcache_per_folder(path, h->folder, namer); 
    799786 
    800787  if (option(OPTHCACHECOMPRESS)) 
    801788    flags |= VL_OZCOMP; 
    802789 
    803   h->db = vlopen(path, flags, VL_CMPLEX); 
     790  h->db = vlopen (path, flags, VL_CMPLEX); 
    804791  if (h->db) 
    805     return h; 
     792    return 0; 
    806793  else 
    807   { 
    808     FREE(&h->folder); 
    809     FREE(&h); 
    810  
    811     return NULL; 
    812   } 
     794    return -1; 
    813795} 
    814796 
     
    843825 
    844826#elif HAVE_GDBM 
    845  
    846 header_cache_t * 
    847 mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer) 
    848 { 
    849   struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE)); 
     827static int 
     828hcache_open_gdbm (struct header_cache* h, const char* path) 
     829{ 
    850830  int pagesize = atoi(HeaderCachePageSize) ? atoi(HeaderCachePageSize) : 16384; 
    851  
    852   h->db = NULL; 
    853   h->folder = get_foldername(folder); 
    854   h->crc = HCACHEVER; 
    855  
    856   if (!path || path[0] == '\0') 
    857   { 
    858     FREE(&h->folder); 
    859     FREE(&h); 
    860     return NULL; 
    861   } 
    862  
    863   path = mutt_hcache_per_folder(path, h->folder, namer); 
    864831 
    865832  h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL); 
    866833  if (h->db) 
    867     return h; 
     834    return 0; 
    868835 
    869836  /* if rw failed try ro */ 
    870837  h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL); 
    871838  if (h->db) 
    872     return h; 
    873   else 
    874   { 
    875     FREE(&h->folder); 
    876     FREE(&h); 
    877  
    878     return NULL; 
    879   } 
     839    return 0; 
     840 
     841  return -1; 
    880842} 
    881843 
     
    928890} 
    929891 
    930 header_cache_t * 
    931 mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer) 
     892static int 
     893hcache_open_db4 (struct header_cache* h, const char* path) 
    932894{ 
    933895  struct stat sb; 
     896  int ret; 
    934897  u_int32_t createflags = DB_CREATE; 
    935   int ret; 
    936   struct header_cache *h = calloc(1, sizeof (HEADER_CACHE)); 
    937   int pagesize = atoi(HeaderCachePageSize); 
    938   char* tmp; 
    939  
    940   h->crc = HCACHEVER; 
    941  
    942   if (!path || path[0] == '\0') 
    943   { 
    944     FREE(&h); 
    945     return NULL; 
    946   } 
    947  
    948   tmp = get_foldername (folder); 
    949   path = mutt_hcache_per_folder(path, tmp, namer); 
    950   snprintf(h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path); 
    951   FREE(&tmp); 
    952  
    953   h->fd = open(h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); 
     898  int pagesize = atoi (HeaderCachePageSize); 
     899 
     900  snprintf (h->lockfile, _POSIX_PATH_MAX, "%s-lock-hack", path); 
     901 
     902  h->fd = open (h->lockfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); 
    954903  if (h->fd < 0) 
    955   { 
    956     FREE(&h); 
    957     return NULL; 
    958   } 
    959  
    960   if (mx_lock_file(h->lockfile, h->fd, 1, 0, 5)) 
    961   { 
    962     close(h->fd); 
    963     FREE(&h); 
    964     return NULL; 
    965   } 
    966  
    967   ret = db_env_create(&h->env, 0); 
     904    return -1; 
     905 
     906  if (mx_lock_file (h->lockfile, h->fd, 1, 0, 5)) 
     907    goto fail_close; 
     908 
     909  ret = db_env_create (&h->env, 0); 
    968910  if (ret) 
    969   { 
    970     mx_unlock_file(h->lockfile, h->fd, 0); 
    971     close(h->fd); 
    972     FREE(&h); 
    973     return NULL; 
    974   } 
     911    goto fail_unlock; 
    975912 
    976913  ret = (*h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, 
    977914        0600); 
    978915  if (ret) 
    979   { 
    980     h->env->close(h->env, 0); 
    981     mx_unlock_file(h->lockfile, h->fd, 0); 
    982     close(h->fd); 
    983     FREE(&h); 
    984     return NULL; 
    985   } 
     916    goto fail_env; 
     917 
    986918  ret = db_create (&h->db, h->env, 0); 
    987919  if (ret) 
    988   { 
    989     h->env->close (h->env, 0); 
    990     mx_unlock_file (h->lockfile, h->fd, 0); 
    991     close (h->fd); 
    992     FREE (&h); 
    993     return NULL; 
    994   } 
     920    goto fail_env; 
    995921 
    996922  if (stat(path, &sb) != 0 && errno == ENOENT) 
     
    1000926  } 
    1001927 
    1002   ret = (*h->db->open)(h->db, NULL, path, folder, DB_BTREE, createflags, 0600); 
     928  ret = (*h->db->open)(h->db, NULL, path, h->folder, DB_BTREE, createflags, 
     929                       0600); 
    1003930  if (ret) 
    1004   { 
    1005     h->db->close(h->db, 0); 
    1006     h->env->close(h->env, 0); 
    1007     mx_unlock_file(h->lockfile, h->fd, 0); 
    1008     close(h->fd); 
    1009     FREE(&h); 
    1010     return NULL; 
    1011   } 
    1012  
    1013   return h; 
     931    goto fail_db; 
     932 
     933  return 0; 
     934 
     935  fail_db: 
     936  h->db->close (h->db, 0); 
     937  fail_env: 
     938  h->env->close (h->env, 0); 
     939  fail_unlock: 
     940  mx_unlock_file (h->lockfile, h->fd, 0); 
     941  fail_close: 
     942  close (h->fd); 
     943  unlink (h->lockfile); 
     944 
     945  return -1; 
    1014946} 
    1015947 
     
    1020952    return; 
    1021953 
    1022   h->db->close(h->db, 0); 
    1023   h->env->close(h->env, 0); 
    1024   mx_unlock_file(h->lockfile, h->fd, 0); 
    1025   close(h->fd); 
    1026   FREE(&h); 
     954  h->db->close (h->db, 0); 
     955  h->env->close (h->env, 0); 
     956  mx_unlock_file (h->lockfile, h->fd, 0); 
     957  close (h->fd); 
     958  unlink (h->lockfile); 
     959  FREE (&h->folder); 
     960  FREE (&h); 
    1027961} 
    1028962 
     
    1043977} 
    1044978#endif 
     979 
     980header_cache_t * 
     981mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer) 
     982{ 
     983  struct header_cache *h = safe_calloc(1, sizeof (HEADER_CACHE)); 
     984  int (*hcache_open) (struct header_cache* h, const char* path); 
     985 
     986#if HAVE_QDBM 
     987  hcache_open = hcache_open_qdbm; 
     988#elif HAVE_GDBM 
     989  hcache_open = hcache_open_gdbm; 
     990#elif HAVE_DB4 
     991  hcache_open = hcache_open_db4; 
     992#endif 
     993 
     994  h->db = NULL; 
     995  h->folder = get_foldername(folder); 
     996  h->crc = HCACHEVER; 
     997 
     998  if (!path || path[0] == '\0') 
     999  { 
     1000    FREE(&h->folder); 
     1001    FREE(&h); 
     1002    return NULL; 
     1003  } 
     1004 
     1005  path = mutt_hcache_per_folder(path, h->folder, namer); 
     1006 
     1007  if (!hcache_open (h, path)) 
     1008    return h; 
     1009  else 
     1010  { 
     1011    FREE(&h->folder); 
     1012    FREE(&h); 
     1013 
     1014    return NULL; 
     1015  } 
     1016}