summaryrefslogtreecommitdiffstats
path: root/cdbmake.c
blob: 3c1c8bdddce18baeb614a08604dbed0bec1cddaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include "error.h"
#include "open.h"
#include "strerr.h"
#include "cdb_make.h"
#include "cdb.h"

#define FATAL "cdbmake: fatal: "

char *fn;
char *fntmp;

void die_usage(void)
{
  strerr_die1x(100,"cdbmake: usage: cdbmake f ftmp");
}
void die_write(void)
{
  strerr_die4sys(111,FATAL,"unable to create ",fntmp,": ");
}
void die_read(void)
{
  strerr_die2sys(111,FATAL,"unable to read input: ");
}
void die_readformat(void)
{
  strerr_die2x(111,FATAL,"unable to read input: bad format");
}

inline void get(char *ch)
{
  switch(buffer_GETC(buffer_0,ch)) {
    case 0: die_readformat();
    case -1: die_read();
  }
}

static struct cdb_make c;

main(int argc,char **argv)
{
  unsigned int klen;
  unsigned int dlen;
  unsigned int i;
  uint32 h;
  int fd;
  char ch;

  if (!*argv) die_usage();
  
  if (!*++argv) die_usage();
  fn = *argv;

  if (!*++argv) die_usage();
  fntmp = *argv;

  fd = open_trunc(fntmp);
  if (fd == -1) die_write();

  if (cdb_make_start(&c,fd) == -1) die_write();

  for (;;) {
    get(&ch);
    if (ch == '\n') break;
    if (ch != '+') die_readformat();
    klen = 0;
    for (;;) {
      get(&ch);
      if (ch == ',') break;
      if ((ch < '0') || (ch > '9')) die_readformat();
      if (klen > 429496720) { errno = error_nomem; die_write(); }
      klen = klen * 10 + (ch - '0');
    }
    dlen = 0;
    for (;;) {
      get(&ch);
      if (ch == ':') break;
      if ((ch < '0') || (ch > '9')) die_readformat();
      if (dlen > 429496720) { errno = error_nomem; die_write(); }
      dlen = dlen * 10 + (ch - '0');
    }

    if (cdb_make_addbegin(&c,klen,dlen) == -1) die_write();
    h = CDB_HASHSTART;
    for (i = 0;i < klen;++i) {
      get(&ch);
      if (buffer_PUTC(&c.b,ch) == -1) die_write();
      h = cdb_hashadd(h,ch);
    }
    get(&ch);
    if (ch != '-') die_readformat();
    get(&ch);
    if (ch != '>') die_readformat();
    for (i = 0;i < dlen;++i) {
      get(&ch);
      if (buffer_PUTC(&c.b,ch) == -1) die_write();
    }
    if (cdb_make_addend(&c,klen,dlen,h) == -1) die_write();

    get(&ch);
    if (ch != '\n') die_readformat();
  }

  if (cdb_make_finish(&c) == -1) die_write();
  if (fsync(fd) == -1) die_write();
  if (close(fd) == -1) die_write(); /* NFS silliness */
  if (rename(fntmp,fn) == -1)
    strerr_die6sys(111,FATAL,"unable to rename ",fntmp," to ",fn,": ");

  _exit(0);
}