From fb0a3d0275107e56a0337cc009bcacd84997337c Mon Sep 17 00:00:00 2001 From: Jeff Donahue Date: Fri, 5 Sep 2014 05:01:46 -0700 Subject: [PATCH] remove uses of tmpnam --- include/caffe/util/io.hpp | 27 +++++++++++++ src/caffe/test/test_data_layer.cpp | 5 ++- src/caffe/test/test_hdf5_output_layer.cpp | 4 +- src/caffe/test/test_image_data_layer.cpp | 15 +++---- src/caffe/test/test_window_data_layer.cpp | 49 ----------------------- 5 files changed, 41 insertions(+), 59 deletions(-) delete mode 100644 src/caffe/test/test_window_data_layer.cpp diff --git a/include/caffe/util/io.hpp b/include/caffe/util/io.hpp index 546c3d6a..8dd338d2 100644 --- a/include/caffe/util/io.hpp +++ b/include/caffe/util/io.hpp @@ -1,6 +1,7 @@ #ifndef CAFFE_UTIL_IO_H_ #define CAFFE_UTIL_IO_H_ +#include #include #include "google/protobuf/message.h" @@ -21,6 +22,32 @@ namespace caffe { using ::google::protobuf::Message; +inline void MakeTempFilename(string* temp_filename) { + temp_filename->clear(); + *temp_filename = "/tmp/caffe_test.XXXXXX"; + char* temp_filename_cstr = new char[temp_filename->size()]; + // NOLINT_NEXT_LINE(runtime/printf) + strcpy(temp_filename_cstr, temp_filename->c_str()); + int fd = mkstemp(temp_filename_cstr); + CHECK_GE(fd, 0) << "Failed to open a temporary file at: " << *temp_filename; + close(fd); + *temp_filename = temp_filename_cstr; + delete temp_filename_cstr; +} + +inline void MakeTempDir(string* temp_dirname) { + temp_dirname->clear(); + *temp_dirname = "/tmp/caffe_test.XXXXXX"; + char* temp_dirname_cstr = new char[temp_dirname->size()]; + // NOLINT_NEXT_LINE(runtime/printf) + strcpy(temp_dirname_cstr, temp_dirname->c_str()); + char* mkdtemp_result = mkdtemp(temp_dirname_cstr); + CHECK(mkdtemp_result != NULL) + << "Failed to create a temporary directory at: " << *temp_dirname; + *temp_dirname = temp_dirname_cstr; + delete temp_dirname_cstr; +} + bool ReadProtoFromTextFile(const char* filename, Message* proto); inline bool ReadProtoFromTextFile(const string& filename, Message* proto) { diff --git a/src/caffe/test/test_data_layer.cpp b/src/caffe/test/test_data_layer.cpp index e01932b7..887124aa 100644 --- a/src/caffe/test/test_data_layer.cpp +++ b/src/caffe/test/test_data_layer.cpp @@ -8,6 +8,7 @@ #include "caffe/common.hpp" #include "caffe/filler.hpp" #include "caffe/proto/caffe.pb.h" +#include "caffe/util/io.hpp" #include "caffe/vision_layers.hpp" #include "caffe/test/test_caffe_main.hpp" @@ -21,11 +22,13 @@ class DataLayerTest : public MultiDeviceTest { protected: DataLayerTest() : backend_(DataParameter_DB_LEVELDB), - filename_(new string(tmpnam(NULL))), blob_top_data_(new Blob()), blob_top_label_(new Blob()), seed_(1701) {} virtual void SetUp() { + filename_.reset(new string()); + MakeTempDir(filename_.get()); + *filename_ += "/db"; blob_top_vec_.push_back(blob_top_data_); blob_top_vec_.push_back(blob_top_label_); } diff --git a/src/caffe/test/test_hdf5_output_layer.cpp b/src/caffe/test/test_hdf5_output_layer.cpp index b2a511d1..eb09c8d1 100644 --- a/src/caffe/test/test_hdf5_output_layer.cpp +++ b/src/caffe/test/test_hdf5_output_layer.cpp @@ -22,8 +22,7 @@ class HDF5OutputLayerTest : public MultiDeviceTest { protected: HDF5OutputLayerTest() - : output_file_name_(tmpnam(NULL)), - input_file_name_( + : input_file_name_( CMAKE_SOURCE_DIR "caffe/test/test_data/sample_data.h5"), blob_data_(new Blob()), blob_label_(new Blob()), @@ -31,6 +30,7 @@ class HDF5OutputLayerTest : public MultiDeviceTest { channels_(8), height_(5), width_(5) { + MakeTempFilename(&output_file_name_); } virtual ~HDF5OutputLayerTest() { diff --git a/src/caffe/test/test_image_data_layer.cpp b/src/caffe/test/test_image_data_layer.cpp index ab2365c6..d098c765 100644 --- a/src/caffe/test/test_image_data_layer.cpp +++ b/src/caffe/test/test_image_data_layer.cpp @@ -8,6 +8,7 @@ #include "caffe/common.hpp" #include "caffe/filler.hpp" #include "caffe/proto/caffe.pb.h" +#include "caffe/util/io.hpp" #include "caffe/vision_layers.hpp" #include "caffe/test/test_caffe_main.hpp" @@ -21,16 +22,16 @@ class ImageDataLayerTest : public MultiDeviceTest { protected: ImageDataLayerTest() : seed_(1701), - filename_(new string(tmpnam(NULL))), blob_top_data_(new Blob()), blob_top_label_(new Blob()) {} virtual void SetUp() { + MakeTempFilename(&filename_); blob_top_vec_.push_back(blob_top_data_); blob_top_vec_.push_back(blob_top_label_); Caffe::set_random_seed(seed_); // Create a Vector of files with labels - std::ofstream outfile(filename_->c_str(), std::ofstream::out); - LOG(INFO) << "Using temporary file " << *filename_; + std::ofstream outfile(filename_.c_str(), std::ofstream::out); + LOG(INFO) << "Using temporary file " << filename_; for (int i = 0; i < 5; ++i) { outfile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << i; } @@ -43,7 +44,7 @@ class ImageDataLayerTest : public MultiDeviceTest { } int seed_; - shared_ptr filename_; + string filename_; Blob* const blob_top_data_; Blob* const blob_top_label_; vector*> blob_bottom_vec_; @@ -57,7 +58,7 @@ TYPED_TEST(ImageDataLayerTest, TestRead) { LayerParameter param; ImageDataParameter* image_data_param = param.mutable_image_data_param(); image_data_param->set_batch_size(5); - image_data_param->set_source(this->filename_->c_str()); + image_data_param->set_source(this->filename_.c_str()); image_data_param->set_shuffle(false); ImageDataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); @@ -83,7 +84,7 @@ TYPED_TEST(ImageDataLayerTest, TestResize) { LayerParameter param; ImageDataParameter* image_data_param = param.mutable_image_data_param(); image_data_param->set_batch_size(5); - image_data_param->set_source(this->filename_->c_str()); + image_data_param->set_source(this->filename_.c_str()); image_data_param->set_new_height(256); image_data_param->set_new_width(256); image_data_param->set_shuffle(false); @@ -111,7 +112,7 @@ TYPED_TEST(ImageDataLayerTest, TestShuffle) { LayerParameter param; ImageDataParameter* image_data_param = param.mutable_image_data_param(); image_data_param->set_batch_size(5); - image_data_param->set_source(this->filename_->c_str()); + image_data_param->set_source(this->filename_.c_str()); image_data_param->set_shuffle(true); ImageDataLayer layer(param); layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_); diff --git a/src/caffe/test/test_window_data_layer.cpp b/src/caffe/test/test_window_data_layer.cpp deleted file mode 100644 index 47591b60..00000000 --- a/src/caffe/test/test_window_data_layer.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include - -#include "gtest/gtest.h" - -#include "caffe/blob.hpp" -#include "caffe/common.hpp" -#include "caffe/data_layers.hpp" -#include "caffe/proto/caffe.pb.h" - -#include "caffe/test/test_caffe_main.hpp" - -namespace caffe { - -template -class WindowDataLayerTest : public MultiDeviceTest { - typedef typename TypeParam::Dtype Dtype; - - protected: - WindowDataLayerTest() - : seed_(1701), - filename_(new string(tmpnam(NULL))), - blob_top_data_(new Blob()), - blob_top_label_(new Blob()) {} - virtual void SetUp() { - blob_top_vec_.push_back(blob_top_data_); - blob_top_vec_.push_back(blob_top_label_); - Caffe::set_random_seed(seed_); - } - - virtual ~WindowDataLayerTest() { - delete blob_top_data_; - delete blob_top_label_; - } - - int seed_; - shared_ptr filename_; - Blob* const blob_top_data_; - Blob* const blob_top_label_; - vector*> blob_bottom_vec_; - vector*> blob_top_vec_; -}; - -TYPED_TEST_CASE(WindowDataLayerTest, TestDtypesAndDevices); - -TYPED_TEST(WindowDataLayerTest, TestNothing) { -} - -} // namespace caffe