20 #include <sys/types.h>
24 #include <tensor/config.h>
25 #include <tensor/sdf.h>
31 #ifdef TENSOR_BIGENDIAN
32 const enum DataFile::endianness DataFile::endian = BIG_ENDIAN_FILE;
34 const enum DataFile::endianness DataFile::endian = LITTLE_ENDIAN_FILE;
39 static int get_lock(
char const *lockName,
bool wait)
44 fd = open(lockName, O_RDWR|O_CREAT, 0666);
46 if( fd >= 0 && flock( fd, LOCK_EX | LOCK_NB ) < 0 )
48 if (errno == ENOTSUP) {
49 std::cout <<
"Locking not supported. We assume you know what you are doing.\n";
63 static void giveup_lock(
int fd,
char const *lockName)
71 const size_t DataFile::var_name_size;
73 DataFile::DataFile(
const std::string &a_filename,
int flags) :
75 _actual_filename(a_filename),
76 _filename(_actual_filename),
77 _lock_filename(_actual_filename +
".lck"),
78 _lock((flags == SDF_SHARED) ? get_lock(_lock_filename.c_str(), true) : 0),
83 if (!delete_file(_actual_filename)) {
84 std::cerr <<
"Cannot overwrite file " << _actual_filename << std::endl;
91 _actual_filename = _actual_filename +
".tmp";
92 delete_file(_actual_filename);
95 std::cerr <<
"Unrecognized DataFile mode " << flags << std::endl;
100 DataFile::~DataFile()
113 giveup_lock(_lock, _lock_filename.c_str());
117 if (file_exists(_actual_filename))
118 rename_file(_actual_filename, _filename);
124 DataFile::tag_to_name(
size_t tag)
126 static const char *names[] = {
127 "RTensor",
"CTensor",
"Real MPS",
"Complex MPS"
131 std::cerr <<
"Not a valid tag code, " << tag <<
" found in " << _filename;