diff --git a/modules/pipebackend/coprocess.cc b/modules/pipebackend/coprocess.cc index 632e50c663a4..88be0220949d 100644 --- a/modules/pipebackend/coprocess.cc +++ b/modules/pipebackend/coprocess.cc @@ -233,7 +233,7 @@ UnixRemote::UnixRemote(const string& path) if (connect(d_fd, (struct sockaddr*)&remote, sizeof(remote)) < 0) unixDie("Unable to connect to remote '" + path + "' using UNIX domain socket"); - d_fp = std::unique_ptr(fdopen(d_fd, "r"), fclose); + d_fp = pdns::UniqueFilePtr(fdopen(d_fd, "r")); } void UnixRemote::send(const string& line) diff --git a/modules/pipebackend/coprocess.hh b/modules/pipebackend/coprocess.hh index c9185132abf4..244d91750f8e 100644 --- a/modules/pipebackend/coprocess.hh +++ b/modules/pipebackend/coprocess.hh @@ -24,6 +24,7 @@ #include #include +#include "pdns/misc.hh" #include "pdns/namespaces.hh" class CoRemote @@ -67,6 +68,6 @@ public: private: int d_fd; - std::unique_ptr d_fp{nullptr, fclose}; + pdns::UniqueFilePtr d_fp{nullptr}; }; bool isUnixSocket(const string& fname); diff --git a/modules/remotebackend/pipeconnector.cc b/modules/remotebackend/pipeconnector.cc index cc90a441978f..21055c073423 100644 --- a/modules/remotebackend/pipeconnector.cc +++ b/modules/remotebackend/pipeconnector.cc @@ -96,7 +96,7 @@ void PipeConnector::launch() setCloseOnExec(d_fd1[1]); close(d_fd2[1]); setCloseOnExec(d_fd2[0]); - if (!(d_fp = std::unique_ptr(fdopen(d_fd2[0], "r"), fclose))) { + if (!(d_fp = pdns::UniqueFilePtr(fdopen(d_fd2[0], "r")))) { throw PDNSException("Unable to associate a file pointer with pipe: " + stringerror()); } if (d_timeout != 0) { diff --git a/modules/remotebackend/remotebackend.hh b/modules/remotebackend/remotebackend.hh index a43546517394..69c80b102011 100644 --- a/modules/remotebackend/remotebackend.hh +++ b/modules/remotebackend/remotebackend.hh @@ -158,7 +158,7 @@ private: int d_fd1[2]{}, d_fd2[2]{}; int d_pid; int d_timeout; - std::unique_ptr d_fp{nullptr, fclose}; + pdns::UniqueFilePtr d_fp{nullptr}; }; class RemoteBackend : public DNSBackend diff --git a/pdns/dnspcap.cc b/pdns/dnspcap.cc index c72c3d7538a7..d0d07450211d 100644 --- a/pdns/dnspcap.cc +++ b/pdns/dnspcap.cc @@ -30,7 +30,7 @@ #include "namespaces.hh" PcapPacketReader::PcapPacketReader(const string& fname) : d_fname(fname) { - d_fp = std::unique_ptr(fopen(fname.c_str(), "r"), fclose); + d_fp = pdns::UniqueFilePtr(fopen(fname.c_str(), "r")); if (!d_fp) { unixDie("Unable to open file " + fname); } @@ -235,7 +235,7 @@ PcapPacketWriter::PcapPacketWriter(const string& fname, const PcapPacketReader& PcapPacketWriter::PcapPacketWriter(const string& fname) : d_fname(fname) { - d_fp = std::unique_ptr(fopen(fname.c_str(),"w"), fclose); + d_fp = pdns::UniqueFilePtr(fopen(fname.c_str(),"w")); if (!d_fp) { unixDie("Unable to open file"); diff --git a/pdns/dnspcap.hh b/pdns/dnspcap.hh index bdeb17676c93..aba0d8205460 100644 --- a/pdns/dnspcap.hh +++ b/pdns/dnspcap.hh @@ -87,7 +87,7 @@ public: } }; - PcapPacketReader(const string& fname); + PcapPacketReader(const string& fname); template void checkedFread(T* ptr) @@ -118,17 +118,17 @@ public: char *d_buffer; size_t d_bufsize; private: - std::unique_ptr d_fp{nullptr, fclose}; + pdns::UniqueFilePtr d_fp{nullptr}; string d_fname; unsigned int d_skipMediaHeader; }; class PcapPacketWriter { -public: +public: PcapPacketWriter(const string& fname, const PcapPacketReader& ppr); PcapPacketWriter(const string& fname); - + void write(); void setPPR(const PcapPacketReader& ppr) { d_ppr = &ppr; } @@ -136,6 +136,6 @@ private: string d_fname; const PcapPacketReader* d_ppr{nullptr}; - std::unique_ptr d_fp{nullptr, fclose}; + pdns::UniqueFilePtr d_fp{nullptr}; bool d_first{true}; -}; +}; diff --git a/pdns/dnspcap2protobuf.cc b/pdns/dnspcap2protobuf.cc index e5ff300d3d0f..0defeb918324 100644 --- a/pdns/dnspcap2protobuf.cc +++ b/pdns/dnspcap2protobuf.cc @@ -63,7 +63,7 @@ try { PcapPacketReader pr(argv[1]); - auto fp = std::unique_ptr(fopen(argv[2], "w"), fclose); + auto fp = pdns::UniqueFilePtr(fopen(argv[2], "w")); if (!fp) { cerr<<"Error opening output file "< DNSCryptoKeyEngine::makeFromISCFile(DNSKEYRecordContent& drc, const char* fname) { string sline, isc; - auto fp = std::unique_ptr(fopen(fname, "r"), fclose); + auto fp = pdns::UniqueFilePtr(fopen(fname, "r")); if(!fp) { throw runtime_error("Unable to read file '"+string(fname)+"' for generating DNS Private Key"); } diff --git a/pdns/dnssecinfra.hh b/pdns/dnssecinfra.hh index 617a9c7df03f..9614ee1373d0 100644 --- a/pdns/dnssecinfra.hh +++ b/pdns/dnssecinfra.hh @@ -66,7 +66,7 @@ class DNSCryptoKeyEngine void createFromPEMString(DNSKEYRecordContent& drc, const std::string& contents) { // NOLINTNEXTLINE(*-cast): POSIX APIs. - unique_ptr inputFile{fmemopen(const_cast(contents.data()), contents.length(), "r"), &std::fclose}; + pdns::UniqueFilePtr inputFile{fmemopen(const_cast(contents.data()), contents.length(), "r")}; createFromPEMFile(drc, *inputFile); } @@ -89,7 +89,7 @@ class DNSCryptoKeyEngine std::string output{}; output.resize(buflen); - unique_ptr outputFile{fmemopen(output.data(), output.length() - 1, "w"), &std::fclose}; + pdns::UniqueFilePtr outputFile{fmemopen(output.data(), output.length() - 1, "w")}; convertToPEMFile(*outputFile); std::fflush(outputFile.get()); output.resize(std::ftell(outputFile.get())); diff --git a/pdns/dnstcpbench.cc b/pdns/dnstcpbench.cc index 32df39a8530b..ccd6e2346fbf 100644 --- a/pdns/dnstcpbench.cc +++ b/pdns/dnstcpbench.cc @@ -262,12 +262,12 @@ try std::vector workers; workers.reserve(numworkers); - std::unique_ptr fp{nullptr, fclose}; + pdns::UniqueFilePtr fp{nullptr}; if (!g_vm.count("file")) { - fp = std::unique_ptr(fdopen(0, "r"), fclose); + fp = pdns::UniqueFilePtr(fdopen(0, "r")); } else { - fp = std::unique_ptr(fopen(g_vm["file"].as().c_str(), "r"), fclose); + fp = pdns::UniqueFilePtr(fopen(g_vm["file"].as().c_str(), "r")); if (!fp) { unixDie("Unable to open "+g_vm["file"].as()+" for input"); } diff --git a/pdns/ixfrutils.cc b/pdns/ixfrutils.cc index 5aa5912ed2d8..40b6b2495d74 100644 --- a/pdns/ixfrutils.cc +++ b/pdns/ixfrutils.cc @@ -134,7 +134,7 @@ void writeZoneToDisk(const records_t& records, const DNSName& zone, const std::s /* ensure that the partial zone file will only be accessible by the current user, not even by other users in the same group, and certainly not by other users. */ umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); - auto filePtr = std::unique_ptr(fopen((fname+".partial").c_str(), "w"), fclose); + auto filePtr = pdns::UniqueFilePtr(fopen((fname+".partial").c_str(), "w")); if (!filePtr) { throw runtime_error("Unable to open file '"+fname+".partial' for writing: "+stringerror()); } diff --git a/pdns/pdnsutil.cc b/pdns/pdnsutil.cc index 15d241682e78..d0c35332fec8 100644 --- a/pdns/pdnsutil.cc +++ b/pdns/pdnsutil.cc @@ -3527,7 +3527,7 @@ try const auto algorithm = pdns::checked_stoi(cmds.at(3)); errno = 0; - std::unique_ptr fp{std::fopen(filename.c_str(), "r"), &std::fclose}; + pdns::UniqueFilePtr fp{std::fopen(filename.c_str(), "r")}; if (fp == nullptr) { auto errMsg = pdns::getMessageFromErrno(errno); throw runtime_error("Failed to open PEM file `" + filename + "`: " + errMsg);