2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-09-05 11:43:24 adam
8 * Complete version of temporary sets. Not tested yet though.
10 * Revision 1.3 1995/09/04 15:20:40 adam
11 * More work on temp sets. is_open member removed.
13 * Revision 1.2 1995/09/04 09:10:56 adam
16 * Revision 1.1 1994/11/04 13:21:30 quinn
24 #include <sys/types.h>
30 static struct rset_control *r_create(const struct rset_control *sel,
32 static int r_open(struct rset_control *ct, int wflag);
33 static void r_close(struct rset_control *ct);
34 static void r_delete(struct rset_control *ct);
35 static void r_rewind(struct rset_control *ct);
36 static int r_count(struct rset_control *ct);
37 static int r_read (rset_control *ct, void *buf);
38 static int r_write (rset_control *ct, const void *buf);
40 static const rset_control control =
54 const rset_control *rset_kind_temp = &control;
56 struct rset_temp_private {
69 static struct rset_control *r_create(const struct rset_control *sel,
73 rset_temp_parms *temp_parms = parms;
74 struct rset_temp_private *info;
76 logf (LOG_DEBUG, "ritemp_create(%s)", sel->desc);
77 newct = xmalloc(sizeof(*newct));
78 memcpy(newct, sel, sizeof(*sel));
79 newct->buf = xmalloc (sizeof(struct rset_temp_private));
84 info->key_size = temp_parms->key_size;
85 info->buf_size = 1024;
86 info->buf_mem = xmalloc (info->buf_size);
95 static int r_open(struct rset_control *ct, int wflag)
97 struct rset_temp_private *info = ct->buf;
99 assert (info->fd == -1);
103 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
105 info->fd = open (info->fname, O_RDONLY);
108 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
116 static void r_flush (struct rset_control *ct, int mk)
118 struct rset_temp_private *info = ct->buf;
120 if (!info->fname && mk)
122 char *s = (char*) tempnam (NULL, "zrs");
124 info->fname = xmalloc (strlen(s)+1);
125 strcpy (info->fname, s);
127 info->fd = open (info->fname, O_RDWR|O_CREAT, 0666);
130 logf (LOG_FATAL|LOG_ERRNO, "open %s", info->fname);
136 assert (info->fd != -1);
141 if (lseek (info->fd, info->pos_buf, SEEK_SET) == -1)
143 logf (LOG_FATAL|LOG_ERRNO, "lseek %s", info->fname);
146 count = info->buf_size;
147 if (count > info->pos_end - info->pos_buf)
148 count = info->pos_end - info->pos_buf;
149 if ((r = write (info->fd, info->buf_mem, count)) < count)
152 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
154 logf (LOG_FATAL, "write of %ld but got %ld",
155 (long) count, (long) r);
163 static void r_close (struct rset_control *ct)
165 struct rset_temp_private *info = ct->buf;
170 assert (info->fd != -1);
176 static void r_delete (struct rset_control *ct)
178 struct rset_temp_private *info = ct->buf;
182 unlink (info->fname);
183 free (info->buf_mem);
188 static void r_reread (struct rset_control *ct)
190 struct rset_temp_private *info = ct->buf;
196 info->pos_border = info->pos_cur + info->buf_size;
197 if (info->pos_border > info->pos_end)
198 info->pos_border = info->pos_end;
199 count = info->pos_border - info->pos_buf;
201 if ((r = read (info->fd, info->buf_mem, count)) < count)
204 logf (LOG_FATAL|LOG_ERRNO, "read %s", info->fname);
206 logf (LOG_FATAL, "read of %ld but got %ld",
207 (long) count, (long) r);
212 info->pos_border = info->pos_end;
215 static void r_rewind (struct rset_control *ct)
217 struct rset_temp_private *info = ct->buf;
225 static int r_count (struct rset_control *ct)
227 struct rset_temp_private *info = ct->buf;
229 return info->pos_end / info->key_size;
232 static int r_read (rset_control *ct, void *buf)
234 struct rset_temp_private *info = ct->buf;
235 size_t nc = info->pos_cur + info->key_size;
237 if (nc > info->pos_border)
239 if (nc > info->pos_end)
242 info->pos_buf = info->pos_cur;
245 memcpy (buf, info->buf_mem + (info->pos_cur - info->pos_buf),
251 static int r_write (rset_control *ct, const void *buf)
253 struct rset_temp_private *info = ct->buf;
254 size_t nc = info->pos_cur + info->key_size;
256 if (nc > info->pos_buf + info->buf_size)
259 info->pos_buf = info->pos_cur;
262 memcpy (info->buf_mem + (info->pos_cur - info->pos_buf), buf,
265 info->pos_border = info->pos_cur = nc;