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<FILE, int (*)(FILE*)>(fdopen(d_fd, "r"), fclose);
+ d_fp = pdns::UniqueFilePtr(fdopen(d_fd, "r"));
}
void UnixRemote::send(const string& line)
#include <stdio.h>
#include <string>
+#include "pdns/misc.hh"
#include "pdns/namespaces.hh"
class CoRemote
private:
int d_fd;
- std::unique_ptr<FILE, int (*)(FILE*)> d_fp{nullptr, fclose};
+ pdns::UniqueFilePtr d_fp{nullptr};
};
bool isUnixSocket(const string& fname);
setCloseOnExec(d_fd1[1]);
close(d_fd2[1]);
setCloseOnExec(d_fd2[0]);
- if (!(d_fp = std::unique_ptr<FILE, int (*)(FILE*)>(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) {
int d_fd1[2]{}, d_fd2[2]{};
int d_pid;
int d_timeout;
- std::unique_ptr<FILE, int (*)(FILE*)> d_fp{nullptr, fclose};
+ pdns::UniqueFilePtr d_fp{nullptr};
};
class RemoteBackend : public DNSBackend
#include "namespaces.hh"
PcapPacketReader::PcapPacketReader(const string& fname) : d_fname(fname)
{
- d_fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname.c_str(), "r"), fclose);
+ d_fp = pdns::UniqueFilePtr(fopen(fname.c_str(), "r"));
if (!d_fp) {
unixDie("Unable to open file " + fname);
}
PcapPacketWriter::PcapPacketWriter(const string& fname) : d_fname(fname)
{
- d_fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(fname.c_str(),"w"), fclose);
+ d_fp = pdns::UniqueFilePtr(fopen(fname.c_str(),"w"));
if (!d_fp) {
unixDie("Unable to open file");
}
};
- PcapPacketReader(const string& fname);
+ PcapPacketReader(const string& fname);
template<typename T>
void checkedFread(T* ptr)
char *d_buffer;
size_t d_bufsize;
private:
- std::unique_ptr<FILE, int(*)(FILE*)> 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; }
string d_fname;
const PcapPacketReader* d_ppr{nullptr};
- std::unique_ptr<FILE, int(*)(FILE*)> d_fp{nullptr, fclose};
+ pdns::UniqueFilePtr d_fp{nullptr};
bool d_first{true};
-};
+};
PcapPacketReader pr(argv[1]);
- auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(argv[2], "w"), fclose);
+ auto fp = pdns::UniqueFilePtr(fopen(argv[2], "w"));
if (!fp) {
cerr<<"Error opening output file "<<argv[2]<<": "<<stringerror()<<endl;
exit(EXIT_FAILURE);
std::unique_ptr<DNSCryptoKeyEngine> DNSCryptoKeyEngine::makeFromISCFile(DNSKEYRecordContent& drc, const char* fname)
{
string sline, isc;
- auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(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");
}
void createFromPEMString(DNSKEYRecordContent& drc, const std::string& contents)
{
// NOLINTNEXTLINE(*-cast): POSIX APIs.
- unique_ptr<std::FILE, decltype(&std::fclose)> inputFile{fmemopen(const_cast<char*>(contents.data()), contents.length(), "r"), &std::fclose};
+ pdns::UniqueFilePtr inputFile{fmemopen(const_cast<char*>(contents.data()), contents.length(), "r")};
createFromPEMFile(drc, *inputFile);
}
std::string output{};
output.resize(buflen);
- unique_ptr<std::FILE, decltype(&std::fclose)> 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()));
std::vector<std::thread> workers;
workers.reserve(numworkers);
- std::unique_ptr<FILE, int(*)(FILE*)> fp{nullptr, fclose};
+ pdns::UniqueFilePtr fp{nullptr};
if (!g_vm.count("file")) {
- fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(0, "r"), fclose);
+ fp = pdns::UniqueFilePtr(fdopen(0, "r"));
}
else {
- fp = std::unique_ptr<FILE, int(*)(FILE*)>(fopen(g_vm["file"].as<string>().c_str(), "r"), fclose);
+ fp = pdns::UniqueFilePtr(fopen(g_vm["file"].as<string>().c_str(), "r"));
if (!fp) {
unixDie("Unable to open "+g_vm["file"].as<string>()+" for input");
}
/* 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<FILE, int(*)(FILE*)>(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());
}
const auto algorithm = pdns::checked_stoi<unsigned int>(cmds.at(3));
errno = 0;
- std::unique_ptr<std::FILE, decltype(&std::fclose)> 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);