Blob Blame History Raw
From ecab80b80e3917d3acf0f909c9cc84691a207fc0 Mon Sep 17 00:00:00 2001
From: chantra <chantr4@gmail.com>
Date: Thu, 3 Feb 2022 21:09:05 -0800
Subject: [PATCH 12/30] [rpmextents] Create an internal library to make
 rpmextents file manipulation easier and less duplicated

---
 lib/Makefile.am           |  3 ++-
 lib/rpmchecksig.c         | 42 +---------------------------------
 lib/rpmextents.c          | 46 +++++++++++++++++++++++++++++++++++++
 lib/rpmextents_internal.h | 22 ++++++++++++++++++
 plugins/reflink.c         | 48 +++++++++++++--------------------------
 rpm2extents.c             |  8 ++-----
 6 files changed, 89 insertions(+), 80 deletions(-)
 create mode 100644 lib/rpmextents.c
 create mode 100644 lib/rpmextents_internal.h

diff --git a/lib/Makefile.am b/lib/Makefile.am
index 5a1b6ca9b..2f1b3597f 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -40,7 +40,8 @@ librpm_la_SOURCES = \
 	rpmscript.h rpmscript.c \
 	rpmchroot.c rpmchroot.h \
 	rpmplugins.c rpmplugins.h rpmplugin.h rpmug.c rpmug.h \
-	rpmtriggers.h rpmtriggers.c rpmvs.c rpmvs.h
+	rpmtriggers.h rpmtriggers.c rpmvs.c rpmvs.h \
+	rpmextents.c rpmextents_internal.h
 
 librpm_la_LDFLAGS = -version-info $(rpm_version_info)
 
diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c
index 6164d012c..dc1726a18 100644
--- a/lib/rpmchecksig.c
+++ b/lib/rpmchecksig.c
@@ -20,15 +20,11 @@
 #include "rpmio/rpmio_internal.h" 	/* fdSetBundle() */
 #include "lib/rpmlead.h"
 #include "lib/header_internal.h"
+#include "lib/rpmextents_internal.h"
 #include "lib/rpmvs.h"
 
 #include "debug.h"
 
-/* magic value at end of file (64 bits) that indicates this is a transcoded
- * rpm.
- */
-#define MAGIC 3472329499408095051
-
 int _print_pkts = 0;
 
 static int doImport(rpmts ts, const char *fn, char *buf, ssize_t blen)
@@ -225,42 +221,6 @@ exit:
     return rc;
 }
 
