remove uses of tmpnam
authorJeff Donahue <jeff.donahue@gmail.com>
Fri, 5 Sep 2014 12:01:46 +0000 (05:01 -0700)
committerJeff Donahue <jeff.donahue@gmail.com>
Sun, 7 Sep 2014 09:14:51 +0000 (11:14 +0200)
include/caffe/util/io.hpp
src/caffe/test/test_data_layer.cpp
src/caffe/test/test_hdf5_output_layer.cpp
src/caffe/test/test_image_data_layer.cpp
src/caffe/test/test_window_data_layer.cpp [deleted file]

index 546c3d6..8dd338d 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef CAFFE_UTIL_IO_H_
 #define CAFFE_UTIL_IO_H_
 
+#include <unistd.h>
 #include <string>
 
 #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) {
index e01932b..887124a 100644 (file)
@@ -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<TypeParam> {
  protected:
   DataLayerTest()
       : backend_(DataParameter_DB_LEVELDB),
-        filename_(new string(tmpnam(NULL))),
         blob_top_data_(new Blob<Dtype>()),
         blob_top_label_(new Blob<Dtype>()),
         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_);
   }
index b2a511d..eb09c8d 100644 (file)
@@ -22,8 +22,7 @@ class HDF5OutputLayerTest : public MultiDeviceTest<TypeParam> {
 
  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<Dtype>()),
         blob_label_(new Blob<Dtype>()),
@@ -31,6 +30,7 @@ class HDF5OutputLayerTest : public MultiDeviceTest<TypeParam> {
         channels_(8),
         height_(5),
         width_(5) {
+    MakeTempFilename(&output_file_name_);
   }
 
   virtual ~HDF5OutputLayerTest() {
index ab2365c..d098c76 100644 (file)
@@ -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<TypeParam> {
  protected:
   ImageDataLayerTest()
       : seed_(1701),
-        filename_(new string(tmpnam(NULL))),
         blob_top_data_(new Blob<Dtype>()),
         blob_top_label_(new Blob<Dtype>()) {}
   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<TypeParam> {
   }
 
   int seed_;
-  shared_ptr<string> filename_;
+  string filename_;
   Blob<Dtype>* const blob_top_data_;
   Blob<Dtype>* const blob_top_label_;
   vector<Blob<Dtype>*> 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<Dtype> 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<Dtype> 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 (file)
index 47591b6..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-#include <string>
-#include <vector>
-
-#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 <typename TypeParam>
-class WindowDataLayerTest : public MultiDeviceTest<TypeParam> {
-  typedef typename TypeParam::Dtype Dtype;
-
- protected:
-  WindowDataLayerTest()
-      : seed_(1701),
-        filename_(new string(tmpnam(NULL))),
-        blob_top_data_(new Blob<Dtype>()),
-        blob_top_label_(new Blob<Dtype>()) {}
-  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<string> filename_;
-  Blob<Dtype>* const blob_top_data_;
-  Blob<Dtype>* const blob_top_label_;
-  vector<Blob<Dtype>*> blob_bottom_vec_;
-  vector<Blob<Dtype>*> blob_top_vec_;
-};
-
-TYPED_TEST_CASE(WindowDataLayerTest, TestDtypesAndDevices);
-
-TYPED_TEST(WindowDataLayerTest, TestNothing) {
-}
-
-}  // namespace caffe