cluster-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Andrew Price <anprice@redhat.com>
To: cluster-devel.redhat.com
Subject: [Cluster-devel] [PATCH 02/13] restoremeta: Abstract out metadata file opening
Date: Thu, 23 Jan 2020 15:55:41 +0000	[thread overview]
Message-ID: <20200123155552.1080247-3-anprice@redhat.com> (raw)
In-Reply-To: <20200123155552.1080247-1-anprice@redhat.com>

Provide wrapper functions for the operations which make use of zlib
functions, to make it simpler to provide ones for other compression
methods later.

Signed-off-by: Andrew Price <anprice@redhat.com>
---
 gfs2/edit/savemeta.c | 46 +++++++++++++++++++++++++++++---------------
 1 file changed, 30 insertions(+), 16 deletions(-)

diff --git a/gfs2/edit/savemeta.c b/gfs2/edit/savemeta.c
index 9de53a97..a20d7aa0 100644
--- a/gfs2/edit/savemeta.c
+++ b/gfs2/edit/savemeta.c
@@ -1191,13 +1191,33 @@ static void complain(const char *complaint)
 	    "<dest file system>\n");
 }
 
+static int open_metadata(const char *path, struct metafd *mfd)
+{
+	mfd->filename = path;
+	mfd->fd = open(path, O_RDONLY|O_CLOEXEC);
+	if (mfd->fd < 0) {
+		perror("Could not open metadata file");
+		return 1;
+	}
+	mfd->gzfd = gzdopen(mfd->fd, "rb");
+	if (!mfd->gzfd) {
+		perror("gzdopen");
+		return 1;
+	}
+	return 0;
+}
+
+static void close_metadata(struct metafd *mfd)
+{
+	gzclose(mfd->gzfd);
+}
+
 void restoremeta(const char *in_fn, const char *out_device, uint64_t printonly)
 {
 	int error;
-	gzFile gzfd;
 	off_t pos = 0;
 	struct savemeta_header smh = {0};
-	int fd;
+	struct metafd mfd = {0};
 
 	termlines = 0;
 	if (!in_fn)
@@ -1205,15 +1225,9 @@ void restoremeta(const char *in_fn, const char *out_device, uint64_t printonly)
 	if (!printonly && !out_device)
 		complain("No destination file system specified.");
 
-	fd = open(in_fn, O_RDONLY|O_CLOEXEC);
-	if (fd < 0) {
-		perror("Could not open file");
-		exit(1);
-	}
-	gzfd = gzdopen(fd, "rb");
-	if (!gzfd)
-		die("Can't open source file %s: %s\n",
-		    in_fn, strerror(errno));
+	error = open_metadata(in_fn, &mfd);
+	if (error != 0)
+		exit(error);
 
 	if (!printonly) {
 		sbd.device_fd = open(out_device, O_RDWR);
@@ -1224,8 +1238,8 @@ void restoremeta(const char *in_fn, const char *out_device, uint64_t printonly)
 				  optional block no */
 		printonly = check_keywords(out_device);
 
-	pos = restore_init(gzfd, &smh);
-	error = restore_super(gzfd, pos);
+	pos = restore_init(mfd.gzfd, &smh);
+	error = restore_super(mfd.gzfd, pos);
 	if (error)
 		exit(1);
 
@@ -1236,16 +1250,16 @@ void restoremeta(const char *in_fn, const char *out_device, uint64_t printonly)
 		printf("There are %"PRIu64" free blocks on the destination device.\n", space);
 	}
 
-	error = find_highest_block(gzfd, pos, sbd.fssize);
+	error = find_highest_block(mfd.gzfd, pos, sbd.fssize);
 	if (error)
 		exit(1);
 
-	error = restore_data(sbd.device_fd, gzfd, pos, printonly);
+	error = restore_data(sbd.device_fd, mfd.gzfd, pos, printonly);
 	printf("File %s %s %s.\n", in_fn,
 	       (printonly ? "print" : "restore"),
 	       (error ? "error" : "successful"));
 
-	gzclose(gzfd);
+	close_metadata(&mfd);
 	if (!printonly)
 		close(sbd.device_fd);
 	free(indirect);
-- 
2.24.1



  parent reply	other threads:[~2020-01-23 15:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-23 15:55 [Cluster-devel] [PATCH 00/13] gfs2_edit restoremeta: Add bzip2 support Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 01/13] restoremeta: Use zlib by file descriptor Andrew Price
2020-01-23 15:55 ` Andrew Price [this message]
2020-01-23 15:55 ` [Cluster-devel] [PATCH 03/13] restoremeta: Use metafd instead of gzFile for parameters Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 04/13] restoremeta: Abstract out decompression operations Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 05/13] restoremeta: Combine restore_init() and open_metadata() Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 06/13] restoremeta: Don't seek in restore_header() Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 07/13] savemeta: Remove anthropomorphize() Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 08/13] restoremeta: Remove find_highest_block() Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 09/13] restoremeta: Metadata file reading overhaul Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 10/13] restoremeta: Convert iseof function to a flag Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 11/13] restoremeta: Combine parse_header() and check_header() Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 12/13] restoremeta: Add bzip2 support Andrew Price
2020-01-23 15:55 ` [Cluster-devel] [PATCH 13/13] restoremeta: Skip the right number of bytes for the superblock Andrew Price

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200123155552.1080247-3-anprice@redhat.com \
    --to=anprice@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).