diff --git a/lib/device/dev-io.c b/lib/device/dev-io.c index 4f2d27e13..565ed6c70 100644 --- a/lib/device/dev-io.c +++ b/lib/device/dev-io.c @@ -38,6 +38,10 @@ int dev_get_size(struct device *dev, uint64_t *size) return 1; } +/* + * FIXME: factor common code out. + */ + int _read(int fd, void *buf, size_t count) { size_t n = 0; @@ -77,9 +81,41 @@ int64_t dev_read(struct device *dev, uint64_t offset, return r; } +int _write(int fd, const void *buf, size_t count) +{ + size_t n = 0; + int tot = 0; + + while (tot < count) { + n = write(fd, buf, count - tot); + + if (n <= 0) + return tot ? tot : n; + + tot += n; + buf += n; + } + + return tot; +} + int64_t dev_write(struct device *dev, uint64_t offset, int64_t len, void *buffer) { - // FIXME: lazy programmer - return 0; + int64_t r; + int fd = open(dev->name, O_WRONLY); + + if (fd < 0) { + log_sys_err("open"); + return 0; + } + + if (lseek(fd, offset, SEEK_SET) < 0) { + log_sys_err("lseek"); + return 0; + } + + r = _write(fd, buffer, len); + close(fd); + return r; } diff --git a/lib/format1/disk-rep.c b/lib/format1/disk-rep.c index 9c2ed875d..446f76e4a 100644 --- a/lib/format1/disk-rep.c +++ b/lib/format1/disk-rep.c @@ -239,9 +239,9 @@ struct disk_list *read_pv(struct device *dev, struct pool *mem, goto bad; } - if (!(_munge_formats(&data->pv))) { + if (!_munge_formats(&data->pv)) { log_verbose("Unknown metadata version %d found on %s", - data->pv.version, dev->name); + data->pv.version, dev->name); goto bad; } @@ -250,7 +250,7 @@ struct disk_list *read_pv(struct device *dev, struct pool *mem, */ if (data->pv.vg_name == '\0') { log_very_verbose("%s is not a member of any VG", - dev->name); + dev->name); return data; }