ctdb: Use stdio's getline() in ctdb_connection_list_read()
authorVolker Lendecke <vl@samba.org>
Fri, 1 Mar 2024 20:16:57 +0000 (21:16 +0100)
committerMartin Schwenke <martins@samba.org>
Tue, 16 Apr 2024 23:51:45 +0000 (23:51 +0000)
This is the only user of common/line.[ch], which can go next.

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Martin Schwenke <mschwenke@ddn.com>
ctdb/protocol/protocol_util.c

index 01756cfa7596f9e5068ef052b45267da818692a7..25e668b73ee58fe2390122e9323eef181bb6e3c3 100644 (file)
 
 #include <talloc.h>
 
-#include "common/line.h"
-
 #include "protocol.h"
 #include "protocol_util.h"
 #include "lib/util/util.h"
 #include "lib/util/smb_strtox.h"
+#include "lib/util/util_file.h"
 
 static struct {
        enum ctdb_runstate runstate;
@@ -712,10 +711,10 @@ struct ctdb_connection_list_read_state {
        bool client_first;
 };
 
-static int ctdb_connection_list_read_line(char *line, void *private_data)
+static int ctdb_connection_list_read_line(
+       char *line,
+       struct ctdb_connection_list_read_state *state)
 {
-       struct ctdb_connection_list_read_state *state =
-               (struct ctdb_connection_list_read_state *)private_data;
        struct ctdb_connection conn;
        int ret;
 
@@ -748,7 +747,11 @@ int ctdb_connection_list_read(TALLOC_CTX *mem_ctx,
                              struct ctdb_connection_list **conn_list)
 {
        struct ctdb_connection_list_read_state state;
+       char *line = NULL;
+       FILE *f = NULL;
        int ret;
+       size_t len = 0;
+       ssize_t nread;
 
        if (conn_list == NULL) {
                return EINVAL;
@@ -761,12 +764,23 @@ int ctdb_connection_list_read(TALLOC_CTX *mem_ctx,
 
        state.client_first = client_first;
 
-       ret = line_read(fd,
-                       128,
-                       mem_ctx,
-                       ctdb_connection_list_read_line,
-                       &state,
-                       NULL);
+       f = fdopen_keepfd(fd, "r");
+       if (f == NULL) {
+               return errno;
+       }
+
+       while ((nread = getline(&line, &len, f)) != -1) {
+               if ((nread > 0) && (line[nread-1] == '\n')) {
+                       line[nread-1] = '\0';
+               }
+               ret = ctdb_connection_list_read_line(line, &state);
+               if (ret != 0) {
+                       break;
+               }
+       }
+
+       free(line);
+       fclose(f);
 
        *conn_list = state.list;