From 3ceae7e051af8071ffc7aed6abe0049d9fa36483 Mon Sep 17 00:00:00 2001
From: Jan Kadlec <jan.kadlec@nic.cz>
Date: Thu, 3 Apr 2014 12:02:51 +0200
Subject: [PATCH] new_node: added missing doxygen, cleanup.

- knot_rrset_copy now returns pointer
---
 src/knot/dnssec/zone-sign.c   | 15 +++++-----
 src/knot/updates/changesets.h |  4 +--
 src/knot/updates/ddns.c       | 10 +++----
 src/knot/updates/ddns.h       |  6 ++--
 src/knot/updates/xfr-in.c     | 10 +++----
 src/knot/zone/node.c          |  2 +-
 src/knot/zone/zone-diff.c     | 22 +++++++--------
 src/libknot/rrset.c           | 47 ++++++++++++-------------------
 src/libknot/rrset.h           | 52 +++++++++++++++++++++++++++--------
 9 files changed, 92 insertions(+), 76 deletions(-)

diff --git a/src/knot/dnssec/zone-sign.c b/src/knot/dnssec/zone-sign.c
index b671732f0..adaaa6c40 100644
--- a/src/knot/dnssec/zone-sign.c
+++ b/src/knot/dnssec/zone-sign.c
@@ -742,7 +742,8 @@ static int remove_invalid_dnskeys(const knot_rrset_t *soa,
 
 	if (knot_rrset_rr_ttl(dnskeys, 0) != knot_rrset_rr_ttl(soa, 0)) {
 		dbg_dnssec_detail("removing DNSKEYs (SOA TTL differs)\n");
-		result = knot_rrset_copy(dnskeys, &to_remove, NULL);
+		to_remove = knot_rrset_copy(dnskeys, NULL);
+		result = to_remove ? KNOT_EOK : KNOT_ENOMEM;
 		goto done;
 	}
 
@@ -1351,15 +1352,15 @@ int knot_zone_sign_update_soa(const knot_rrset_t *soa,
 	knot_rrset_t *soa_from = NULL;
 	knot_rrset_t *soa_to = NULL;
 
-	result = knot_rrset_copy(soa, &soa_from, NULL);
-	if (result != KNOT_EOK) {
-		return result;
+	soa_from = knot_rrset_copy(soa, NULL);
+	if (soa_from == NULL) {
+		return KNOT_ENOMEM;
 	}
 
-	result = knot_rrset_copy(soa, &soa_to, NULL);
-	if (result != KNOT_EOK) {
+	soa_to =  knot_rrset_copy(soa, NULL);
+	if (soa_to == NULL) {
 		knot_rrset_free(&soa_from, NULL);
-		return result;
+		return KNOT_ENOMEM;
 	}
 
 	knot_rrs_soa_serial_set(&soa_to->rrs, new_serial);
diff --git a/src/knot/updates/changesets.h b/src/knot/updates/changesets.h
index 1e2281b98..423c98737 100644
--- a/src/knot/updates/changesets.h
+++ b/src/knot/updates/changesets.h
@@ -55,8 +55,8 @@ typedef struct knot_changeset {
 	uint32_t serial_from; /*!< SOA start serial. */
 	uint32_t serial_to; /*!< SOA destination serial. */
 	uint32_t flags; /*!< DDNS / IXFR flags. */
-	list_t old_data;
-	list_t new_data;
+	list_t old_data; /*!< Old data, to be freed after succesfull update. */
+	list_t new_data; /*!< New data, to be freed after failed update. */
 } knot_changeset_t;
 
 /*----------------------------------------------------------------------------*/
diff --git a/src/knot/updates/ddns.c b/src/knot/updates/ddns.c
index 5aa304059..47e4d2edf 100644
--- a/src/knot/updates/ddns.c
+++ b/src/knot/updates/ddns.c
@@ -61,7 +61,7 @@ static int add_rr_to_list(list_t *l, const knot_rrset_t *rr)
 		}
 	};
 
-	knot_rrset_t *rr_copy = knot_rrset_cpy(rr, NULL);
+	knot_rrset_t *rr_copy = knot_rrset_copy(rr, NULL);
 	if (rr_copy == NULL) {
 		return KNOT_ENOMEM;
 	}
@@ -497,7 +497,7 @@ static bool skip_record_addition(knot_changeset_t *changeset,
 static int add_rr_to_chgset(const knot_rrset_t *rr, knot_changeset_t *changeset,
                             int *apex_ns_rem)
 {
-	knot_rrset_t *rr_copy = knot_rrset_cpy(rr, NULL);
+	knot_rrset_t *rr_copy = knot_rrset_copy(rr, NULL);
 	if (rr_copy == NULL) {
 		return KNOT_ENOMEM;
 	}
@@ -536,7 +536,7 @@ static bool skip_record_removal(knot_changeset_t *changeset, knot_rrset_t *rr)
 static int rem_rr_to_chgset(const knot_rrset_t *rr, knot_changeset_t *changeset,
                             int *apex_ns_rem)
 {
-	knot_rrset_t *rr_copy = knot_rrset_cpy(rr, NULL);
+	knot_rrset_t *rr_copy = knot_rrset_copy(rr, NULL);
 	if (rr_copy == NULL) {
 		return KNOT_ENOMEM;
 	}
@@ -1016,7 +1016,7 @@ int knot_ddns_process_update(const knot_zone_contents_t *zone,
 			}
 			int64_t sn_rr = knot_rrs_soa_serial(&rr->rrs);
 			assert(knot_serial_compare(sn_rr, sn_old) > 0);
-			soa_end = knot_rrset_cpy(rr, NULL);
+			soa_end = knot_rrset_copy(rr, NULL);
 			if (soa_end == NULL) {
 				return KNOT_ENOMEM;
 			}
@@ -1030,7 +1030,7 @@ int knot_ddns_process_update(const knot_zone_contents_t *zone,
 			return KNOT_EOK;
 		}
 
-		soa_end = knot_rrset_cpy(soa_begin, NULL);
+		soa_end = knot_rrset_copy(soa_begin, NULL);
 		if (soa_end == NULL) {
 			*rcode = KNOT_RCODE_SERVFAIL;
 			return KNOT_ENOMEM;
diff --git a/src/knot/updates/ddns.h b/src/knot/updates/ddns.h
index 67652615e..796b0d56f 100644
--- a/src/knot/updates/ddns.h
+++ b/src/knot/updates/ddns.h
@@ -59,9 +59,9 @@ int knot_ddns_process_prereqs(const knot_pkt_t *query,
  * \return KNOT_E*
  */
 int knot_ddns_process_update(const knot_zone_contents_t *zone,
-                              const knot_pkt_t *query,
-                              knot_changeset_t *changeset,
-                              uint16_t *rcode, uint32_t new_serial);
+                             const knot_pkt_t *query,
+                             knot_changeset_t *changeset,
+                             uint16_t *rcode, uint32_t new_serial);
 
 #endif /* _KNOT_DDNS_H_ */
 
diff --git a/src/knot/updates/xfr-in.c b/src/knot/updates/xfr-in.c
index b5cdef9fd..78a62a7af 100644
--- a/src/knot/updates/xfr-in.c
+++ b/src/knot/updates/xfr-in.c
@@ -344,7 +344,7 @@ int xfrin_process_ixfr_packet(knot_pkt_t *pkt, knot_ns_xfr_t *xfr)
 		}
 
 		// just store the first SOA for later use
-		(*chs)->first_soa = knot_rrset_cpy(rr, NULL);
+		(*chs)->first_soa = knot_rrset_copy(rr, NULL);
 		if ((*chs)->first_soa == NULL) {
 			ret = KNOT_ENOMEM;
 			goto cleanup;
@@ -508,7 +508,7 @@ dbg_xfrin_exec_verb(
 				if (chset == NULL) {
 					goto cleanup;
 				}
-				knot_rrset_t *soa = knot_rrset_cpy(rr, NULL);
+				knot_rrset_t *soa = knot_rrset_copy(rr, NULL);
 				if (soa == NULL) {
 					ret = KNOT_ENOMEM;
 					goto cleanup;
@@ -526,7 +526,7 @@ dbg_xfrin_exec_verb(
 			if (rr->type == KNOT_RRTYPE_SOA) {
 				// we should not be here if soa_from is not set
 				assert(chset->soa_from != NULL);
-				knot_rrset_t *soa = knot_rrset_cpy(rr, NULL);
+				knot_rrset_t *soa = knot_rrset_copy(rr, NULL);
 				if (soa == NULL) {
 					ret = KNOT_ENOMEM;
 					goto cleanup;
@@ -537,7 +537,7 @@ dbg_xfrin_exec_verb(
 			} else {
 				// just add the RR to the REMOVE part and
 				// continue
-				knot_rrset_t *cpy = knot_rrset_cpy(rr, NULL);
+				knot_rrset_t *cpy = knot_rrset_copy(rr, NULL);
 				if (cpy == NULL) {
 					ret = KNOT_ENOMEM;
 					goto cleanup;
@@ -561,7 +561,7 @@ dbg_xfrin_exec_verb(
 				continue;
 			} else {
 				// just add the RR to the ADD part and continue
-				knot_rrset_t *cpy = knot_rrset_cpy(rr, NULL);
+				knot_rrset_t *cpy = knot_rrset_copy(rr, NULL);
 				if (cpy == NULL) {
 					ret = KNOT_ENOMEM;
 					goto cleanup;
diff --git a/src/knot/zone/node.c b/src/knot/zone/node.c
index 91f7ad692..6fd84319f 100644
--- a/src/knot/zone/node.c
+++ b/src/knot/zone/node.c
@@ -199,7 +199,7 @@ knot_rrset_t *knot_node_create_rrset(const knot_node_t *node, uint16_t type)
 	for (uint16_t i = 0; i < node->rrset_count; ++i) {
 		if (node->rrs[i].type == type) {
 			knot_rrset_t rrset = NODE_RR_INIT_N(node, i);
-			return knot_rrset_cpy(&rrset, NULL);
+			return knot_rrset_copy(&rrset, NULL);
 		}
 	}
 
diff --git a/src/knot/zone/zone-diff.c b/src/knot/zone/zone-diff.c
index 3d17234f5..be3e17461 100644
--- a/src/knot/zone/zone-diff.c
+++ b/src/knot/zone/zone-diff.c
@@ -107,15 +107,14 @@ static int knot_zone_diff_changeset_add_rrset(knot_changeset_t *changeset,
 		return KNOT_EOK;
 	}
 
-	knot_rrset_t *rrset_copy = NULL;
-	int ret = knot_rrset_copy(rrset, &rrset_copy, NULL);
-	if (ret != KNOT_EOK) {
+	knot_rrset_t *rrset_copy = knot_rrset_copy(rrset, NULL);
+	if (rrset_copy == NULL) {
 		dbg_zonediff("zone_diff: add_rrset: Cannot copy RRSet.\n");
-		return ret;
+		return KNOT_ENOMEM;
 	}
 
-	ret = knot_changeset_add_rrset(changeset, rrset_copy,
-	                               KNOT_CHANGESET_ADD);
+	int ret = knot_changeset_add_rrset(changeset, rrset_copy,
+	                                   KNOT_CHANGESET_ADD);
 	if (ret != KNOT_EOK) {
 		/* We have to free the copy now! */
 		knot_rrset_free(&rrset_copy, NULL);
@@ -146,15 +145,14 @@ static int knot_zone_diff_changeset_remove_rrset(knot_changeset_t *changeset,
 		return KNOT_EOK;
 	}
 
-	knot_rrset_t *rrset_copy = NULL;
-	int ret = knot_rrset_copy(rrset, &rrset_copy, NULL);
-	if (ret != KNOT_EOK) {
+	knot_rrset_t *rrset_copy = knot_rrset_copy(rrset, NULL);
+	if (rrset_copy == NULL) {
 		dbg_zonediff("zone_diff: remove_rrset: Cannot copy RRSet.\n");
-		return ret;
+		return KNOT_ENOMEM;
 	}
 
-	ret = knot_changeset_add_rrset(changeset, rrset_copy,
-	                               KNOT_CHANGESET_REMOVE);
+	int ret = knot_changeset_add_rrset(changeset, rrset_copy,
+	                                   KNOT_CHANGESET_REMOVE);
 	if (ret != KNOT_EOK) {
 		/* We have to free the copy now. */
 		knot_rrset_free(&rrset_copy, NULL);
diff --git a/src/libknot/rrset.c b/src/libknot/rrset.c
index d42ba6fb4..ba1e7223d 100644
--- a/src/libknot/rrset.c
+++ b/src/libknot/rrset.c
@@ -724,31 +724,6 @@ bool knot_rrset_equal(const knot_rrset_t *r1,
 	return true;
 }
 
-int knot_rrset_copy(const knot_rrset_t *from, knot_rrset_t **to, mm_ctx_t *mm)
-{
-	if (from == NULL || to == NULL) {
-		return KNOT_EINVAL;
-	}
-
-	dbg_rrset_detail("rr: deep_copy: Copying RRs of type %d\n",
-	                 from->type);
-	*to = knot_rrset_new_from(from, mm);
-	if (*to == NULL) {
-		*to = NULL;
-		return KNOT_ENOMEM;
-	}
-
-	int ret = knot_rrs_copy(&(*to)->rrs, &from->rrs, mm);
-	if (ret != KNOT_EOK) {
-		knot_rrset_free(to, mm);
-		return ret;
-	}
-
-	(*to)->additional = NULL;
-
-	return KNOT_EOK;
-}
-
 static void rrset_deep_free_content(knot_rrset_t *rrset,
                                     mm_ctx_t *mm)
 {
@@ -1134,10 +1109,24 @@ int knot_rrset_copy_int(knot_rrset_t *dst, const knot_rrset_t *src, mm_ctx_t *mm
 	return KNOT_EOK;
 }
 
-knot_rrset_t *knot_rrset_cpy(const knot_rrset_t *src, mm_ctx_t *mm)
+knot_rrset_t *knot_rrset_copy(const knot_rrset_t *src, mm_ctx_t *mm)
 {
-	knot_rrset_t *dst = NULL;
-	int ret = knot_rrset_copy(src, &dst, mm);
-	return ret == KNOT_EOK ? dst : NULL;
+	if (src == NULL) {
+		return NULL;
+	}
+
+	knot_rrset_t *rrset = knot_rrset_new_from(src, mm);
+	if (rrset == NULL) {
+		return NULL;
+	}
+
+	int ret = knot_rrs_copy(&rrset->rrs, &src->rrs, mm);
+	if (ret != KNOT_EOK) {
+		knot_rrset_free(&rrset, mm);
+		return NULL;
+	}
+
+	rrset->additional = NULL;
+	return rrset;
 }
 
diff --git a/src/libknot/rrset.h b/src/libknot/rrset.h
index 92f356354..b68ef11b2 100644
--- a/src/libknot/rrset.h
+++ b/src/libknot/rrset.h
@@ -173,17 +173,6 @@ bool knot_rrset_equal(const knot_rrset_t *r1,
                       const knot_rrset_t *r2,
                       knot_rrset_compare_type_t cmp);
 
-/*!
- * \brief Complete copy of RRSet structure.
- *
- * \param from  Source data.
- * \param to    Destionation data.
- * \param mm    Memory context.
- *
- * \return KNOT_E*
- */
-int knot_rrset_copy(const knot_rrset_t *from, knot_rrset_t **to, mm_ctx_t *mm);
-
 /*!
  * \brief Destroys the RRSet structure and all its substructures.
  )
@@ -346,9 +335,48 @@ int knot_rrset_synth_rrsig(const knot_dname_t *owner, uint16_t type,
                            const knot_rrset_t *rrsigs,
                            knot_rrset_t **out_sig, mm_ctx_t *mm);
 
+/*!
+ * \brief Checks whether RRSet is empty.
+ *
+ * \param rrset  RRSet to check.
+ *
+ * \retval True if RRSet is empty.
+ * \retval False if RRSet is not empty.
+ */
 bool knot_rrset_empty(const knot_rrset_t *rrset);
+
+/*!
+ * \brief Deep copies one RRSet into another.
+ *
+ * \param dst  Destination RRSet.
+ * \param src  Source RRSet.
+ * \param mm   Memory context.
+ *
+ * \return KNOT_E*
+ */
 int knot_rrset_copy_int(knot_rrset_t *dst, const knot_rrset_t *src, mm_ctx_t *mm);
-knot_rrset_t *knot_rrset_cpy(const knot_rrset_t *src, mm_ctx_t *mm);
+
+/*!
+ * \brief Creates new RRSet from \a src RRSet.
+ *
+ * \param src  Source RRSet.
+ * \param mm   Memory context.
+ *
+ * \retval Pointer to new RRSet if all went OK.
+ * \retval NULL on error.
+ */
+knot_rrset_t *knot_rrset_copy(const knot_rrset_t *src, mm_ctx_t *mm);
+
+/*!
+ * \brief RRSet intersection.
+ *
+ * \param a    First RRSet to intersect.
+ * \param b    Second RRset to intersect.
+ * \param out  Output RRSet with intersection.
+ * \param mm   Memory context.
+ *
+ * \return KNOT_E*
+ */
 int knot_rrset_intersection(const knot_rrset_t *a, const knot_rrset_t *b,
                             knot_rrset_t *out, mm_ctx_t *mm);
 
-- 
GitLab