11#include " TRestDataBase.h"
22
33#include < errno.h>
4+ #include < fcntl.h>
45#include < stdio.h>
6+ #include < sys/file.h>
57#include < sys/stat.h>
68#include < sys/types.h>
9+ #include < unistd.h>
10+
11+ #include < chrono>
12+ #include < thread>
713
814#include " TClass.h"
915#include " TRestStringHelper.h"
@@ -152,18 +158,28 @@ TRestDataBase::TRestDataBase() {
152158// / Note that this file saves run number of the **next** run. So it returns
153159// / The run number -1.
154160int TRestDataBase::get_lastrun () {
155- int runNr;
161+ int runNr = 1 ;
156162 string runFilename = REST_USER_PATH + " /runNumber" ;
157- if (!TRestTools::fileExists (runFilename)) {
158- if (TRestTools::isPathWritable (REST_USER_PATH)) {
159- // we fix the "runNumber" file
160- TRestTools::Execute (" echo 1 > " + runFilename);
161- runNr = 1 ;
162- }
163+ bool fileExist = TRestTools::fileExists (runFilename);
164+ int fd = open (runFilename.c_str (), O_RDWR | O_CREAT, 0666 );
165+ if (fd == -1 ) {
166+ RESTError << " Error opening file " << runFilename << strerror (errno) << RESTendl;
167+ return -1 ;
168+ }
169+ flock (fd, LOCK_EX);
170+ if (!fileExist) {
171+ string newRun = to_string (runNr) + " \n " ;
172+ if (write (fd, newRun.c_str (), newRun.size ()) == -1 )
173+ RESTError << " Error writing file " << runFilename << strerror (errno) << RESTendl;
174+ fsync (fd);
163175 } else {
164- ifstream ifs (runFilename);
165- ifs >> runNr;
176+ lseek (fd, 0 , SEEK_SET);
177+ char buffer[64 ] = {0 };
178+ ssize_t bytesReaded = read (fd, buffer, sizeof (buffer) - 1 );
179+ if (bytesReaded > 0 ) runNr = std::atoi (buffer);
166180 }
181+ flock (fd, LOCK_UN);
182+ close (fd);
167183 // the number recorded in "runNumber" file is for the next run, we subtract 1 to get the latest run.
168184 return runNr - 1 ;
169185}
@@ -181,7 +197,7 @@ int TRestDataBase::get_lastrun() {
181197// /
182198// / The method in derived class shall follow this rule.
183199int TRestDataBase::set_run (DBEntry info, bool overwrite) {
184- int newRunNr;
200+ int newRunNr = - 1 ;
185201 if (info.runNr == 0 ) {
186202 newRunNr = get_lastrun () + 1 ;
187203 } else if (info.runNr > 0 ) {
@@ -192,13 +208,24 @@ int TRestDataBase::set_run(DBEntry info, bool overwrite) {
192208
193209 string runFilename = REST_USER_PATH + " /runNumber" ;
194210 if (TRestTools::isPathWritable (REST_USER_PATH)) {
195- TRestTools::Execute (" echo " + ToString (newRunNr + 1 ) + " > " + runFilename);
211+ int fd = open (runFilename.c_str (), O_RDWR | O_CREAT, 0666 );
212+ if (fd == -1 ) {
213+ RESTError << " Error opening file " << runFilename << strerror (errno) << RESTendl;
214+ return -1 ;
215+ }
216+ flock (fd, LOCK_EX);
217+ string newRun = to_string (newRunNr + 1 ) + " \n " ;
218+ if (write (fd, newRun.c_str (), newRun.size ()) == -1 )
219+ RESTError << " Error writing file " << runFilename << strerror (errno) << RESTendl;
220+ fsync (fd);
221+ flock (fd, LOCK_UN);
222+ close (fd);
223+
196224 } else {
197225 RESTWarning << " runNumber file not writable. auto run number "
198226 " increment is disabled"
199227 << RESTendl;
200228 }
201-
202229 return newRunNr;
203230}
204231
0 commit comments