[pacman-dev] [PATCH] Ensure a terminating newline from scriptlet output

Simon Gomizelj simongmzlj at gmail.com
Thu Dec 13 10:47:10 EST 2012


Prevent scriptlets from clobbering pacman.log because they don't end
their outputs with a newline.
---
 lib/libalpm/util.c | 40 ++++++++++++++++++++++++++--------------
 1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 033058a..3b4f4e0 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -555,23 +555,35 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[])
 		exit(1);
 	} else {
 		/* this code runs for the parent only (wait on the child) */
-		int status;
-		FILE *pipe_file;
+		int status, need_newline = 0;
 
 		CLOSE(pipefd[1]);
-		pipe_file = fdopen(pipefd[0], "r");
-		if(pipe_file == NULL) {
-			CLOSE(pipefd[0]);
-			retval = 1;
-		} else {
-			while(!feof(pipe_file)) {
-				char line[PATH_MAX];
-				if(fgets(line, PATH_MAX, pipe_file) == NULL)
-					break;
-				alpm_logaction(handle, "%s", line);
-				EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL);
+		for(;;) {
+			char line[BUFSIZ];
+			int nbytes_r = read(pipefd[0], line, BUFSIZ);
+
+			if(nbytes_r == 0) {
+				break;
+			} else if(nbytes_r == -1) {
+				if (errno != EINTR) {
+					_alpm_log(handle, ALPM_LOG_ERROR, _("call to read failed (%s)\n"), strerror(errno));
+					CLOSE(pipefd[0]);
+					retval = 1;
+					goto cleanup;
+				}
+				continue;
 			}
-			fclose(pipe_file);
+
+			/* we'll need to manually add a newline if the read data doesn't end with one already */
+			need_newline = (line[nbytes_r - 1] != '\n');
+
+			alpm_logaction(handle, "%s", line);
+			EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL);
+		}
+		CLOSE(pipefd[0]);
+
+		if (need_newline) {
+			alpm_logaction(handle, "\n");
 		}
 
 		while(waitpid(pid, &status, 0) == -1) {
-- 
1.8.0.2



More information about the pacman-dev mailing list