From a161ce7b8dce58ad4ca5acd9fec6844d6489e582 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Wed, 12 Jun 2019 18:55:15 +0800 Subject: [PATCH] io: don't truncate, and use pread/pwrite Signed-off-by: Sean Cross --- src/io.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/io.c b/src/io.c index d22fca8..6b3f58a 100644 --- a/src/io.c +++ b/src/io.c @@ -10,19 +10,15 @@ struct pang_io { }; ssize_t pang_write(struct pang_io *io, off_t offset, const void *buf, size_t count) { - if (-1 == lseek(io->fd, offset, SEEK_SET)) - return -1; - return write(io->fd, buf, count); + return pwrite(io->fd, buf, count, offset); } ssize_t pang_read(struct pang_io *io, off_t offset, void *buf, size_t count) { - if (-1 == lseek(io->fd, offset, SEEK_SET)) - return -1; - return read(io->fd, buf, count); + return pread(io->fd, buf, count, offset); } struct pang_io *pang_open(const char *pathname) { - int fd = open(pathname, O_RDWR | O_TRUNC | O_CREAT, 0777); + int fd = open(pathname, O_RDWR | O_CREAT, 0777); if (-1 == fd) return NULL; struct pang_io *io = malloc(sizeof(struct pang_io));