From 1771c8198094b2f5bdbb228b90627817dcc7d934 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 10 Jan 2025 17:51:56 +0000 Subject: [PATCH] pty: Implement to stream a filelist into stdin This is an improved implementation that even supports writing some data if there is not enough buffer space to write the full filename at once. Signed-off-by: Michael Tremer --- src/pakfire/pty.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++ src/pakfire/pty.h | 15 +++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/pakfire/pty.c b/src/pakfire/pty.c index 0c9ab9d84..37bdb082b 100644 --- a/src/pakfire/pty.c +++ b/src/pakfire/pty.c @@ -30,6 +30,8 @@ #include #include +#include +#include #include #include #include @@ -1357,3 +1359,70 @@ ssize_t pakfire_pty_send_buffer(struct pakfire_ctx* ctx, return length; } + +ssize_t pakfire_pty_send_filelist(struct pakfire_ctx* ctx, + void* data, char* buffer, size_t length) { + struct pakfire_pty_filelist* input = data; + struct pakfire_file* file = NULL; + int r; + + // If there is any path data left, we send that first + if (input->p) { + // How much data do we have left? + size_t l = strlen(input->p); + + // Cap the length of the buffer + if (l < length) + length = l; + + memcpy(buffer, input->p, length); + + // If we could not send all data, we will move the pointer forward + if (l > length) + input->p += length; + + // If we have sent all data, we reset the pointer + else if (l == length) + input->p = NULL; + + return length; + } + + // Read the next file + file = pakfire_filelist_get(input->filelist, input->i++); + + // If we could not fetch a file, we have reached the end of the list + if (!file) { + // Reset the counter so we can run again + input->i = 0; + + return 0; + } + + // Fetch the path + const char* path = pakfire_file_get_path(file); + if (!path) { + r = -EINVAL; + goto ERROR; + } + + // Copy the path to the buffer + r = pakfire_string_format(input->buffer, "%s\n", path); + if (r < 0) + goto ERROR; + + // Set the pointer to the start + input->p = input->buffer; + + // Free the file + pakfire_file_unref(file); + + // Send the buffer + return pakfire_pty_send_filelist(ctx, input, buffer, length); + +ERROR: + if (file) + pakfire_file_unref(file); + + return r; +} diff --git a/src/pakfire/pty.h b/src/pakfire/pty.h index 38bccb295..b9c5fdedd 100644 --- a/src/pakfire/pty.h +++ b/src/pakfire/pty.h @@ -25,6 +25,7 @@ #include #include +#include struct pakfire_pty; @@ -66,4 +67,18 @@ struct pakfire_pty_buffer { ssize_t pakfire_pty_send_buffer(struct pakfire_ctx* ctx, void* data, char* buffer, size_t length); +// Stream a filelist + +struct pakfire_pty_filelist { + struct pakfire_filelist* filelist; + size_t i; + + // Buffer for the path + char buffer[PATH_MAX]; + const char* p; +}; + +ssize_t pakfire_pty_send_filelist(struct pakfire_ctx* ctx, + void* data, char* buffer, size_t length); + #endif /* PAKFIRE_PTY_H */ -- 2.47.3