1
1
/* vim:set cin ft=c sw=4 sts=4 ts=8 et ai cino=Ls\:0t0(0 : -*- mode:c;fill-column:80;tab-width:8;c-basic-offset:4;indent-tabs-mode:nil;c-file-style:"k&r" -*-*/
2
- /* Last modified by Alex Smith, 2015 -07-21 */
2
+ /* Last modified by Alex Smith, 2016 -07-07 */
3
3
/* Copyright (c) Daniel Thaler, 2011. */
4
4
/* NetHack may be freely redistributed. See license for details. */
5
5
@@ -2403,18 +2403,38 @@ log_replay_save_line(void)
2403
2403
char * logline ;
2404
2404
struct memfile bsave ;
2405
2405
2406
- if (!change_fd_lock (program_state .logfile , TRUE, LT_READ , 2 ))
2407
- panic ("Could not upgrade to read lock on logfile" );
2406
+ int tries = 30 ;
2408
2407
2409
- lseek (program_state .logfile ,
2410
- program_state .end_of_gamestate_location , SEEK_SET );
2408
+ /* The file should always end with a save line. If we don't have one,
2409
+ either the process got interrupted or we outraced the process that
2410
+ was meant to write it. */
2411
+ do {
2411
2412
2412
- logline = lgetline_malloc (program_state .logfile );
2413
+ if (!change_fd_lock (program_state .logfile , TRUE, LT_READ , 2 ))
2414
+ panic ("Could not upgrade to read lock on logfile" );
2413
2415
2414
- if (!change_fd_lock (program_state .logfile , TRUE, LT_MONITOR , 2 ))
2415
- panic ("Could not downgrade to monitor lock on logfile" );
2416
+ lseek (program_state .logfile ,
2417
+ program_state .end_of_gamestate_location , SEEK_SET );
2418
+
2419
+ logline = lgetline_malloc (program_state .logfile );
2420
+
2421
+ if (!change_fd_lock (program_state .logfile , TRUE, LT_MONITOR , 2 ))
2422
+ panic ("Could not downgrade to monitor lock on logfile" );
2423
+
2424
+ #ifndef WIN32
2425
+ /* Don't bother sleeping on Windows; this situation should be
2426
+ impossible anyway because watching doesn't work there */
2427
+ if (!logline )
2428
+ nanosleep (& (struct timespec ){.tv_nsec = 100000000 }, NULL );
2429
+ #endif
2430
+
2431
+ } while (!logline && tries -- );
2432
+
2433
+ if (!logline )
2434
+ return ; /* will probably cause an error if watching but that's what we
2435
+ want */
2416
2436
2417
- if (logline && * logline == '~' ) {
2437
+ if (* logline == '~' ) {
2418
2438
2419
2439
bsave = program_state .binary_save ;
2420
2440
program_state .binary_save_allocated = FALSE;
@@ -2424,15 +2444,15 @@ log_replay_save_line(void)
2424
2444
program_state .end_of_gamestate_location ;
2425
2445
load_gamestate_from_binary_save (TRUE);
2426
2446
2427
- } else if (logline && * logline == '*' ) {
2447
+ } else if (* logline == '*' ) {
2428
2448
2429
2449
load_save_backup_from_string (logline );
2430
2450
program_state .binary_save_location =
2431
2451
program_state .save_backup_location =
2432
2452
program_state .end_of_gamestate_location ;
2433
2453
load_gamestate_from_binary_save (TRUE);
2434
2454
2435
- } else if (logline && * logline == 'Q' ) {
2455
+ } else if (* logline == 'Q' ) {
2436
2456
2437
2457
terminate (GAME_ALREADY_OVER );
2438
2458
0 commit comments