[pacman-dev] [PATCH 6/8] Add events on pacnew/pacsave/pacorig file creation

Olivier Brunel jjk at jjacky.com
Mon Dec 2 15:45:18 EST 2013


ALPM still adds a warning to the log, but doesn't emit an event about
said warning, instead using a specific event to let the frontend what
happened/how to inform the user.

Signed-off-by: Olivier Brunel <jjk at jjacky.com>
---
 lib/libalpm/add.c     | 41 +++++++++++++++++++++++++++++++++++------
 lib/libalpm/alpm.h    | 41 ++++++++++++++++++++++++++++++++++++++++-
 lib/libalpm/remove.c  |  7 ++++++-
 src/pacman/callback.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/pacman/util.c     | 13 +++++++++++++
 src/pacman/util.h     |  1 +
 6 files changed, 143 insertions(+), 8 deletions(-)

diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index 3067df5..50e35a6 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -363,8 +363,14 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
 				if(try_rename(handle, checkfile, newpath)) {
 					errors++;
 				} else {
-					_alpm_log(handle, ALPM_LOG_WARNING, _("%s installed as %s\n"),
-							filename, newpath);
+					alpm_event_pacnew_created_t event = {
+						.type = ALPM_EVENT_PACNEW_CREATED,
+						.from_noupgrade = 0,
+						.oldpkg = oldpkg,
+						.newpkg = newpkg,
+						.oldfile = filename
+					};
+					EVENT(handle, &event);
 					alpm_logaction(handle, ALPM_CALLER_PREFIX,
 							"warning: %s installed as %s\n", filename, newpath);
 				}
@@ -391,8 +397,12 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
 					if(try_rename(handle, checkfile, filename)) {
 						errors++;
 					} else {
-						_alpm_log(handle, ALPM_LOG_WARNING,
-								_("%s saved as %s\n"), filename, newpath);
+						alpm_event_pacorig_created_t event = {
+							.type = ALPM_EVENT_PACORIG_CREATED,
+							.newpkg = newpkg,
+							.oldfile = filename
+						};
+						EVENT(handle, &event);
 						alpm_logaction(handle, ALPM_CALLER_PREFIX,
 								"warning: %s saved as %s\n", filename, newpath);
 					}
@@ -407,14 +417,16 @@ needbackup_cleanup:
 		free(hash_local);
 		free(hash_pkg);
 	} else {
+		size_t len;
 		/* we didn't need a backup */
 		if(notouch) {
 			/* change the path to a .pacnew extension */
 			_alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoUpgrade -- skipping\n", filename);
-			_alpm_log(handle, ALPM_LOG_WARNING, _("extracting %s as %s.pacnew\n"), filename, filename);
 			alpm_logaction(handle, ALPM_CALLER_PREFIX,
 					"warning: extracting %s as %s.pacnew\n", filename, filename);
-			strncat(filename, ".pacnew", PATH_MAX - strlen(filename));
+			/* remember len so we can get the old filename back for the event */
+			len = strlen(filename);
+			strncat(filename, ".pacnew", PATH_MAX - len);
 		} else {
 			_alpm_log(handle, ALPM_LOG_DEBUG, "extracting %s\n", filename);
 		}
@@ -433,6 +445,23 @@ needbackup_cleanup:
 			return errors;
 		}
 
+		if(notouch) {
+			alpm_event_pacnew_created_t event = {
+				.type = ALPM_EVENT_PACNEW_CREATED,
+				.from_noupgrade = 1,
+				.oldpkg = oldpkg,
+				.newpkg = newpkg,
+				.oldfile = filename
+			};
+			/* "remove" the .pacnew suffix */
+			if(len < PATH_MAX)
+				filename[len] = '\0';
+			EVENT(handle, &event);
+			/* restore */
+			if(len < PATH_MAX)
+				filename[len] = '.';
+		}
+
 		/* calculate an hash if this is in newpkg's backup */
 		alpm_list_t *i;
 		for(i = alpm_pkg_get_backup(newpkg); i; i = i->next) {
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index bf11e41..2f8d6d5 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -337,7 +337,15 @@ typedef enum _alpm_event_type_t {
 	/** Key downloading is finished. */
 	ALPM_EVENT_KEY_DOWNLOAD_DONE,
 	/** A log message was emitted; See alpm_event_log_t for arguments. */
-	ALPM_EVENT_LOG
+	ALPM_EVENT_LOG,
+	/** A .pacnew file was created; See alpm_event_pacnew_created_t for arguments. */
+	ALPM_EVENT_PACNEW_CREATED,
+	/** A .pacsave file was created; See alpm_event_pacsave_created_t for
+	 * arguments */
+	ALPM_EVENT_PACSAVE_CREATED,
+	/** A .pacorig file was created; See alpm_event_pacorig_created_t for
+	 * arguments */
+	ALPM_EVENT_PACORIG_CREATED
 } alpm_event_type_t;
 
 /** Events.
@@ -424,6 +432,37 @@ typedef struct _alpm_event_log_t {
 	va_list args;
 } alpm_event_log_t;
 
+typedef struct _alpm_event_pacnew_created_t {
+	/** Type of event. */
+	alpm_event_type_t type;
+	/** Whether the creation was result of a NoUpgrade or not */
+	int from_noupgrade;
+	/** Old package. */
+	alpm_pkg_t *oldpkg;
+	/** New Package. */
+	alpm_pkg_t *newpkg;
+	/** Filename of the old file. */
+	const char *oldfile;
+} alpm_event_pacnew_created_t;
+
+typedef struct _alpm_event_pacsave_created_t {
+	/** Type of event. */
+	alpm_event_type_t type;
+	/** Old package. */
+	alpm_pkg_t *oldpkg;
+	/** Filename of the old file. */
+	const char *oldfile;
+} alpm_event_pacsave_created_t;
+
+typedef struct _alpm_event_pacorig_created_t {
+	/** Type of event. */
+	alpm_event_type_t type;
+	/** New package. */
+	alpm_pkg_t *newpkg;
+	/** Filename of the old file. */
+	const char *oldfile;
+} alpm_event_pacorig_created_t;
+
 /** Event callback. */
 typedef void (*alpm_cb_event)(alpm_event_t *);
 
diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c
index 32d71ab..f6dc99b 100644
--- a/lib/libalpm/remove.c
+++ b/lib/libalpm/remove.c
@@ -526,6 +526,11 @@ static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
 				int cmp = filehash ? strcmp(filehash, backup->hash) : 0;
 				FREE(filehash);
 				if(cmp != 0) {
+					alpm_event_pacsave_created_t event = {
+						.type = ALPM_EVENT_PACSAVE_CREATED,
+						.oldpkg = oldpkg,
+						.oldfile = file
+					};
 					char *newpath;
 					size_t len = strlen(file) + 8 + 1;
 					MALLOC(newpath, len, RET_ERR(handle, ALPM_ERR_MEMORY, -1));
@@ -540,7 +545,7 @@ static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
 						free(newpath);
 						return -1;
 					}
-					_alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
+					EVENT(handle, &event);
 					alpm_logaction(handle, ALPM_CALLER_PREFIX,
 							"warning: %s saved as %s\n", file, newpath);
 					free(newpath);
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index af1c99d..c881b69 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -288,6 +288,54 @@ void cb_event(alpm_event_t *event)
 				}
 			}
 			break;
+		case ALPM_EVENT_PACNEW_CREATED:
+			{
+				alpm_event_pacnew_created_t *e = (alpm_event_pacnew_created_t *) event;
+				if(on_progress) {
+					char *string = NULL;
+					pm_sprintf(&string, ALPM_LOG_WARNING, _("%s installed as %s.pacnew\n"),
+							e->oldfile, e->oldfile);
+					if(string != NULL) {
+						output = alpm_list_add(output, string);
+					}
+				} else {
+					pm_printf(ALPM_LOG_WARNING, _("%s installed as %s.pacnew\n"),
+							e->oldfile, e->oldfile);
+				}
+			}
+			break;
+		case ALPM_EVENT_PACSAVE_CREATED:
+			{
+				alpm_event_pacsave_created_t *e = (alpm_event_pacsave_created_t *) event;
+				if(on_progress) {
+					char *string = NULL;
+					pm_sprintf(&string, ALPM_LOG_WARNING, _("%s saved as %s.pacsave\n"),
+							e->oldfile, e->oldfile);
+					if(string != NULL) {
+						output = alpm_list_add(output, string);
+					}
+				} else {
+					pm_printf(ALPM_LOG_WARNING, _("%s saved as %s.pacsave\n"),
+							e->oldfile, e->oldfile);
+				}
+			}
+			break;
+		case ALPM_EVENT_PACORIG_CREATED:
+			{
+				alpm_event_pacorig_created_t *e = (alpm_event_pacorig_created_t *) event;
+				if(on_progress) {
+					char *string = NULL;
+					pm_sprintf(&string, ALPM_LOG_WARNING, _("%s saved as %s.pacorig\n"),
+							e->oldfile, e->oldfile);
+					if(string != NULL) {
+						output = alpm_list_add(output, string);
+					}
+				} else {
+					pm_printf(ALPM_LOG_WARNING, _("%s saved as %s.pacorig\n"),
+							e->oldfile, e->oldfile);
+				}
+			}
+			break;
 		/* all the simple done events, with fallthrough for each */
 		case ALPM_EVENT_FILECONFLICTS_DONE:
 		case ALPM_EVENT_CHECKDEPS_DONE:
diff --git a/src/pacman/util.c b/src/pacman/util.c
index 4877cc3..ccec4b8 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -1577,6 +1577,19 @@ int pm_asprintf(char **string, const char *format, ...)
 	return ret;
 }
 
+int pm_sprintf(char **string, alpm_loglevel_t level, const char *format, ...)
+{
+	int ret = 0;
+	va_list args;
+
+	/* print the message using va_arg list */
+	va_start(args, format);
+	ret = pm_vasprintf(string, level, format, args);
+	va_end(args);
+
+	return ret;
+}
+
 int pm_vasprintf(char **string, alpm_loglevel_t level, const char *format, va_list args)
 {
 	int ret = 0;
diff --git a/src/pacman/util.h b/src/pacman/util.h
index e2297f8..0e85caa 100644
--- a/src/pacman/util.h
+++ b/src/pacman/util.h
@@ -78,6 +78,7 @@ int noyes(const char *format, ...) __attribute__((format(printf, 1, 2)));
 int pm_printf(alpm_loglevel_t level, const char *format, ...) __attribute__((format(printf,2,3)));
 int pm_asprintf(char **string, const char *format, ...) __attribute__((format(printf,2,3)));
 int pm_vfprintf(FILE *stream, alpm_loglevel_t level, const char *format, va_list args) __attribute__((format(printf,3,0)));
+int pm_sprintf(char **string, alpm_loglevel_t level, const char *format, ...) __attribute__((format(printf,3,4)));
 int pm_vasprintf(char **string, alpm_loglevel_t level, const char *format, va_list args) __attribute__((format(printf,3,0)));
 
 #endif /* _PM_UTIL_H */
-- 
1.8.4.2



More information about the pacman-dev mailing list