Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create temporary path with boost::filesystem for cross platform compatibility #1063

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ endif
LIBRARIES += pthread \
glog gflags protobuf leveldb snappy \
lmdb \
boost_system \
boost_filesystem boost_system \
hdf5_hl hdf5 \
opencv_core opencv_highgui opencv_imgproc
PYTHON_LIBRARIES := boost_python python2.7
Expand Down
82 changes: 45 additions & 37 deletions include/caffe/util/io.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CAFFE_UTIL_IO_H_
#define CAFFE_UTIL_IO_H_

#include <unistd.h>
#include <boost/filesystem.hpp>
#include <string>

#include "google/protobuf/message.h"
Expand All @@ -22,30 +22,41 @@ namespace caffe {

using ::google::protobuf::Message;

static inline boost::filesystem::path unique_tmp_path() {
boost::system::error_code error;
const boost::filesystem::path tmp_path =
boost::filesystem::temp_directory_path(error);
const string pattern =
(tmp_path / "caffe_test.%%%%-%%%%-%%%%-%%%%").string();
boost::filesystem::path path;
do {
path = boost::filesystem::unique_path(pattern, error);
} while (boost::system::errc::success != error.value()
|| boost::filesystem::exists(path));
return path;
}

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;
*temp_filename = unique_tmp_path().string();
}

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;
const boost::filesystem::path path = unique_tmp_path();
boost::system::error_code error;
do {
try {
if (boost::filesystem::create_directories(path, error)) {
break;
}
} catch (...) {
LOG(ERROR) << "Failed to create a temporary directory at: "
<< path.string();
}
} while (boost::system::errc::success != error.value()
|| !boost::filesystem::exists(path));
*temp_dirname = path.string();
}

bool ReadProtoFromTextFile(const char* filename, Message* proto);
Expand Down Expand Up @@ -82,41 +93,38 @@ inline void ReadProtoFromBinaryFileOrDie(const string& filename,
ReadProtoFromBinaryFileOrDie(filename.c_str(), proto);
}


void WriteProtoToBinaryFile(const Message& proto, const char* filename);
inline void WriteProtoToBinaryFile(
const Message& proto, const string& filename) {
inline void WriteProtoToBinaryFile(const Message& proto,
const string& filename) {
WriteProtoToBinaryFile(proto, filename.c_str());
}

bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, const bool is_color, Datum* datum);
bool ReadImageToDatum(const string& filename, const int label, const int height,
const int width, const bool is_color, Datum* datum);

inline bool ReadImageToDatum(const string& filename, const int label,
const int height, const int width, Datum* datum) {
const int height, const int width, Datum* datum) {
return ReadImageToDatum(filename, label, height, width, true, datum);
}

inline bool ReadImageToDatum(const string& filename, const int label,
Datum* datum) {
Datum* datum) {
return ReadImageToDatum(filename, label, 0, 0, datum);
}

leveldb::Options GetLevelDBOptions();

template <typename Dtype>
void hdf5_load_nd_dataset_helper(
hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
Blob<Dtype>* blob);
template<typename Dtype>
void hdf5_load_nd_dataset_helper(hid_t file_id, const char* dataset_name_,
int min_dim, int max_dim, Blob<Dtype>* blob);

template <typename Dtype>
void hdf5_load_nd_dataset(
hid_t file_id, const char* dataset_name_, int min_dim, int max_dim,
Blob<Dtype>* blob);
template<typename Dtype>
void hdf5_load_nd_dataset(hid_t file_id, const char* dataset_name_, int min_dim,
int max_dim, Blob<Dtype>* blob);

template <typename Dtype>
void hdf5_save_nd_dataset(
const hid_t file_id, const string dataset_name, const Blob<Dtype>& blob);
template<typename Dtype>
void hdf5_save_nd_dataset(const hid_t file_id, const string dataset_name,
const Blob<Dtype>& blob);

} // namespace caffe

Expand Down
2 changes: 1 addition & 1 deletion scripts/travis/travis_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ apt-get install \
wget git curl \
python-dev python-numpy \
libleveldb-dev libsnappy-dev libopencv-dev \
libboost-dev libboost-system-dev libboost-python-dev libboost-thread-dev \
libboost-dev libboost-system-dev libboost-filesystem-dev libboost-python-dev libboost-thread-dev \
libprotobuf-dev protobuf-compiler \
libatlas-dev libatlas-base-dev \
libhdf5-serial-dev \
Expand Down
2 changes: 1 addition & 1 deletion src/caffe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ find_package(LMDB REQUIRED)
include_directories(${LMDB_INCLUDE_DIR})

# Boost
find_package(Boost 1.46 COMPONENTS system thread REQUIRED)
find_package(Boost 1.46 COMPONENTS filesystem system thread REQUIRED)
include_directories( ${Boost_INCLUDE_DIR} )
link_directories( ${Boost_LIBRARY_DIRS} )

Expand Down