e2b73b
From 251c3931fedc9275bd57bc75c2153674a540bbce Mon Sep 17 00:00:00 2001
e2b73b
From: "Dmitry V. Levin" <ldv@altlinux.org>
e2b73b
Date: Thu, 18 May 2017 17:19:25 +0300
e2b73b
Subject: [PATCH] quotaops: check return code of ftruncate and lseek calls
e2b73b
MIME-Version: 1.0
e2b73b
Content-Type: text/plain; charset=UTF-8
e2b73b
Content-Transfer-Encoding: 8bit
e2b73b
e2b73b
ftruncate and lseek syscalls may fail for different reasons,
e2b73b
do not ignore these errors.  Create a helper function and use it
e2b73b
instead of duplicating error checks and diagnostic messages.
e2b73b
e2b73b
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
e2b73b
Signed-off-by: Jan Kara <jack@suse.cz>
e2b73b
Signed-off-by: Petr Písař <ppisar@redhat.com>
e2b73b
---
e2b73b
 quotaops.c | 53 +++++++++++++++++++++++++++++------------------------
e2b73b
 1 file changed, 29 insertions(+), 24 deletions(-)
e2b73b
e2b73b
diff --git a/quotaops.c b/quotaops.c
e2b73b
index 6f245b7..81d7be0 100644
e2b73b
--- a/quotaops.c
e2b73b
+++ b/quotaops.c
e2b73b
@@ -252,6 +252,28 @@ int editprivs(char *tmpfile)
e2b73b
 }
e2b73b
 
e2b73b
 /*
e2b73b
+ * Duplicate a file descriptor, resetting the file offset.
e2b73b
+ * If it's a write descriptor, also truncate the file.
e2b73b
+ */
e2b73b
+static FILE *dup_file(int fd, int is_write)
e2b73b
+{
e2b73b
+	FILE *fp;
e2b73b
+
e2b73b
+	if (is_write && ftruncate(fd, 0))
e2b73b
+		die(1, _("Cannot truncate a file: %s\n"), strerror(errno));
e2b73b
+	if (lseek(fd, 0, SEEK_SET))
e2b73b
+		die(1, _("Cannot reset a file offset: %s\n"), strerror(errno));
e2b73b
+	if ((fd = dup(fd)) < 0)
e2b73b
+		die(1, _("Cannot duplicate a file descriptor: %s\n"), strerror(errno));
e2b73b
+	if (!(fp = fdopen(fd, is_write ? "w" : "r")))
e2b73b
+		die(1, is_write ? _("Cannot open a stream to write to: %s\n")
e2b73b
+				: _("Cannot open a stream to read from: %s\n"),
e2b73b
+		    strerror(errno));
e2b73b
+
e2b73b
+	return fp;
e2b73b
+}
e2b73b
+
e2b73b
+/*
e2b73b
  * Convert a dquot list to an ASCII file.
e2b73b
  */
e2b73b
 int writeprivs(struct dquot *qlist, int outfd, char *name, int quotatype)
e2b73b
@@ -259,10 +281,7 @@ int writeprivs(struct dquot *qlist, int outfd, char *name, int quotatype)
e2b73b
 	struct dquot *q;
e2b73b
 	FILE *fd;
e2b73b
 
e2b73b
-	ftruncate(outfd, 0);
e2b73b
-	lseek(outfd, 0, SEEK_SET);
e2b73b
-	if (!(fd = fdopen(dup(outfd), "w")))
e2b73b
-		die(1, _("Cannot duplicate descriptor of file to write to: %s\n"), strerror(errno));
e2b73b
+	fd = dup_file(outfd, 1);
e2b73b
 
e2b73b
 	fprintf(fd, _("Disk quotas for %s %s (%cid %d):\n"),
e2b73b
 		_(type2name(quotatype)), name, *type2name(quotatype), qlist->dq_id);
