xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Jackson <Ian.Jackson@eu.citrix.com>,
	Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 03/11] libxl: New utility functions in for reading and writing files.
Date: Thu, 25 Mar 2010 19:04:06 +0000	[thread overview]
Message-ID: <1269543854-7780-4-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1269543854-7780-1-git-send-email-ian.jackson@eu.citrix.com>

We introduce these functions in libxl_utils.h:

  int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
                               void **data_r, int *datalen_r);
  int libxl_read_exactly(struct libxl_ctx *ctx, int fd, void *data, ssize_t sz,
                         const char *filename, const char *what);
  int libxl_write_exactly(struct libxl_ctx *ctx, int fd, const void *data,
                          ssize_t sz, const char *filename, const char *what);

They will be needed by the following patches.  They have to be in
libxl.a rather than libxutil.a because they will be used, amongst
other places, in libxl itself.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
---
 tools/libxl/libxl_utils.c |  103 +++++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_utils.h |   18 ++++++++
 2 files changed, 121 insertions(+), 0 deletions(-)

diff --git a/tools/libxl/libxl_utils.c b/tools/libxl/libxl_utils.c
index afc852a..3a1f095 100644
--- a/tools/libxl/libxl_utils.c
+++ b/tools/libxl/libxl_utils.c
@@ -26,6 +26,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
+#include <assert.h>
 
 #include "libxl_utils.h"
 #include "libxl_internal.h"
@@ -177,3 +178,105 @@ out:
     return rc;
 }
 
