Blame SOURCES/fapolicyd-trust-file-append.patch

b63e47
diff --color -ru a/src/library/trust-file.c b/src/library/trust-file.c
b63e47
--- a/src/library/trust-file.c	2021-12-13 09:37:56.633741747 +0100
b63e47
+++ b/src/library/trust-file.c	2021-12-13 13:44:13.689151921 +0100
b63e47
@@ -176,8 +176,11 @@
b63e47
 	list_t content;
b63e47
 	list_init(&content);
b63e47
 	int rc = trust_file_load(fpath, &content);
b63e47
-	if (rc)
b63e47
+	// if trust file does not exist, we ignore it as it will be created while writing
b63e47
+	if (rc == 2) {
b63e47
+		// exit on parse error, we dont want invalid entries to be removed
b63e47
 		return 1;
b63e47
+	}
b63e47
 
b63e47
 	for (list_item_t *lptr = list->first; lptr; lptr = lptr->next) {
b63e47
 		int i = 0;
b63e47
@@ -187,9 +190,16 @@
b63e47
 	list_merge(&content, list);
b63e47
 	write_out_list(&content, fpath);
b63e47
 	list_empty(&content);
b63e47
-	return rc ? 1 : 0;
b63e47
+	return 0;
b63e47
 }
b63e47
 
b63e47
+/**
b63e47
+ * @brief Load trust file into list
b63e47
+ *
b63e47
+ * @param fpath Full path to trust file
b63e47
+ * @param list Trust file will be loaded into this list
b63e47
+ * @return 0 on success, 1 if file can't be open, 2 on parsing error
b63e47
+ */
b63e47
 int trust_file_load(const char *fpath, list_t *list)
b63e47
 {
b63e47
 	char buffer[BUFFER_SIZE];
b63e47
@@ -197,10 +207,8 @@
b63e47
 	long line = 0;
b63e47
 
b63e47
 	FILE *file = fopen(fpath, "r");
b63e47
-	if (!file) {
b63e47
-		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
+	if (!file)
b63e47
 		return 1;
b63e47
-	}
b63e47
 
b63e47
 	while (fgets(buffer, BUFFER_SIZE, file)) {
b63e47
 		char name[4097], sha[65], *index = NULL, *data = NULL;
b63e47
@@ -257,7 +265,17 @@
b63e47
 {
b63e47
 	list_t list;
b63e47
 	list_init(&list);
b63e47
-	trust_file_load(fpath, &list);
b63e47
+	int rc = trust_file_load(fpath, &list);
b63e47
+	switch (rc) {
b63e47
+	case 1:
b63e47
+		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
+		return 0;
b63e47
+	case 2:
b63e47
+		list_empty(&list);
b63e47
+		return -1;
b63e47
+	default:
b63e47
+		break;
b63e47
+	}
b63e47
 
b63e47
 	int count = 0;
b63e47
 	size_t path_len = strlen(path);
b63e47
@@ -295,7 +313,17 @@
b63e47
 {
b63e47
 	list_t list;
b63e47
 	list_init(&list);
b63e47
-	trust_file_load(fpath, &list);
b63e47
+	int rc = trust_file_load(fpath, &list);
b63e47
+	switch (rc) {
b63e47
+	case 1:
b63e47
+		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
+		return 0;
b63e47
+	case 2:
b63e47
+		list_empty(&list);
b63e47
+		return -1;
b63e47
+	default:
b63e47
+		break;
b63e47
+	}
b63e47
 
b63e47
 	int count = 0;
b63e47
 	size_t path_len = strlen(path);
b63e47
@@ -320,20 +348,26 @@
b63e47
 {
b63e47
 	list_t trust_file;
b63e47
 	list_init(&trust_file);
b63e47
-
b63e47
 	int rc = trust_file_load(fpath, &trust_file);
b63e47
-	if (rc)
b63e47
-		goto cleanup;
b63e47
-
b63e47
+	switch (rc) {
b63e47
+	case 1:
b63e47
+		msg(LOG_ERR, "Cannot open %s", fpath);
b63e47
+		return -1;
b63e47
+	case 2:
b63e47
+		list_empty(&trust_file);
b63e47
+		return -1;
b63e47
+	default:
b63e47
+		break;
b63e47
+	}
b63e47
+	
b63e47
 	for (list_item_t *lptr = trust_file.first; lptr; lptr = lptr->next) {
b63e47
 		list_remove(list, lptr->index);
b63e47
 		if (list->count == 0)
b63e47
 			break;
b63e47
 	}
b63e47
 
b63e47
-cleanup:
b63e47
 	list_empty(&trust_file);
b63e47
-	return rc;
b63e47
+	return 0;
b63e47
 }
b63e47
 
b63e47