Don't truncate the log file while checking flock

install-platforms
Lukas Werling 2017-11-08 00:23:44 +01:00
parent 60692c793d
commit 1182e5ebd5
1 changed files with 12 additions and 2 deletions

View File

@ -52,12 +52,18 @@ bool OpenLog()
#ifdef _WIN32
while (!(C4LogFile = _fsopen(Config.AtUserDataPath(sLogFileName.getData()), "wt", _SH_DENYWR)))
#elif defined(HAVE_SYS_FILE_H)
while (!(C4LogFile = fopen(Config.AtUserDataPath(sLogFileName.getData()), "wb")) || flock(fileno(C4LogFile),LOCK_EX|LOCK_NB))
int fd = 0;
while (!(fd = open(Config.AtUserDataPath(sLogFileName.getData()), O_WRONLY | O_CREAT)) || flock(fd, LOCK_EX|LOCK_NB))
#else
while (!(C4LogFile = fopen(Config.AtUserDataPath(sLogFileName.getData()), "wb")))
#endif
{
if(C4LogFile) fclose(C4LogFile); // Already locked by another instance?
// Already locked by another instance?
#ifdef HAVE_SYS_FILE_H
if (fd) close(fd);
#else
if (C4LogFile) fclose(C4LogFile);
#endif
// If the file does not yet exist, the directory is r/o
// don't go on then, or we have an infinite loop
if (access(Config.AtUserDataPath(sLogFileName.getData()), 0))
@ -65,6 +71,10 @@ bool OpenLog()
// try different name
sLogFileName.Format(C4CFN_LogEx, iLog++);
}
#ifdef HAVE_SYS_FILE_H
ftruncate(fd, 0);
C4LogFile = fdopen(fd, "wb");
#endif
// save start time
time(&C4LogStartTime);
return true;