2f13d7
From 5f762af17c6e72e86e4710975dcdfd71fc5d1b07 Mon Sep 17 00:00:00 2001
2f13d7
From: chantra <chantr4@gmail.com>
2f13d7
Date: Tue, 8 Feb 2022 18:24:12 -0800
2f13d7
Subject: [PATCH 19/30] [reflink] use `rpmpluginsCallFsmFileArchiveReader` to
2f13d7
 provide custom rpmfi
2f13d7
2f13d7
As the plugin can now be responsible to provide an Archive Reader to
2f13d7
`rpmPackageFilesInstall`, we also don't need to mutate the
2f13d7
`RPMTAG_PAYLOADFORMAT` header in memory, removing some special-casing.
2f13d7
---
2f13d7
 lib/depends.c     |  2 --
2f13d7
 lib/fsm.c         | 38 +++++++++++++++++++-------------------
2f13d7
 plugins/reflink.c | 18 +++++++++++++-----
2f13d7
 3 files changed, 32 insertions(+), 26 deletions(-)
2f13d7
2f13d7
diff --git a/lib/depends.c b/lib/depends.c
2f13d7
index 8998afcd3..30234df3d 100644
2f13d7
--- a/lib/depends.c
2f13d7
+++ b/lib/depends.c
2f13d7
@@ -81,8 +81,6 @@ static rpmRC headerCheckPayloadFormat(Header h) {
2f13d7
      */
2f13d7
     if (!payloadfmt) return rc;
2f13d7
 
2f13d7
-    if (rstreq(payloadfmt, "clon")) return rc;
2f13d7
-
2f13d7
     if (!rstreq(payloadfmt, "cpio")) {
2f13d7
         char *nevra = headerGetAsString(h, RPMTAG_NEVRA);
2f13d7
         if (payloadfmt && rstreq(payloadfmt, "drpm")) {
2f13d7
diff --git a/lib/fsm.c b/lib/fsm.c
2f13d7
index 711f553d3..6972602e0 100644
2f13d7
--- a/lib/fsm.c
2f13d7
+++ b/lib/fsm.c
2f13d7
@@ -19,7 +19,6 @@
2f13d7
 
2f13d7
 #include "rpmio/rpmio_internal.h"	/* fdInit/FiniDigest */
2f13d7
 #include "lib/fsm.h"
2f13d7
-#include "lib/rpmlib.h"
2f13d7
 #include "lib/rpmte_internal.h"	/* XXX rpmfs */
2f13d7
 #include "lib/rpmplugins.h"	/* rpm plugins hooks */
2f13d7
 #include "lib/rpmug.h"
2f13d7
@@ -892,14 +891,6 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
2f13d7
     struct filedata_s *fdata = xcalloc(fc, sizeof(*fdata));
2f13d7
     struct filedata_s *firstlink = NULL;
2f13d7
 
2f13d7
-    Header h = rpmteHeader(te);
2f13d7
-    const char *payloadfmt = headerGetString(h, RPMTAG_PAYLOADFORMAT);
2f13d7
-    int cpio = 1;
2f13d7
-
2f13d7
-    if (payloadfmt && rstreq(payloadfmt, "clon")) {
2f13d7
-	cpio = 0;
2f13d7
-    }
2f13d7
-
2f13d7
     /* transaction id used for temporary path suffix while installing */
2f13d7
     rasprintf(&tid, ";%08x", (unsigned)rpmtsGetTid(ts));
2f13d7
 
2f13d7
@@ -933,14 +924,24 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
2f13d7
     if (rc)
2f13d7
 	goto exit;
2f13d7
 
2f13d7
-    if (cpio) {
2f13d7
-	fi = rpmfiNewArchiveReader(payload, files, RPMFI_ITER_READ_ARCHIVE);
2f13d7
-	if (fi == NULL) {
2f13d7
-	    rc = RPMERR_BAD_MAGIC;
2f13d7
-	    goto exit;
2f13d7
-	}
2f13d7
-    } else {
2f13d7
-	fi = rpmfilesIter(files, RPMFI_ITER_FWD);
2f13d7
+    rpmRC plugin_rc = rpmpluginsCallFsmFileArchiveReader(plugins, payload, files, &fi);
2f13d7
+    switch(plugin_rc) {
2f13d7
+	case RPMRC_PLUGIN_CONTENTS:
2f13d7
+	    if(fi == NULL) {
2f13d7
+		rc = RPMERR_BAD_MAGIC;
2f13d7
+		goto exit;
2f13d7
+	    }
2f13d7
+	    rc = RPMRC_OK;
2f13d7
+	    break;
2f13d7
+	case RPMRC_OK:
2f13d7
+	    fi = rpmfiNewArchiveReader(payload, files, RPMFI_ITER_READ_ARCHIVE);
2f13d7
+	    if (fi == NULL) {
2f13d7
+		rc = RPMERR_BAD_MAGIC;
2f13d7
+		goto exit;
2f13d7
+	    }
2f13d7
+	    break;
2f13d7
+	default:
2f13d7
+	    rc = RPMRC_FAIL;
2f13d7
     }
2f13d7
 
2f13d7
     /* Detect and create directories not explicitly in package. */
2f13d7
@@ -988,7 +989,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
2f13d7
 	    } else if (S_ISREG(fp->sb.st_mode)) {
2f13d7
 		if (rc == RPMERR_ENOENT) {
2f13d7
 		    rc = fsmMkfile(fi, fp, files, psm, nodigest,
2f13d7
-			&firstlink, &firstlinkfile);
2f13d7
+				   &firstlink, &firstlinkfile);
2f13d7
 		}
2f13d7
             } else if (S_ISDIR(fp->sb.st_mode)) {
2f13d7
                 if (rc == RPMERR_ENOENT) {
2f13d7
@@ -1096,7 +1097,6 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
2f13d7
     rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST), fdOp(payload, FDSTAT_DIGEST));
2f13d7
 
2f13d7
 exit:
2f13d7
-    h = headerFree(h);
2f13d7
     fi = rpmfiFree(fi);
2f13d7
     Fclose(payload);
2f13d7
     free(tid);
2f13d7
diff --git a/plugins/reflink.c b/plugins/reflink.c
2f13d7
index 7dda06d8e..d5e6db27a 100644
2f13d7
--- a/plugins/reflink.c
2f13d7
+++ b/plugins/reflink.c
2f13d7
@@ -54,6 +54,7 @@ struct reflink_state_s {
2f13d7
     FD_t fd;
2f13d7
     rpmfiles files;
2f13d7
     inodeIndexHash inodeIndexes;
2f13d7
+    int transcoded;
2f13d7
 };
2f13d7
 
2f13d7
 typedef struct reflink_state_s * reflink_state;
2f13d7
@@ -116,12 +117,8 @@ static rpmRC reflink_psm_pre(rpmPlugin plugin, rpmte te) {
2f13d7
 	    break;
2f13d7
     }
2f13d7
     rpmlog(RPMLOG_DEBUG, _("reflink: *is* transcoded\n"));
2f13d7
-    Header h = rpmteHeader(te);
2f13d7
+    state->transcoded = 1;
2f13d7
 
2f13d7
-    /* replace/add header that main fsm.c can read */
2f13d7
-    headerDel(h, RPMTAG_PAYLOADFORMAT);
2f13d7
-    headerPutString(h, RPMTAG_PAYLOADFORMAT, "clon");
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(extents_magic_t)), SEEK_END) < 0) {
2f13d7
@@ -350,10 +347,21 @@ static rpmRC reflink_fsm_file_install(rpmPlugin plugin, rpmfi fi, const char* pa
2f13d7
     return RPMRC_OK;
2f13d7
 }
2f13d7
 
2f13d7
+static rpmRC reflink_fsm_file_archive_reader(rpmPlugin plugin, FD_t payload,
2f13d7
+					     rpmfiles files, rpmfi *fi) {
2f13d7
+    reflink_state state = rpmPluginGetData(plugin);
2f13d7
+    if(state->transcoded) {
2f13d7
+	*fi = rpmfilesIter(files, RPMFI_ITER_FWD);
2f13d7
+	return RPMRC_PLUGIN_CONTENTS;
2f13d7
+    }
2f13d7
+    return RPMRC_OK;
2f13d7
+}
2f13d7
+
2f13d7
 struct rpmPluginHooks_s reflink_hooks = {
2f13d7
     .init = reflink_init,
2f13d7
     .cleanup = reflink_cleanup,
2f13d7
     .psm_pre = reflink_psm_pre,
2f13d7
     .psm_post = reflink_psm_post,
2f13d7
     .fsm_file_install = reflink_fsm_file_install,
2f13d7
+    .fsm_file_archive_reader = reflink_fsm_file_archive_reader,
2f13d7
 };
2f13d7
-- 
2f13d7
2.35.1
2f13d7