From: Geert Uytterhoeven <geert@linux-m68k.org>
To: Simon Horman <horms@verge.net.au>
Cc: linux-m68k@lists.linux-m68k.org, kexec@lists.infradead.org,
Geert Uytterhoeven <geert@linux-m68k.org>,
Dave Young <dyoung@redhat.com>
Subject: [PATCH 2/3] kexec: Extract slurp_fd()
Date: Tue, 15 Oct 2013 19:50:59 +0200 [thread overview]
Message-ID: <1381859460-13645-3-git-send-email-geert@linux-m68k.org> (raw)
In-Reply-To: <1381859460-13645-1-git-send-email-geert@linux-m68k.org>
Factor out the common part of slurp_file() and slurp_file_len() into
a new helper function slurp_fd().
Signed-off-by: Geert Uytterhoeven <geert@linux-m68k.org>
Cc: Dave Young <dyoung@redhat.com>
---
kexec/kexec.c | 92 +++++++++++++++++++++++++++------------------------------
1 file changed, 43 insertions(+), 49 deletions(-)
diff --git a/kexec/kexec.c b/kexec/kexec.c
index 185c85bef342..583a35a24a70 100644
--- a/kexec/kexec.c
+++ b/kexec/kexec.c
@@ -469,14 +469,46 @@ static int add_backup_segments(struct kexec_info *info,
return 0;
}
+static char *slurp_fd(int fd, const char *filename, off_t size, off_t *nread)
+{
+ char *buf;
+ off_t progress;
+ ssize_t result;
+
+ buf = xmalloc(size);
+ progress = 0;
+ while (progress < size) {
+ result = read(fd, buf + progress, size - progress);
+ if (result < 0) {
+ if ((errno == EINTR) || (errno == EAGAIN))
+ continue;
+ fprintf(stderr, "Read on %s failed: %s\n", filename,
+ strerror(errno));
+ free(buf);
+ close(fd);
+ return NULL;
+ }
+ if (result == 0)
+ /* EOF */
+ break;
+ progress += result;
+ }
+ result = close(fd);
+ if (result < 0)
+ die("Close of %s failed: %s\n", filename, strerror(errno));
+
+ if (nread)
+ *nread = progress;
+ return buf;
+}
+
char *slurp_file(const char *filename, off_t *r_size)
{
int fd;
char *buf;
- off_t size, progress, err;
+ off_t size, err, nread;
ssize_t result;
struct stat stats;
-
if (!filename) {
*r_size = 0;
@@ -512,25 +544,14 @@ char *slurp_file(const char *filename, off_t *r_size)
size = stats.st_size;
}
+ buf = slurp_fd(fd, filename, size, &nread);
+ if (!buf)
+ die("Cannot read %s", filename);
+
+ if (nread != size)
+ die("Read on %s ended before stat said it should\n", filename);
+
*r_size = size;
- buf = xmalloc(size);
- progress = 0;
- while(progress < size) {
- result = read(fd, buf + progress, size - progress);
- if (result < 0) {
- if ((errno == EINTR) || (errno == EAGAIN))
- continue;
- die("read on %s of %ld bytes failed: %s\n", filename,
- (size - progress)+ 0UL, strerror(errno));
- }
- if (result == 0)
- die("read on %s ended before stat said it should\n", filename);
- progress += result;
- }
- result = close(fd);
- if (result < 0) {
- die("Close of %s failed: %s\n", filename, strerror(errno));
- }
return buf;
}
@@ -540,9 +561,6 @@ char *slurp_file(const char *filename, off_t *r_size)
char *slurp_file_len(const char *filename, off_t size, off_t *nread)
{
int fd;
- char *buf;
- off_t progress;
- ssize_t result;
if (!filename)
return 0;
@@ -552,32 +570,8 @@ char *slurp_file_len(const char *filename, off_t size, off_t *nread)
strerror(errno));
return 0;
}
- buf = xmalloc(size);
- progress = 0;
- while(progress < size) {
- result = read(fd, buf + progress, size - progress);
- if (result < 0) {
- if ((errno == EINTR) || (errno == EAGAIN))
- continue;
- fprintf(stderr, "read on %s of %ld bytes failed: %s\n",
- filename, (size - progress)+ 0UL,
- strerror(errno));
- free(buf);
- return 0;
- }
- if (result == 0)
- /* EOF */
- break;
- progress += result;
- }
- result = close(fd);
- if (result < 0) {
- die("Close of %s failed: %s\n",
- filename, strerror(errno));
- }
- if (nread)
- *nread = progress;
- return buf;
+
+ return slurp_fd(fd, filename, size, nread);
}
char *slurp_decompress_file(const char *filename, off_t *r_size)
--
1.7.9.5
next prev parent reply other threads:[~2013-10-15 17:50 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-10-15 17:50 [PATCH v2 0/3] kexec support for Linux/m68k (tools part) Geert Uytterhoeven
2013-10-15 17:50 ` [PATCH 1/3] kexec: Let slurp_file_len() return the number of bytes read Geert Uytterhoeven
2013-10-15 17:50 ` Geert Uytterhoeven [this message]
2013-10-15 17:51 ` [PATCH 3/3] kexec: Add m68k support Geert Uytterhoeven
2014-09-22 20:04 ` [PATCH] kexec: distribute kexec/arch/m68k/bootinfo.h Andreas Schwab
2014-10-12 19:37 ` Andreas Schwab
2014-10-14 5:06 ` Simon Horman
2013-10-16 7:22 ` [PATCH v2 0/3] kexec support for Linux/m68k (tools part) Simon Horman
2013-11-03 14:08 ` Geert Uytterhoeven
2013-11-05 7:05 ` Simon Horman
2013-12-01 11:40 ` Geert Uytterhoeven
2013-12-13 0:51 ` Simon Horman
2013-12-13 0:53 ` Simon Horman
2013-12-13 8:03 ` Geert Uytterhoeven
2013-12-14 0:31 ` Simon Horman
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=1381859460-13645-3-git-send-email-geert@linux-m68k.org \
--to=geert@linux-m68k.org \
--cc=dyoung@redhat.com \
--cc=horms@verge.net.au \
--cc=kexec@lists.infradead.org \
--cc=linux-m68k@lists.linux-m68k.org \
/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