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

Simon Gomizelj simongmzlj at gmail.com
Wed May 22 00:39:53 EDT 2013


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

diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 033058a..c1fd0bb 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -555,23 +555,34 @@ 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)
+		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, _("failed to read from pipe (%s)\n"), strerror(errno));
+					retval = 1;
 					break;
-				alpm_logaction(handle, "%s", line);
-				EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL);
+				}
+				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) {
@@ -582,11 +593,6 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[])
 			}
 		}
 
-		/* report error from above after the child has exited */
-		if(retval != 0) {
-			_alpm_log(handle, ALPM_LOG_ERROR, _("could not open pipe (%s)\n"), strerror(errno));
-			goto cleanup;
-		}
 		/* check the return status, make sure it is 0 (success) */
 		if(WIFEXITED(status)) {
 			_alpm_log(handle, ALPM_LOG_DEBUG, "call to waitpid succeeded\n");
-- 
1.8.0.2



More information about the pacman-dev mailing list