From: H.G. Muller Date: Sun, 24 Apr 2011 15:15:23 +0000 (+0200) Subject: Lock game an position file during writing X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=5bd571321c23608ce3f07bf523a8bbf2ddda2210;p=xboard.git Lock game an position file during writing The use of flock() on the file desctiptor of the opened stream should guarantee multiple XBoard instances, saving on the same file, will not interleave their PGN games or FEN positions. --- diff --git a/backend.c b/backend.c index f6e75c4..9ecc707 100644 --- a/backend.c +++ b/backend.c @@ -270,6 +270,7 @@ char chatPartner[MAX_CHAT][MSG_SIZ]; /* [HGM] chat: list of chatting partners */ extern int chatCount; int chattingPartner; char marker[BOARD_RANKS][BOARD_FILES]; /* [HGM] marks for target squares */ +char lastMsg[MSG_SIZ]; ChessSquare pieceSweep = EmptySquare; ChessSquare promoSweep = EmptySquare, defaultPromoChoice; int promoDefaultAltered; @@ -10991,6 +10992,7 @@ SaveGameToFile(filename, append) { FILE *f; char buf[MSG_SIZ]; + int result; if (strcmp(filename, "-") == 0) { return SaveGame(stdout, 0, NULL); @@ -11001,7 +11003,14 @@ SaveGameToFile(filename, append) DisplayError(buf, errno); return FALSE; } else { - return SaveGame(f, 0, NULL); + safeStrCpy(buf, lastMsg, MSG_SIZ); + DisplayMessage(_("Waiting for access to save file"), ""); + flock(fileno(f), LOCK_EX); // [HGM] lock: lock file while we are writing + DisplayMessage(_("Saving game"), ""); + if(lseek(fileno(f), 0, SEEK_END) == -1) DisplayError("Bad Seek", errno); // better safe than sorry... + result = SaveGame(f, 0, NULL); + DisplayMessage(buf, ""); + return result; } } } @@ -11363,7 +11372,13 @@ SavePositionToFile(filename) DisplayError(buf, errno); return FALSE; } else { + safeStrCpy(buf, lastMsg, MSG_SIZ); + DisplayMessage(_("Waiting for access to save file"), ""); + flock(fileno(f), LOCK_EX); // [HGM] lock + DisplayMessage(_("Saving position"), ""); + lseek(fileno(f), 0, SEEK_END); // better safe than sorry... SavePosition(f, 0, NULL); + DisplayMessage(buf, ""); return TRUE; } } diff --git a/backend.h b/backend.h index fe3faeb..0a290a9 100644 --- a/backend.h +++ b/backend.h @@ -110,6 +110,7 @@ extern char* programVersion; extern ProcRef firstProgramPR, secondProgramPR; extern Board boards[]; extern char marker[BOARD_RANKS][BOARD_FILES]; +extern char lastMsg[MSG_SIZ]; char *CmailMsg P((void)); /* Tord: Added the useFEN960 parameter in PositionToFEN() below */ diff --git a/winboard/winboard.c b/winboard/winboard.c index 4d823eb..c8bf0b2 100644 --- a/winboard/winboard.c +++ b/winboard/winboard.c @@ -8114,7 +8114,8 @@ DisplayMessage(char *str1, char *str2) if (len > remain) len = remain; strncat(messageText, str2, len); } - messageText[MESSAGE_TEXT_MAX - 1] = NULLCHAR; + messageText[MESSAGE_TEXT_MAX - 1] = NULLCHAR; + safeStrCpy(lastMsg, messageText, MSG_SIZ); if (hwndMain == NULL || IsIconic(hwndMain)) return; diff --git a/xboard.c b/xboard.c index a2746e8..e9c4347 100644 --- a/xboard.c +++ b/xboard.c @@ -6764,6 +6764,8 @@ void DisplayMessage(message, extMessage) }; }; + safeStrCpy(lastMsg, message, MSG_SIZ); // [HGM] make available + /* need to test if messageWidget already exists, since this function can also be called during the startup, if for example a Xresource is not set up correctly */