[Asterisk-code-review] app voicemail: Fix file copy error handling. (asterisk[15])

Jenkins2 asteriskteam at digium.com
Fri Dec 22 07:33:25 CST 2017


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7703 )

Change subject: app_voicemail: Fix file copy error handling.
......................................................................

app_voicemail: Fix file copy error handling.

Fix error where input/output file descriptors would be closed multiple
times.

Change-Id: Iba5140b60cb7de79e3d5d92be3c256947aa99da9
---
M apps/app_voicemail.c
1 file changed, 42 insertions(+), 35 deletions(-)

Approvals:
  George Joseph: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/apps/app_voicemail.c b/apps/app_voicemail.c
index 01111a1..3ef9c44 100644
--- a/apps/app_voicemail.c
+++ b/apps/app_voicemail.c
@@ -4628,49 +4628,56 @@
 {
 	int ifd;
 	int ofd;
-	int res;
+	int res = -1;
 	int len;
 	char buf[4096];
 
 #ifdef HARDLINK_WHEN_POSSIBLE
 	/* Hard link if possible; saves disk space & is faster */
-	if (link(infile, outfile)) {
-#endif
-		if ((ifd = open(infile, O_RDONLY)) < 0) {
-			ast_log(AST_LOG_WARNING, "Unable to open %s in read-only mode: %s\n", infile, strerror(errno));
-			return -1;
-		}
-		if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, VOICEMAIL_FILE_MODE)) < 0) {
-			ast_log(AST_LOG_WARNING, "Unable to open %s in write-only mode: %s\n", outfile, strerror(errno));
-			close(ifd);
-			return -1;
-		}
-		do {
-			len = read(ifd, buf, sizeof(buf));
-			if (len < 0) {
-				ast_log(AST_LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
-				close(ifd);
-				close(ofd);
-				unlink(outfile);
-			} else if (len) {
-				res = write(ofd, buf, len);
-				if (errno == ENOMEM || errno == ENOSPC || res != len) {
-					ast_log(AST_LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, res, len, strerror(errno));
-					close(ifd);
-					close(ofd);
-					unlink(outfile);
-				}
-			}
-		} while (len);
-		close(ifd);
-		close(ofd);
-		return 0;
-#ifdef HARDLINK_WHEN_POSSIBLE
-	} else {
-		/* Hard link succeeded */
+	if (!link(infile, outfile)) {
 		return 0;
 	}
 #endif
+
+	if ((ifd = open(infile, O_RDONLY)) < 0) {
+		ast_log(AST_LOG_WARNING, "Unable to open %s in read-only mode: %s\n", infile, strerror(errno));
+		return -1;
+	}
+
+	if ((ofd = open(outfile, O_WRONLY | O_TRUNC | O_CREAT, VOICEMAIL_FILE_MODE)) < 0) {
+		ast_log(AST_LOG_WARNING, "Unable to open %s in write-only mode: %s\n", outfile, strerror(errno));
+		close(ifd);
+		return -1;
+	}
+
+	for (;;) {
+		int wrlen;
+
+		len = read(ifd, buf, sizeof(buf));
+		if (!len) {
+			res = 0;
+			break;
+		}
+
+		if (len < 0) {
+			ast_log(AST_LOG_WARNING, "Read failed on %s: %s\n", infile, strerror(errno));
+			break;
+		}
+
+		wrlen = write(ofd, buf, len);
+		if (errno == ENOMEM || errno == ENOSPC || wrlen != len) {
+			ast_log(AST_LOG_WARNING, "Write failed on %s (%d of %d): %s\n", outfile, wrlen, len, strerror(errno));
+			break;
+		}
+	}
+
+	close(ifd);
+	close(ofd);
+	if (res) {
+		unlink(outfile);
+	}
+
+	return res;
 }
 
 /*!

-- 
To view, visit https://gerrit.asterisk.org/7703
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: merged
Gerrit-Change-Id: Iba5140b60cb7de79e3d5d92be3c256947aa99da9
Gerrit-Change-Number: 7703
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20171222/fd832ee7/attachment.html>


More information about the asterisk-code-review mailing list