alexk / rpms / rpm

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