17 #define FATAL "rbldns-data: fatal: "
21 strerr_die2x(111,FATAL
,"out of memory");
34 unsigned long linenum
= 0;
36 char strnum
[FMT_ULONG
];
38 void syntaxerror(const char *why
)
40 strnum
[fmt_ulong(strnum
,linenum
)] = 0;
41 strerr_die4x(111,FATAL
,"unable to parse data line ",strnum
,why
);
43 void die_datatmp(void)
45 strerr_die2sys(111,FATAL
,"unable to create data.tmp: ");
58 fd
= open_read("data");
59 if (fd
== -1) strerr_die2sys(111,FATAL
,"unable to open data: ");
60 buffer_init(&b
,buffer_unixread
,fd
,bspace
,sizeof bspace
);
62 fdcdb
= open_trunc("data.tmp");
63 if (fdcdb
== -1) die_datatmp();
64 if (cdb_make_start(&cdb
,fdcdb
) == -1) die_datatmp();
68 if (getln(&b
,&line
,&match
,'\n') == -1)
69 strerr_die2sys(111,FATAL
,"unable to read line: ");
72 ch
= line
.s
[line
.len
- 1];
73 if ((ch
!= ' ') && (ch
!= '\t') && (ch
!= '\n')) break;
76 if (!line
.len
) continue;
80 syntaxerror(": unrecognized leading character");
84 j
= byte_chr(line
.s
+ 1,line
.len
- 1,':');
85 if (j
>= line
.len
- 1) syntaxerror(": missing colon");
86 if (ip4_scan(line
.s
+ 1,ip
) != j
) syntaxerror(": malformed IP address");
87 if (!stralloc_copyb(&tmp
,ip
,4)) nomem();
88 if (!stralloc_catb(&tmp
,line
.s
+ j
+ 2,line
.len
- j
- 2)) nomem();
89 if (cdb_make_add(&cdb
,"",0,tmp
.s
,tmp
.len
) == -1)
92 case '0': case '1': case '2': case '3': case '4':
93 case '5': case '6': case '7': case '8': case '9':
94 if (!stralloc_0(&line
)) nomem();
96 if (!stralloc_copys(&tmp
,"")) nomem();
98 k
= scan_ulong(line
.s
+ j
,&u
);
101 if (!stralloc_catb(&tmp
,&ch
,1)) nomem();
103 if (line
.s
[j
] != '.') break;
106 if (!stralloc_catb(&tmp
,"\0\0\0\0",4)) nomem();
108 if (line
.s
[j
] == '/')
109 scan_ulong(line
.s
+ j
+ 1,&u
);
114 if (!stralloc_catb(&tmp
,&ch
,1)) nomem();
115 if (cdb_make_add(&cdb
,tmp
.s
,tmp
.len
,"",0) == -1)
121 if (cdb_make_finish(&cdb
) == -1) die_datatmp();
122 if (fsync(fdcdb
) == -1) die_datatmp();
123 if (close(fdcdb
) == -1) die_datatmp(); /* NFS stupidity */
124 if (rename("data.tmp","data.cdb") == -1)
125 strerr_die2sys(111,FATAL
,"unable to move data.tmp to data.cdb: ");