From e6a0033a8ce3f3f409b63652fb6ef04ef0e5e052 Mon Sep 17 00:00:00 2001 From: Tom Finegan Date: Mon, 7 Mar 2016 11:08:14 -0800 Subject: [PATCH] Add file_util. Move file utility classes and functions from testing/test_util to common/file_util, and make them part of libwebm. Change-Id: If5b25a63b20efacc16b0fecaa8876ade4ecc4b26 --- CMakeLists.txt | 2 ++ Makefile.unix | 2 +- common/file_util.cc | 64 +++++++++++++++++++++++++++++++++++++++ common/file_util.h | 41 +++++++++++++++++++++++++ common/libwebm_utils.h | 2 ++ testing/muxer_tests.cc | 1 + testing/test_util.cc | 43 +------------------------- testing/test_util.h | 23 +------------- testing/webm2pes_tests.cc | 5 +-- 9 files changed, 116 insertions(+), 67 deletions(-) create mode 100644 common/file_util.cc create mode 100644 common/file_util.h diff --git a/CMakeLists.txt b/CMakeLists.txt index aefc34c..fe7257c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -106,6 +106,8 @@ endif () # Libwebm section. add_library(webm STATIC + "${LIBWEBM_SRC_DIR}/common/file_util.cc" + "${LIBWEBM_SRC_DIR}/common/file_util.h" "${LIBWEBM_SRC_DIR}/common/hdr_util.cc" "${LIBWEBM_SRC_DIR}/common/hdr_util.h" "${LIBWEBM_SRC_DIR}/mkvmuxer.cpp" diff --git a/Makefile.unix b/Makefile.unix index 502869c..c850e32 100644 --- a/Makefile.unix +++ b/Makefile.unix @@ -3,7 +3,7 @@ CXXFLAGS := -W -Wall -g -MMD -MP LIBWEBMA := libwebm.a LIBWEBMSO := libwebm.so WEBMOBJS := mkvparser.o mkvreader.o mkvmuxer.o mkvmuxerutil.o mkvwriter.o -WEBMOBJS += common/hdr_util.o +WEBMOBJS += common/file_util.o common/hdr_util.o OBJSA := $(WEBMOBJS:.o=_a.o) OBJSSO := $(WEBMOBJS:.o=_so.o) OBJECTS1 := sample.o diff --git a/common/file_util.cc b/common/file_util.cc new file mode 100644 index 0000000..0bed848 --- /dev/null +++ b/common/file_util.cc @@ -0,0 +1,64 @@ +// Copyright (c) 2016 The WebM project authors. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +#include "common/file_util.h" + +#include +#ifndef _MSC_VER +#include // close() +#endif + +#include +#include +#include +#include + +namespace libwebm { + +std::string GetTempFileName() { +#ifndef _MSC_VER + char temp_file_name_template[] = "libwebm_temp.XXXXXX"; + int fd = mkstemp(temp_file_name_template); + if (fd != -1) { + close(fd); + return std::string(temp_file_name_template); + } + return std::string(); +#else + // TODO(tomfinegan): Add the MSVC version of mkstemp() to quiet the MSVC + // version of the security warning. + return std::tmpnam(nullptr); +#endif +} + +uint64_t GetFileSize(const std::string& file_name) { + uint64_t file_size = 0; +#ifndef _MSC_VER + struct stat st; + st.st_size = 0; + if (stat(file_name.c_str(), &st) == 0) { +#else + struct _stat st; + st.st_size = 0; + if (_stat(file_name.c_str(), &st) == 0) { +#endif + file_size = st.st_size; + } + return file_size; +} + +TempFileDeleter::TempFileDeleter() { file_name_ = GetTempFileName(); } + +TempFileDeleter::~TempFileDeleter() { + std::ifstream file(file_name_.c_str()); + if (file.good()) { + file.close(); + std::remove(file_name_.c_str()); + } +} + +} // namespace libwebm diff --git a/common/file_util.h b/common/file_util.h new file mode 100644 index 0000000..cd7d48c --- /dev/null +++ b/common/file_util.h @@ -0,0 +1,41 @@ +// Copyright (c) 2016 The WebM project authors. All Rights Reserved. +// +// Use of this source code is governed by a BSD-style license +// that can be found in the LICENSE file in the root of the source +// tree. An additional intellectual property rights grant can be found +// in the file PATENTS. All contributing project authors may +// be found in the AUTHORS file in the root of the source tree. +#ifndef LIBWEBM_COMMON_FILE_UTIL_H_ +#define LIBWEBM_COMMON_FILE_UTIL_H_ + +#include + +#include + +#include "../mkvmuxertypes.hpp" // LIBWEBM_DISALLOW_COPY_AND_ASSIGN() + +namespace libwebm { + +// Returns a temporary file name. +std::string GetTempFileName(); + +// Returns size of file specified by |file_name|, or 0 upon failure. +uint64_t GetFileSize(const std::string& file_name); + +// Manages life of temporary file specified at time of construction. Deletes +// file upon destruction. +class TempFileDeleter { + public: + TempFileDeleter(); + explicit TempFileDeleter(std::string file_name) : file_name_(file_name) {} + ~TempFileDeleter(); + const std::string& name() const { return file_name_; } + + private: + std::string file_name_; + LIBWEBM_DISALLOW_COPY_AND_ASSIGN(TempFileDeleter); +}; + +} // namespace libwebm + +#endif // LIBWEBM_COMMON_FILE_UTIL_H_ \ No newline at end of file diff --git a/common/libwebm_utils.h b/common/libwebm_utils.h index 2582a9e..8fdfda5 100644 --- a/common/libwebm_utils.h +++ b/common/libwebm_utils.h @@ -16,6 +16,8 @@ namespace libwebm { // fclose functor for wrapping FILE in std::unique_ptr. +// TODO(tomfinegan): Move this to file_util once c++11 restrictions are +// relaxed. struct FILEDeleter { int operator()(std::FILE* f) { if (f != nullptr) diff --git a/testing/muxer_tests.cc b/testing/muxer_tests.cc index 97d5fef..72d55cd 100644 --- a/testing/muxer_tests.cc +++ b/testing/muxer_tests.cc @@ -17,6 +17,7 @@ #include "mkvreader.hpp" #include "mkvwriter.hpp" +#include "common/file_util.h" #include "common/libwebm_utils.h" #include "testing/test_util.h" diff --git a/testing/test_util.cc b/testing/test_util.cc index 42c27ad..72c4b7d 100644 --- a/testing/test_util.cc +++ b/testing/test_util.cc @@ -21,6 +21,7 @@ #include #include +#include "common/file_util.h" #include "common/libwebm_utils.h" namespace libwebm { @@ -60,49 +61,7 @@ bool CompareFiles(const std::string& file1, const std::string& file2) { return std::feof(f1.get()) && std::feof(f2.get()); } -std::string GetTempFileName() { -#ifndef _MSC_VER - char temp_file_name_template[] = "libwebm_temp.XXXXXX"; - int fd = mkstemp(temp_file_name_template); - if (fd != -1) { - close(fd); - return std::string(temp_file_name_template); - } - return std::string(); -#else - // TODO(tomfinegan): Add the MSVC version of mkstemp() to quiet the MSVC - // version of the security warning. - return std::tmpnam(nullptr); -#endif -} -std::uint64_t GetFileSize(const std::string& file_name) { - std::uint64_t file_size = 0; -#ifndef _MSC_VER - struct stat st; - st.st_size = 0; - if (stat(file_name.c_str(), &st) == 0) { -#else - struct _stat st; - st.st_size = 0; - if (_stat(file_name.c_str(), &st) == 0) { -#endif - file_size = st.st_size; - } - return file_size; -} - -TempFileDeleter::TempFileDeleter() { - file_name_ = GetTempFileName(); -} - -TempFileDeleter::~TempFileDeleter() { - std::ifstream file(file_name_); - if (file.good()) { - file.close(); - std::remove(file_name_.c_str()); - } -} } // namespace test } // namespace libwebm diff --git a/testing/test_util.h b/testing/test_util.h index a21f2a4..efd6b12 100644 --- a/testing/test_util.h +++ b/testing/test_util.h @@ -51,28 +51,7 @@ std::string GetTestFilePath(const std::string& name); // files match exactly, false otherwise. bool CompareFiles(const std::string& file1, const std::string& file2); -// Returns a temporary file name. -std::string GetTempFileName(); - -// Returns size of file specified by |file_name|, or 0 upon failure. -std::uint64_t GetFileSize(const std::string& file_name); - -// Manages life of temporary file specified at time of construction. Deletes -// file upon destruction. -class TempFileDeleter { - public: - TempFileDeleter(); - explicit TempFileDeleter(std::string file_name) : file_name_(file_name) {} - ~TempFileDeleter(); - TempFileDeleter(const TempFileDeleter&) = delete; - TempFileDeleter(TempFileDeleter&&) = delete; - const std::string& name() const { return file_name_; } - - private: - std::string file_name_; -}; - } // namespace test } // namespace libwebm -#endif // LIBWEBM_TESTING_TEST_UTIL_H_ +#endif // LIBWEBM_TESTING_TEST_UTIL_H_ \ No newline at end of file diff --git a/testing/webm2pes_tests.cc b/testing/webm2pes_tests.cc index 93c904c..06699b6 100644 --- a/testing/webm2pes_tests.cc +++ b/testing/webm2pes_tests.cc @@ -14,6 +14,7 @@ #include "gtest/gtest.h" +#include "common/file_util.h" #include "common/libwebm_utils.h" #include "testing/test_util.h" @@ -96,7 +97,7 @@ class Webm2PesTests : public ::testing::Test { void CreateAndLoadTestInput() { libwebm::Webm2Pes converter(input_file_name_, temp_file_name_.name()); ASSERT_TRUE(converter.ConvertToFile()); - pes_file_size_ = libwebm::test::GetFileSize(pes_file_name()); + pes_file_size_ = libwebm::GetFileSize(pes_file_name()); ASSERT_GT(pes_file_size_, 0); pes_file_data_.reserve(pes_file_size_); libwebm::FilePtr file = libwebm::FilePtr( @@ -245,7 +246,7 @@ class Webm2PesTests : public ::testing::Test { const PesFileData& pes_file_data() const { return pes_file_data_; } private: - const libwebm::test::TempFileDeleter temp_file_name_; + const libwebm::TempFileDeleter temp_file_name_; const std::string input_file_name_ = libwebm::test::GetTestFilePath("bbb_480p_vp9_opus_1second.webm"); std::int64_t pes_file_size_ = 0;