From 1896c2128b6f1b9a4216059b3e7293c6c1c13d9c Mon Sep 17 00:00:00 2001 From: Michael Giacomelli Date: Mon, 5 Oct 2026 10:54:16 -0400 Subject: [PATCH] test_codec: keep the log if the plugin is left early A file's length and first cluster are only written to its directory entry when it is closed, and the log was closed at the end of plugin_start() alone. Leaving by USB or by power off goes through exit() instead, so a whole run's log could be left as an empty file with its data in clusters nothing pointed to. Close the log from an atexit handler, close it when a run over a folder ends, and close and reopen it after each track so that at most one result is lost if the player dies. Co-Authored-By: Claude Opus 5.5 Change-Id: I613d0ad8c637d7f48a82eb09bc310c6fcc6d77f9 --- apps/plugins/test_codec.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/apps/plugins/test_codec.c b/apps/plugins/test_codec.c index d706c2eced..b38ae221e0 100644 --- a/apps/plugins/test_codec.c +++ b/apps/plugins/test_codec.c @@ -44,16 +44,31 @@ static int line = 0; static int max_line = 0; static int line_height = 0; static int log_fd = -1; +static char logfilename[MAX_PATH]; static void log_close(void) { if (log_fd >= 0) rb->close(log_fd); + log_fd = -1; +} + +/* The file's length only reaches the disk when it is closed. So that what + has been logged survives the player being switched off, or its battery + running out, before the plugin is left: close the log and open it again + to carry on. */ +static void log_commit(void) +{ + if (log_fd >= 0) + { + rb->close(log_fd); + log_fd = rb->open(logfilename, O_WRONLY|O_APPEND); + } } static bool log_init(bool use_logfile) { - char logfilename[MAX_PATH]; + static bool close_at_exit = false; rb->lcd_getstringsize("A", NULL, &line_height); max_line = LCD_HEIGHT / line_height; @@ -61,8 +76,16 @@ static bool log_init(bool use_logfile) rb->lcd_clear_display(); rb->lcd_update(); + log_close(); + if (use_logfile) { - log_close(); + /* Leaving on USB or power off goes through exit(), not the end of + plugin_start(). */ + if (!close_at_exit) + { + atexit(log_close); + close_at_exit = true; + } rb->create_numbered_filename(logfilename, HOME_DIR, "test_codec_log_", ".txt", 2 IF_CNFN_NUM_(, NULL)); log_fd = rb->open(logfilename, O_RDWR|O_CREAT|O_TRUNC, 0666); @@ -1014,6 +1037,7 @@ menu: break; log_text("", true); + log_commit(); } /* Read next entry */ @@ -1022,6 +1046,9 @@ menu: rb->closedir(dir); } + + /* The run is over: nothing more goes in the log. */ + log_close(); } else { /* Just test the file */ res = test_track(parameter);