]> git.ozlabs.org Git - ccan/blobdiff - ccan/failtest/failtest.c
failtest: capture pread/pwrite
[ccan] / ccan / failtest / failtest.c
index 2b78b8e3f8bdc655aaa9234ffa1335fe73c6ac8a..a9e1aa49887e0b97b4a4be7c89aeb5c8271e9d24 100644 (file)
@@ -418,17 +418,17 @@ void *failtest_realloc(void *ptr, size_t size, const char *file, unsigned line)
        return p->u.realloc.ret;
 }
 
        return p->u.realloc.ret;
 }
 
-int failtest_open(const char *pathname, int flags,
+int failtest_open(const char *pathname,
                  const char *file, unsigned line, ...)
 {
        struct failtest_call *p;
        struct open_call call;
                  const char *file, unsigned line, ...)
 {
        struct failtest_call *p;
        struct open_call call;
+       va_list ap;
 
        call.pathname = strdup(pathname);
 
        call.pathname = strdup(pathname);
-       call.flags = flags;
-       if (flags & O_CREAT) {
-               va_list ap;
-               va_start(ap, line);
+       va_start(ap, line);
+       call.flags = va_arg(ap, int);
+       if (call.flags & O_CREAT) {
                call.mode = va_arg(ap, mode_t);
                va_end(ap);
        }
                call.mode = va_arg(ap, mode_t);
                va_end(ap);
        }
@@ -441,7 +441,7 @@ int failtest_open(const char *pathname, int flags,
                /* FIXME: Play with error codes? */
                p->error = EACCES;
        } else {
                /* FIXME: Play with error codes? */
                p->error = EACCES;
        } else {
-               p->u.open.ret = open(pathname, flags, call.mode);
+               p->u.open.ret = open(pathname, call.flags, call.mode);
        }
        errno = p->error;
        return p->u.open.ret;
        }
        errno = p->error;
        return p->u.open.ret;
@@ -466,14 +466,15 @@ int failtest_pipe(int pipefd[2], const char *file, unsigned line)
        return p->u.pipe.ret;
 }
 
        return p->u.pipe.ret;
 }
 
-ssize_t failtest_read(int fd, void *buf, size_t count,
-                     const char *file, unsigned line)
+ssize_t failtest_pread(int fd, void *buf, size_t count, off_t off,
+                      const char *file, unsigned line)
 {
        struct failtest_call *p;
        struct read_call call;
        call.fd = fd;
        call.buf = buf;
        call.count = count;
 {
        struct failtest_call *p;
        struct read_call call;
        call.fd = fd;
        call.buf = buf;
        call.count = count;
+       call.off = off;
        p = add_history(FAILTEST_READ, file, line, &call);
 
        /* This is going to change seek offset, so save it. */
        p = add_history(FAILTEST_READ, file, line, &call);
 
        /* This is going to change seek offset, so save it. */
@@ -485,7 +486,7 @@ ssize_t failtest_read(int fd, void *buf, size_t count,
                p->u.read.ret = -1;
                p->error = EIO;
        } else {
                p->u.read.ret = -1;
                p->error = EIO;
        } else {
-               p->u.read.ret = read(fd, buf, count);
+               p->u.read.ret = pread(fd, buf, count, off);
        }
        errno = p->error;
        return p->u.read.ret;
        }
        errno = p->error;
        return p->u.read.ret;
@@ -497,8 +498,8 @@ static struct write_info *new_write(void)
        return &writes[writes_num++];
 }
 
        return &writes[writes_num++];
 }
 
-ssize_t failtest_write(int fd, const void *buf, size_t count,
-                      const char *file, unsigned line)
+ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t off,
+                       const char *file, unsigned line)
 {
        struct failtest_call *p;
        struct write_call call;
 {
        struct failtest_call *p;
        struct write_call call;
@@ -507,6 +508,7 @@ ssize_t failtest_write(int fd, const void *buf, size_t count,
        call.fd = fd;
        call.buf = buf;
        call.count = count;
        call.fd = fd;
        call.buf = buf;
        call.count = count;
+       call.off = off;
        p = add_history(FAILTEST_WRITE, file, line, &call);
 
        offset = lseek(fd, 0, SEEK_CUR);
        p = add_history(FAILTEST_WRITE, file, line, &call);
 
        offset = lseek(fd, 0, SEEK_CUR);
@@ -570,12 +572,26 @@ ssize_t failtest_write(int fd, const void *buf, size_t count,
                                return p->u.write.ret;
                        }
                }
                                return p->u.write.ret;
                        }
                }
-               p->u.write.ret = write(fd, buf, count);
+               p->u.write.ret = pwrite(fd, buf, count, off);
        }
        errno = p->error;
        return p->u.write.ret;
 }
 
        }
        errno = p->error;
        return p->u.write.ret;
 }
 
+ssize_t failtest_read(int fd, void *buf, size_t count,
+                     const char *file, unsigned line)
+{
+       return failtest_pread(fd, buf, count, lseek(fd, 0, SEEK_CUR),
+                             file, line);
+}
+
+ssize_t failtest_write(int fd, const void *buf, size_t count,
+                      const char *file, unsigned line)
+{
+       return failtest_pwrite(fd, buf, count, lseek(fd, 0, SEEK_CUR),
+                              file, line);
+}
+
 static struct lock_info *WARN_UNUSED_RESULT
 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
 {
 static struct lock_info *WARN_UNUSED_RESULT
 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
 {