summaryrefslogtreecommitdiffstats
path: root/daemon/file.c
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2010-09-26 18:00:11 +0100
committerRichard W.M. Jones <rjones@redhat.com>2010-09-26 22:21:36 +0100
commiteeaab2ba84441a96977281a4472b1bccb9ec80a9 (patch)
tree0e0e103b582b957d3d018469843dd49912e8893d /daemon/file.c
parent5a8c8b8bf6e846b8d2d7e710f814d24d9a0183c3 (diff)
downloadlibguestfs-eeaab2ba84441a96977281a4472b1bccb9ec80a9.tar.gz
libguestfs-eeaab2ba84441a96977281a4472b1bccb9ec80a9.tar.xz
libguestfs-eeaab2ba84441a96977281a4472b1bccb9ec80a9.zip
New API: pwrite-device
This is the same as the existing 'pwrite' API call, but allows you to write to a device.
Diffstat (limited to 'daemon/file.c')
-rw-r--r--daemon/file.c44
1 files changed, 35 insertions, 9 deletions
diff --git a/daemon/file.c b/daemon/file.c
index 08494564..9403100d 100644
--- a/daemon/file.c
+++ b/daemon/file.c
@@ -463,11 +463,32 @@ do_pread (const char *path, int count, int64_t offset, size_t *size_r)
return buf;
}
+static int
+pwrite_fd (int fd, const char *content, size_t size, int64_t offset,
+ const char *display_path)
+{
+ ssize_t r;
+
+ r = pwrite (fd, content, size, offset);
+ if (r == -1) {
+ reply_with_perror ("pwrite: %s", display_path);
+ close (fd);
+ return -1;
+ }
+
+ if (close (fd) == -1) {
+ reply_with_perror ("close: %s", display_path);
+ close (fd);
+ return -1;
+ }
+
+ return r;
+}
+
int
do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
{
int fd;
- ssize_t r;
if (offset < 0) {
reply_with_error ("offset is negative");
@@ -483,20 +504,25 @@ do_pwrite (const char *path, const char *content, size_t size, int64_t offset)
return -1;
}
- r = pwrite (fd, content, size, offset);
- if (r == -1) {
- reply_with_perror ("pwrite: %s", path);
- close (fd);
+ return pwrite_fd (fd, content, size, offset, path);
+}
+
+int
+do_pwrite_device (const char *device, const char *content, size_t size,
+ int64_t offset)
+{
+ if (offset < 0) {
+ reply_with_error ("offset is negative");
return -1;
}
- if (close (fd) == -1) {
- reply_with_perror ("close: %s", path);
- close (fd);
+ int fd = open (device, O_WRONLY);
+ if (fd == -1) {
+ reply_with_perror ("open: %s", device);
return -1;
}
- return r;
+ return pwrite_fd (fd, content, size, offset, device);
}
/* This runs the 'file' command. */