chantra / rpms / rpm

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