diff mbox

[1/3] kvmtool/setup: Introduce extract_file() helper

Message ID 20151019105942.GA12425@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Oleg Nesterov Oct. 19, 2015, 10:59 a.m. UTC
Turn kvm_setup_guest_init(guestfs_name) into a more generic helper,
extract_file(guestfs_name, filename, data, size) and reimplement
kvm_setup_guest_init() as a trivial wrapper.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 builtin-setup.c | 24 ++++++++++++++----------
 1 file changed, 14 insertions(+), 10 deletions(-)
diff mbox

Patch

diff --git a/builtin-setup.c b/builtin-setup.c
index 40fef15..bb7c71c 100644
--- a/builtin-setup.c
+++ b/builtin-setup.c
@@ -122,30 +122,34 @@  static const char *guestfs_symlinks[] = {
 };
 
 #ifdef CONFIG_GUEST_INIT
-extern char _binary_guest_init_start;
-extern char _binary_guest_init_size;
-
-int kvm_setup_guest_init(const char *guestfs_name)
+static int extract_file(const char *guestfs_name, const char *filename,
+			const void *data, const void *_size)
 {
 	char path[PATH_MAX];
-	size_t size;
 	int fd, ret;
-	char *data;
 
-	size = (size_t)&_binary_guest_init_size;
-	data = (char *)&_binary_guest_init_start;
-	snprintf(path, PATH_MAX, "%s%s/virt/init", kvm__get_dir(), guestfs_name);
+	snprintf(path, PATH_MAX, "%s%s/%s", kvm__get_dir(),
+				guestfs_name, filename);
 	remove(path);
 	fd = open(path, O_CREAT | O_WRONLY, 0755);
 	if (fd < 0)
 		die("Fail to setup %s", path);
-	ret = xwrite(fd, data, size);
+	ret = xwrite(fd, data, (size_t)_size);
 	if (ret < 0)
 		die("Fail to setup %s", path);
 	close(fd);
 
 	return 0;
+}
 
+extern char _binary_guest_init_start;
+extern char _binary_guest_init_size;
+
+int kvm_setup_guest_init(const char *guestfs_name)
+{
+	return extract_file(guestfs_name, "virt/init",
+				&_binary_guest_init_start,
+				&_binary_guest_init_size);
 }
 #else
 int kvm_setup_guest_init(const char *guestfs_name)