aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxl/libxl_internal.c
diff options
context:
space:
mode:
authorIan Campbell <ian.campbell@citrix.com>2010-08-19 15:25:15 +0100
committerIan Campbell <ian.campbell@citrix.com>2010-08-19 15:25:15 +0100
commit10cf11b43638a854e372e39275edb7863614a7b9 (patch)
tree9c96bae68871baf1b0df633c6382b9c98b6f04be /tools/libxl/libxl_internal.c
parent60ce81bc56b4b8a997a7d8791390d3da1a389a71 (diff)
downloadxen-10cf11b43638a854e372e39275edb7863614a7b9.tar.gz
xen-10cf11b43638a854e372e39275edb7863614a7b9.tar.bz2
xen-10cf11b43638a854e372e39275edb7863614a7b9.zip
libxl: implement destroy for libxl_file_reference builtin type
As well as freeing data any file mappings need to be torn down so implement an explicit destroy function. Also the map and unmap function are internal to libxl so make that so. [PATCH 09 of 16 of libxl: autogenerate type definitions and destructor functions] Signed-off-by: Ian Campbell <ian.campbell@citrix.com> Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Diffstat (limited to 'tools/libxl/libxl_internal.c')
-rw-r--r--tools/libxl/libxl_internal.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 5eb27a6f4d..05ef3e3130 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -19,6 +19,12 @@
#include <stdarg.h>
#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
#include "libxl.h"
#include "libxl_internal.h"
#include "libxl_utils.h"
@@ -185,3 +191,48 @@ char *libxl_abs_path(libxl_gc *gc, char *s, const char *path)
return libxl_sprintf(gc, "%s/%s", path, s);
}
+
+int libxl__file_reference_map(libxl_file_reference *f)
+{
+ struct stat st_buf;
+ int ret, fd;
+ void *data;
+
+ if (f->mapped)
+ return 0;
+
+ fd = open(f->path, O_RDONLY);
+ if (f < 0)
+ return ERROR_FAIL;
+
+ ret = fstat(fd, &st_buf);
+ if (ret < 0)
+ goto out;
+
+ ret = -1;
+ data = mmap(NULL, st_buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (data == NULL)
+ goto out;
+
+ f->mapped = 1;
+ f->data = data;
+ f->size = st_buf.st_size;
+
+ ret = 0;
+out:
+ close(fd);
+
+ return ret == 0 ? 0 : ERROR_FAIL;
+}
+
+int libxl__file_reference_unmap(libxl_file_reference *f)
+{
+ int ret;
+
+ if (!f->mapped)
+ return 0;
+
+ ret = munmap(f->data, f->size);
+
+ return ret == 0 ? 0 : ERROR_FAIL;
+}