From 70f5b535e4452b79b317f94d9fd0188e97f93102 Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Fri, 22 May 2015 10:54:41 -0400 Subject: [PATCH] libpriv: Add RpmOstreeRefTs and API to create one from a commit This API is intended for use by the caching framework to hold open an rpmdb for the previous commit. --- Makefile-libpriv.am | 2 + src/libpriv/rpmostree-refts.c | 64 ++++++++++++++++++++++++++++++++ src/libpriv/rpmostree-refts.h | 45 ++++++++++++++++++++++ src/libpriv/rpmostree-rpm-util.c | 32 ++++++++++++++++ src/libpriv/rpmostree-rpm-util.h | 8 ++++ 5 files changed, 151 insertions(+) create mode 100644 src/libpriv/rpmostree-refts.c create mode 100644 src/libpriv/rpmostree-refts.h diff --git a/Makefile-libpriv.am b/Makefile-libpriv.am index 2774991b..be7af190 100644 --- a/Makefile-libpriv.am +++ b/Makefile-libpriv.am @@ -26,6 +26,8 @@ librpmostreepriv_la_SOURCES = \ src/libpriv/rpmostree-util.h \ src/libpriv/rpmostree-passwd-util.c \ src/libpriv/rpmostree-passwd-util.h \ + src/libpriv/rpmostree-refts.h \ + src/libpriv/rpmostree-refts.c \ src/libpriv/rpmostree-refsack.h \ src/libpriv/rpmostree-refsack.c \ src/libpriv/rpmostree-cleanup.h \ diff --git a/src/libpriv/rpmostree-refts.c b/src/libpriv/rpmostree-refts.c new file mode 100644 index 00000000..72d1ae43 --- /dev/null +++ b/src/libpriv/rpmostree-refts.c @@ -0,0 +1,64 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Colin Walters + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation; either version 2 of the licence or (at + * your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "config.h" + +#include +#include "rpmostree-refts.h" +#include "rpmostree-rpm-util.h" + +/* + * A wrapper for an `rpmts` that supports: + * + * - Reference counting + * - Possibly holding a pointer to a tempdir, and cleaning it when unref'd + */ + +RpmOstreeRefTs * +rpmostree_refts_new (rpmts ts, int temp_base_dfd, const char *temp_path) +{ + RpmOstreeRefTs *rts = g_new0 (RpmOstreeRefTs, 1); + rts->ts = ts; + rts->refcount = 1; + rts->temp_base_dfd = temp_base_dfd; + rts->temp_path = g_strdup (temp_path); + return rts; +} + +RpmOstreeRefTs * +rpmostree_refts_ref (RpmOstreeRefTs *rts) +{ + g_atomic_int_inc (&rts->refcount); + return rts; +} + +void +rpmostree_refts_unref (RpmOstreeRefTs *rts) +{ + if (!g_atomic_int_dec_and_test (&rts->refcount)) + return; + rpmtsFree (rts->ts); + + if (rts->temp_path) + (void) glnx_shutil_rm_rf_at (rts->temp_base_dfd, rts->temp_path, NULL, NULL); + + g_free (rts->temp_path); + g_free (rts); +} diff --git a/src/libpriv/rpmostree-refts.h b/src/libpriv/rpmostree-refts.h new file mode 100644 index 00000000..28a44b5f --- /dev/null +++ b/src/libpriv/rpmostree-refts.h @@ -0,0 +1,45 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2015 Red Hat, In.c + * + * Licensed under the GNU Lesser General Public License Version 2.1 + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#pragma once + +#include +#include +#include "rpmostree-cleanup.h" + +typedef struct { + volatile gint refcount; + rpmts ts; + int temp_base_dfd; + char *temp_path; +} RpmOstreeRefTs; + +RpmOstreeRefTs * +rpmostree_refts_new (rpmts ts, int temp_base_dfd, const char *temp_path); + +RpmOstreeRefTs * +rpmostree_refts_ref (RpmOstreeRefTs *rts); + +void +rpmostree_refts_unref (RpmOstreeRefTs *rts); + +G_DEFINE_AUTOPTR_CLEANUP_FUNC(RpmOstreeRefTs, rpmostree_refts_unref); + diff --git a/src/libpriv/rpmostree-rpm-util.c b/src/libpriv/rpmostree-rpm-util.c index 08e18e2e..c92bd49c 100644 --- a/src/libpriv/rpmostree-rpm-util.c +++ b/src/libpriv/rpmostree-rpm-util.c @@ -869,6 +869,38 @@ rpmostree_get_refsack_for_commit (OstreeRepo *repo, return ret; } +gboolean +rpmostree_get_refts_for_commit (OstreeRepo *repo, + const char *ref, + RpmOstreeRefTs **out_ts, + GCancellable *cancellable, + GError **error) +{ + gboolean ret = FALSE; + g_autofree char *tempdir = NULL; + rpmts ts; + int r; + + if (!rpmostree_checkout_only_rpmdb_tempdir (repo, ref, &tempdir, NULL, + cancellable, error)) + goto out; + + ts = rpmtsCreate (); + /* This actually makes sense because we know we've verified it at build time */ + rpmtsSetVSFlags (ts, _RPMVSF_NODIGESTS | _RPMVSF_NOSIGNATURES); + + r = rpmtsSetRootDir (ts, tempdir); + g_assert_cmpint (r, ==, 0); + + ret = TRUE; + *out_ts = rpmostree_refts_new (ts, AT_FDCWD, tempdir); + g_clear_pointer (&tempdir, g_free); + out: + if (tempdir) + (void) glnx_shutil_rm_rf_at (AT_FDCWD, tempdir, NULL, NULL); + return ret; +} + gboolean rpmostree_get_pkglist_for_root (int dfd, const char *path, diff --git a/src/libpriv/rpmostree-rpm-util.h b/src/libpriv/rpmostree-rpm-util.h index 61676c15..bf6d0b6e 100644 --- a/src/libpriv/rpmostree-rpm-util.h +++ b/src/libpriv/rpmostree-rpm-util.h @@ -26,6 +26,7 @@ #include #include "rpmostree-util.h" #include "rpmostree-refsack.h" +#include "rpmostree-refts.h" #include "rpmostree-cleanup.h" #include "libglnx.h" @@ -101,6 +102,13 @@ rpmostree_get_refsack_for_root (int dfd, GCancellable *cancellable, GError **error); +gboolean +rpmostree_get_refts_for_commit (OstreeRepo *repo, + const char *ref, + RpmOstreeRefTs **out_ts, + GCancellable *cancellable, + GError **error); + gboolean rpmostree_get_pkglist_for_root (int dfd, const char *path,