From: Miod Vallat Date: Mon, 11 Aug 2025 07:47:09 +0000 (+0200) Subject: Move some of editZone into separate routines. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51117c7bb15a4ea576668c1ad685755d19b0365a;p=thirdparty%2Fpdns.git Move some of editZone into separate routines. Signed-off-by: Miod Vallat --- diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index a017512582..2175f00214 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -1827,6 +1827,78 @@ static bool spawnEditor(const std::string& editor, std::string_view tmpfile, int return false; } +// Fill the file `tmpnam' (possibly already open if `tmpfd' is valid) with the +// contents of zone `info', in bind format. +// Returns the zone records in sorted order, with the file closed and `tmpfd' +// reset to -1. +static std::vector fillZoneFile(int& tmpfd, const char* tmpnam, DomainInfo& info) +{ + std::vector records; + + info.backend->list(info.zone, info.id); + if (tmpfd < 0 && (tmpfd = open(tmpnam, O_CREAT | O_WRONLY | O_TRUNC, 0600)) < 0) { + unixDie("Error reopening temporary file "+string(tmpnam)); + } + const std::string_view header("; Warning - every name in this file is ABSOLUTE!\n$ORIGIN .\n"); + if (write(tmpfd, header.data(), header.length()) < 0) { + unixDie("Writing zone to temporary file"); + } + DNSResourceRecord rr; + while (info.backend->get(rr)) { + if (rr.qtype.getCode() == QType::ENT) { + continue; + } + DNSRecord dr(rr); + records.emplace_back(std::move(dr)); + } + sort(records.begin(), records.end(), DNSRecord::prettyCompare); + for (const auto& dr : records) { + ostringstream os; + os<getZoneRepresentation(true)<& records) +{ + records.clear(); + ZoneParserTNG zpt(tmpnam, g_rootzonename); + zpt.setMaxGenerateSteps(::arg().asNum("max-generate-steps")); + zpt.setMaxIncludes(::arg().asNum("max-include-depth")); + DNSResourceRecord zrr; + try { + while(zpt.get(zrr)) { + DNSRecord dr(zrr); + records.push_back(dr); + } + } + catch(std::exception& e) { + cerr<<"Problem: "<list(zone, di.id); - pre.clear(); - post.clear(); - { - if(tmpfd < 0 && (tmpfd=open(tmpnam, O_CREAT | O_WRONLY | O_TRUNC, 0600)) < 0) { - unixDie("Error reopening temporary file "+string(tmpnam)); - } - const string header("; Warning - every name in this file is ABSOLUTE!\n$ORIGIN .\n"); - if(write(tmpfd, header.c_str(), header.length()) < 0) { - unixDie("Writing zone to temporary file"); - } - DNSResourceRecord rr; - while(di.backend->get(rr)) { - if(rr.qtype.getCode() == QType::ENT) { - continue; - } - DNSRecord dr(rr); - pre.emplace_back(std::move(dr)); - } - sort(pre.begin(), pre.end(), DNSRecord::prettyCompare); - for(const auto& dr : pre) { - ostringstream os; - os<getZoneRepresentation(true)<(tmpnam), g_rootzonename); - zpt.setMaxGenerateSteps(::arg().asNum("max-generate-steps")); - zpt.setMaxIncludes(::arg().asNum("max-include-depth")); - DNSResourceRecord zrr; - try { - while(zpt.get(zrr)) { - DNSRecord dr(zrr); - post.push_back(dr); - grouped[{dr.d_name,dr.d_type}].push_back(dr); - } - } - catch(std::exception& e) { - cerr<<"Problem: "< checkrr; + checkrr.reserve(post.size()); for(const DNSRecord& rr : post) { DNSResourceRecord drr = DNSResourceRecord::fromWire(rr); drr.domain_id = di.id; @@ -1988,16 +2012,20 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) if (resp != '\n') { cerr << endl; } - if(resp=='e') { + switch (resp) { + case 'e': post.clear(); state = EDITFILE; - } else if(resp=='r') { + break; + case 'r': post.clear(); state = CREATEZONEFILE; - } else if(resp=='q') { + break; + case 'q': return EXIT_FAILURE; - } else if(resp=='a') { + case 'a': state = VALIDATE; + break; } break; case VALIDATE: @@ -2023,10 +2051,14 @@ static int editZone(const ZoneName &zone, const PDNSColors& col) for(const auto& c : changed) { cout<startTransaction(zone, UnknownDomainID); for(const auto& change : changed) { vector vrr;