michal-grzedzicki / rpms / rpm

Forked from rpms/rpm 5 months ago
Clone
ea2e7d
diff --git a/Makefile.am b/Makefile.am
ea2e7d
index e5c75d7b4..288668819 100644
ea2e7d
--- a/Makefile.am
ea2e7d
+++ b/Makefile.am
ea2e7d
@@ -99,7 +99,7 @@ pkginclude_HEADERS += build/rpmfc.h
ea2e7d
 pkginclude_HEADERS += build/rpmspec.h
ea2e7d
 
ea2e7d
 
ea2e7d
-bin_PROGRAMS =		rpm rpm2cpio rpmbuild rpmdb rpmkeys rpmsign rpmspec
ea2e7d
+bin_PROGRAMS =		rpm rpm2cpio rpmbuild rpmdb rpmkeys rpmsign rpmspec rpm2extents
ea2e7d
 if WITH_ARCHIVE
ea2e7d
 bin_PROGRAMS += 	rpm2archive 
ea2e7d
 endif
ea2e7d
@@ -154,6 +154,10 @@ rpm2cpio_SOURCES =	rpm2cpio.c debug.h system.h
ea2e7d
 rpm2cpio_LDADD =	lib/librpm.la rpmio/librpmio.la
ea2e7d
 rpm2cpio_LDADD +=	@WITH_POPT_LIB@
ea2e7d
 
ea2e7d
+rpm2extents_SOURCES =	rpm2extents.c debug.h system.h
ea2e7d
+rpm2extents_LDADD =	lib/librpm.la rpmio/librpmio.la
ea2e7d
+rpm2extents_LDADD +=	@WITH_POPT_LIB@
ea2e7d
+
ea2e7d
 rpm2archive_SOURCES =	rpm2archive.c debug.h system.h
ea2e7d
 rpm2archive_LDADD =	lib/librpm.la rpmio/librpmio.la
ea2e7d
 rpm2archive_LDADD +=	@WITH_POPT_LIB@ @WITH_ARCHIVE_LIB@