-static rpmRC isTranscodedRpm(FD_t fd) {
-    rpmRC rc = RPMRC_NOTFOUND;
-    rpm_loff_t current;
-    uint64_t magic;
-    size_t len;
-
-    // If the file is not seekable, we cannot detect whether or not it is transcoded.
-    if(Fseek(fd, 0, SEEK_CUR) < 0) {
-        return RPMRC_FAIL;
-    }
-    current = Ftell(fd);
-
-    if(Fseek(fd, -(sizeof(magic)), SEEK_END) < 0) {
-	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: failed to seek for magic\n"));
-	rc = RPMRC_FAIL;
-	goto exit;
-    }
-    len = sizeof(magic);
-    if (Fread(&magic, len, 1, fd) != len) {
-	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to read magic\n"));
-	rc = RPMRC_FAIL;
-	goto exit;
-    }
-    if (magic != MAGIC) {
-	rpmlog(RPMLOG_DEBUG, _("isTranscodedRpm: not transcoded\n"));
-	rc = RPMRC_NOTFOUND;
-	goto exit;
-    }
-    rc = RPMRC_OK;
-exit:
-    if (Fseek(fd, current, SEEK_SET) < 0) {
-	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to seek back to original location\n"));
-    }
-    return rc;
-}
-
 static int rpmpkgVerifySigsTranscoded(FD_t fd){
     rpm_loff_t current;
     uint64_t magic;
diff --git a/lib/rpmextents.c b/lib/rpmextents.c
new file mode 100644
index 000000000..015277751
--- /dev/null
+++ b/lib/rpmextents.c
@@ -0,0 +1,46 @@
+
+#include "system.h"
+
+#include <rpm/rpmlog.h>
+#include <rpm/rpmio.h>
+
+#include "lib/rpmextents_internal.h"
+
+rpmRC isTranscodedRpm(FD_t fd) {
+    rpmRC rc = RPMRC_NOTFOUND;
+    rpm_loff_t current;
+    extents_magic_t magic;
+    size_t len;
+
+    // If the file is not seekable, we cannot detect whether or not it is transcoded.
+    if(Fseek(fd, 0, SEEK_CUR) < 0) {
+        return RPMRC_FAIL;
+    }
+    current = Ftell(fd);
+
+    if(Fseek(fd, -(sizeof(magic)), SEEK_END) < 0) {
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: failed to seek for magic\n"));
+	rc = RPMRC_FAIL;
+	goto exit;
+    }
+    len = sizeof(magic);
+    if (Fread(&magic, len, 1, fd) != len) {
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to read magic\n"));
+	rc = RPMRC_FAIL;
+	goto exit;
+    }
+    if (magic != EXTENTS_MAGIC) {
+	rpmlog(RPMLOG_DEBUG, _("isTranscodedRpm: not transcoded\n"));
+	rc = RPMRC_NOTFOUND;
+	goto exit;
+    }
+    rc = RPMRC_OK;
+exit:
+    if (Fseek(fd, current, SEEK_SET) < 0) {
+	rpmlog(RPMLOG_ERR, _("isTranscodedRpm: unable to seek back to original location\n"));
+	rc = RPMRC_FAIL;
+    }
+    return rc;
+}
+
+
diff --git a/lib/rpmextents_internal.h b/lib/rpmextents_internal.h
new file mode 100644
index 000000000..57cecfc31
--- /dev/null
+++ b/lib/rpmextents_internal.h
@@ -0,0 +1,22 @@
+#ifndef _RPMEXTENTS_INTERNAL_H
+#define _RPMEXTENTS_INTERNAL_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdint.h>
+
+/* magic value at end of file (64 bits) that indicates this is a transcoded
+ * rpm.
+ */
+#define EXTENTS_MAGIC 3472329499408095051
+
+typedef uint64_t extents_magic_t;
+
+rpmRC isTranscodedRpm(FD_t fd);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/plugins/reflink.c b/plugins/reflink.c
index 513887604..ec575f55e 100644
--- a/plugins/reflink.c
+++ b/plugins/reflink.c
@@ -13,6 +13,7 @@
 #include <rpm/rpmlog.h>
 #include "lib/rpmlib.h"
 #include "lib/rpmplugin.h"
+#include "lib/rpmextents_internal.h"
 #include "lib/rpmte_internal.h"
 #include <rpm/rpmfileutil.h>
 #include "rpmio/rpmio_internal.h"
@@ -40,11 +41,6 @@
 
 #define BUFFER_SIZE (1024 * 128)
 
-/* magic value at end of file (64 bits) that indicates this is a transcoded
- * rpm.
- */
-#define MAGIC 3472329499408095051
-
 struct reflink_state_s {
     /* Stuff that's used across rpms */
     long fundamental_block_size;
@@ -96,40 +92,28 @@ static void reflink_cleanup(rpmPlugin plugin) {
 }
 
 static rpmRC reflink_psm_pre(rpmPlugin plugin, rpmte te) {
+    rpmRC rc;
+    size_t len;
+
     reflink_state state = rpmPluginGetData(plugin);
     state->fd = rpmteFd(te);
     if (state->fd == 0) {
 	rpmlog(RPMLOG_DEBUG, _("reflink: fd = 0, no install\n"));
 	return RPMRC_OK;
     }
+
     rpm_loff_t current = Ftell(state->fd);
-    uint64_t magic;
-    if (Fseek(state->fd, -(sizeof(magic)), SEEK_END) < 0) {
-	rpmlog(RPMLOG_ERR, _("reflink: failed to seek for magic\n"));
-	if (Fseek(state->fd, current, SEEK_SET) < 0) {
-	    /* yes this gets a bit repetitive */
-	    rpmlog(RPMLOG_ERR,
-		 _("reflink: unable to seek back to original location\n"));
-	}
-	return RPMRC_FAIL;
-    }
-    size_t len = sizeof(magic);
-    if (Fread(&magic, len, 1, state->fd) != len) {
-	rpmlog(RPMLOG_ERR, _("reflink: unable to read magic\n"));
-	if (Fseek(state->fd, current, SEEK_SET) < 0) {
-	    rpmlog(RPMLOG_ERR,
-		   _("reflink: unable to seek back to original location\n"));
-	}
-	return RPMRC_FAIL;
-    }
-    if (magic != MAGIC) {
-	rpmlog(RPMLOG_DEBUG, _("reflink: not transcoded\n"));
-	if (Fseek(state->fd, current, SEEK_SET) < 0) {
-	    rpmlog(RPMLOG_ERR,
-		   _("reflink: unable to seek back to original location\n"));
+    rc = isTranscodedRpm(state->fd);
+
+    switch(rc){
+	// Fail to parse the file, fail the plugin.
+	case RPMRC_FAIL:
 	    return RPMRC_FAIL;
-	}
-	return RPMRC_OK;
+	// This is not a transcoded file, do nothing.
+	case RPMRC_NOTFOUND:
+	    return RPMRC_OK;
+	default:
+	    break;
     }
     rpmlog(RPMLOG_DEBUG, _("reflink: *is* transcoded\n"));
     Header h = rpmteHeader(te);
@@ -140,7 +124,7 @@ static rpmRC reflink_psm_pre(rpmPlugin plugin, rpmte te) {
     headerFree(h);
     state->files = rpmteFiles(te);
     /* tail of file contains offset_table, offset_checksums then magic */
-    if (Fseek(state->fd, -(sizeof(rpm_loff_t) * 2 + sizeof(magic)), SEEK_END) < 0) {
+    if (Fseek(state->fd, -(sizeof(rpm_loff_t) * 2 + sizeof(extents_magic_t)), SEEK_END) < 0) {
 	rpmlog(RPMLOG_ERR, _("reflink: failed to seek for tail %p\n"),
 	       state->fd);
 	return RPMRC_FAIL;
diff --git a/rpm2extents.c b/rpm2extents.c
index e316a2834..a326e3857 100644
--- a/rpm2extents.c
+++ b/rpm2extents.c
@@ -15,6 +15,7 @@
 #include "lib/rpmts.h"
 #include "lib/signature.h"
 #include "lib/header_internal.h"
+#include "lib/rpmextents_internal.h"
 #include "rpmio/rpmio_internal.h"
 
 #include <unistd.h>
@@ -37,11 +38,6 @@
 #include "lib/rpmhash.H"
 #include "lib/rpmhash.C"
 
-/* magic value at end of file (64 bits) that indicates this is a transcoded
- * rpm.
- */
-#define MAGIC 3472329499408095051
-
 struct digestoffset {
     const unsigned char * digest;
     rpm_loff_t pos;
@@ -402,7 +398,7 @@ static rpmRC process_package(FD_t fdi, FD_t digestori, FD_t validationi)
 	rc = RPMRC_FAIL;
 	goto exit;
     }
-    uint64_t magic = MAGIC;
+    extents_magic_t magic = EXTENTS_MAGIC;
     len = sizeof(magic);
     if (Fwrite(&magic, len, 1, fdo) != len) {
 	fprintf(stderr, _("Unable to write magic\n"));
-- 
2.35.1