iostat - add read/write details to output
[dragonfly.git] / contrib / bind / lib / dns / masterdump.c
1 /*
2  * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id: masterdump.c,v 1.89.128.5.2.1 2009/11/18 23:41:18 marka Exp $ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <stdlib.h>
25
26 #include <isc/event.h>
27 #include <isc/file.h>
28 #include <isc/magic.h>
29 #include <isc/mem.h>
30 #include <isc/print.h>
31 #include <isc/stdio.h>
32 #include <isc/string.h>
33 #include <isc/task.h>
34 #include <isc/time.h>
35 #include <isc/util.h>
36
37 #include <dns/db.h>
38 #include <dns/dbiterator.h>
39 #include <dns/events.h>
40 #include <dns/fixedname.h>
41 #include <dns/lib.h>
42 #include <dns/log.h>
43 #include <dns/master.h>
44 #include <dns/masterdump.h>
45 #include <dns/rdata.h>
46 #include <dns/rdataclass.h>
47 #include <dns/rdataset.h>
48 #include <dns/rdatasetiter.h>
49 #include <dns/rdatatype.h>
50 #include <dns/result.h>
51 #include <dns/time.h>
52 #include <dns/ttl.h>
53
54 #define DNS_DCTX_MAGIC          ISC_MAGIC('D', 'c', 't', 'x')
55 #define DNS_DCTX_VALID(d)       ISC_MAGIC_VALID(d, DNS_DCTX_MAGIC)
56
57 #define RETERR(x) do { \
58         isc_result_t _r = (x); \
59         if (_r != ISC_R_SUCCESS) \
60                 return (_r); \
61         } while (0)
62
63 struct dns_master_style {
64         unsigned int flags;             /* DNS_STYLEFLAG_* */
65         unsigned int ttl_column;
66         unsigned int class_column;
67         unsigned int type_column;
68         unsigned int rdata_column;
69         unsigned int line_length;
70         unsigned int tab_width;
71 };
72
73 /*%
74  * The maximum length of the newline+indentation that is output
75  * when inserting a line break in an RR.  This effectively puts an
76  * upper limits on the value of "rdata_column", because if it is
77  * very large, the tabs and spaces needed to reach it will not fit.
78  */
79 #define DNS_TOTEXT_LINEBREAK_MAXLEN 100
80
81 /*%
82  * Context structure for a masterfile dump in progress.
83  */
84 typedef struct dns_totext_ctx {
85         dns_master_style_t      style;
86         isc_boolean_t           class_printed;
87         char *                  linebreak;
88         char                    linebreak_buf[DNS_TOTEXT_LINEBREAK_MAXLEN];
89         dns_name_t *            origin;
90         dns_name_t *            neworigin;
91         dns_fixedname_t         origin_fixname;
92         isc_uint32_t            current_ttl;
93         isc_boolean_t           current_ttl_valid;
94 } dns_totext_ctx_t;
95
96 LIBDNS_EXTERNAL_DATA const dns_master_style_t
97 dns_master_style_default = {
98         DNS_STYLEFLAG_OMIT_OWNER |
99         DNS_STYLEFLAG_OMIT_CLASS |
100         DNS_STYLEFLAG_REL_OWNER |
101         DNS_STYLEFLAG_REL_DATA |
102         DNS_STYLEFLAG_OMIT_TTL |
103         DNS_STYLEFLAG_TTL |
104         DNS_STYLEFLAG_COMMENT |
105         DNS_STYLEFLAG_MULTILINE,
106         24, 24, 24, 32, 80, 8
107 };
108
109 LIBDNS_EXTERNAL_DATA const dns_master_style_t
110 dns_master_style_full = {
111         DNS_STYLEFLAG_COMMENT,
112         46, 46, 46, 64, 120, 8
113 };
114
115 LIBDNS_EXTERNAL_DATA const dns_master_style_t
116 dns_master_style_explicitttl = {
117         DNS_STYLEFLAG_OMIT_OWNER |
118         DNS_STYLEFLAG_OMIT_CLASS |
119         DNS_STYLEFLAG_REL_OWNER |
120         DNS_STYLEFLAG_REL_DATA |
121         DNS_STYLEFLAG_COMMENT |
122         DNS_STYLEFLAG_MULTILINE,
123         24, 32, 32, 40, 80, 8
124 };
125
126 LIBDNS_EXTERNAL_DATA const dns_master_style_t
127 dns_master_style_cache = {
128         DNS_STYLEFLAG_OMIT_OWNER |
129         DNS_STYLEFLAG_OMIT_CLASS |
130         DNS_STYLEFLAG_MULTILINE |
131         DNS_STYLEFLAG_TRUST |
132         DNS_STYLEFLAG_NCACHE,
133         24, 32, 32, 40, 80, 8
134 };
135
136 LIBDNS_EXTERNAL_DATA const dns_master_style_t
137 dns_master_style_simple = {
138         0,
139         24, 32, 32, 40, 80, 8
140 };
141
142 /*%
143  * A style suitable for dns_rdataset_totext().
144  */
145 LIBDNS_EXTERNAL_DATA const dns_master_style_t
146 dns_master_style_debug = {
147         DNS_STYLEFLAG_REL_OWNER,
148         24, 32, 40, 48, 80, 8
149 };
150
151
152 #define N_SPACES 10
153 static char spaces[N_SPACES+1] = "          ";
154
155 #define N_TABS 10
156 static char tabs[N_TABS+1] = "\t\t\t\t\t\t\t\t\t\t";
157
158 struct dns_dumpctx {
159         unsigned int            magic;
160         isc_mem_t               *mctx;
161         isc_mutex_t             lock;
162         unsigned int            references;
163         isc_boolean_t           canceled;
164         isc_boolean_t           first;
165         isc_boolean_t           do_date;
166         isc_stdtime_t           now;
167         FILE                    *f;
168         dns_db_t                *db;
169         dns_dbversion_t         *version;
170         dns_dbiterator_t        *dbiter;
171         dns_totext_ctx_t        tctx;
172         isc_task_t              *task;
173         dns_dumpdonefunc_t      done;
174         void                    *done_arg;
175         unsigned int            nodes;
176         /* dns_master_dumpinc() */
177         char                    *file;
178         char                    *tmpfile;
179         dns_masterformat_t      format;
180         isc_result_t            (*dumpsets)(isc_mem_t *mctx, dns_name_t *name,
181                                             dns_rdatasetiter_t *rdsiter,
182                                             dns_totext_ctx_t *ctx,
183                                             isc_buffer_t *buffer, FILE *f);
184 };
185
186 #define NXDOMAIN(x) (((x)->attributes & DNS_RDATASETATTR_NXDOMAIN) != 0)
187
188 /*%
189  * Output tabs and spaces to go from column '*current' to
190  * column 'to', and update '*current' to reflect the new
191  * current column.
192  */
193 static isc_result_t
194 indent(unsigned int *current, unsigned int to, int tabwidth,
195        isc_buffer_t *target)
196 {
197         isc_region_t r;
198         unsigned char *p;
199         unsigned int from;
200         int ntabs, nspaces, t;
201
202         from = *current;
203
204         if (to < from + 1)
205                 to = from + 1;
206
207         ntabs = to / tabwidth - from / tabwidth;
208         if (ntabs < 0)
209                 ntabs = 0;
210
211         if (ntabs > 0) {
212                 isc_buffer_availableregion(target, &r);
213                 if (r.length < (unsigned) ntabs)
214                         return (ISC_R_NOSPACE);
215                 p = r.base;
216
217                 t = ntabs;
218                 while (t) {
219                         int n = t;
220                         if (n > N_TABS)
221                                 n = N_TABS;
222                         memcpy(p, tabs, n);
223                         p += n;
224                         t -= n;
225                 }
226                 isc_buffer_add(target, ntabs);
227                 from = (to / tabwidth) * tabwidth;
228         }
229
230         nspaces = to - from;
231         INSIST(nspaces >= 0);
232
233         isc_buffer_availableregion(target, &r);
234         if (r.length < (unsigned) nspaces)
235                 return (ISC_R_NOSPACE);
236         p = r.base;
237
238         t = nspaces;
239         while (t) {
240                 int n = t;
241                 if (n > N_SPACES)
242                         n = N_SPACES;
243                 memcpy(p, spaces, n);
244                 p += n;
245                 t -= n;
246         }
247         isc_buffer_add(target, nspaces);
248
249         *current = to;
250         return (ISC_R_SUCCESS);
251 }
252
253 static isc_result_t
254 totext_ctx_init(const dns_master_style_t *style, dns_totext_ctx_t *ctx) {
255         isc_result_t result;
256
257         REQUIRE(style->tab_width != 0);
258
259         ctx->style = *style;
260         ctx->class_printed = ISC_FALSE;
261
262         dns_fixedname_init(&ctx->origin_fixname);
263
264         /*
265          * Set up the line break string if needed.
266          */
267         if ((ctx->style.flags & DNS_STYLEFLAG_MULTILINE) != 0) {
268                 isc_buffer_t buf;
269                 isc_region_t r;
270                 unsigned int col = 0;
271
272                 isc_buffer_init(&buf, ctx->linebreak_buf,
273                                 sizeof(ctx->linebreak_buf));
274
275                 isc_buffer_availableregion(&buf, &r);
276                 if (r.length < 1)
277                         return (DNS_R_TEXTTOOLONG);
278                 r.base[0] = '\n';
279                 isc_buffer_add(&buf, 1);
280
281                 result = indent(&col, ctx->style.rdata_column,
282                                 ctx->style.tab_width, &buf);
283                 /*
284                  * Do not return ISC_R_NOSPACE if the line break string
285                  * buffer is too small, because that would just make
286                  * dump_rdataset() retry indefinitely with ever
287                  * bigger target buffers.  That's a different buffer,
288                  * so it won't help.  Use DNS_R_TEXTTOOLONG as a substitute.
289                  */
290                 if (result == ISC_R_NOSPACE)
291                         return (DNS_R_TEXTTOOLONG);
292                 if (result != ISC_R_SUCCESS)
293                         return (result);
294
295                 isc_buffer_availableregion(&buf, &r);
296                 if (r.length < 1)
297                         return (DNS_R_TEXTTOOLONG);
298                 r.base[0] = '\0';
299                 isc_buffer_add(&buf, 1);
300                 ctx->linebreak = ctx->linebreak_buf;
301         } else {
302                 ctx->linebreak = NULL;
303         }
304
305         ctx->origin = NULL;
306         ctx->neworigin = NULL;
307         ctx->current_ttl = 0;
308         ctx->current_ttl_valid = ISC_FALSE;
309
310         return (ISC_R_SUCCESS);
311 }
312
313 #define INDENT_TO(col) \
314         do { \
315                  if ((result = indent(&column, ctx->style.col, \
316                                       ctx->style.tab_width, target)) \
317                      != ISC_R_SUCCESS) \
318                             return (result); \
319         } while (0)
320
321
322 static isc_result_t
323 str_totext(const char *source, isc_buffer_t *target) {
324         unsigned int l;
325         isc_region_t region;
326
327         isc_buffer_availableregion(target, &region);
328         l = strlen(source);
329
330         if (l > region.length)
331                 return (ISC_R_NOSPACE);
332
333         memcpy(region.base, source, l);
334         isc_buffer_add(target, l);
335         return (ISC_R_SUCCESS);
336 }
337
338 /*
339  * Convert 'rdataset' to master file text format according to 'ctx',
340  * storing the result in 'target'.  If 'owner_name' is NULL, it
341  * is omitted; otherwise 'owner_name' must be valid and have at least
342  * one label.
343  */
344
345 static isc_result_t
346 rdataset_totext(dns_rdataset_t *rdataset,
347                 dns_name_t *owner_name,
348                 dns_totext_ctx_t *ctx,
349                 isc_boolean_t omit_final_dot,
350                 isc_buffer_t *target)
351 {
352         isc_result_t result;
353         unsigned int column;
354         isc_boolean_t first = ISC_TRUE;
355         isc_uint32_t current_ttl;
356         isc_boolean_t current_ttl_valid;
357         dns_rdatatype_t type;
358
359         REQUIRE(DNS_RDATASET_VALID(rdataset));
360
361         rdataset->attributes |= DNS_RDATASETATTR_LOADORDER;
362         result = dns_rdataset_first(rdataset);
363         REQUIRE(result == ISC_R_SUCCESS);
364
365         current_ttl = ctx->current_ttl;
366         current_ttl_valid = ctx->current_ttl_valid;
367
368         do {
369                 column = 0;
370
371                 /*
372                  * Owner name.
373                  */
374                 if (owner_name != NULL &&
375                     ! ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0 &&
376                        !first))
377                 {
378                         unsigned int name_start = target->used;
379                         RETERR(dns_name_totext(owner_name,
380                                                omit_final_dot,
381                                                target));
382                         column += target->used - name_start;
383                 }
384
385                 /*
386                  * TTL.
387                  */
388                 if ((ctx->style.flags & DNS_STYLEFLAG_NO_TTL) == 0 &&
389                     !((ctx->style.flags & DNS_STYLEFLAG_OMIT_TTL) != 0 &&
390                       current_ttl_valid &&
391                       rdataset->ttl == current_ttl))
392                 {
393                         char ttlbuf[64];
394                         isc_region_t r;
395                         unsigned int length;
396
397                         INDENT_TO(ttl_column);
398                         length = snprintf(ttlbuf, sizeof(ttlbuf), "%u",
399                                           rdataset->ttl);
400                         INSIST(length <= sizeof(ttlbuf));
401                         isc_buffer_availableregion(target, &r);
402                         if (r.length < length)
403                                 return (ISC_R_NOSPACE);
404                         memcpy(r.base, ttlbuf, length);
405                         isc_buffer_add(target, length);
406                         column += length;
407
408                         /*
409                          * If the $TTL directive is not in use, the TTL we
410                          * just printed becomes the default for subsequent RRs.
411                          */
412                         if ((ctx->style.flags & DNS_STYLEFLAG_TTL) == 0) {
413                                 current_ttl = rdataset->ttl;
414                                 current_ttl_valid = ISC_TRUE;
415                         }
416                 }
417
418                 /*
419                  * Class.
420                  */
421                 if ((ctx->style.flags & DNS_STYLEFLAG_NO_CLASS) == 0 &&
422                     ((ctx->style.flags & DNS_STYLEFLAG_OMIT_CLASS) == 0 ||
423                      ctx->class_printed == ISC_FALSE))
424                 {
425                         unsigned int class_start;
426                         INDENT_TO(class_column);
427                         class_start = target->used;
428                         result = dns_rdataclass_totext(rdataset->rdclass,
429                                                        target);
430                         if (result != ISC_R_SUCCESS)
431                                 return (result);
432                         column += (target->used - class_start);
433                 }
434
435                 /*
436                  * Type.
437                  */
438
439                 if (rdataset->type == 0) {
440                         type = rdataset->covers;
441                 } else {
442                         type = rdataset->type;
443                 }
444
445                 {
446                         unsigned int type_start;
447                         INDENT_TO(type_column);
448                         type_start = target->used;
449                         if (rdataset->type == 0)
450                                 RETERR(str_totext("\\-", target));
451                         result = dns_rdatatype_totext(type, target);
452                         if (result != ISC_R_SUCCESS)
453                                 return (result);
454                         column += (target->used - type_start);
455                 }
456
457                 /*
458                  * Rdata.
459                  */
460                 INDENT_TO(rdata_column);
461                 if (rdataset->type == 0) {
462                         if (NXDOMAIN(rdataset))
463                                 RETERR(str_totext(";-$NXDOMAIN\n", target));
464                         else
465                                 RETERR(str_totext(";-$NXRRSET\n", target));
466                 } else {
467                         dns_rdata_t rdata = DNS_RDATA_INIT;
468                         isc_region_t r;
469
470                         dns_rdataset_current(rdataset, &rdata);
471
472                         RETERR(dns_rdata_tofmttext(&rdata,
473                                                    ctx->origin,
474                                                    ctx->style.flags,
475                                                    ctx->style.line_length -
476                                                        ctx->style.rdata_column,
477                                                    ctx->linebreak,
478                                                    target));
479
480                         isc_buffer_availableregion(target, &r);
481                         if (r.length < 1)
482                                 return (ISC_R_NOSPACE);
483                         r.base[0] = '\n';
484                         isc_buffer_add(target, 1);
485                 }
486
487                 first = ISC_FALSE;
488                 result = dns_rdataset_next(rdataset);
489         } while (result == ISC_R_SUCCESS);
490
491         if (result != ISC_R_NOMORE)
492                 return (result);
493
494         /*
495          * Update the ctx state to reflect what we just printed.
496          * This is done last, only when we are sure we will return
497          * success, because this function may be called multiple
498          * times with increasing buffer sizes until it succeeds,
499          * and failed attempts must not update the state prematurely.
500          */
501         ctx->class_printed = ISC_TRUE;
502         ctx->current_ttl= current_ttl;
503         ctx->current_ttl_valid = current_ttl_valid;
504
505         return (ISC_R_SUCCESS);
506 }
507
508 /*
509  * Print the name, type, and class of an empty rdataset,
510  * such as those used to represent the question section
511  * of a DNS message.
512  */
513 static isc_result_t
514 question_totext(dns_rdataset_t *rdataset,
515                 dns_name_t *owner_name,
516                 dns_totext_ctx_t *ctx,
517                 isc_boolean_t omit_final_dot,
518                 isc_buffer_t *target)
519 {
520         unsigned int column;
521         isc_result_t result;
522         isc_region_t r;
523
524         REQUIRE(DNS_RDATASET_VALID(rdataset));
525         result = dns_rdataset_first(rdataset);
526         REQUIRE(result == ISC_R_NOMORE);
527
528         column = 0;
529
530         /* Owner name */
531         {
532                 unsigned int name_start = target->used;
533                 RETERR(dns_name_totext(owner_name,
534                                        omit_final_dot,
535                                        target));
536                 column += target->used - name_start;
537         }
538
539         /* Class */
540         {
541                 unsigned int class_start;
542                 INDENT_TO(class_column);
543                 class_start = target->used;
544                 result = dns_rdataclass_totext(rdataset->rdclass, target);
545                 if (result != ISC_R_SUCCESS)
546                         return (result);
547                 column += (target->used - class_start);
548         }
549
550         /* Type */
551         {
552                 unsigned int type_start;
553                 INDENT_TO(type_column);
554                 type_start = target->used;
555                 result = dns_rdatatype_totext(rdataset->type, target);
556                 if (result != ISC_R_SUCCESS)
557                         return (result);
558                 column += (target->used - type_start);
559         }
560
561         isc_buffer_availableregion(target, &r);
562         if (r.length < 1)
563                 return (ISC_R_NOSPACE);
564         r.base[0] = '\n';
565         isc_buffer_add(target, 1);
566
567         return (ISC_R_SUCCESS);
568 }
569
570 isc_result_t
571 dns_rdataset_totext(dns_rdataset_t *rdataset,
572                     dns_name_t *owner_name,
573                     isc_boolean_t omit_final_dot,
574                     isc_boolean_t question,
575                     isc_buffer_t *target)
576 {
577         dns_totext_ctx_t ctx;
578         isc_result_t result;
579         result = totext_ctx_init(&dns_master_style_debug, &ctx);
580         if (result != ISC_R_SUCCESS) {
581                 UNEXPECTED_ERROR(__FILE__, __LINE__,
582                                  "could not set master file style");
583                 return (ISC_R_UNEXPECTED);
584         }
585
586         /*
587          * The caller might want to give us an empty owner
588          * name (e.g. if they are outputting into a master
589          * file and this rdataset has the same name as the
590          * previous one.)
591          */
592         if (dns_name_countlabels(owner_name) == 0)
593                 owner_name = NULL;
594
595         if (question)
596                 return (question_totext(rdataset, owner_name, &ctx,
597                                         omit_final_dot, target));
598         else
599                 return (rdataset_totext(rdataset, owner_name, &ctx,
600                                         omit_final_dot, target));
601 }
602
603 isc_result_t
604 dns_master_rdatasettotext(dns_name_t *owner_name,
605                           dns_rdataset_t *rdataset,
606                           const dns_master_style_t *style,
607                           isc_buffer_t *target)
608 {
609         dns_totext_ctx_t ctx;
610         isc_result_t result;
611         result = totext_ctx_init(style, &ctx);
612         if (result != ISC_R_SUCCESS) {
613                 UNEXPECTED_ERROR(__FILE__, __LINE__,
614                                  "could not set master file style");
615                 return (ISC_R_UNEXPECTED);
616         }
617
618         return (rdataset_totext(rdataset, owner_name, &ctx,
619                                 ISC_FALSE, target));
620 }
621
622 isc_result_t
623 dns_master_questiontotext(dns_name_t *owner_name,
624                           dns_rdataset_t *rdataset,
625                           const dns_master_style_t *style,
626                           isc_buffer_t *target)
627 {
628         dns_totext_ctx_t ctx;
629         isc_result_t result;
630         result = totext_ctx_init(style, &ctx);
631         if (result != ISC_R_SUCCESS) {
632                 UNEXPECTED_ERROR(__FILE__, __LINE__,
633                                  "could not set master file style");
634                 return (ISC_R_UNEXPECTED);
635         }
636
637         return (question_totext(rdataset, owner_name, &ctx,
638                                 ISC_FALSE, target));
639 }
640
641 /*
642  * Print an rdataset.  'buffer' is a scratch buffer, which must have been
643  * dynamically allocated by the caller.  It must be large enough to
644  * hold the result from dns_ttl_totext().  If more than that is needed,
645  * the buffer will be grown automatically.
646  */
647
648 static isc_result_t
649 dump_rdataset(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
650               dns_totext_ctx_t *ctx,
651               isc_buffer_t *buffer, FILE *f)
652 {
653         isc_region_t r;
654         isc_result_t result;
655
656         REQUIRE(buffer->length > 0);
657
658         /*
659          * Output a $TTL directive if needed.
660          */
661
662         if ((ctx->style.flags & DNS_STYLEFLAG_TTL) != 0) {
663                 if (ctx->current_ttl_valid == ISC_FALSE ||
664                     ctx->current_ttl != rdataset->ttl)
665                 {
666                         if ((ctx->style.flags & DNS_STYLEFLAG_COMMENT) != 0)
667                         {
668                                 isc_buffer_clear(buffer);
669                                 result = dns_ttl_totext(rdataset->ttl,
670                                                         ISC_TRUE, buffer);
671                                 INSIST(result == ISC_R_SUCCESS);
672                                 isc_buffer_usedregion(buffer, &r);
673                                 fprintf(f, "$TTL %u\t; %.*s\n", rdataset->ttl,
674                                         (int) r.length, (char *) r.base);
675                         } else {
676                                 fprintf(f, "$TTL %u\n", rdataset->ttl);
677                         }
678                         ctx->current_ttl = rdataset->ttl;
679                         ctx->current_ttl_valid = ISC_TRUE;
680                 }
681         }
682
683         isc_buffer_clear(buffer);
684
685         /*
686          * Generate the text representation of the rdataset into
687          * the buffer.  If the buffer is too small, grow it.
688          */
689         for (;;) {
690                 int newlength;
691                 void *newmem;
692                 result = rdataset_totext(rdataset, name, ctx,
693                                          ISC_FALSE, buffer);
694                 if (result != ISC_R_NOSPACE)
695                         break;
696
697                 newlength = buffer->length * 2;
698                 newmem = isc_mem_get(mctx, newlength);
699                 if (newmem == NULL)
700                         return (ISC_R_NOMEMORY);
701                 isc_mem_put(mctx, buffer->base, buffer->length);
702                 isc_buffer_init(buffer, newmem, newlength);
703         }
704         if (result != ISC_R_SUCCESS)
705                 return (result);
706
707         /*
708          * Write the buffer contents to the master file.
709          */
710         isc_buffer_usedregion(buffer, &r);
711         result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
712
713         if (result != ISC_R_SUCCESS) {
714                 UNEXPECTED_ERROR(__FILE__, __LINE__,
715                                  "master file write failed: %s",
716                                  isc_result_totext(result));
717                 return (result);
718         }
719
720         return (ISC_R_SUCCESS);
721 }
722
723 /*
724  * Define the order in which rdatasets should be printed in zone
725  * files.  We will print SOA and NS records before others, SIGs
726  * immediately following the things they sign, and order everything
727  * else by RR number.  This is all just for aesthetics and
728  * compatibility with buggy software that expects the SOA to be first;
729  * the DNS specifications allow any order.
730  */
731
732 static int
733 dump_order(const dns_rdataset_t *rds) {
734         int t;
735         int sig;
736         if (rds->type == dns_rdatatype_rrsig) {
737                 t = rds->covers;
738                 sig = 1;
739         } else {
740                 t = rds->type;
741                 sig = 0;
742         }
743         switch (t) {
744         case dns_rdatatype_soa:
745                 t = 0;
746                 break;
747         case dns_rdatatype_ns:
748                 t = 1;
749                 break;
750         default:
751                 t += 2;
752                 break;
753         }
754         return (t << 1) + sig;
755 }
756
757 static int
758 dump_order_compare(const void *a, const void *b) {
759         return (dump_order(*((const dns_rdataset_t * const *) a)) -
760                 dump_order(*((const dns_rdataset_t * const *) b)));
761 }
762
763 /*
764  * Dump all the rdatasets of a domain name to a master file.  We make
765  * a "best effort" attempt to sort the RRsets in a nice order, but if
766  * there are more than MAXSORT RRsets, we punt and only sort them in
767  * groups of MAXSORT.  This is not expected to ever happen in practice
768  * since much less than 64 RR types have been registered with the
769  * IANA, so far, and the output will be correct (though not
770  * aesthetically pleasing) even if it does happen.
771  */
772
773 #define MAXSORT 64
774
775 static const char *trustnames[] = {
776         "none",
777         "pending-additional",
778         "pending-answer",
779         "additional",
780         "glue",
781         "answer",
782         "authauthority",
783         "authanswer",
784         "secure",
785         "local" /* aka ultimate */
786 };
787
788 static isc_result_t
789 dump_rdatasets_text(isc_mem_t *mctx, dns_name_t *name,
790                     dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
791                     isc_buffer_t *buffer, FILE *f)
792 {
793         isc_result_t itresult, dumpresult;
794         isc_region_t r;
795         dns_rdataset_t rdatasets[MAXSORT];
796         dns_rdataset_t *sorted[MAXSORT];
797         int i, n;
798
799         itresult = dns_rdatasetiter_first(rdsiter);
800         dumpresult = ISC_R_SUCCESS;
801
802         if (itresult == ISC_R_SUCCESS && ctx->neworigin != NULL) {
803                 isc_buffer_clear(buffer);
804                 itresult = dns_name_totext(ctx->neworigin, ISC_FALSE, buffer);
805                 RUNTIME_CHECK(itresult == ISC_R_SUCCESS);
806                 isc_buffer_usedregion(buffer, &r);
807                 fprintf(f, "$ORIGIN %.*s\n", (int) r.length, (char *) r.base);
808                 ctx->neworigin = NULL;
809         }
810
811  again:
812         for (i = 0;
813              itresult == ISC_R_SUCCESS && i < MAXSORT;
814              itresult = dns_rdatasetiter_next(rdsiter), i++) {
815                 dns_rdataset_init(&rdatasets[i]);
816                 dns_rdatasetiter_current(rdsiter, &rdatasets[i]);
817                 sorted[i] = &rdatasets[i];
818         }
819         n = i;
820         INSIST(n <= MAXSORT);
821
822         qsort(sorted, n, sizeof(sorted[0]), dump_order_compare);
823
824         for (i = 0; i < n; i++) {
825                 dns_rdataset_t *rds = sorted[i];
826                 if (ctx->style.flags & DNS_STYLEFLAG_TRUST) {
827                         unsigned int trust = rds->trust;
828                         INSIST(trust < (sizeof(trustnames) /
829                                         sizeof(trustnames[0])));
830                         fprintf(f, "; %s\n", trustnames[trust]);
831                 }
832                 if (rds->type == 0 &&
833                     (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
834                         /* Omit negative cache entries */
835                 } else {
836                         isc_result_t result =
837                                 dump_rdataset(mctx, name, rds, ctx,
838                                                buffer, f);
839                         if (result != ISC_R_SUCCESS)
840                                 dumpresult = result;
841                         if ((ctx->style.flags & DNS_STYLEFLAG_OMIT_OWNER) != 0)
842                                 name = NULL;
843                 }
844                 dns_rdataset_disassociate(rds);
845         }
846
847         if (dumpresult != ISC_R_SUCCESS)
848                 return (dumpresult);
849
850         /*
851          * If we got more data than could be sorted at once,
852          * go handle the rest.
853          */
854         if (itresult == ISC_R_SUCCESS)
855                 goto again;
856
857         if (itresult == ISC_R_NOMORE)
858                 itresult = ISC_R_SUCCESS;
859
860         return (itresult);
861 }
862
863 /*
864  * Dump given RRsets in the "raw" format.
865  */
866 static isc_result_t
867 dump_rdataset_raw(isc_mem_t *mctx, dns_name_t *name, dns_rdataset_t *rdataset,
868                   isc_buffer_t *buffer, FILE *f)
869 {
870         isc_result_t result;
871         isc_uint32_t totallen;
872         isc_uint16_t dlen;
873         isc_region_t r, r_hdr;
874
875         REQUIRE(buffer->length > 0);
876         REQUIRE(DNS_RDATASET_VALID(rdataset));
877
878  restart:
879         totallen = 0;
880         result = dns_rdataset_first(rdataset);
881         REQUIRE(result == ISC_R_SUCCESS);
882
883         isc_buffer_clear(buffer);
884
885         /*
886          * Common header and owner name (length followed by name)
887          * These fields should be in a moderate length, so we assume we
888          * can store all of them in the initial buffer.
889          */
890         isc_buffer_availableregion(buffer, &r_hdr);
891         INSIST(r_hdr.length >= sizeof(dns_masterrawrdataset_t));
892         isc_buffer_putuint32(buffer, totallen); /* XXX: leave space */
893         isc_buffer_putuint16(buffer, rdataset->rdclass); /* 16-bit class */
894         isc_buffer_putuint16(buffer, rdataset->type); /* 16-bit type */
895         isc_buffer_putuint16(buffer, rdataset->covers); /* same as type */
896         isc_buffer_putuint32(buffer, rdataset->ttl); /* 32-bit TTL */
897         isc_buffer_putuint32(buffer, dns_rdataset_count(rdataset));
898         totallen = isc_buffer_usedlength(buffer);
899         INSIST(totallen <= sizeof(dns_masterrawrdataset_t));
900
901         dns_name_toregion(name, &r);
902         INSIST(isc_buffer_availablelength(buffer) >=
903                (sizeof(dlen) + r.length));
904         dlen = (isc_uint16_t)r.length;
905         isc_buffer_putuint16(buffer, dlen);
906         isc_buffer_copyregion(buffer, &r);
907         totallen += sizeof(dlen) + r.length;
908
909         do {
910                 dns_rdata_t rdata = DNS_RDATA_INIT;
911                 isc_region_t r;
912
913                 dns_rdataset_current(rdataset, &rdata);
914                 dns_rdata_toregion(&rdata, &r);
915                 INSIST(r.length <= 0xffffU);
916                 dlen = (isc_uint16_t)r.length;
917
918                 /*
919                  * Copy the rdata into the buffer.  If the buffer is too small,
920                  * grow it.  This should be rare, so we'll simply restart the
921                  * entire procedure (or should we copy the old data and
922                  * continue?).
923                  */
924                 if (isc_buffer_availablelength(buffer) <
925                                                  sizeof(dlen) + r.length) {
926                         int newlength;
927                         void *newmem;
928
929                         newlength = buffer->length * 2;
930                         newmem = isc_mem_get(mctx, newlength);
931                         if (newmem == NULL)
932                                 return (ISC_R_NOMEMORY);
933                         isc_mem_put(mctx, buffer->base, buffer->length);
934                         isc_buffer_init(buffer, newmem, newlength);
935                         goto restart;
936                 }
937                 isc_buffer_putuint16(buffer, dlen);
938                 isc_buffer_copyregion(buffer, &r);
939                 totallen += sizeof(dlen) + r.length;
940
941                 result = dns_rdataset_next(rdataset);
942         } while (result == ISC_R_SUCCESS);
943
944         if (result != ISC_R_NOMORE)
945                 return (result);
946
947         /*
948          * Fill in the total length field.
949          * XXX: this is a bit tricky.  Since we have already "used" the space
950          * for the total length in the buffer, we first remember the entire
951          * buffer length in the region, "rewind", and then write the value.
952          */
953         isc_buffer_usedregion(buffer, &r);
954         isc_buffer_clear(buffer);
955         isc_buffer_putuint32(buffer, totallen);
956         INSIST(isc_buffer_usedlength(buffer) < totallen);
957
958         /*
959          * Write the buffer contents to the raw master file.
960          */
961         result = isc_stdio_write(r.base, 1, (size_t)r.length, f, NULL);
962
963         if (result != ISC_R_SUCCESS) {
964                 UNEXPECTED_ERROR(__FILE__, __LINE__,
965                                  "raw master file write failed: %s",
966                                  isc_result_totext(result));
967                 return (result);
968         }
969
970         return (result);
971 }
972
973 static isc_result_t
974 dump_rdatasets_raw(isc_mem_t *mctx, dns_name_t *name,
975                    dns_rdatasetiter_t *rdsiter, dns_totext_ctx_t *ctx,
976                    isc_buffer_t *buffer, FILE *f)
977 {
978         isc_result_t result;
979         dns_rdataset_t rdataset;
980
981         for (result = dns_rdatasetiter_first(rdsiter);
982              result == ISC_R_SUCCESS;
983              result = dns_rdatasetiter_next(rdsiter)) {
984
985                 dns_rdataset_init(&rdataset);
986                 dns_rdatasetiter_current(rdsiter, &rdataset);
987
988                 if (rdataset.type == 0 &&
989                     (ctx->style.flags & DNS_STYLEFLAG_NCACHE) == 0) {
990                         /* Omit negative cache entries */
991                 } else {
992                         result = dump_rdataset_raw(mctx, name, &rdataset,
993                                                    buffer, f);
994                 }
995                 dns_rdataset_disassociate(&rdataset);
996         }
997
998         if (result == ISC_R_NOMORE)
999                 result = ISC_R_SUCCESS;
1000
1001         return (result);
1002 }
1003
1004 /*
1005  * Initial size of text conversion buffer.  The buffer is used
1006  * for several purposes: converting origin names, rdatasets,
1007  * $DATE timestamps, and comment strings for $TTL directives.
1008  *
1009  * When converting rdatasets, it is dynamically resized, but
1010  * when converting origins, timestamps, etc it is not.  Therefore,
1011  * the initial size must large enough to hold the longest possible
1012  * text representation of any domain name (for $ORIGIN).
1013  */
1014 static const int initial_buffer_length = 1200;
1015
1016 static isc_result_t
1017 dumptostreaminc(dns_dumpctx_t *dctx);
1018
1019 static void
1020 dumpctx_destroy(dns_dumpctx_t *dctx) {
1021
1022         dctx->magic = 0;
1023         DESTROYLOCK(&dctx->lock);
1024         dns_dbiterator_destroy(&dctx->dbiter);
1025         if (dctx->version != NULL)
1026                 dns_db_closeversion(dctx->db, &dctx->version, ISC_FALSE);
1027         dns_db_detach(&dctx->db);
1028         if (dctx->task != NULL)
1029                 isc_task_detach(&dctx->task);
1030         if (dctx->file != NULL)
1031                 isc_mem_free(dctx->mctx, dctx->file);
1032         if (dctx->tmpfile != NULL)
1033                 isc_mem_free(dctx->mctx, dctx->tmpfile);
1034         isc_mem_putanddetach(&dctx->mctx, dctx, sizeof(*dctx));
1035 }
1036
1037 void
1038 dns_dumpctx_attach(dns_dumpctx_t *source, dns_dumpctx_t **target) {
1039
1040         REQUIRE(DNS_DCTX_VALID(source));
1041         REQUIRE(target != NULL && *target == NULL);
1042
1043         LOCK(&source->lock);
1044         INSIST(source->references > 0);
1045         source->references++;
1046         INSIST(source->references != 0);        /* Overflow? */
1047         UNLOCK(&source->lock);
1048
1049         *target = source;
1050 }
1051
1052 void
1053 dns_dumpctx_detach(dns_dumpctx_t **dctxp) {
1054         dns_dumpctx_t *dctx;
1055         isc_boolean_t need_destroy = ISC_FALSE;
1056
1057         REQUIRE(dctxp != NULL);
1058         dctx = *dctxp;
1059         REQUIRE(DNS_DCTX_VALID(dctx));
1060
1061         *dctxp = NULL;
1062
1063         LOCK(&dctx->lock);
1064         INSIST(dctx->references != 0);
1065         dctx->references--;
1066         if (dctx->references == 0)
1067                 need_destroy = ISC_TRUE;
1068         UNLOCK(&dctx->lock);
1069         if (need_destroy)
1070                 dumpctx_destroy(dctx);
1071 }
1072
1073 dns_dbversion_t *
1074 dns_dumpctx_version(dns_dumpctx_t *dctx) {
1075         REQUIRE(DNS_DCTX_VALID(dctx));
1076         return (dctx->version);
1077 }
1078
1079 dns_db_t *
1080 dns_dumpctx_db(dns_dumpctx_t *dctx) {
1081         REQUIRE(DNS_DCTX_VALID(dctx));
1082         return (dctx->db);
1083 }
1084
1085 void
1086 dns_dumpctx_cancel(dns_dumpctx_t *dctx) {
1087         REQUIRE(DNS_DCTX_VALID(dctx));
1088
1089         LOCK(&dctx->lock);
1090         dctx->canceled = ISC_TRUE;
1091         UNLOCK(&dctx->lock);
1092 }
1093
1094 static isc_result_t
1095 closeandrename(FILE *f, isc_result_t result, const char *temp, const char *file)
1096 {
1097         isc_result_t tresult;
1098         isc_boolean_t logit = ISC_TF(result == ISC_R_SUCCESS);
1099
1100         if (result == ISC_R_SUCCESS)
1101                 result = isc_stdio_sync(f);
1102         if (result != ISC_R_SUCCESS && logit) {
1103                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1104                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1105                               "dumping master file: %s: fsync: %s",
1106                               temp, isc_result_totext(result));
1107                 logit = ISC_FALSE;
1108         }
1109         tresult = isc_stdio_close(f);
1110         if (result == ISC_R_SUCCESS)
1111                 result = tresult;
1112         if (result != ISC_R_SUCCESS && logit) {
1113                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1114                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1115                               "dumping master file: %s: fclose: %s",
1116                               temp, isc_result_totext(result));
1117                 logit = ISC_FALSE;
1118         }
1119         if (result == ISC_R_SUCCESS)
1120                 result = isc_file_rename(temp, file);
1121         else
1122                 (void)isc_file_remove(temp);
1123         if (result != ISC_R_SUCCESS && logit) {
1124                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1125                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1126                               "dumping master file: rename: %s: %s",
1127                               file, isc_result_totext(result));
1128         }
1129         return (result);
1130 }
1131
1132 static void
1133 dump_quantum(isc_task_t *task, isc_event_t *event) {
1134         isc_result_t result;
1135         isc_result_t tresult;
1136         dns_dumpctx_t *dctx;
1137
1138         REQUIRE(event != NULL);
1139         dctx = event->ev_arg;
1140         REQUIRE(DNS_DCTX_VALID(dctx));
1141         if (dctx->canceled)
1142                 result = ISC_R_CANCELED;
1143         else
1144                 result = dumptostreaminc(dctx);
1145         if (result == DNS_R_CONTINUE) {
1146                 event->ev_arg = dctx;
1147                 isc_task_send(task, &event);
1148                 return;
1149         }
1150
1151         if (dctx->file != NULL) {
1152                 tresult = closeandrename(dctx->f, result,
1153                                          dctx->tmpfile, dctx->file);
1154                 if (tresult != ISC_R_SUCCESS && result == ISC_R_SUCCESS)
1155                         result = tresult;
1156         }
1157         (dctx->done)(dctx->done_arg, result);
1158         isc_event_free(&event);
1159         dns_dumpctx_detach(&dctx);
1160 }
1161
1162 static isc_result_t
1163 task_send(dns_dumpctx_t *dctx) {
1164         isc_event_t *event;
1165
1166         event = isc_event_allocate(dctx->mctx, NULL, DNS_EVENT_DUMPQUANTUM,
1167                                    dump_quantum, dctx, sizeof(*event));
1168         if (event == NULL)
1169                 return (ISC_R_NOMEMORY);
1170         isc_task_send(dctx->task, &event);
1171         return (ISC_R_SUCCESS);
1172 }
1173
1174 static isc_result_t
1175 dumpctx_create(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1176                const dns_master_style_t *style, FILE *f, dns_dumpctx_t **dctxp,
1177                dns_masterformat_t format)
1178 {
1179         dns_dumpctx_t *dctx;
1180         isc_result_t result;
1181         isc_boolean_t relative;
1182
1183         dctx = isc_mem_get(mctx, sizeof(*dctx));
1184         if (dctx == NULL)
1185                 return (ISC_R_NOMEMORY);
1186
1187         dctx->mctx = NULL;
1188         dctx->f = f;
1189         dctx->dbiter = NULL;
1190         dctx->db = NULL;
1191         dctx->version = NULL;
1192         dctx->done = NULL;
1193         dctx->done_arg = NULL;
1194         dctx->task = NULL;
1195         dctx->nodes = 0;
1196         dctx->first = ISC_TRUE;
1197         dctx->canceled = ISC_FALSE;
1198         dctx->file = NULL;
1199         dctx->tmpfile = NULL;
1200         dctx->format = format;
1201
1202         switch (format) {
1203         case dns_masterformat_text:
1204                 dctx->dumpsets = dump_rdatasets_text;
1205                 break;
1206         case dns_masterformat_raw:
1207                 dctx->dumpsets = dump_rdatasets_raw;
1208                 break;
1209         default:
1210                 INSIST(0);
1211                 break;
1212         }
1213
1214         result = totext_ctx_init(style, &dctx->tctx);
1215         if (result != ISC_R_SUCCESS) {
1216                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1217                                  "could not set master file style");
1218                 goto cleanup;
1219         }
1220
1221         isc_stdtime_get(&dctx->now);
1222         dns_db_attach(db, &dctx->db);
1223
1224         dctx->do_date = dns_db_iscache(dctx->db);
1225
1226         if (dctx->format == dns_masterformat_text &&
1227             (dctx->tctx.style.flags & DNS_STYLEFLAG_REL_OWNER) != 0) {
1228                 relative = ISC_TRUE;
1229         } else
1230                 relative = ISC_FALSE;
1231         result = dns_db_createiterator(dctx->db, relative, &dctx->dbiter);
1232         if (result != ISC_R_SUCCESS)
1233                 goto cleanup;
1234
1235         result = isc_mutex_init(&dctx->lock);
1236         if (result != ISC_R_SUCCESS)
1237                 goto cleanup;
1238         if (version != NULL)
1239                 dns_db_attachversion(dctx->db, version, &dctx->version);
1240         else if (!dns_db_iscache(db))
1241                 dns_db_currentversion(dctx->db, &dctx->version);
1242         isc_mem_attach(mctx, &dctx->mctx);
1243         dctx->references = 1;
1244         dctx->magic = DNS_DCTX_MAGIC;
1245         *dctxp = dctx;
1246         return (ISC_R_SUCCESS);
1247
1248  cleanup:
1249         if (dctx->dbiter != NULL)
1250                 dns_dbiterator_destroy(&dctx->dbiter);
1251         if (dctx->db != NULL)
1252                 dns_db_detach(&dctx->db);
1253         if (dctx != NULL)
1254                 isc_mem_put(mctx, dctx, sizeof(*dctx));
1255         return (result);
1256 }
1257
1258 static isc_result_t
1259 dumptostreaminc(dns_dumpctx_t *dctx) {
1260         isc_result_t result;
1261         isc_buffer_t buffer;
1262         char *bufmem;
1263         isc_region_t r;
1264         dns_name_t *name;
1265         dns_fixedname_t fixname;
1266         unsigned int nodes;
1267         dns_masterrawheader_t rawheader;
1268         isc_uint32_t now32;
1269         isc_time_t start;
1270
1271         bufmem = isc_mem_get(dctx->mctx, initial_buffer_length);
1272         if (bufmem == NULL)
1273                 return (ISC_R_NOMEMORY);
1274
1275         isc_buffer_init(&buffer, bufmem, initial_buffer_length);
1276
1277         dns_fixedname_init(&fixname);
1278         name = dns_fixedname_name(&fixname);
1279
1280         if (dctx->first) {
1281                 switch (dctx->format) {
1282                 case dns_masterformat_text:
1283                         /*
1284                          * If the database has cache semantics, output an
1285                          * RFC2540 $DATE directive so that the TTLs can be
1286                          * adjusted when it is reloaded.  For zones it is not
1287                          * really needed, and it would make the file
1288                          * incompatible with pre-RFC2540 software, so we omit
1289                          * it in the zone case.
1290                          */
1291                         if (dctx->do_date) {
1292                                 result = dns_time32_totext(dctx->now, &buffer);
1293                                 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1294                                 isc_buffer_usedregion(&buffer, &r);
1295                                 fprintf(dctx->f, "$DATE %.*s\n",
1296                                         (int) r.length, (char *) r.base);
1297                         }
1298                         break;
1299                 case dns_masterformat_raw:
1300                         r.base = (unsigned char *)&rawheader;
1301                         r.length = sizeof(rawheader);
1302                         isc_buffer_region(&buffer, &r);
1303                         isc_buffer_putuint32(&buffer, dns_masterformat_raw);
1304                         isc_buffer_putuint32(&buffer, DNS_RAWFORMAT_VERSION);
1305                         if (sizeof(now32) != sizeof(dctx->now)) {
1306                                 /*
1307                                  * We assume isc_stdtime_t is a 32-bit integer,
1308                                  * which should be the case on most cases.
1309                                  * If it turns out to be uncommon, we'll need
1310                                  * to bump the version number and revise the
1311                                  * header format.
1312                                  */
1313                                 isc_log_write(dns_lctx,
1314                                               ISC_LOGCATEGORY_GENERAL,
1315                                               DNS_LOGMODULE_MASTERDUMP,
1316                                               ISC_LOG_INFO,
1317                                               "dumping master file in raw "
1318                                               "format: stdtime is not 32bits");
1319                                 now32 = 0;
1320                         } else
1321                                 now32 = dctx->now;
1322                         isc_buffer_putuint32(&buffer, now32);
1323                         INSIST(isc_buffer_usedlength(&buffer) <=
1324                                sizeof(rawheader));
1325                         result = isc_stdio_write(buffer.base, 1,
1326                                                  isc_buffer_usedlength(&buffer),
1327                                                  dctx->f, NULL);
1328                         if (result != ISC_R_SUCCESS)
1329                                 return (result);
1330                         isc_buffer_clear(&buffer);
1331                         break;
1332                 default:
1333                         INSIST(0);
1334                 }
1335
1336                 result = dns_dbiterator_first(dctx->dbiter);
1337                 dctx->first = ISC_FALSE;
1338         } else
1339                 result = ISC_R_SUCCESS;
1340
1341         nodes = dctx->nodes;
1342         isc_time_now(&start);
1343         while (result == ISC_R_SUCCESS && (dctx->nodes == 0 || nodes--)) {
1344                 dns_rdatasetiter_t *rdsiter = NULL;
1345                 dns_dbnode_t *node = NULL;
1346
1347                 result = dns_dbiterator_current(dctx->dbiter, &node, name);
1348                 if (result != ISC_R_SUCCESS && result != DNS_R_NEWORIGIN)
1349                         break;
1350                 if (result == DNS_R_NEWORIGIN) {
1351                         dns_name_t *origin =
1352                                 dns_fixedname_name(&dctx->tctx.origin_fixname);
1353                         result = dns_dbiterator_origin(dctx->dbiter, origin);
1354                         RUNTIME_CHECK(result == ISC_R_SUCCESS);
1355                         if ((dctx->tctx.style.flags & DNS_STYLEFLAG_REL_DATA) != 0)
1356                                 dctx->tctx.origin = origin;
1357                         dctx->tctx.neworigin = origin;
1358                 }
1359                 result = dns_db_allrdatasets(dctx->db, node, dctx->version,
1360                                              dctx->now, &rdsiter);
1361                 if (result != ISC_R_SUCCESS) {
1362                         dns_db_detachnode(dctx->db, &node);
1363                         goto fail;
1364                 }
1365                 result = (dctx->dumpsets)(dctx->mctx, name, rdsiter,
1366                                           &dctx->tctx, &buffer, dctx->f);
1367                 dns_rdatasetiter_destroy(&rdsiter);
1368                 if (result != ISC_R_SUCCESS) {
1369                         dns_db_detachnode(dctx->db, &node);
1370                         goto fail;
1371                 }
1372                 dns_db_detachnode(dctx->db, &node);
1373                 result = dns_dbiterator_next(dctx->dbiter);
1374         }
1375
1376         /*
1377          * Work out how many nodes can be written in the time between
1378          * two requests to the nameserver.  Smooth the resulting number and
1379          * use it as a estimate for the number of nodes to be written in the
1380          * next iteration.
1381          */
1382         if (dctx->nodes != 0 && result == ISC_R_SUCCESS) {
1383                 unsigned int pps = dns_pps;     /* packets per second */
1384                 unsigned int interval;
1385                 isc_uint64_t usecs;
1386                 isc_time_t end;
1387
1388                 isc_time_now(&end);
1389                 if (pps < 100)
1390                         pps = 100;
1391                 interval = 1000000 / pps;       /* interval in usecs */
1392                 if (interval == 0)
1393                         interval = 1;
1394                 usecs = isc_time_microdiff(&end, &start);
1395                 if (usecs == 0) {
1396                         dctx->nodes = dctx->nodes * 2;
1397                         if (dctx->nodes > 1000)
1398                                 dctx->nodes = 1000;
1399                 } else {
1400                         nodes = dctx->nodes * interval;
1401                         nodes /= (unsigned int)usecs;
1402                         if (nodes == 0)
1403                                 nodes = 1;
1404                         else if (nodes > 1000)
1405                                 nodes = 1000;
1406
1407                         /* Smooth and assign. */
1408                         dctx->nodes = (nodes + dctx->nodes * 7) / 8;
1409
1410                         isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1411                                       DNS_LOGMODULE_MASTERDUMP,
1412                                       ISC_LOG_DEBUG(1),
1413                                       "dumptostreaminc(%p) new nodes -> %d\n",
1414                                       dctx, dctx->nodes);
1415                 }
1416                 result = DNS_R_CONTINUE;
1417         } else if (result == ISC_R_NOMORE)
1418                 result = ISC_R_SUCCESS;
1419  fail:
1420         RUNTIME_CHECK(dns_dbiterator_pause(dctx->dbiter) == ISC_R_SUCCESS);
1421         isc_mem_put(dctx->mctx, buffer.base, buffer.length);
1422         return (result);
1423 }
1424
1425 isc_result_t
1426 dns_master_dumptostreaminc(isc_mem_t *mctx, dns_db_t *db,
1427                            dns_dbversion_t *version,
1428                            const dns_master_style_t *style,
1429                            FILE *f, isc_task_t *task,
1430                            dns_dumpdonefunc_t done, void *done_arg,
1431                            dns_dumpctx_t **dctxp)
1432 {
1433         dns_dumpctx_t *dctx = NULL;
1434         isc_result_t result;
1435
1436         REQUIRE(task != NULL);
1437         REQUIRE(f != NULL);
1438         REQUIRE(done != NULL);
1439
1440         result = dumpctx_create(mctx, db, version, style, f, &dctx,
1441                                 dns_masterformat_text);
1442         if (result != ISC_R_SUCCESS)
1443                 return (result);
1444         isc_task_attach(task, &dctx->task);
1445         dctx->done = done;
1446         dctx->done_arg = done_arg;
1447         dctx->nodes = 100;
1448
1449         result = task_send(dctx);
1450         if (result == ISC_R_SUCCESS) {
1451                 dns_dumpctx_attach(dctx, dctxp);
1452                 return (DNS_R_CONTINUE);
1453         }
1454
1455         dns_dumpctx_detach(&dctx);
1456         return (result);
1457 }
1458
1459 /*
1460  * Dump an entire database into a master file.
1461  */
1462 isc_result_t
1463 dns_master_dumptostream(isc_mem_t *mctx, dns_db_t *db,
1464                         dns_dbversion_t *version,
1465                         const dns_master_style_t *style,
1466                         FILE *f)
1467 {
1468         return (dns_master_dumptostream2(mctx, db, version, style,
1469                                          dns_masterformat_text, f));
1470 }
1471
1472 isc_result_t
1473 dns_master_dumptostream2(isc_mem_t *mctx, dns_db_t *db,
1474                          dns_dbversion_t *version,
1475                          const dns_master_style_t *style,
1476                          dns_masterformat_t format, FILE *f)
1477 {
1478         dns_dumpctx_t *dctx = NULL;
1479         isc_result_t result;
1480
1481         result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1482         if (result != ISC_R_SUCCESS)
1483                 return (result);
1484
1485         result = dumptostreaminc(dctx);
1486         INSIST(result != DNS_R_CONTINUE);
1487         dns_dumpctx_detach(&dctx);
1488         return (result);
1489 }
1490
1491 static isc_result_t
1492 opentmp(isc_mem_t *mctx, const char *file, char **tempp, FILE **fp) {
1493         FILE *f = NULL;
1494         isc_result_t result;
1495         char *tempname = NULL;
1496         int tempnamelen;
1497
1498         tempnamelen = strlen(file) + 20;
1499         tempname = isc_mem_allocate(mctx, tempnamelen);
1500         if (tempname == NULL)
1501                 return (ISC_R_NOMEMORY);
1502
1503         result = isc_file_mktemplate(file, tempname, tempnamelen);
1504         if (result != ISC_R_SUCCESS)
1505                 goto cleanup;
1506
1507         result = isc_file_openunique(tempname, &f);
1508         if (result != ISC_R_SUCCESS) {
1509                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1510                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1511                               "dumping master file: %s: open: %s",
1512                               tempname, isc_result_totext(result));
1513                 goto cleanup;
1514         }
1515         *tempp = tempname;
1516         *fp = f;
1517         return (ISC_R_SUCCESS);
1518
1519 cleanup:
1520         isc_mem_free(mctx, tempname);
1521         return (result);
1522 }
1523
1524 isc_result_t
1525 dns_master_dumpinc(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1526                    const dns_master_style_t *style, const char *filename,
1527                    isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
1528                    dns_dumpctx_t **dctxp)
1529 {
1530         return (dns_master_dumpinc2(mctx, db, version, style, filename, task,
1531                                     done, done_arg, dctxp,
1532                                     dns_masterformat_text));
1533 }
1534
1535 isc_result_t
1536 dns_master_dumpinc2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1537                     const dns_master_style_t *style, const char *filename,
1538                     isc_task_t *task, dns_dumpdonefunc_t done, void *done_arg,
1539                     dns_dumpctx_t **dctxp, dns_masterformat_t format)
1540 {
1541         FILE *f = NULL;
1542         isc_result_t result;
1543         char *tempname = NULL;
1544         char *file = NULL;
1545         dns_dumpctx_t *dctx = NULL;
1546
1547         file = isc_mem_strdup(mctx, filename);
1548         if (file == NULL)
1549                 return (ISC_R_NOMEMORY);
1550
1551         result = opentmp(mctx, filename, &tempname, &f);
1552         if (result != ISC_R_SUCCESS)
1553                 goto cleanup;
1554
1555         result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1556         if (result != ISC_R_SUCCESS) {
1557                 (void)isc_stdio_close(f);
1558                 (void)isc_file_remove(tempname);
1559                 goto cleanup;
1560         }
1561
1562         isc_task_attach(task, &dctx->task);
1563         dctx->done = done;
1564         dctx->done_arg = done_arg;
1565         dctx->nodes = 100;
1566         dctx->file = file;
1567         file = NULL;
1568         dctx->tmpfile = tempname;
1569         tempname = NULL;
1570
1571         result = task_send(dctx);
1572         if (result == ISC_R_SUCCESS) {
1573                 dns_dumpctx_attach(dctx, dctxp);
1574                 return (DNS_R_CONTINUE);
1575         }
1576
1577  cleanup:
1578         if (dctx != NULL)
1579                 dns_dumpctx_detach(&dctx);
1580         if (file != NULL)
1581                 isc_mem_free(mctx, file);
1582         if (tempname != NULL)
1583                 isc_mem_free(mctx, tempname);
1584         return (result);
1585 }
1586
1587 isc_result_t
1588 dns_master_dump(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1589                 const dns_master_style_t *style, const char *filename)
1590 {
1591         return (dns_master_dump2(mctx, db, version, style, filename,
1592                                  dns_masterformat_text));
1593 }
1594
1595 isc_result_t
1596 dns_master_dump2(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1597                  const dns_master_style_t *style, const char *filename,
1598                  dns_masterformat_t format)
1599 {
1600         FILE *f = NULL;
1601         isc_result_t result;
1602         char *tempname;
1603         dns_dumpctx_t *dctx = NULL;
1604
1605         result = opentmp(mctx, filename, &tempname, &f);
1606         if (result != ISC_R_SUCCESS)
1607                 return (result);
1608
1609         result = dumpctx_create(mctx, db, version, style, f, &dctx, format);
1610         if (result != ISC_R_SUCCESS)
1611                 goto cleanup;
1612
1613         result = dumptostreaminc(dctx);
1614         INSIST(result != DNS_R_CONTINUE);
1615         dns_dumpctx_detach(&dctx);
1616
1617         result = closeandrename(f, result, tempname, filename);
1618
1619  cleanup:
1620         isc_mem_free(mctx, tempname);
1621         return (result);
1622 }
1623
1624 /*
1625  * Dump a database node into a master file.
1626  * XXX: this function assumes the text format.
1627  */
1628 isc_result_t
1629 dns_master_dumpnodetostream(isc_mem_t *mctx, dns_db_t *db,
1630                             dns_dbversion_t *version,
1631                             dns_dbnode_t *node, dns_name_t *name,
1632                             const dns_master_style_t *style,
1633                             FILE *f)
1634 {
1635         isc_result_t result;
1636         isc_buffer_t buffer;
1637         char *bufmem;
1638         isc_stdtime_t now;
1639         dns_totext_ctx_t ctx;
1640         dns_rdatasetiter_t *rdsiter = NULL;
1641
1642         result = totext_ctx_init(style, &ctx);
1643         if (result != ISC_R_SUCCESS) {
1644                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1645                                  "could not set master file style");
1646                 return (ISC_R_UNEXPECTED);
1647         }
1648
1649         isc_stdtime_get(&now);
1650
1651         bufmem = isc_mem_get(mctx, initial_buffer_length);
1652         if (bufmem == NULL)
1653                 return (ISC_R_NOMEMORY);
1654
1655         isc_buffer_init(&buffer, bufmem, initial_buffer_length);
1656
1657         result = dns_db_allrdatasets(db, node, version, now, &rdsiter);
1658         if (result != ISC_R_SUCCESS)
1659                 goto failure;
1660         result = dump_rdatasets_text(mctx, name, rdsiter, &ctx, &buffer, f);
1661         if (result != ISC_R_SUCCESS)
1662                 goto failure;
1663         dns_rdatasetiter_destroy(&rdsiter);
1664
1665         result = ISC_R_SUCCESS;
1666
1667  failure:
1668         isc_mem_put(mctx, buffer.base, buffer.length);
1669         return (result);
1670 }
1671
1672 isc_result_t
1673 dns_master_dumpnode(isc_mem_t *mctx, dns_db_t *db, dns_dbversion_t *version,
1674                     dns_dbnode_t *node, dns_name_t *name,
1675                     const dns_master_style_t *style, const char *filename)
1676 {
1677         FILE *f = NULL;
1678         isc_result_t result;
1679
1680         result = isc_stdio_open(filename, "w", &f);
1681         if (result != ISC_R_SUCCESS) {
1682                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1683                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1684                               "dumping node to file: %s: open: %s", filename,
1685                               isc_result_totext(result));
1686                 return (ISC_R_UNEXPECTED);
1687         }
1688
1689         result = dns_master_dumpnodetostream(mctx, db, version, node, name,
1690                                              style, f);
1691
1692         result = isc_stdio_close(f);
1693         if (result != ISC_R_SUCCESS) {
1694                 isc_log_write(dns_lctx, ISC_LOGCATEGORY_GENERAL,
1695                               DNS_LOGMODULE_MASTERDUMP, ISC_LOG_ERROR,
1696                               "dumping master file: %s: close: %s", filename,
1697                               isc_result_totext(result));
1698                 return (ISC_R_UNEXPECTED);
1699         }
1700
1701         return (result);
1702 }
1703
1704 isc_result_t
1705 dns_master_stylecreate(dns_master_style_t **stylep, unsigned int flags,
1706                        unsigned int ttl_column, unsigned int class_column,
1707                        unsigned int type_column, unsigned int rdata_column,
1708                        unsigned int line_length, unsigned int tab_width,
1709                        isc_mem_t *mctx)
1710 {
1711         dns_master_style_t *style;
1712
1713         REQUIRE(stylep != NULL && *stylep == NULL);
1714         style = isc_mem_get(mctx, sizeof(*style));
1715         if (style == NULL)
1716                 return (ISC_R_NOMEMORY);
1717
1718         style->flags = flags;
1719         style->ttl_column = ttl_column;
1720         style->class_column = class_column;
1721         style->type_column = type_column;
1722         style->rdata_column = rdata_column;
1723         style->line_length = line_length;
1724         style->tab_width = tab_width;
1725
1726         *stylep = style;
1727         return (ISC_R_SUCCESS);
1728 }
1729
1730 void
1731 dns_master_styledestroy(dns_master_style_t **stylep, isc_mem_t *mctx) {
1732         dns_master_style_t *style;
1733
1734         REQUIRE(stylep != NULL && *stylep != NULL);
1735         style = *stylep;
1736         *stylep = NULL;
1737         isc_mem_put(mctx, style, sizeof(*style));
1738 }