From: Alexander Schmidt <alex@treefish.org>
Date: Thu, 13 Feb 2014 20:11:00 +0000 (+0100)
Subject: Opening cachefiles exclusively with O_EXCL.
X-Git-Url: http://git.treefish.org/~alex/phys/latlib.git/commitdiff_plain/d945435d46acac2f758c23f0a5ae99cbb9894b91

Opening cachefiles exclusively with O_EXCL.
---

diff --git a/configcache.cpp b/configcache.cpp
index 34789ee..5987789 100644
--- a/configcache.cpp
+++ b/configcache.cpp
@@ -4,6 +4,9 @@
 #include <iostream>
 #include <time.h>
 #include <dirent.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 #include <boost/iostreams/filtering_streambuf.hpp>
 #include <boost/iostreams/stream.hpp>
@@ -274,10 +277,26 @@ void configcache::readConfig(bool *readnewconfig, int *nequileft, vector<unsigne
 void configcache::openOutFile(int actnequi)
 { 
   time_t secstamp = time(NULL);
+  int iseq=0;
+  
+  while (true) {
+    outFileName.str("");
+    outFileName << DATADIR << "/" << secstamp << "." << iseq << "_" << getFileId(actnequi) << "_.edat.tmp";
+
+    int tmpfd = open(outFileName.str().c_str(), O_CREAT | O_EXCL, 0644);
 
-  outFileName.str("");
-  outFileName << DATADIR << "/" << secstamp << "_" << getFileId(actnequi) << "_.edat.tmp";
+    if ( tmpfd != -1 ) {
+      close(tmpfd);
+      break;
+    }
+    else if ( errno != EEXIST ) {
+      if(log) *log << "CCACHE: Could not create cachefile!" << endl << flush;
+      exit(1);
+    }
 
+    iseq++;
+  }
+  
   outFile.open( outFileName.str().c_str(), std::ios::binary );
 
   ioBuffers->out = new boost::iostreams::filtering_ostreambuf;