e2b73b
@@ -323,9 +342,7 @@ int readprivs(struct dquot *qlist, int infd)
e2b73b
 	char inodesstring[BUFSIZ], isoftstring[BUFSIZ], ihardstring[BUFSIZ];
e2b73b
 	const char *error;
e2b73b
 
e2b73b
-	lseek(infd, 0, SEEK_SET);
e2b73b
-	if (!(fd = fdopen(dup(infd), "r")))
e2b73b
-		die(1, _("Cannot duplicate descriptor of temp file: %s\n"), strerror(errno));
e2b73b
+	fd = dup_file(infd, 0);
e2b73b
 
e2b73b
 	/*
e2b73b
 	 * Discard title lines, then read lines to process.
e2b73b
@@ -435,10 +452,7 @@ int writeindividualtimes(struct dquot *qlist, int outfd, char *name, int quotaty
e2b73b
 	time_t now;
e2b73b
 	char btimestr[MAXTIMELEN], itimestr[MAXTIMELEN];
e2b73b
 
e2b73b
-	ftruncate(outfd, 0);
e2b73b
-	lseek(outfd, 0, SEEK_SET);
e2b73b
-	if (!(fd = fdopen(dup(outfd), "w")))
e2b73b
-		die(1, _("Cannot duplicate descriptor of file to write to: %s\n"), strerror(errno));
e2b73b
+	fd = dup_file(outfd, 1);
e2b73b
 
e2b73b
 	fprintf(fd, _("Times to enforce softlimit for %s %s (%cid %d):\n"),
e2b73b
 		_(type2name(quotatype)), name, *type2name(quotatype), qlist->dq_id);
e2b73b
@@ -478,9 +492,7 @@ int readindividualtimes(struct dquot *qlist, int infd)
e2b73b
 	char iunits[BUFSIZ], bunits[BUFSIZ];
e2b73b
 	time_t now, bseconds, iseconds;
e2b73b
 
e2b73b
-	lseek(infd, 0, SEEK_SET);
e2b73b
-	if (!(fd = fdopen(dup(infd), "r")))
e2b73b
-		die(1, _("Cannot duplicate descriptor of temp file: %s\n"), strerror(errno));
e2b73b
+	fd = dup_file(infd, 0);
e2b73b
 
e2b73b
 	/*
e2b73b
 	 * Discard title lines, then read lines to process.
e2b73b
@@ -543,10 +555,7 @@ int writetimes(struct quota_handle **handles, int outfd)
e2b73b
 	if (!handles[0])
e2b73b
 		return 0;
e2b73b
 
e2b73b
-	ftruncate(outfd, 0);
e2b73b
-	lseek(outfd, 0, SEEK_SET);
e2b73b
-	if ((fd = fdopen(dup(outfd), "w")) == NULL)
e2b73b
-		die(1, _("Cannot duplicate descriptor of file to edit: %s\n"), strerror(errno));
e2b73b
+	fd = dup_file(outfd, 1);
e2b73b
 
e2b73b
 	fprintf(fd, _("Grace period before enforcing soft limits for %ss:\n"),
e2b73b
 		_(type2name(handles[0]->qh_type)));
e2b73b
@@ -575,12 +584,8 @@ int readtimes(struct quota_handle **handles, int infd)
e2b73b
 
e2b73b
 	if (!handles[0])
e2b73b
 		return 0;
e2b73b
-	lseek(infd, 0, SEEK_SET);
e2b73b
-	if (!(fd = fdopen(dup(infd), "r"))) {
e2b73b
-		errstr(_("Cannot reopen temp file: %s\n"),
e2b73b
-			strerror(errno));
e2b73b
-		return -1;
e2b73b
-	}
e2b73b
+
e2b73b
+	fd = dup_file(infd, 0);
e2b73b
 
e2b73b
 	/* Set all grace times to default values */
e2b73b
 	for (i = 0; handles[i]; i++) {
e2b73b
-- 
e2b73b
2.9.4
e2b73b