ea2e7d
diff --git a/lib/depends.c b/lib/depends.c
ea2e7d
index 30234df3d..8998afcd3 100644
ea2e7d
--- a/lib/depends.c
ea2e7d
+++ b/lib/depends.c
ea2e7d
@@ -81,6 +81,8 @@ static rpmRC headerCheckPayloadFormat(Header h) {
ea2e7d
      */
ea2e7d
     if (!payloadfmt) return rc;
ea2e7d
 
ea2e7d
+    if (rstreq(payloadfmt, "clon")) return rc;
ea2e7d
+
ea2e7d
     if (!rstreq(payloadfmt, "cpio")) {
ea2e7d
         char *nevra = headerGetAsString(h, RPMTAG_NEVRA);
ea2e7d
         if (payloadfmt && rstreq(payloadfmt, "drpm")) {
ea2e7d
diff --git a/lib/fsm.c b/lib/fsm.c
ea2e7d
index 935a0a5c6..feda3750c 100644
ea2e7d
--- a/lib/fsm.c
ea2e7d
+++ b/lib/fsm.c
ea2e7d
@@ -19,6 +19,7 @@
ea2e7d
 
ea2e7d
 #include "rpmio/rpmio_internal.h"	/* fdInit/FiniDigest */
ea2e7d
 #include "lib/fsm.h"
ea2e7d
+#include "lib/rpmlib.h"
ea2e7d
 #include "lib/rpmte_internal.h"	/* XXX rpmfs */
ea2e7d
 #include "lib/rpmplugins.h"	/* rpm plugins hooks */
ea2e7d
 #include "lib/rpmug.h"
ea2e7d
@@ -52,6 +53,7 @@ struct filedata_s {
ea2e7d
     int stage;
ea2e7d
     int setmeta;
ea2e7d
     int skip;
ea2e7d
+    int plugin_contents;
ea2e7d
     rpmFileAction action;
ea2e7d
     const char *suffix;
ea2e7d
     char *fpath;
ea2e7d
@@ -891,6 +893,14 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
ea2e7d
     struct filedata_s *fdata = xcalloc(fc, sizeof(*fdata));
ea2e7d
     struct filedata_s *firstlink = NULL;
ea2e7d
 
ea2e7d
+    Header h = rpmteHeader(te);
ea2e7d
+    const char *payloadfmt = headerGetString(h, RPMTAG_PAYLOADFORMAT);
ea2e7d
+    int cpio = 1;
ea2e7d
+
ea2e7d
+    if (payloadfmt && rstreq(payloadfmt, "clon")) {
ea2e7d
+	cpio = 0;
ea2e7d
+    }
ea2e7d
+
ea2e7d
     /* transaction id used for temporary path suffix while installing */
ea2e7d
     rasprintf(&tid, ";%08x", (unsigned)rpmtsGetTid(ts));
ea2e7d
 
ea2e7d
@@ -911,12 +921,23 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
ea2e7d
 	/* Remap file perms, owner, and group. */
ea2e7d
 	rc = rpmfiStat(fi, 1, &fp->sb);
ea2e7d
 
ea2e7d
-	setFileState(fs, fx);
ea2e7d
 	fsmDebug(fp->fpath, fp->action, &fp->sb);
ea2e7d
 
ea2e7d
 	/* Run fsm file pre hook for all plugins */
ea2e7d
 	rc = rpmpluginsCallFsmFilePre(plugins, fi, fp->fpath,
ea2e7d
 				      fp->sb.st_mode, fp->action);
ea2e7d
+	fp->plugin_contents = 0;
ea2e7d
+	switch (rc) {
ea2e7d
+	case RPMRC_OK:
ea2e7d
+	    setFileState(fs, fx);
ea2e7d
+	    break;
ea2e7d
+	case RPMRC_PLUGIN_CONTENTS:
ea2e7d
+	    fp->plugin_contents = 1;
ea2e7d
+	    // reduce reads on cpio to this value. Could be zero if
ea2e7d
+	    // this is from a hard link.
ea2e7d
+	    rc = RPMRC_OK;
ea2e7d
+	    break;
ea2e7d
+	}
ea2e7d
 	fp->stage = FILE_PRE;
ea2e7d
     }
ea2e7d
     fi = rpmfiFree(fi);
ea2e7d
@@ -924,10 +945,14 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
ea2e7d
     if (rc)
ea2e7d
 	goto exit;
ea2e7d
 
ea2e7d
-    fi = rpmfiNewArchiveReader(payload, files, RPMFI_ITER_READ_ARCHIVE);
ea2e7d
-    if (fi == NULL) {
ea2e7d
-        rc = RPMERR_BAD_MAGIC;
ea2e7d
-        goto exit;
ea2e7d
+    if (cpio) {
ea2e7d
+	fi = rpmfiNewArchiveReader(payload, files, RPMFI_ITER_READ_ARCHIVE);
ea2e7d
+	if (fi == NULL) {
ea2e7d
+	    rc = RPMERR_BAD_MAGIC;
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+    } else {
ea2e7d
+	fi = rpmfilesIter(files, RPMFI_ITER_FWD);
ea2e7d
     }
ea2e7d
 
ea2e7d
     /* Detect and create directories not explicitly in package. */
ea2e7d
@@ -969,8 +994,12 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
ea2e7d
 
ea2e7d
             if (S_ISREG(fp->sb.st_mode)) {
ea2e7d
 		if (rc == RPMERR_ENOENT) {
ea2e7d
-		    rc = fsmMkfile(fi, fp, files, psm, nodigest,
ea2e7d
-				   &firstlink, &firstlinkfile);
ea2e7d
+		    if(fp->plugin_contents) {
ea2e7d
+			rc = RPMRC_OK;
ea2e7d
+		    }else {
ea2e7d
+			rc = fsmMkfile(fi, fp, files, psm, nodigest,
ea2e7d
+			    &firstlink, &firstlinkfile);
ea2e7d
+		    }
ea2e7d
 		}
ea2e7d
             } else if (S_ISDIR(fp->sb.st_mode)) {
ea2e7d
                 if (rc == RPMERR_ENOENT) {
ea2e7d
@@ -1078,6 +1107,7 @@ int rpmPackageFilesInstall(rpmts ts, rpmte te, rpmfiles files,
ea2e7d
     rpmswAdd(rpmtsOp(ts, RPMTS_OP_DIGEST), fdOp(payload, FDSTAT_DIGEST));
ea2e7d
 
ea2e7d
 exit:
ea2e7d
+    h = headerFree(h);
ea2e7d
     fi = rpmfiFree(fi);
ea2e7d
     Fclose(payload);
ea2e7d
     free(tid);
ea2e7d
diff --git a/lib/package.c b/lib/package.c
ea2e7d
index 281275029..90bd0d8a7 100644
ea2e7d
--- a/lib/package.c
ea2e7d
+++ b/lib/package.c
ea2e7d
@@ -404,5 +404,45 @@ rpmRC rpmReadPackageFile(rpmts ts, FD_t fd, const char * fn, Header * hdrp)
ea2e7d
     return rc;
ea2e7d
 }
ea2e7d
 
ea2e7d
+rpmRC rpmReadPackageRaw(FD_t fd, Header * sigp, Header * hdrp)
ea2e7d
+{
ea2e7d
+    char *msg = NULL;
ea2e7d
+    hdrblob sigblob = hdrblobCreate();
ea2e7d
+    hdrblob blob = hdrblobCreate();
ea2e7d
+    Header h = NULL;
ea2e7d
+    Header sigh = NULL;
ea2e7d
+
ea2e7d
+    rpmRC rc = rpmLeadRead(fd, &msg;;
ea2e7d
+    if (rc != RPMRC_OK)
ea2e7d
+	goto exit;
ea2e7d
+
ea2e7d
+    rc = hdrblobRead(fd, 1, 0, RPMTAG_HEADERSIGNATURES, sigblob, &msg;;
ea2e7d
+    if (rc != RPMRC_OK)
ea2e7d
+	goto exit;
ea2e7d
+
ea2e7d
+    rc = hdrblobRead(fd, 1, 1, RPMTAG_HEADERIMMUTABLE, blob, &msg;;
ea2e7d
+    if (rc != RPMRC_OK)
ea2e7d
+	goto exit;
ea2e7d
+
ea2e7d
+    rc = hdrblobImport(sigblob, 0, &sigh, &msg;;
ea2e7d
+    if (rc)
ea2e7d
+	goto exit;
ea2e7d
 
ea2e7d
+    rc = hdrblobImport(blob, 0, &h, &msg;;
ea2e7d
+    if (rc)
ea2e7d
+	goto exit;
ea2e7d
 
ea2e7d
+    *sigp = headerLink(sigh);
ea2e7d
+    *hdrp = headerLink(h);
ea2e7d
+
ea2e7d
+exit:
ea2e7d
+    if (rc != RPMRC_OK && msg)
ea2e7d
+	rpmlog(RPMLOG_ERR, "%s: %s\n", Fdescr(fd), msg);
ea2e7d
+    hdrblobFree(sigblob);
ea2e7d
+    hdrblobFree(blob);
ea2e7d
+    headerFree(sigh);
ea2e7d
+    headerFree(h);
ea2e7d
+    free(msg);
ea2e7d
+
ea2e7d
+    return rc;
ea2e7d
+}
ea2e7d
diff --git a/lib/rpmlib.h b/lib/rpmlib.h
ea2e7d
index 0879d04e5..a09ba0daf 100644
ea2e7d
--- a/lib/rpmlib.h
ea2e7d
+++ b/lib/rpmlib.h
ea2e7d
@@ -155,6 +155,15 @@ rpmRC rpmReadHeader(rpmts ts, FD_t fd, Header *hdrp, char ** msg);
ea2e7d
 rpmRC rpmReadPackageFile(rpmts ts, FD_t fd,
ea2e7d
 		const char * fn, Header * hdrp);
ea2e7d
 
ea2e7d
+/** \ingroup header
ea2e7d
+ * Return package signature, header from file handle, no verification.
ea2e7d
+ * @param fd		file handle
ea2e7d
+ * @param[out] sigp		address of header (or NULL)
ea2e7d
+ * @param[out] hdrp		address of header (or NULL)
ea2e7d
+ * @return		RPMRC_OK on success
ea2e7d
+ */
ea2e7d
+rpmRC rpmReadPackageRaw(FD_t fd, Header * sigp, Header * hdrp);
ea2e7d
+
ea2e7d
 /** \ingroup rpmtrans
ea2e7d
  * Install source package.
ea2e7d
  * @param ts		transaction set
ea2e7d
diff --git a/lib/rpmplugins.c b/lib/rpmplugins.c
ea2e7d
index 62d75c4cf..3da3097af 100644
ea2e7d
--- a/lib/rpmplugins.c
ea2e7d
+++ b/lib/rpmplugins.c
ea2e7d
@@ -356,13 +356,28 @@ rpmRC rpmpluginsCallFsmFilePre(rpmPlugins plugins, rpmfi fi, const char *path,
ea2e7d
     plugin_fsm_file_pre_func hookFunc;
ea2e7d
     int i;
ea2e7d
     rpmRC rc = RPMRC_OK;
ea2e7d
+    rpmRC hook_rc;
ea2e7d
 
ea2e7d
     for (i = 0; i < plugins->count; i++) {
ea2e7d
 	rpmPlugin plugin = plugins->plugins[i];
ea2e7d
 	RPMPLUGINS_SET_HOOK_FUNC(fsm_file_pre);
ea2e7d
-	if (hookFunc && hookFunc(plugin, fi, path, file_mode, op) == RPMRC_FAIL) {
ea2e7d
-	    rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_pre failed\n", plugin->name);
ea2e7d
-	    rc = RPMRC_FAIL;
ea2e7d
+	if (hookFunc) {
ea2e7d
+	    hook_rc = hookFunc(plugin, fi, path, file_mode, op);
ea2e7d
+	    if (hook_rc == RPMRC_FAIL) {
ea2e7d
+		rpmlog(RPMLOG_ERR, "Plugin %s: hook fsm_file_pre failed\n", plugin->name);
ea2e7d
+		rc = RPMRC_FAIL;
ea2e7d
+	    } else if (hook_rc == RPMRC_PLUGIN_CONTENTS && rc != RPMRC_FAIL) {
ea2e7d
+		if (rc == RPMRC_PLUGIN_CONTENTS) {
ea2e7d
+		    /* Another plugin already said it'd handle contents. It's
ea2e7d
+		     * undefined how these would combine, so treat this as a
ea2e7d
+		     * failure condition.
ea2e7d
+		    */
ea2e7d
+		    rc = RPMRC_FAIL;
ea2e7d
+		} else {
ea2e7d
+		    /* Plugin will handle content */
ea2e7d
+		    rc = RPMRC_PLUGIN_CONTENTS;
ea2e7d
+		}
ea2e7d
+	    }
ea2e7d
 	}
ea2e7d
     }
ea2e7d
 
ea2e7d
diff --git a/lib/rpmte.c b/lib/rpmte.c
ea2e7d
index 3663604e7..d43dc41ad 100644
ea2e7d
--- a/lib/rpmte.c
ea2e7d
+++ b/lib/rpmte.c
ea2e7d
@@ -423,6 +423,11 @@ FD_t rpmteSetFd(rpmte te, FD_t fd)
ea2e7d
     return NULL;
ea2e7d
 }
ea2e7d
 
ea2e7d
+FD_t rpmteFd(rpmte te)
ea2e7d
+{
ea2e7d
+    return (te != NULL ? te->fd : NULL);
ea2e7d
+}
ea2e7d
+
ea2e7d
 fnpyKey rpmteKey(rpmte te)
ea2e7d
 {
ea2e7d
     return (te != NULL ? te->key : NULL);
ea2e7d
diff --git a/lib/rpmte.h b/lib/rpmte.h
ea2e7d
index 81acf7a19..6fc0a9f91 100644
ea2e7d
--- a/lib/rpmte.h
ea2e7d
+++ b/lib/rpmte.h
ea2e7d
@@ -209,6 +209,8 @@ const char * rpmteNEVR(rpmte te);
ea2e7d
  */
ea2e7d
 const char * rpmteNEVRA(rpmte te);
ea2e7d
 
ea2e7d
+FD_t rpmteFd(rpmte te);
ea2e7d
+
ea2e7d
 /** \ingroup rpmte
ea2e7d
  * Retrieve key from transaction element.
ea2e7d
  * @param te		transaction element
ea2e7d
diff --git a/lib/rpmtypes.h b/lib/rpmtypes.h
ea2e7d
index e8e69b506..af2611e9e 100644
ea2e7d
--- a/lib/rpmtypes.h
ea2e7d
+++ b/lib/rpmtypes.h
ea2e7d
@@ -106,7 +106,8 @@ typedef	enum rpmRC_e {
ea2e7d
     RPMRC_NOTFOUND	= 1,	/*!< Generic not found code. */
ea2e7d
     RPMRC_FAIL		= 2,	/*!< Generic failure code. */
ea2e7d
     RPMRC_NOTTRUSTED	= 3,	/*!< Signature is OK, but key is not trusted. */
ea2e7d
-    RPMRC_NOKEY		= 4	/*!< Public key is unavailable. */
ea2e7d
+    RPMRC_NOKEY		= 4,	/*!< Public key is unavailable. */
ea2e7d
+    RPMRC_PLUGIN_CONTENTS = 5     /*!< fsm_file_pre plugin is handling content */
ea2e7d
 } rpmRC;
ea2e7d
 
ea2e7d
 #ifdef __cplusplus
ea2e7d
diff --git a/macros.in b/macros.in
ea2e7d
index e90cefa9a..363252b0f 100644
ea2e7d
--- a/macros.in
ea2e7d
+++ b/macros.in
ea2e7d
@@ -1143,6 +1143,7 @@ package or when debugging this package.\
ea2e7d
 
ea2e7d
 # Transaction plugin macros
ea2e7d
 %__plugindir		%{_libdir}/rpm-plugins
ea2e7d
+%__transaction_reflink		%{__plugindir}/reflink.so
ea2e7d
 %__transaction_systemd_inhibit	%{__plugindir}/systemd_inhibit.so
ea2e7d
 %__transaction_selinux		%{__plugindir}/selinux.so
ea2e7d
 %__transaction_syslog		%{__plugindir}/syslog.so
ea2e7d
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
ea2e7d
index 3a929d0ce..ad0d3bce7 100644
ea2e7d
--- a/plugins/Makefile.am
ea2e7d
+++ b/plugins/Makefile.am
ea2e7d
@@ -42,6 +42,10 @@ prioreset_la_SOURCES = prioreset.c
ea2e7d
 prioreset_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
ea2e7d
 plugins_LTLIBRARIES += prioreset.la
ea2e7d
 
ea2e7d
+reflink_la_SOURCES = reflink.c
ea2e7d
+reflink_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
ea2e7d
+plugins_LTLIBRARIES += reflink.la
ea2e7d
+
ea2e7d
 syslog_la_SOURCES = syslog.c
ea2e7d
 syslog_la_LIBADD = $(top_builddir)/lib/librpm.la $(top_builddir)/rpmio/librpmio.la
ea2e7d
 plugins_LTLIBRARIES += syslog.la
ea2e7d
diff --git a/plugins/reflink.c b/plugins/reflink.c
ea2e7d
new file mode 100644
ea2e7d
index 000000000..513887604
ea2e7d
--- /dev/null
ea2e7d
+++ b/plugins/reflink.c
ea2e7d
@@ -0,0 +1,375 @@
ea2e7d
+#include "system.h"
ea2e7d
+
ea2e7d
+#include <errno.h>
ea2e7d
+#include <sys/resource.h>
ea2e7d
+#include <unistd.h>
ea2e7d
+#include <sys/types.h>
ea2e7d
+#include <sys/stat.h>
ea2e7d
+#include <fcntl.h>
ea2e7d
+#if defined(__linux__)
ea2e7d
+#include <linux/fs.h>        /* For FICLONE */
ea2e7d
+#endif
ea2e7d
+
ea2e7d
+#include <rpm/rpmlog.h>
ea2e7d
+#include "lib/rpmlib.h"
ea2e7d
+#include "lib/rpmplugin.h"
ea2e7d
+#include "lib/rpmte_internal.h"
ea2e7d
+#include <rpm/rpmfileutil.h>
ea2e7d
+#include "rpmio/rpmio_internal.h"
ea2e7d
+
ea2e7d
+
ea2e7d
+#include "debug.h"
ea2e7d
+
ea2e7d
+#include <sys/ioctl.h>
ea2e7d
+
ea2e7d
+/* use hash table to remember inode -> ix (for rpmfilesFN(ix)) lookups */
ea2e7d
+#undef HASHTYPE
ea2e7d
+#undef HTKEYTYPE
ea2e7d
+#undef HTDATATYPE
ea2e7d
+#define HASHTYPE inodeIndexHash
ea2e7d
+#define HTKEYTYPE rpm_ino_t
ea2e7d
+#define HTDATATYPE int
ea2e7d
+#include "lib/rpmhash.H"
ea2e7d
+#include "lib/rpmhash.C"
ea2e7d
+
ea2e7d
+/* We use this in find to indicate a key wasn't found. This is an
ea2e7d
+ * unrecoverable error, but we can at least show a decent error. 0 is never a
ea2e7d
+ * valid offset because it's the offset of the start of the file.
ea2e7d
+ */
ea2e7d
+#define NOT_FOUND 0
ea2e7d
+
ea2e7d
+#define BUFFER_SIZE (1024 * 128)
ea2e7d
+
ea2e7d
+/* magic value at end of file (64 bits) that indicates this is a transcoded
ea2e7d
+ * rpm.
ea2e7d
+ */
ea2e7d
+#define MAGIC 3472329499408095051
ea2e7d
+
ea2e7d
+struct reflink_state_s {
ea2e7d
+    /* Stuff that's used across rpms */
ea2e7d
+    long fundamental_block_size;
ea2e7d
+    char *buffer;
ea2e7d
+
ea2e7d
+    /* stuff that's used/updated per psm */
ea2e7d
+    uint32_t keys, keysize;
ea2e7d
+
ea2e7d
+    /* table for current rpm, keys * (keysize + sizeof(rpm_loff_t)) */
ea2e7d
+    unsigned char *table;
ea2e7d
+    FD_t fd;
ea2e7d
+    rpmfiles files;
ea2e7d
+    inodeIndexHash inodeIndexes;
ea2e7d
+};
ea2e7d
+
ea2e7d
+typedef struct reflink_state_s * reflink_state;
ea2e7d
+
ea2e7d
+static int inodeCmp(rpm_ino_t a, rpm_ino_t b)
ea2e7d
+{
ea2e7d
+    return (a != b);
ea2e7d
+}
ea2e7d
+
ea2e7d
+static unsigned int inodeId(rpm_ino_t a)
ea2e7d
+{
ea2e7d
+    /* rpm_ino_t is uint32_t so maps safely to unsigned int */
ea2e7d
+    return (unsigned int)a;
ea2e7d
+}
ea2e7d
+
ea2e7d
+static rpmRC reflink_init(rpmPlugin plugin, rpmts ts) {
ea2e7d
+    reflink_state state = rcalloc(1, sizeof(struct reflink_state_s));
ea2e7d
+
ea2e7d
+    /* IOCTL-FICLONERANGE(2): ...Disk filesystems generally require the offset
ea2e7d
+     * and length arguments to be aligned to the fundamental block size.
ea2e7d
+     *
ea2e7d
+     * The value of "fundamental block size" is directly related to the
ea2e7d
+     * system's page size, so we should use that.
ea2e7d
+     */
ea2e7d
+    state->fundamental_block_size = sysconf(_SC_PAGESIZE);
ea2e7d
+    state->buffer = rcalloc(1, BUFFER_SIZE);
ea2e7d
+    rpmPluginSetData(plugin, state);
ea2e7d
+
ea2e7d
+    return RPMRC_OK;
ea2e7d
+}
ea2e7d
+
ea2e7d
+static void reflink_cleanup(rpmPlugin plugin) {
ea2e7d
+    reflink_state state = rpmPluginGetData(plugin);
ea2e7d
+    free(state->buffer);
ea2e7d
+    free(state);
ea2e7d
+}
ea2e7d
+
ea2e7d
+static rpmRC reflink_psm_pre(rpmPlugin plugin, rpmte te) {
ea2e7d
+    reflink_state state = rpmPluginGetData(plugin);
ea2e7d
+    state->fd = rpmteFd(te);
ea2e7d
+    if (state->fd == 0) {
ea2e7d
+	rpmlog(RPMLOG_DEBUG, _("reflink: fd = 0, no install\n"));
ea2e7d
+	return RPMRC_OK;
ea2e7d
+    }
ea2e7d
+    rpm_loff_t current = Ftell(state->fd);
ea2e7d
+    uint64_t magic;
ea2e7d
+    if (Fseek(state->fd, -(sizeof(magic)), SEEK_END) < 0) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: failed to seek for magic\n"));
ea2e7d
+	if (Fseek(state->fd, current, SEEK_SET) < 0) {
ea2e7d
+	    /* yes this gets a bit repetitive */
ea2e7d
+	    rpmlog(RPMLOG_ERR,
ea2e7d
+		 _("reflink: unable to seek back to original location\n"));
ea2e7d
+	}
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    size_t len = sizeof(magic);
ea2e7d
+    if (Fread(&magic, len, 1, state->fd) != len) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: unable to read magic\n"));
ea2e7d
+	if (Fseek(state->fd, current, SEEK_SET) < 0) {
ea2e7d
+	    rpmlog(RPMLOG_ERR,
ea2e7d
+		   _("reflink: unable to seek back to original location\n"));
ea2e7d
+	}
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    if (magic != MAGIC) {
ea2e7d
+	rpmlog(RPMLOG_DEBUG, _("reflink: not transcoded\n"));
ea2e7d
+	if (Fseek(state->fd, current, SEEK_SET) < 0) {
ea2e7d
+	    rpmlog(RPMLOG_ERR,
ea2e7d
+		   _("reflink: unable to seek back to original location\n"));
ea2e7d
+	    return RPMRC_FAIL;
ea2e7d
+	}
ea2e7d
+	return RPMRC_OK;
ea2e7d
+    }
ea2e7d
+    rpmlog(RPMLOG_DEBUG, _("reflink: *is* transcoded\n"));
ea2e7d
+    Header h = rpmteHeader(te);
ea2e7d
+
ea2e7d
+    /* replace/add header that main fsm.c can read */
ea2e7d
+    headerDel(h, RPMTAG_PAYLOADFORMAT);
ea2e7d
+    headerPutString(h, RPMTAG_PAYLOADFORMAT, "clon");
ea2e7d
+    headerFree(h);
ea2e7d
+    state->files = rpmteFiles(te);
ea2e7d
+    /* tail of file contains offset_table, offset_checksums then magic */
ea2e7d
+    if (Fseek(state->fd, -(sizeof(rpm_loff_t) * 2 + sizeof(magic)), SEEK_END) < 0) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: failed to seek for tail %p\n"),
ea2e7d
+	       state->fd);
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    rpm_loff_t table_start;
ea2e7d
+    len = sizeof(table_start);
ea2e7d
+    if (Fread(&table_start, len, 1, state->fd) != len) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: unable to read table_start\n"));
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    if (Fseek(state->fd, table_start, SEEK_SET) < 0) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: unable to seek to table_start\n"));
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    len = sizeof(state->keys);
ea2e7d
+    if (Fread(&state->keys, len, 1, state->fd) != len) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: unable to read number of keys\n"));
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    len = sizeof(state->keysize);
ea2e7d
+    if (Fread(&state->keysize, len, 1, state->fd) != len) {
ea2e7d
+	rpmlog(RPMLOG_ERR, _("reflink: unable to read keysize\n"));
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    rpmlog(
ea2e7d
+	RPMLOG_DEBUG,
ea2e7d
+	_("reflink: table_start=0x%lx, keys=%d, keysize=%d\n"),
ea2e7d
+	table_start, state->keys, state->keysize
ea2e7d
+    );
ea2e7d
+    /* now get digest table if there is a reason to have one. */
ea2e7d
+    if (state->keys == 0 || state->keysize == 0) {
ea2e7d
+	/* no files (or no digests(!)) */
ea2e7d
+	state->table = NULL;
ea2e7d
+    } else {
ea2e7d
+	int table_size = state->keys * (state->keysize + sizeof(rpm_loff_t));
ea2e7d
+	state->table = rcalloc(1, table_size);
ea2e7d
+	if (Fread(state->table, table_size, 1, state->fd) != table_size) {
ea2e7d
+	    rpmlog(RPMLOG_ERR, _("reflink: unable to read table\n"));
ea2e7d
+	    return RPMRC_FAIL;
ea2e7d
+	}
ea2e7d
+	state->inodeIndexes = inodeIndexHashCreate(
ea2e7d
+	    state->keys, inodeId, inodeCmp, NULL, NULL
ea2e7d
+	);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    /* Seek back to original location.
ea2e7d
+     * Might not be needed if we seek to offset immediately
ea2e7d
+     */
ea2e7d
+    if (Fseek(state->fd, current, SEEK_SET) < 0) {
ea2e7d
+	rpmlog(RPMLOG_ERR,
ea2e7d
+	       _("reflink: unable to seek back to original location\n"));
ea2e7d
+	return RPMRC_FAIL;
ea2e7d
+    }
ea2e7d
+    return RPMRC_OK;
ea2e7d
+}
ea2e7d
+
ea2e7d
+static rpmRC reflink_psm_post(rpmPlugin plugin, rpmte te, int res)
ea2e7d
+{
ea2e7d
+    reflink_state state = rpmPluginGetData(plugin);
ea2e7d
+    state->files = rpmfilesFree(state->files);
ea2e7d
+    if (state->table) {
ea2e7d
+	free(state->table);
ea2e7d
+	state->table = NULL;
ea2e7d
+    }
ea2e7d
+    if (state->inodeIndexes) {
ea2e7d
+	inodeIndexHashFree(state->inodeIndexes);
ea2e7d
+	state->inodeIndexes = NULL;
ea2e7d
+    }
ea2e7d
+    return RPMRC_OK;
ea2e7d
+}
ea2e7d
+
ea2e7d
+
ea2e7d
+/* have a prototype, warnings system */
ea2e7d
+rpm_loff_t find(const unsigned char *digest, reflink_state state);
ea2e7d
+
ea2e7d
+rpm_loff_t find(const unsigned char *digest, reflink_state state) {
ea2e7d
+# if defined(__GNUC__)
ea2e7d
+    /* GCC nested function because bsearch's comparison function can't access
ea2e7d
+     * state-keysize otherwise
ea2e7d
+     */
ea2e7d
+    int cmpdigest(const void *k1, const void *k2) {
ea2e7d
+	rpmlog(RPMLOG_DEBUG, _("reflink: cmpdigest k1=%p k2=%p\n"), k1, k2);
ea2e7d
+	return memcmp(k1, k2, state->keysize);
ea2e7d
+    }
ea2e7d
+# endif
ea2e7d
+    rpmlog(RPMLOG_DEBUG,
ea2e7d
+	   _("reflink: bsearch(key=%p, base=%p, nmemb=%d, size=%lu)\n"),
ea2e7d
+	   digest, state->table, state->keys,
ea2e7d
+	   state->keysize + sizeof(rpm_loff_t));
ea2e7d
+    char *entry = bsearch(digest, state->table, state->keys,
ea2e7d
+			  state->keysize + sizeof(rpm_loff_t), cmpdigest);
ea2e7d
+    if (entry == NULL) {
ea2e7d
+	return NOT_FOUND;
ea2e7d
+    }
ea2e7d
+    rpm_loff_t offset = *(rpm_loff_t *)(entry + state->keysize);
ea2e7d
+    return offset;
ea2e7d
+}
ea2e7d
+
ea2e7d
+static rpmRC reflink_fsm_file_pre(rpmPlugin plugin, rpmfi fi, const char* path,
ea2e7d
+                                  mode_t file_mode, rpmFsmOp op)
ea2e7d
+{
ea2e7d
+    struct file_clone_range fcr;
ea2e7d
+    rpm_loff_t size;
ea2e7d
+    int dst, rc;
ea2e7d
+    int *hlix;
ea2e7d
+
ea2e7d
+    reflink_state state = rpmPluginGetData(plugin);
ea2e7d
+    if (state->table == NULL) {
ea2e7d
+	/* no table means rpm is not in reflink format, so leave. Now. */
ea2e7d
+	return RPMRC_OK;
ea2e7d
+    }
ea2e7d
+    if (op == FA_TOUCH) {
ea2e7d
+	/* we're not overwriting an existing file. */
ea2e7d
+	return RPMRC_OK;
ea2e7d
+    }
ea2e7d
+    fcr.dest_offset = 0;
ea2e7d
+    if (S_ISREG(file_mode) && !(rpmfiFFlags(fi) & RPMFILE_GHOST)) {
ea2e7d
+	rpm_ino_t inode = rpmfiFInode(fi);
ea2e7d
+	/* check for hard link entry in table. GetEntry overwrites hlix with
ea2e7d
+	 * the address of the first match.
ea2e7d
+	 */
ea2e7d
+	if (inodeIndexHashGetEntry(state->inodeIndexes, inode, &hlix, NULL,
ea2e7d
+	                           NULL)) {
ea2e7d
+	    /* entry is in table, use hard link */
ea2e7d
+	    char *fn = rpmfilesFN(state->files, hlix[0]);
ea2e7d
+	    if (link(fn, path) != 0) {
ea2e7d
+		rpmlog(RPMLOG_ERR,
ea2e7d
+		       _("reflink: Unable to hard link %s -> %s due to %s\n"),
ea2e7d
+		       fn, path, strerror(errno));
ea2e7d
+		free(fn);
ea2e7d
+		return RPMRC_FAIL;
ea2e7d
+	    }
ea2e7d
+	    free(fn);
ea2e7d
+	    return RPMRC_PLUGIN_CONTENTS;
ea2e7d
+	}
ea2e7d
+	/* if we didn't hard link, then we'll track this inode as being
ea2e7d
+	 * created soon
ea2e7d
+	 */
ea2e7d
+	if (rpmfiFNlink(fi) > 1) {
ea2e7d
+	    /* minor optimization: only store files with more than one link */
ea2e7d
+	    inodeIndexHashAddEntry(state->inodeIndexes, inode, rpmfiFX(fi));
ea2e7d
+	}
ea2e7d
+	/* derived from wfd_open in fsm.c */
ea2e7d
+	mode_t old_umask = umask(0577);
ea2e7d
+	dst = open(path, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR);
ea2e7d
+	umask(old_umask);
ea2e7d
+	if (dst == -1) {
ea2e7d
+	    rpmlog(RPMLOG_ERR,
ea2e7d
+		   _("reflink: Unable to open %s for writing due to %s, flags = %x\n"),
ea2e7d
+		   path, strerror(errno), rpmfiFFlags(fi));
ea2e7d
+	    return RPMRC_FAIL;
ea2e7d
+	}
ea2e7d
+	size = rpmfiFSize(fi);
ea2e7d
+	if (size > 0) {
ea2e7d
+	    /* round src_length down to fundamental_block_size multiple */
ea2e7d
+	    fcr.src_length = size / state->fundamental_block_size * state->fundamental_block_size;
ea2e7d
+	    if ((size % state->fundamental_block_size) > 0) {
ea2e7d
+		/* round up to next fundamental_block_size. We expect the data
ea2e7d
+		 * in the rpm to be similarly padded.
ea2e7d
+		 */
ea2e7d
+		fcr.src_length += state->fundamental_block_size;
ea2e7d
+	    }
ea2e7d
+	    fcr.src_fd = Fileno(state->fd);
ea2e7d
+	    if (fcr.src_fd == -1) {
ea2e7d
+		close(dst);
ea2e7d
+		rpmlog(RPMLOG_ERR, _("reflink: src fd lookup failed\n"));
ea2e7d
+		return RPMRC_FAIL;
ea2e7d
+	    }
ea2e7d
+	    fcr.src_offset = find(rpmfiFDigest(fi, NULL, NULL), state);
ea2e7d
+	    if (fcr.src_offset == NOT_FOUND) {
ea2e7d
+		close(dst);
ea2e7d
+		rpmlog(RPMLOG_ERR, _("reflink: digest not found\n"));
ea2e7d
+		return RPMRC_FAIL;
ea2e7d
+	    }
ea2e7d
+	    rpmlog(RPMLOG_DEBUG,
ea2e7d
+	           _("reflink: Reflinking %llu bytes at %llu to %s orig size=%ld, file=%lld\n"),
ea2e7d
+		   fcr.src_length, fcr.src_offset, path, size, fcr.src_fd);
ea2e7d
+	    rc = ioctl(dst, FICLONERANGE, &fcr;;
ea2e7d
+	    if (rc) {
ea2e7d
+		rpmlog(RPMLOG_WARNING,
ea2e7d
+		       _("reflink: falling back to copying bits for %s due to %d, %d = %s\n"),
ea2e7d
+		       path, rc, errno, strerror(errno));
ea2e7d
+		if (Fseek(state->fd, fcr.src_offset, SEEK_SET) < 0) {
ea2e7d
+		    close(dst);
ea2e7d
+		    rpmlog(RPMLOG_ERR,
ea2e7d
+			   _("reflink: unable to seek on copying bits\n"));
ea2e7d
+		    return RPMRC_FAIL;
ea2e7d
+		}
ea2e7d
+		rpm_loff_t left = size;
ea2e7d
+		size_t len, read, written;
ea2e7d
+		while (left) {
ea2e7d
+		    len = (left > BUFFER_SIZE ? BUFFER_SIZE : left);
ea2e7d
+		    read = Fread(state->buffer, len, 1, state->fd);
ea2e7d
+		    if (read != len) {
ea2e7d
+			close(dst);
ea2e7d
+			rpmlog(RPMLOG_ERR,
ea2e7d
+			       _("reflink: short read on copying bits\n"));
ea2e7d
+			return RPMRC_FAIL;
ea2e7d
+		    }
ea2e7d
+		    written = write(dst, state->buffer, len);
ea2e7d
+		    if (read != written) {
ea2e7d
+			close(dst);
ea2e7d
+			rpmlog(RPMLOG_ERR,
ea2e7d
+			       _("reflink: short write on copying bits\n"));
ea2e7d
+			return RPMRC_FAIL;
ea2e7d
+		    }
ea2e7d
+		    left -= len;
ea2e7d
+		}
ea2e7d
+	    } else {
ea2e7d
+		/* reflink worked, so truncate */
ea2e7d
+		rc = ftruncate(dst, size);
ea2e7d
+		if (rc) {
ea2e7d
+		    rpmlog(RPMLOG_ERR,
ea2e7d
+			   _("reflink: Unable to truncate %s to %ld due to %s\n"),
ea2e7d
+			   path, size, strerror(errno));
ea2e7d
+		     return RPMRC_FAIL;
ea2e7d
+		}
ea2e7d
+	    }
ea2e7d
+	}
ea2e7d
+	close(dst);
ea2e7d
+	return RPMRC_PLUGIN_CONTENTS;
ea2e7d
+    }
ea2e7d
+    return RPMRC_OK;
ea2e7d
+}
ea2e7d
+
ea2e7d
+struct rpmPluginHooks_s reflink_hooks = {
ea2e7d
+    .init = reflink_init,
ea2e7d
+    .cleanup = reflink_cleanup,
ea2e7d
+    .psm_pre = reflink_psm_pre,
ea2e7d
+    .psm_post = reflink_psm_post,
ea2e7d
+    .fsm_file_pre = reflink_fsm_file_pre,
ea2e7d
+};
ea2e7d
diff --git a/rpm2extents.c b/rpm2extents.c
ea2e7d
new file mode 100644
ea2e7d
index 000000000..c111be0a2
ea2e7d
--- /dev/null
ea2e7d
+++ b/rpm2extents.c
ea2e7d
@@ -0,0 +1,433 @@
ea2e7d
+/* rpm2extents: convert payload to inline extents */
ea2e7d
+
ea2e7d
+#include "system.h"
ea2e7d
+
ea2e7d
+#include <rpm/rpmlib.h>		/* rpmReadPackageFile .. */
ea2e7d
+#include <rpm/rpmfi.h>
ea2e7d
+#include <rpm/rpmtag.h>
ea2e7d
+#include <rpm/rpmio.h>
ea2e7d
+#include <rpm/rpmpgp.h>
ea2e7d
+
ea2e7d
+#include <rpm/rpmts.h>
ea2e7d
+#include "lib/rpmlead.h"
ea2e7d
+#include "lib/signature.h"
ea2e7d
+#include "lib/header_internal.h"
ea2e7d
+#include "rpmio/rpmio_internal.h"
ea2e7d
+
ea2e7d
+#include <unistd.h>
ea2e7d
+#include <sys/types.h>
ea2e7d
+#include <sys/wait.h>
ea2e7d
+#include <signal.h>
ea2e7d
+#include <errno.h>
ea2e7d
+#include <string.h>
ea2e7d
+
ea2e7d
+#include "debug.h"
ea2e7d
+
ea2e7d
+/* hash of void * (pointers) to file digests to offsets within output.
ea2e7d
+ * The length of the key depends on what the FILEDIGESTALGO is.
ea2e7d
+ */
ea2e7d
+#undef HASHTYPE
ea2e7d
+#undef HTKEYTYPE
ea2e7d
+#undef HTDATATYPE
ea2e7d
+#define HASHTYPE digestSet
ea2e7d
+#define HTKEYTYPE const unsigned char *
ea2e7d
+#include "lib/rpmhash.H"
ea2e7d
+#include "lib/rpmhash.C"
ea2e7d
+
ea2e7d
+/* magic value at end of file (64 bits) that indicates this is a transcoded
ea2e7d
+ * rpm.
ea2e7d
+ */
ea2e7d
+#define MAGIC 3472329499408095051
ea2e7d
+
ea2e7d
+struct digestoffset {
ea2e7d
+    const unsigned char * digest;
ea2e7d
+    rpm_loff_t pos;
ea2e7d
+};
ea2e7d
+
ea2e7d
+rpm_loff_t pad_to(rpm_loff_t pos, rpm_loff_t unit);
ea2e7d
+
ea2e7d
+rpm_loff_t pad_to(rpm_loff_t pos, rpm_loff_t unit)
ea2e7d
+{
ea2e7d
+    return (unit - (pos % unit)) % unit;
ea2e7d
+}
ea2e7d
+
ea2e7d
+static int digestor(
ea2e7d
+    FD_t fdi,
ea2e7d
+    FD_t fdo,
ea2e7d
+    FD_t validationo,
ea2e7d
+    uint8_t algos[],
ea2e7d
+    uint32_t algos_len
ea2e7d
+)
ea2e7d
+{
ea2e7d
+    ssize_t fdilength;
ea2e7d
+    const char *filedigest, *algo_name;
ea2e7d
+    size_t filedigest_len, len;
ea2e7d
+    uint32_t algo_name_len, algo_digest_len;
ea2e7d
+    int algo;
ea2e7d
+    rpmRC rc = RPMRC_FAIL;
ea2e7d
+
ea2e7d
+    for (algo = 0; algo < algos_len; algo++) {
ea2e7d
+	fdInitDigest(fdi, algos[algo], 0);
ea2e7d
+    }
ea2e7d
+    fdilength = ufdCopy(fdi, fdo);
ea2e7d
+    if (fdilength == -1) {
ea2e7d
+	fprintf(stderr, _("digest cat failed\n"));
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    len = sizeof(fdilength);
ea2e7d
+    if (Fwrite(&fdilength, len, 1, validationo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write input length %zd\n"), fdilength);
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    len = sizeof(algos_len);
ea2e7d
+    if (Fwrite(&algos_len, len, 1, validationo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write number of validation digests\n"));
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    for (algo = 0; algo < algos_len; algo++) {
ea2e7d
+	fdFiniDigest(fdi, algos[algo], (void **)&filedigest, &filedigest_len, 0);
ea2e7d
+
ea2e7d
+	algo_name = pgpValString(PGPVAL_HASHALGO, algos[algo]);
ea2e7d
+	algo_name_len = (uint32_t)strlen(algo_name);
ea2e7d
+	algo_digest_len = (uint32_t)filedigest_len;
ea2e7d
+
ea2e7d
+	len = sizeof(algo_name_len);
ea2e7d
+	if (Fwrite(&algo_name_len, len, 1, validationo) != len) {
ea2e7d
+	    fprintf(stderr,
ea2e7d
+		    _("Unable to write validation algo name length\n"));
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+	len = sizeof(algo_digest_len);
ea2e7d
+	if (Fwrite(&algo_digest_len, len, 1, validationo) != len) {
ea2e7d
+	    fprintf(stderr,
ea2e7d
+		    _("Unable to write number of bytes for validation digest\n"));
ea2e7d
+	     goto exit;
ea2e7d
+	}
ea2e7d
+	if (Fwrite(algo_name, algo_name_len, 1, validationo) != algo_name_len) {
ea2e7d
+	    fprintf(stderr, _("Unable to write validation algo name\n"));
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+	if (Fwrite(filedigest, algo_digest_len, 1, validationo ) != algo_digest_len) {
ea2e7d
+	    fprintf(stderr,
ea2e7d
+		    _("Unable to write validation digest value %u, %zu\n"),
ea2e7d
+		    algo_digest_len, filedigest_len);
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+    }
ea2e7d
+    rc = RPMRC_OK;
ea2e7d
+exit:
ea2e7d
+    return rc;
ea2e7d
+}
ea2e7d
+
ea2e7d
+static rpmRC process_package(FD_t fdi, FD_t validationi)
ea2e7d
+{
ea2e7d
+    uint32_t diglen;
ea2e7d
+    /* GNU C extension: can use diglen from outer context */
ea2e7d
+    int digestSetCmp(const unsigned char * a, const unsigned char * b) {
ea2e7d
+	return memcmp(a, b, diglen);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    unsigned int digestSetHash(const unsigned char * digest) {
ea2e7d
+        /* assumes sizeof(unsigned int) < diglen */
ea2e7d
+        return *(unsigned int *)digest;
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    int digestoffsetCmp(const void * a, const void * b) {
ea2e7d
+	return digestSetCmp(
ea2e7d
+	    ((struct digestoffset *)a)->digest,
ea2e7d
+	    ((struct digestoffset *)b)->digest
ea2e7d
+	);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    FD_t fdo;
ea2e7d
+    FD_t gzdi;
ea2e7d
+    Header h, sigh;
ea2e7d
+    long fundamental_block_size = sysconf(_SC_PAGESIZE);
ea2e7d
+    rpmRC rc = RPMRC_OK;
ea2e7d
+    rpm_mode_t mode;
ea2e7d
+    char *rpmio_flags = NULL, *zeros;
ea2e7d
+    const unsigned char *digest;
ea2e7d
+    rpm_loff_t pos, size, pad, validation_pos;
ea2e7d
+    uint32_t offset_ix = 0;
ea2e7d
+    size_t len;
ea2e7d
+    int next = 0;
ea2e7d
+
ea2e7d
+    fdo = fdDup(STDOUT_FILENO);
ea2e7d
+
ea2e7d
+    if (rpmReadPackageRaw(fdi, &sigh, &h)) {
ea2e7d
+	fprintf(stderr, _("Error reading package\n"));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    if (rpmLeadWrite(fdo, h))
ea2e7d
+    {
ea2e7d
+	fprintf(stderr, _("Unable to write package lead: %s\n"),
ea2e7d
+		Fstrerror(fdo));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    if (rpmWriteSignature(fdo, sigh)) {
ea2e7d
+	fprintf(stderr, _("Unable to write signature: %s\n"), Fstrerror(fdo));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    if (headerWrite(fdo, h, HEADER_MAGIC_YES)) {
ea2e7d
+	fprintf(stderr, _("Unable to write headers: %s\n"), Fstrerror(fdo));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    /* Retrieve payload size and compression type. */
ea2e7d
+    {
ea2e7d
+	const char *compr = headerGetString(h, RPMTAG_PAYLOADCOMPRESSOR);
ea2e7d
+	rpmio_flags = rstrscat(NULL, "r.", compr ? compr : "gzip", NULL);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    gzdi = Fdopen(fdi, rpmio_flags);	/* XXX gzdi == fdi */
ea2e7d
+    free(rpmio_flags);
ea2e7d
+
ea2e7d
+    if (gzdi == NULL) {
ea2e7d
+	fprintf(stderr, _("cannot re-open payload: %s\n"), Fstrerror(gzdi));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    rpmfiles files = rpmfilesNew(NULL, h, 0, RPMFI_KEEPHEADER);
ea2e7d
+    rpmfi fi = rpmfiNewArchiveReader(gzdi, files,
ea2e7d
+				     RPMFI_ITER_READ_ARCHIVE_CONTENT_FIRST);
ea2e7d
+
ea2e7d
+    /* this is encoded in the file format, so needs to be fixed size (for
ea2e7d
+     * now?)
ea2e7d
+     */
ea2e7d
+    diglen = (uint32_t)rpmDigestLength(rpmfiDigestAlgo(fi));
ea2e7d
+    digestSet ds = digestSetCreate(rpmfiFC(fi), digestSetHash, digestSetCmp,
ea2e7d
+				   NULL);
ea2e7d
+    struct digestoffset offsets[rpmfiFC(fi)];
ea2e7d
+    pos = RPMLEAD_SIZE + headerSizeof(sigh, HEADER_MAGIC_YES);
ea2e7d
+
ea2e7d
+    /* main headers are aligned to 8 byte boundry */
ea2e7d
+    pos += pad_to(pos, 8);
ea2e7d
+    pos += headerSizeof(h, HEADER_MAGIC_YES);
ea2e7d
+
ea2e7d
+    zeros = xcalloc(fundamental_block_size, 1);
ea2e7d
+
ea2e7d
+    while (next >= 0) {
ea2e7d
+	next = rpmfiNext(fi);
ea2e7d
+	if (next == RPMERR_ITER_END) {
ea2e7d
+	    rc = RPMRC_OK;
ea2e7d
+	    break;
ea2e7d
+	}
ea2e7d
+	mode = rpmfiFMode(fi);
ea2e7d
+	if (!S_ISREG(mode) || !rpmfiArchiveHasContent(fi)) {
ea2e7d
+	    /* not a regular file, or the archive doesn't contain any content
ea2e7d
+	     * for this entry.
ea2e7d
+	    */
ea2e7d
+	    continue;
ea2e7d
+	}
ea2e7d
+	digest = rpmfiFDigest(fi, NULL, NULL);
ea2e7d
+	if (digestSetGetEntry(ds, digest, NULL)) {
ea2e7d
+	    /* This specific digest has already been included, so skip it. */
ea2e7d
+	    continue;
ea2e7d
+	}
ea2e7d
+	pad = pad_to(pos, fundamental_block_size);
ea2e7d
+	if (Fwrite(zeros, sizeof(char), pad, fdo) != pad) {
ea2e7d
+	    fprintf(stderr, _("Unable to write padding\n"));
ea2e7d
+	    rc = RPMRC_FAIL;
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+	/* round up to next fundamental_block_size */
ea2e7d
+	pos += pad;
ea2e7d
+	digestSetAddEntry(ds, digest);
ea2e7d
+	offsets[offset_ix].digest = digest;
ea2e7d
+	offsets[offset_ix].pos = pos;
ea2e7d
+	offset_ix++;
ea2e7d
+	size = rpmfiFSize(fi);
ea2e7d
+	rc = rpmfiArchiveReadToFile(fi, fdo, 0);
ea2e7d
+	if (rc != RPMRC_OK) {
ea2e7d
+	    fprintf(stderr, _("rpmfiArchiveReadToFile failed with %d\n"), rc);
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+	pos += size;
ea2e7d
+    }
ea2e7d
+    Fclose(gzdi);	/* XXX gzdi == fdi */
ea2e7d
+
ea2e7d
+    qsort(offsets, (size_t)offset_ix, sizeof(struct digestoffset),
ea2e7d
+	  digestoffsetCmp);
ea2e7d
+
ea2e7d
+    len = sizeof(offset_ix);
ea2e7d
+    if (Fwrite(&offset_ix, len, 1, fdo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write length of table\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    len = sizeof(diglen);
ea2e7d
+    if (Fwrite(&diglen, len, 1, fdo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write length of digest\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    len = sizeof(rpm_loff_t);
ea2e7d
+    for (int x = 0; x < offset_ix; x++) {
ea2e7d
+	if (Fwrite(offsets[x].digest, diglen, 1, fdo) != diglen) {
ea2e7d
+	    fprintf(stderr, _("Unable to write digest\n"));
ea2e7d
+	    rc = RPMRC_FAIL;
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+	if (Fwrite(&offsets[x].pos, len, 1, fdo) != len) {
ea2e7d
+	    fprintf(stderr, _("Unable to write offset\n"));
ea2e7d
+	    rc = RPMRC_FAIL;
ea2e7d
+	    goto exit;
ea2e7d
+	}
ea2e7d
+    }
ea2e7d
+    validation_pos = (
ea2e7d
+	pos + sizeof(offset_ix) + sizeof(diglen) +
ea2e7d
+	offset_ix * (diglen + sizeof(rpm_loff_t))
ea2e7d
+    );
ea2e7d
+
ea2e7d
+    ssize_t validation_len = ufdCopy(validationi, fdo);
ea2e7d
+    if (validation_len == -1) {
ea2e7d
+	fprintf(stderr, _("digest table ufdCopy failed\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    /* add more padding so the last file can be cloned. It doesn't matter that
ea2e7d
+     * the table and validation etc are in this space. In fact, it's pretty
ea2e7d
+     * efficient if it is.
ea2e7d
+    */
ea2e7d
+
ea2e7d
+    pad = pad_to((validation_pos + validation_len + 2 * sizeof(rpm_loff_t) +
ea2e7d
+		 sizeof(uint64_t)), fundamental_block_size);
ea2e7d
+    if (Fwrite(zeros, sizeof(char), pad, fdo) != pad) {
ea2e7d
+	fprintf(stderr, _("Unable to write final padding\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    zeros = _free(zeros);
ea2e7d
+    if (Fwrite(&pos, len, 1, fdo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write offset of digest table\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    if (Fwrite(&validation_pos, len, 1, fdo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write offset of validation table\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+    uint64_t magic = MAGIC;
ea2e7d
+    len = sizeof(magic);
ea2e7d
+    if (Fwrite(&magic, len, 1, fdo) != len) {
ea2e7d
+	fprintf(stderr, _("Unable to write magic\n"));
ea2e7d
+	rc = RPMRC_FAIL;
ea2e7d
+	goto exit;
ea2e7d
+    }
ea2e7d
+
ea2e7d
+exit:
ea2e7d
+    rpmfilesFree(files);
ea2e7d
+    rpmfiFree(fi);
ea2e7d
+    headerFree(h);
ea2e7d
+    return rc;
ea2e7d
+}
ea2e7d
+
ea2e7d
+int main(int argc, char *argv[]) {
ea2e7d
+    rpmRC rc;
ea2e7d
+    int cprc = 0;
ea2e7d
+    uint8_t algos[argc - 1];
ea2e7d
+    int mainpipefd[2];
ea2e7d
+    int metapipefd[2];
ea2e7d
+    pid_t cpid, w;
ea2e7d
+    int wstatus;
ea2e7d
+
ea2e7d
+    xsetprogname(argv[0]);	/* Portability call -- see system.h */
ea2e7d
+    rpmReadConfigFiles(NULL, NULL);
ea2e7d
+
ea2e7d
+    if (argc > 1 && (rstreq(argv[1], "-h") || rstreq(argv[1], "--help"))) {
ea2e7d
+	fprintf(stderr, _("Usage: %s [DIGESTALGO]...\n"), argv[0]);
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    if (argc == 1) {
ea2e7d
+	fprintf(stderr,
ea2e7d
+		_("Need at least one DIGESTALGO parameter, e.g. 'SHA256'\n"));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+
ea2e7d
+    for (int x = 0; x < (argc - 1); x++) {
ea2e7d
+	if (pgpStringVal(PGPVAL_HASHALGO, argv[x + 1], &algos[x]) != 0)
ea2e7d
+	{
ea2e7d
+	    fprintf(stderr,
ea2e7d
+		    _("Unable to resolve '%s' as a digest algorithm, exiting\n"),
ea2e7d
+		    argv[x + 1]);
ea2e7d
+	    exit(EXIT_FAILURE);
ea2e7d
+	}
ea2e7d
+    }
ea2e7d
+
ea2e7d
+
ea2e7d
+    if (pipe(mainpipefd) == -1) {
ea2e7d
+	fprintf(stderr, _("Main pipe failure\n"));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+    if (pipe(metapipefd) == -1) {
ea2e7d
+	fprintf(stderr, _("Meta pipe failure\n"));
ea2e7d
+	exit(EXIT_FAILURE);
ea2e7d
+    }
ea2e7d
+    cpid = fork();
ea2e7d
+    if (cpid == 0) {
ea2e7d
+	/* child: digestor */
ea2e7d
+	close(mainpipefd[0]);
ea2e7d
+	close(metapipefd[0]);
ea2e7d
+	FD_t fdi = fdDup(STDIN_FILENO);
ea2e7d
+	FD_t fdo = fdDup(mainpipefd[1]);
ea2e7d
+	FD_t validationo = fdDup(metapipefd[1]);
ea2e7d
+	rc = digestor(fdi, fdo, validationo, algos, argc - 1);
ea2e7d
+	Fclose(validationo);
ea2e7d
+	Fclose(fdo);
ea2e7d
+	Fclose(fdi);
ea2e7d
+    } else {
ea2e7d
+	/* parent: main program */
ea2e7d
+	close(mainpipefd[1]);
ea2e7d
+	close(metapipefd[1]);
ea2e7d
+	FD_t fdi = fdDup(mainpipefd[0]);
ea2e7d
+	FD_t validationi = fdDup(metapipefd[0]);
ea2e7d
+	rc = process_package(fdi, validationi);
ea2e7d
+	Fclose(validationi);
ea2e7d
+	/* fdi is normally closed through the stacked file gzdi in the
ea2e7d
+	 * function.
ea2e7d
+	 * Wait for child process (digestor for stdin) to complete.
ea2e7d
+	 */
ea2e7d
+	if (rc != RPMRC_OK) {
ea2e7d
+	    if (kill(cpid, SIGTERM) != 0) {
ea2e7d
+		fprintf(stderr,
ea2e7d
+		        _("Failed to kill digest process when main process failed: %s\n"),
ea2e7d
+			strerror(errno));
ea2e7d
+	    }
ea2e7d
+	}
ea2e7d
+	w = waitpid(cpid, &wstatus, 0);
ea2e7d
+	if (w == -1) {
ea2e7d
+	    fprintf(stderr, _("waitpid failed\n"));
ea2e7d
+	    cprc = EXIT_FAILURE;
ea2e7d
+	} else if (WIFEXITED(wstatus)) {
ea2e7d
+	    cprc = WEXITSTATUS(wstatus);
ea2e7d
+	    if (cprc != 0) {
ea2e7d
+		fprintf(stderr,
ea2e7d
+			_("Digest process non-zero exit code %d\n"),
ea2e7d
+			cprc);
ea2e7d
+	    }
ea2e7d
+	} else if (WIFSIGNALED(wstatus)) {
ea2e7d
+	    fprintf(stderr,
ea2e7d
+		    _("Digest process was terminated with a signal: %d\n"),
ea2e7d
+		    WTERMSIG(wstatus));
ea2e7d
+	    cprc = EXIT_FAILURE;
ea2e7d
+	} else {
ea2e7d
+	    /* Don't think this can happen, but covering all bases */
ea2e7d
+	    fprintf(stderr, _("Unhandled circumstance in waitpid\n"));
ea2e7d
+	    cprc = EXIT_FAILURE;
ea2e7d
+	}
ea2e7d
+	if (cprc != EXIT_SUCCESS) {
ea2e7d
+	    rc = RPMRC_FAIL;
ea2e7d
+	}
ea2e7d
+    }
ea2e7d
+    if (rc != RPMRC_OK) {
ea2e7d
+	/* translate rpmRC into generic failure return code. */
ea2e7d
+	return EXIT_FAILURE;
ea2e7d
+    }
ea2e7d
+    return EXIT_SUCCESS;
ea2e7d
+}
ea2e7d
diff --git a/rpmio/rpmpgp.c b/rpmio/rpmpgp.c
ea2e7d
index 015c15a5c..7b972b4a6 100644
ea2e7d
--- a/rpmio/rpmpgp.c
ea2e7d
+++ b/rpmio/rpmpgp.c
ea2e7d
@@ -283,6 +283,16 @@ int pgpValTok(pgpValTbl vs, const char * s, const char * se)
ea2e7d
     return vs->val;
ea2e7d
 }
ea2e7d
 
ea2e7d
+int pgpStringVal(pgpValType type, const char *str, uint8_t *val)
ea2e7d
+{
ea2e7d
+    pgpValTbl tbl = pgpValTable(type);
ea2e7d
+    if (tbl == NULL) return -1;
ea2e7d
+    int v = pgpValTok(tbl, str, str + strlen(str));
ea2e7d
+    if (v == -1) return -1;
ea2e7d
+    *val = (uint8_t)v;
ea2e7d
+    return 0;
ea2e7d
+}
ea2e7d
+
ea2e7d
 /** \ingroup rpmpgp
ea2e7d
  * Decode length from 1, 2, or 5 octet body length encoding, used in
ea2e7d
  * new format packet headers and V4 signature subpackets.
ea2e7d
diff --git a/rpmio/rpmpgp.h b/rpmio/rpmpgp.h
ea2e7d
index c53e29b01..2b57318ba 100644
ea2e7d
--- a/rpmio/rpmpgp.h
ea2e7d
+++ b/rpmio/rpmpgp.h
ea2e7d
@@ -973,6 +973,15 @@ typedef rpmFlags rpmDigestFlags;
ea2e7d
  */
ea2e7d
 const char * pgpValString(pgpValType type, uint8_t val);
ea2e7d
 
ea2e7d
+/** \ingroup rpmpgp
ea2e7d
+ * Return  OpenPGP value for a string.
ea2e7d
+ * @param type		type of value
ea2e7d
+ * @param str		string to lookup
ea2e7d
+ * @param[out] val  byte value associated with string
ea2e7d
+ * @return		0 on success else -1
ea2e7d
+ */
ea2e7d
+int pgpStringVal(pgpValType type, const char *str, uint8_t *val);
ea2e7d
+
ea2e7d
 /** \ingroup rpmpgp
ea2e7d
  * Return (native-endian) integer from big-endian representation.
ea2e7d
  * @param s		pointer to big-endian integer