diff options
author | Geert Uytterhoeven <geert@linux-m68k.org> | 2013-10-15 19:50:59 +0200 |
---|---|---|
committer | Simon Horman <horms@verge.net.au> | 2013-12-13 09:52:47 +0900 |
commit | a57cc363c27dc0d836d62ec2c2cee04be95587c5 (patch) | |
tree | 7646e7a3ddaf06298fb617db45b777b2b8f3bf0d /kexec/kexec.c | |
parent | 65cffca0a1166682b3d7b44998096f21b39bc5a0 (diff) |
kexec: Extract slurp_fd()
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>
Signed-off-by: Simon Horman <horms@verge.net.au>
Diffstat (limited to 'kexec/kexec.c')
-rw-r--r-- | kexec/kexec.c | 92 |
1 files changed, 43 insertions, 49 deletions
diff --git a/kexec/kexec.c b/kexec/kexec.c index d903373..f13e512 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) |