From 7b04057502fde1b2984601e8f7a817c8d2b0e2a8 Mon Sep 17 00:00:00 2001 From: John Darrington Date: Fri, 16 Jan 2015 14:43:48 +0100 Subject: [PATCH] replace_file_start: On error, ensure that the appropriate errno value is held --- src/data/make-file.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/data/make-file.c b/src/data/make-file.c index 63792e90b4..e4520f411e 100644 --- a/src/data/make-file.c +++ b/src/data/make-file.c @@ -59,6 +59,7 @@ replace_file_start (const char *file_name, const char *mode, struct stat s; struct replace_file *rf; int fd; + int saved_errno = errno; /* If FILE_NAME represents a special file, write to it directly instead of trying to replace it. */ @@ -68,8 +69,10 @@ replace_file_start (const char *file_name, const char *mode, fd = open (file_name, O_WRONLY); if (fd < 0) { + saved_errno = errno; msg (ME, _("Opening %s for writing: %s."), - file_name, strerror (errno)); + file_name, strerror (saved_errno)); + errno = saved_errno; return NULL; } @@ -77,9 +80,11 @@ replace_file_start (const char *file_name, const char *mode, *fp = fdopen (fd, mode); if (*fp == NULL) { - msg (ME, _("Opening stream for %s: %s."), - file_name, strerror (errno)); + saved_errno = errno; + msg (ME, _("Opening stream for %s: %s."), + file_name, strerror (saved_errno)); close (fd); + errno = saved_errno; return NULL; } @@ -108,6 +113,7 @@ replace_file_start (const char *file_name, const char *mode, { msg (ME, _("Creating temporary file to replace %s: %s."), rf->file_name, strerror (errno)); + saved_errno = errno; goto error; } @@ -119,6 +125,7 @@ replace_file_start (const char *file_name, const char *mode, { msg (ME, _("Creating temporary file %s: %s."), rf->tmp_name, strerror (errno)); + saved_errno = errno; goto error; } free (rf->tmp_name); @@ -133,6 +140,7 @@ replace_file_start (const char *file_name, const char *mode, rf->tmp_name, strerror (errno)); close (fd); unlink (rf->tmp_name); + saved_errno = errno; goto error; } @@ -151,6 +159,7 @@ error: *fp = NULL; if (tmp_name != NULL) *tmp_name = NULL; + errno = saved_errno; return NULL; } -- 2.30.2