Mercurial > notdcc
comparison srvrlib/read_rcd.c @ 0:c7f6b056b673
First import of vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Tue, 10 Mar 2009 13:49:58 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c7f6b056b673 |
---|---|
1 /* Distributed Checksum Clearinghouse | |
2 * | |
3 * Copyright (c) 2008 by Rhyolite Software, LLC | |
4 * | |
5 * This agreement is not applicable to any entity which sells anti-spam | |
6 * solutions to others or provides an anti-spam solution as part of a | |
7 * security solution sold to other entities, or to a private network | |
8 * which employs the DCC or uses data provided by operation of the DCC | |
9 * but does not provide corresponding data to other users. | |
10 * | |
11 * Permission to use, copy, modify, and distribute this software without | |
12 * changes for any purpose with or without fee is hereby granted, provided | |
13 * that the above copyright notice and this permission notice appear in all | |
14 * copies and any distributed versions or copies are either unchanged | |
15 * or not called anything similar to "DCC" or "Distributed Checksum | |
16 * Clearinghouse". | |
17 * | |
18 * Parties not eligible to receive a license under this agreement can | |
19 * obtain a commercial license to use DCC by contacting Rhyolite Software | |
20 * at sales@rhyolite.com. | |
21 * | |
22 * A commercial license would be for Distributed Checksum and Reputation | |
23 * Clearinghouse software. That software includes additional features. This | |
24 * free license for Distributed ChecksumClearinghouse Software does not in any | |
25 * way grant permision to use Distributed Checksum and Reputation Clearinghouse | |
26 * software | |
27 * | |
28 * THE SOFTWARE IS PROVIDED "AS IS" AND RHYOLITE SOFTWARE, LLC DISCLAIMS ALL | |
29 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
30 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL RHYOLITE SOFTWARE, LLC | |
31 * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES | |
32 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, | |
33 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, | |
34 * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
35 * SOFTWARE. | |
36 * | |
37 * Rhyolite Software DCC 1.3.103-1.24 $Revision$ | |
38 */ | |
39 | |
40 #include "srvr_defs.h" | |
41 | |
42 static int read_rcd_fd = -1; | |
43 #define BUF_SIZE (64*1024) | |
44 static u_char read_rcd_buf[BUF_SIZE]; | |
45 static u_int read_rcd_bufsize = BUF_SIZE; | |
46 static off_t read_rcd_base; | |
47 static u_int read_rcd_buflen; | |
48 | |
49 static int system_pagesize; | |
50 | |
51 | |
52 | |
53 int | |
54 read_db(DCC_EMSG emsg, void *buf, u_int buf_len, | |
55 int fd, off_t pos, const char *file_nm) | |
56 { | |
57 int i; | |
58 | |
59 if (-1 == lseek(fd, pos, SEEK_SET)) { | |
60 dcc_pemsg(EX_IOERR, emsg, "lseek(%s, 0): %s", | |
61 DB_NM2PATH_ERR(file_nm), ERROR_STR()); | |
62 return -1; | |
63 } | |
64 | |
65 i = read(fd, buf, buf_len); | |
66 if (i >= 0) | |
67 return i; | |
68 | |
69 dcc_pemsg(EX_IOERR, emsg, "read(%s): %s", | |
70 DB_NM2PATH_ERR(file_nm), ERROR_STR()); | |
71 return -1; | |
72 } | |
73 | |
74 | |
75 | |
76 u_char | |
77 read_db_hdr(DCC_EMSG emsg, DB_HDR *hdrp, | |
78 int fd, const char *file_nm) | |
79 { | |
80 return (sizeof(*hdrp) == read_db(emsg, hdrp, sizeof(*hdrp), | |
81 fd, 0, file_nm)); | |
82 } | |
83 | |
84 | |
85 | |
86 /* invalidate the read_rcd() read-ahead cache */ | |
87 void | |
88 read_rcd_invalidate(u_int bufsize) | |
89 { | |
90 read_rcd_fd = -1; | |
91 | |
92 if (system_pagesize == 0) | |
93 system_pagesize = getpagesize(); | |
94 bufsize = ((bufsize + system_pagesize-1) / system_pagesize | |
95 * system_pagesize); | |
96 if (bufsize == 0 || bufsize > BUF_SIZE) | |
97 bufsize = BUF_SIZE; | |
98 read_rcd_bufsize = bufsize; | |
99 } | |
100 | |
101 | |
102 | |
103 static int | |
104 read_rcd_sub(DCC_EMSG emsg, void *buf, u_int buflen, | |
105 int fd, off_t pos, const char *file_nm) | |
106 { | |
107 int i; | |
108 | |
109 if (fd == read_rcd_fd | |
110 && pos >= read_rcd_base | |
111 && pos+buflen <= read_rcd_base+read_rcd_buflen) { | |
112 memcpy(buf, &read_rcd_buf[pos-read_rcd_base], buflen); | |
113 return buflen; | |
114 } | |
115 | |
116 if (system_pagesize == 0) | |
117 system_pagesize = getpagesize(); | |
118 read_rcd_base = pos - (pos % system_pagesize); | |
119 | |
120 i = read_db(emsg, read_rcd_buf, read_rcd_bufsize, | |
121 read_rcd_fd = fd, | |
122 read_rcd_base, file_nm); | |
123 if (i <= 0) { | |
124 read_rcd_fd = -1; | |
125 return i; | |
126 } | |
127 read_rcd_buflen = i; | |
128 if (buflen > read_rcd_buflen) | |
129 buflen = read_rcd_buflen; | |
130 memcpy(buf, &read_rcd_buf[pos-read_rcd_base], buflen); | |
131 return buflen; | |
132 } | |
133 | |
134 | |
135 | |
136 int /* -1=error 0=eof >0=record length */ | |
137 read_rcd(DCC_EMSG emsg, DB_RCD *rcd, | |
138 int fd, off_t pos, const char *file_nm) | |
139 { | |
140 int i, cks_len, num_cks; | |
141 | |
142 /* read a record, starting with its beginning */ | |
143 i = read_rcd_sub(emsg, rcd, DB_RCD_HDR_LEN, | |
144 fd, pos, file_nm); | |
145 if (DB_RCD_HDR_LEN != i) { | |
146 if (i == 0) | |
147 return 0; | |
148 if (i > 0) { | |
149 dcc_pemsg(EX_DATAERR, emsg, | |
150 "header of record at "OFF_HPAT" of %s" | |
151 " truncated by %d bytes", | |
152 lseek(fd, 0, SEEK_CUR) - i, | |
153 DB_NM2PATH_ERR(file_nm), DB_RCD_HDR_LEN-i); | |
154 } | |
155 return -1; | |
156 } | |
157 | |
158 num_cks = DB_NUM_CKS(rcd); | |
159 if (num_cks > DIM(rcd->cks)) { | |
160 dcc_pemsg(EX_DATAERR, emsg, | |
161 "bogus checksum count %#x at "OFF_HPAT" of %s", | |
162 rcd->fgs_num_cks, lseek(fd, 0, SEEK_CUR), | |
163 DB_NM2PATH_ERR(file_nm)); | |
164 return -1; | |
165 } | |
166 cks_len = num_cks * sizeof(rcd->cks[0]); | |
167 if (cks_len != 0) { | |
168 i = read_rcd_sub(emsg, rcd->cks, cks_len, | |
169 fd, pos+DB_RCD_HDR_LEN, file_nm); | |
170 if (i != cks_len) { | |
171 if (i < 0) | |
172 return -1; | |
173 if (!i) | |
174 dcc_pemsg(EX_DATAERR, emsg, | |
175 "record at "OFF_HPAT" of %s" | |
176 " truncated after header", | |
177 lseek(fd, 0, SEEK_CUR), | |
178 DB_NM2PATH_ERR(file_nm)); | |
179 else | |
180 dcc_pemsg(EX_DATAERR, emsg, | |
181 "record at "OFF_HPAT" of %s" | |
182 " truncated by %d bytes", | |
183 lseek(fd, 0, SEEK_CUR), | |
184 DB_NM2PATH_ERR(file_nm), cks_len-i); | |
185 return 0; | |
186 } | |
187 } | |
188 | |
189 return DB_RCD_HDR_LEN+cks_len; | |
190 } |