From da989d636e9d8adc22f59cbbaf335c1937057289 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Wed, 15 Nov 2017 14:34:22 +0200 Subject: [PATCH] MXS-1461 Add TempFile Class for creating a temporary file. --- .../filter/dbfwfilter/test/tempfile.cc | 51 ++++++++++++++ .../filter/dbfwfilter/test/tempfile.hh | 69 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 server/modules/filter/dbfwfilter/test/tempfile.cc create mode 100644 server/modules/filter/dbfwfilter/test/tempfile.hh diff --git a/server/modules/filter/dbfwfilter/test/tempfile.cc b/server/modules/filter/dbfwfilter/test/tempfile.cc new file mode 100644 index 000000000..5f3d889d3 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/tempfile.cc @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include "tempfile.hh" +#include +#include +#include + +namespace +{ + +static char NAME_TEMPLATE[] = "/tmp/XXXXXX"; + +} + +TempFile::TempFile() + : m_fd(-1) + , m_name(NAME_TEMPLATE) +{ + m_fd = mkstemp((char*)m_name.c_str()); + ss_dassert(m_fd != -1); +} + +TempFile::~TempFile() +{ + int rc = unlink(m_name.c_str()); + ss_dassert(rc != -1); + close(m_fd); +} + +void TempFile::write(const void* pData, size_t count) +{ + int rc = ::write(m_fd, pData, count); + ss_dassert(rc != -1); + ss_dassert((size_t)rc == count); +} + +void TempFile::write(const char* zData) +{ + write(zData, strlen(zData)); +} diff --git a/server/modules/filter/dbfwfilter/test/tempfile.hh b/server/modules/filter/dbfwfilter/test/tempfile.hh new file mode 100644 index 000000000..09d47f996 --- /dev/null +++ b/server/modules/filter/dbfwfilter/test/tempfile.hh @@ -0,0 +1,69 @@ +#pragma once +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include + +/** + * TempFile is a class using which a temporary file can be created and + * written to. + */ +class TempFile +{ + TempFile(const TempFile&); + TempFile& operator = (const TempFile&); + +public: + /** + * Constructor + * + * Create temporary file in /tmp + */ + TempFile(); + + /** + * Destructor + * + * Close and unlink file. + */ + ~TempFile(); + + /** + * The name of the created temporary file. + * + * @return The name of the file. + */ + std::string name() const + { + return m_name; + } + + /** + * Write data to the file. + * + * @param pData Pointer to data. + * @param count The number of bytes to write. + */ + void write(const void* pData, size_t count); + + /** + * Write data to the file. + * + * @param zData Null terminated buffer to write. + */ + void write(const char* zData); + +private: + int m_fd; + std::string m_name; +};