BUG/MINOR: server-state: Ignore empty files
Users might want to pre-create an empty file for later dumping
server-states. This commit allows for that by emiting a notice in case
file is empty and a warning if file is not empty, but version is unknown
Fix partially: #2190
CF: Technically speaking, this patch can be backported to all stable
versions. But it is better to do so to 2.8 only for now.
(cherry picked from commit 122a903b94d5db52923ebd2f49a960defddfd40a)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
diff --git a/src/server_state.c b/src/server_state.c
index d2cc4d8..9636b9f 100644
--- a/src/server_state.c
+++ b/src/server_state.c
@@ -522,6 +522,7 @@
/*
* read next line from file <f> and return the server state version if one found.
+ * If file is empty, then -1 is returned
* If no version is found, then 0 is returned
* Note that this should be the first read on <f>
*/
@@ -532,7 +533,7 @@
/* first character of first line of the file must contain the version of the export */
if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL)
- return 0;
+ return -1;
vsn = strtol(mybuf, &endptr, 10);
if (endptr == mybuf || *endptr != '\n') {
@@ -803,9 +804,13 @@
}
global_vsn = srv_state_get_version(f);
- if (global_vsn == 0) {
- ha_warning("config: Can't get version of the global server state file '%s'.\n",
- file);
+ if (global_vsn < 1) {
+ if (global_vsn == -1)
+ ha_notice("config: Empty global server state file '%s'.\n",
+ file);
+ if (global_vsn == 0)
+ ha_warning("config: Can't get version of the global server state file '%s'.\n",
+ file);
goto close_globalfile;
}
@@ -873,9 +878,13 @@
/* first character of first line of the file must contain the version of the export */
local_vsn = srv_state_get_version(f);
- if (local_vsn == 0) {
- ha_warning("Proxy '%s': Can't get version of the server state file '%s'.\n",
- curproxy->id, file);
+ if (local_vsn < 1) {
+ if (local_vsn == -1)
+ ha_notice("Proxy '%s': Empty server state file '%s'.\n",
+ curproxy->id, file);
+ if (local_vsn == 0)
+ ha_warning("Proxy '%s': Can't get version of the server state file '%s'.\n",
+ curproxy->id, file);
goto close_localfile;
}