Changeset 5117:ddd38b4cf15c
- 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:
-
Legend:
- Unmodified
- Added
- Removed
-
|
r5116
|
r5117
|
|
| 68 | 68 | DB_ENV *env; |
| 69 | 69 | DB *db; |
| | 70 | char *folder; |
| 70 | 71 | unsigned int crc; |
| 71 | 72 | int fd; |
| … |
… |
|
| 779 | 780 | |
| 780 | 781 | #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)); |
| | 782 | static int |
| | 783 | hcache_open_qdbm (struct header_cache* h, const char* path) |
| | 784 | { |
| 785 | 785 | 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); |
| 799 | 786 | |
| 800 | 787 | if (option(OPTHCACHECOMPRESS)) |
| 801 | 788 | flags |= VL_OZCOMP; |
| 802 | 789 | |
| 803 | | h->db = vlopen(path, flags, VL_CMPLEX); |
| | 790 | h->db = vlopen (path, flags, VL_CMPLEX); |
| 804 | 791 | if (h->db) |
| 805 | | return h; |
| | 792 | return 0; |
| 806 | 793 | else |
| 807 | | { |
| 808 | | FREE(&h->folder); |
| 809 | | FREE(&h); |
| 810 | | |
| 811 | | return NULL; |
| 812 | | } |
| | 794 | return -1; |
| 813 | 795 | } |
| 814 | 796 | |
| … |
… |
|
| 843 | 825 | |
| 844 | 826 | #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)); |
| | 827 | static int |
| | 828 | hcache_open_gdbm (struct header_cache* h, const char* path) |
| | 829 | { |
| 850 | 830 | 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); |
| 864 | 831 | |
| 865 | 832 | h->db = gdbm_open((char *) path, pagesize, GDBM_WRCREAT, 00600, NULL); |
| 866 | 833 | if (h->db) |
| 867 | | return h; |
| | 834 | return 0; |
| 868 | 835 | |
| 869 | 836 | /* if rw failed try ro */ |
| 870 | 837 | h->db = gdbm_open((char *) path, pagesize, GDBM_READER, 00600, NULL); |
| 871 | 838 | 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; |
| 880 | 842 | } |
| 881 | 843 | |
| … |
… |
|
| 928 | 890 | } |
| 929 | 891 | |
| 930 | | header_cache_t * |
| 931 | | mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer) |
| | 892 | static int |
| | 893 | hcache_open_db4 (struct header_cache* h, const char* path) |
| 932 | 894 | { |
| 933 | 895 | struct stat sb; |
| | 896 | int ret; |
| 934 | 897 | 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); |
| 954 | 903 | 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); |
| 968 | 910 | 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; |
| 975 | 912 | |
| 976 | 913 | ret = (*h->env->open)(h->env, NULL, DB_INIT_MPOOL | DB_CREATE | DB_PRIVATE, |
| 977 | 914 | 0600); |
| 978 | 915 | 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 | |
| 986 | 918 | ret = db_create (&h->db, h->env, 0); |
| 987 | 919 | 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; |
| 995 | 921 | |
| 996 | 922 | if (stat(path, &sb) != 0 && errno == ENOENT) |
| … |
… |
|
| 1000 | 926 | } |
| 1001 | 927 | |
| 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); |
| 1003 | 930 | 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; |
| 1014 | 946 | } |
| 1015 | 947 | |
| … |
… |
|
| 1020 | 952 | return; |
| 1021 | 953 | |
| 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); |
| 1027 | 961 | } |
| 1028 | 962 | |
| … |
… |
|
| 1043 | 977 | } |
| 1044 | 978 | #endif |
| | 979 | |
| | 980 | header_cache_t * |
| | 981 | mutt_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 | } |