+int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
+                             void **data_r, int *datalen_r) {
+    FILE *f = 0;
+    uint8_t *data = 0;
+    int datalen = 0;
+    int e;
+    struct stat stab;
+    ssize_t rs;
+    
+    f = fopen(filename, "r");
+    if (!f) {
+        if (errno == ENOENT) return ENOENT;
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to open %s", filename);
+        goto xe;
+    }
+
+    if (fstat(fileno(f), &stab)) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to fstat %s", filename);
+        goto xe;
+    }
+
+    if (!S_ISREG(stab.st_mode)) {
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "%s is not a plain file", filename);
+        errno = ENOTTY;
+        goto xe;
+    }
+
+    if (stab.st_size > INT_MAX) {
+        XL_LOG(ctx, XL_LOG_ERROR, "file %s is far too large", filename);
+        errno = EFBIG;
+        goto xe;
+    }
+
+    datalen = stab.st_size;
+
+    if (stab.st_size && data_r) {
+        data = malloc(datalen);
+        if (!data) goto xe;
+
+        rs = fread(data, 1, datalen, f);
+        if (rs != datalen) {
+            if (ferror(f))
+                XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to read %s", filename);
+            else if (feof(f))
+                XL_LOG(ctx, XL_LOG_ERROR, "%s changed size while we"
+                       " were reading it", filename);
+            else
+                abort();
+            goto xe;
+        }
+    }
+
+    if (fclose(f)) {
+        f = 0;
+        XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to close %s", filename);
+        goto xe;
+    }
+
+    if (data_r) *data_r = data;
+    if (datalen_r) *datalen_r = datalen;
+
+    return 0;
+
+ xe:
+    e = errno;
+    assert(e != ENOENT);
+    if (f) fclose(f);
+    if (data) free(data);
+    return e;
+}
+
+#define READ_WRITE_EXACTLY(rw, zero_is_eof, constdata)                    \
+                                                                          \
+  int libxl_##rw##_exactly(struct libxl_ctx *ctx, int fd,                 \
+                           constdata void *data, ssize_t sz,              \
+                           const char *filename, const char *what) {      \
+      ssize_t got;                                                        \
+                                                                          \
+      while (sz > 0) {                                                    \
+          got = rw(fd, data, sz);                                         \
+          if (got == -1) {                                                \
+              if (errno == EINTR) continue;                               \
+              XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to " #rw " %s%s%s", \
+                           what?what:"", what?" from ":"", filename);     \
+              return errno;                                               \
+          }                                                               \
+          if (got == 0) {                                                 \
+              XL_LOG(ctx, XL_LOG_ERROR,                                   \
+                     zero_is_eof                                          \
+                     ? "file/stream truncated reading %s%s%s"             \
+                     : "file/stream write returned 0! writing %s%s%s",    \
+                     what?what:"", what?" from ":"", filename);           \
+              return EPROTO;                                              \
+          }                                                               \
+          sz -= got;                                                      \
+          data = (char*)data + got;                                       \
+      }                                                                   \
+      return 0;                                                           \
+  }
+
+READ_WRITE_EXACTLY(read, 1, /* */)
+READ_WRITE_EXACTLY(write, 0, const)
diff --git a/tools/libxl/libxl_utils.h b/tools/libxl/libxl_utils.h
index 05fa4e3..65e7e31 100644
--- a/tools/libxl/libxl_utils.h
+++ b/tools/libxl/libxl_utils.h
@@ -26,5 +26,23 @@ int libxl_is_stubdom(struct libxl_ctx *ctx, uint32_t domid, uint32_t *target_dom
 int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name);
 int libxl_string_to_phystype(struct libxl_ctx *ctx, char *s, libxl_disk_phystype *phystype);
 
+int libxl_read_file_contents(struct libxl_ctx *ctx, const char *filename,
+                             void **data_r, int *datalen_r);
+  /* Reads the contents of the plain file filename into a mallocd
+   * buffer.  Returns 0 or errno.  Any errors other than ENOENT are logged.
+   * If the file is empty, *data_r and *datalen_r are set to 0.
+   * On error, *data_r and *datalen_r are undefined.
+   * data_r and/or datalen_r may be 0.
+   */
+
+int libxl_read_exactly(struct libxl_ctx *ctx, int fd, void *data, ssize_t sz,
+                       const char *filename, const char *what);
+int libxl_write_exactly(struct libxl_ctx *ctx, int fd, const void *data,
+                        ssize_t sz, const char *filename, const char *what);
+  /* Returns 0 or errno.  If file is truncated on reading, returns
+   * EPROTO and you have no way to tell how much was read.  Errors are
+   * logged using filename (which is only used for logging) and what
+   * (which may be 0). */
+    
 #endif
 
-- 
1.5.6.5

  parent reply	other threads:[~2010-03-25 19:04 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-25 19:04 [PATCH 00/11] Migration support for "xl" Ian Jackson
2010-03-25 19:04 ` [PATCH 01/11] libxl: Make logging functions preserve errno Ian Jackson
2010-03-25 19:04 ` [PATCH 02/11] libxl: Report error if logfile rotation fails Ian Jackson
2010-03-25 19:04 ` Ian Jackson [this message]
2010-03-25 19:04 ` [PATCH 04/11] libxl: libxl_domain_restore: Put fd back to blocking mode Ian Jackson
2010-03-25 19:04 ` [PATCH 05/11] libxl: Provide libxl_domain_rename Ian Jackson
2010-03-26 10:01   ` Vincent Hanquez
2010-03-26 10:23     ` Ian Jackson
2010-03-26 10:34       ` Vincent Hanquez
2010-03-26 10:40         ` Ian Jackson
2010-04-01 11:00         ` Stefano Stabellini
2010-03-25 19:04 ` [PATCH 06/11] libxl: Expose functions for helping with subprocesses Ian Jackson
2010-03-25 19:04 ` [PATCH 07/11] libxl: Expose libxl_report_exitstatus Ian Jackson
2010-03-25 19:04 ` [PATCH 08/11] libxl: Per-domain data storage for the convenience of the library user Ian Jackson
2010-03-26 10:06   ` Vincent Hanquez
2010-03-26 10:20     ` Ian Jackson
2010-03-26 10:29       ` Vincent Hanquez
2010-03-26 10:34         ` Ian Jackson
2010-03-26 10:41           ` Vincent Hanquez
2010-03-26 10:45             ` Ian Jackson
2010-03-26 11:27               ` Vincent Hanquez
2010-03-26 11:56                 ` Ian Jackson
2010-04-01 11:04       ` Stefano Stabellini
2010-03-25 19:04 ` [PATCH 09/11] xl: New savefile format. Save domain config when saving a domain Ian Jackson
2010-04-01 11:08   ` Stefano Stabellini
2010-03-25 19:04 ` [PATCH 10/11] xl: Domain creation logging fixes Ian Jackson
2010-03-25 19:04 ` [PATCH 11/11] xl: Migration support Ian Jackson
2010-04-01 11:10   ` Stefano Stabellini
2010-04-01 11:10 ` [PATCH 00/11] Migration support for "xl" Stefano Stabellini

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=1269543854-7780-4-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /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;
as well as URLs for NNTP newsgroup(s).