+
+ for (i = 0; i<32; i++)
+ ws[i] = NULL;
+
+ while (off < reckeys->buf_used)
+ {
+ const char *src = reckeys->buf + off;
+ char attrSet;
+ short attrUse;
+ int seqno;
+ const char *wstart;
+
+ memcpy (&attrSet, src, sizeof(attrSet));
+ src += sizeof(attrSet);
+
+ memcpy (&attrUse, src, sizeof(attrUse));
+ src += sizeof(attrUse);
+
+ wstart = src;
+ while (*src++)
+ ;
+
+ memcpy (&seqno, src, sizeof(seqno));
+ src += sizeof(seqno);
+
+#if 0
+ logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
+#endif
+ if (attrUseS == attrUse && attrSetS == attrSet)
+ {
+ int woff;
+
+
+ if (startSeq == -1)
+ startSeq = seqno;
+ woff = seqno - startSeq;
+ if (woff >= 0 && woff < 31)
+ ws[woff] = wstart;
+ }
+
+ off = src - reckeys->buf;
+ }
+ assert (off == reckeys->buf_used);
+ return ws;
+}
+
+static void addRecordKeyAny (const RecWord *p)
+{
+ if (p->attrSet != 1 || p->attrUse != 1016)
+ {
+ RecWord w;
+
+ memcpy (&w, p, sizeof(w));
+ w.attrSet = 1;
+ w.attrUse = 1016;
+ addRecordKey (&w);
+ }
+ if (p->attrSet != -1)
+ addRecordKey (p);
+}
+
+#define FILE_READ_BUFSIZE 4096
+struct file_read_info {
+ int file_noread;
+ int fd;
+#if FILE_READ_BUFSIZE
+ char *file_buf;
+ int file_offset;
+ int file_bufsize;
+#endif
+};
+
+static struct file_read_info *file_read_start (int fd)
+{
+ struct file_read_info *fi = xmalloc (sizeof(*fi));
+
+ fi->fd = fd;
+ fi->file_noread = 0;
+#if FILE_READ_BUFSIZE
+ fi->file_offset = 0;
+ fi->file_buf = xmalloc (FILE_READ_BUFSIZE);
+ fi->file_bufsize = read (fd, fi->file_buf, FILE_READ_BUFSIZE);
+#endif
+ return fi;
+}
+
+static void file_read_stop (struct file_read_info *fi)
+{
+ assert (fi);
+#if FILE_READ_BUFSIZE
+ xfree (fi->file_buf);
+ fi->file_buf = NULL;
+#endif
+ xfree (fi);
+}
+
+static int file_read (void *handle, char *buf, size_t count)
+{
+ struct file_read_info *p = handle;
+ int fd = p->fd;
+#if FILE_READ_BUFSIZE
+ int l = p->file_bufsize - p->file_offset;
+
+ if (count > l)
+ {
+ int r;
+ if (l > 0)
+ memcpy (buf, p->file_buf + p->file_offset, l);
+ count = count-l;
+ if (count > FILE_READ_BUFSIZE)
+ {
+ if ((r = read (fd, buf + l, count)) == -1)
+ {
+ logf (LOG_FATAL|LOG_ERRNO, "read");
+ exit (1);
+ }
+ p->file_bufsize = 0;
+ p->file_offset = 0;
+ p->file_noread += l+r;
+ return l+r;
+ }
+ p->file_bufsize = r = read (fd, p->file_buf, FILE_READ_BUFSIZE);
+ if (r == -1)
+ {
+ logf (LOG_FATAL|LOG_ERRNO, "read");
+ exit (1);
+ }
+ else if (r <= count)
+ {
+ p->file_offset = r;
+ memcpy (buf + l, p->file_buf, r);
+ p->file_noread += l+r;
+ return l+r;
+ }
+ else
+ {
+ p->file_offset = count;
+ memcpy (buf + l, p->file_buf, count - l);
+ p->file_noread += count;
+ return count;
+ }
+ }
+ memcpy (buf, p->file_buf + p->file_offset, count);
+ p->file_offset += count;
+ p->file_noread += count;
+ return count;
+#else
+ int r;
+ r = read (fd, buf, count);
+ if (r > 0)
+ p->file_noread += r;
+ return r;
+#endif
+}
+
+static int atois (const char **s)
+{
+ int val = 0, c;
+ while ( (c=**s) >= '0' && c <= '9')
+ {
+ val = val*10 + c - '0';
+ ++(*s);
+ }
+ return val;
+}
+
+static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
+ const char *fname,
+ const char *spec)
+{
+ static char dstBuf[2048];
+ char *dst = dstBuf;
+ const char *s = spec;
+ static const char **w;
+ int i;
+
+ while (1)
+ {
+ while (*s == ' ' || *s == '\t')
+ s++;
+ if (!*s)
+ break;
+ if (*s == '(')
+ {
+ char matchFlag[32];
+ int attrSet, attrUse;
+ int first = 1;
+
+ s++;
+ attrSet = atois (&s);
+ if (*s != ',')
+ {
+ logf (LOG_WARN, "Missing , in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ s++;
+ attrUse = atois (&s);
+ w = searchRecordKey (reckeys, attrSet, attrUse);
+ assert (w);
+
+ if (*s == ')')
+ {
+ for (i = 0; i<32; i++)
+ matchFlag[i] = 1;
+ }
+ else
+ {
+ logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ s++;
+
+ for (i = 0; i<32; i++)
+ if (matchFlag[i] && w[i])
+ {
+ if (first)
+ {
+ *dst++ = ' ';
+ first = 0;
+ }
+ strcpy (dst, w[i]);
+ dst += strlen(w[i]);
+ }
+ if (first)
+ {
+ logf (LOG_WARN, "Record in file %s didn't contain match"
+ " fields in (%d,%d)", fname, attrSet, attrUse);
+ return NULL;
+ }
+ }
+ else if (*s == '$')
+ {
+ int spec_len;
+ char special[64];
+ const char *spec_src = NULL;
+ const char *s1 = ++s;
+ while (*s1 && *s1 != ' ' && *s1 != '\t')
+ s1++;
+
+ spec_len = s1 - s;
+ if (spec_len > 63)
+ spec_len = 63;
+ memcpy (special, s, spec_len);
+ special[spec_len] = '\0';
+ s = s1;
+
+ if (!strcmp (special, "group"))
+ spec_src = rGroup->groupName;
+ else if (!strcmp (special, "database"))
+ spec_src = rGroup->databaseName;
+ else if (!strcmp (special, "filename"))
+ spec_src = fname;
+ else if (!strcmp (special, "type"))
+ spec_src = rGroup->recordType;
+ else
+ spec_src = NULL;
+ if (spec_src)
+ {
+ strcpy (dst, spec_src);
+ dst += strlen (spec_src);
+ }
+ }
+ else if (*s == '\"' || *s == '\'')
+ {
+ int stopMarker = *s++;
+ char tmpString[64];
+ int i = 0;
+
+ while (*s && *s != stopMarker)
+ {
+ if (i < 63)
+ tmpString[i++] = *s++;
+ }
+ if (*s)
+ s++;
+ tmpString[i] = '\0';
+ strcpy (dst, tmpString);
+ dst += strlen (tmpString);
+ }
+ else
+ {
+ logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
+ spec, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ *dst++ = 1;
+ }
+ if (dst == dstBuf)
+ {
+ logf (LOG_WARN, "No match criteria for record %s in group %s",
+ fname, rGroup->groupName ? rGroup->groupName : "none");
+ return NULL;
+ }
+ return dstBuf;
+}
+
+static int recordExtract (SYSNO *sysno, const char *fname,
+ struct recordGroup *rGroup, int deleteFlag,
+ struct file_read_info *fi, RecType recType,
+ char *subType)
+{
+ struct recExtractCtrl extractCtrl;
+ int r;
+ char *matchStr;
+ SYSNO sysnotmp;
+ Record rec;
+
+ if (fi->fd != -1)
+ {
+ extractCtrl.fh = fi;
+ /* extract keys */
+ extractCtrl.subType = subType;
+ extractCtrl.init = wordInit;
+ extractCtrl.add = addRecordKeyAny;
+
+ reckeys.buf_used = 0;
+ extractCtrl.readf = file_read;
+ r = (*recType->extract)(&extractCtrl);
+
+ if (r)
+ {
+ logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
+ return 0;
+ }
+ }
+
+ /* perform match if sysno not known and if match criteria is specified */
+
+ matchStr = NULL;
+ if (!sysno)
+ {
+ sysnotmp = 0;
+ sysno = &sysnotmp;
+ if (rGroup->recordId && *rGroup->recordId)
+ {
+ char *rinfo;
+
+ matchStr = fileMatchStr (&reckeys, rGroup, fname,
+ rGroup->recordId);
+ if (matchStr)
+ {
+ rinfo = dict_lookup (matchDict, matchStr);
+ if (rinfo)
+ memcpy (sysno, rinfo+1, sizeof(*sysno));
+ }
+ else
+ {
+ logf (LOG_WARN, "Record not inserted");
+ return 0;
+ }
+ }
+ }
+
+ /* new record ? */
+ if (! *sysno)
+ {
+ if (deleteFlag)
+ {
+ logf (LOG_LOG, "? %s", fname);
+ return 1;
+ }
+ logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
+ rec = rec_new (records);
+ *sysno = rec->sysno;
+
+ if (matchStr)
+ {
+ dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
+ }
+ flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
+
+ records_inserted++;
+ }
+ else
+ {
+ struct recKeys delkeys;
+
+ rec = rec_get (records, *sysno);
+ assert (rec);
+ delkeys.buf_used = rec->size[recInfo_delKeys];
+ delkeys.buf = rec->info[recInfo_delKeys];
+ flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
+ if (deleteFlag)
+ {
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_WARN, "cannot delete %s: storeKeys false",
+ fname);
+ }
+ else
+ {
+ logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
+ records_deleted++;
+ if (matchStr)
+ dict_delete (matchDict, matchStr);
+ rec_del (records, &rec);
+ }
+ return 1;
+ }
+ else
+ {
+ if (!delkeys.buf_used)
+ {
+ logf (LOG_WARN, "cannot update %s: storeKeys false",
+ fname);
+ }
+ else
+ {
+ logf (LOG_LOG, "update %s %s", rGroup->recordType,
+ fname);
+ flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
+ records_updated++;
+ }
+ }
+ }
+ xfree (rec->info[recInfo_fileType]);
+ rec->info[recInfo_fileType] =
+ rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
+
+ xfree (rec->info[recInfo_filename]);
+ rec->info[recInfo_filename] =
+ rec_strdup (fname, &rec->size[recInfo_filename]);
+
+ xfree (rec->info[recInfo_delKeys]);
+ if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
+ {
+ rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
+ rec->size[recInfo_delKeys] = reckeys.buf_used;
+ memcpy (rec->info[recInfo_delKeys], reckeys.buf,
+ rec->size[recInfo_delKeys]);
+ }
+ else
+ {
+ rec->info[recInfo_delKeys] = NULL;
+ rec->size[recInfo_delKeys] = 0;
+ }
+
+ xfree (rec->info[recInfo_storeData]);
+ if (rGroup->flagStoreData == 1)
+ {
+ rec->size[recInfo_storeData] = fi->file_noread;
+ rec->info[recInfo_storeData] = xmalloc (fi->file_noread);
+#if FILE_READ_BUFSIZE
+ if (fi->file_noread < FILE_READ_BUFSIZE)
+ memcpy (rec->info[recInfo_storeData], fi->file_buf,
+ fi->file_noread);
+ else
+#endif
+ {
+ if (lseek (fi->fd, 0L, SEEK_SET) < 0)
+ {
+ logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
+ exit (1);
+ }
+ if (read (fi->fd, rec->info[recInfo_storeData], fi->file_noread)
+ < fi->file_noread)
+ {
+ logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
+ fi->file_noread, fname);
+ exit (1);
+ }
+ }
+ }
+ else
+ {
+ rec->info[recInfo_storeData] = NULL;
+ rec->size[recInfo_storeData] = 0;
+ }
+ xfree (rec->info[recInfo_databaseName]);
+ rec->info[recInfo_databaseName] =
+ rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
+
+ rec_put (records, &rec);
+ return 1;
+}
+
+int fileExtract (SYSNO *sysno, const char *fname,
+ const struct recordGroup *rGroupP, int deleteFlag)
+{
+ int i, fd;
+ char gprefix[128];