Browse Source

failtest: capture pread/pwrite

Rusty Russell 15 years ago
parent
commit
6c02fd599f

+ 22 - 6
ccan/failtest/failtest.c

@@ -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 failtest_call *p;
 	struct read_call call;
 	struct read_call call;
 	call.fd = fd;
 	call.fd = fd;
 	call.buf = buf;
 	call.buf = buf;
 	call.count = count;
 	call.count = count;
+	call.off = off;
 	p = add_history(FAILTEST_READ, file, line, &call);
 	p = add_history(FAILTEST_READ, file, line, &call);
 
 
 	/* This is going to change seek offset, so save it. */
 	/* 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->u.read.ret = -1;
 		p->error = EIO;
 		p->error = EIO;
 	} else {
 	} else {
-		p->u.read.ret = read(fd, buf, count);
+		p->u.read.ret = pread(fd, buf, count, off);
 	}
 	}
 	errno = p->error;
 	errno = p->error;
 	return p->u.read.ret;
 	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 failtest_call *p;
 	struct write_call call;
 	struct write_call call;
@@ -507,6 +508,7 @@ ssize_t failtest_write(int fd, const void *buf, size_t count,
 	call.fd = fd;
 	call.fd = fd;
 	call.buf = buf;
 	call.buf = buf;
 	call.count = count;
 	call.count = count;
+	call.off = off;
 	p = add_history(FAILTEST_WRITE, file, line, &call);
 	p = add_history(FAILTEST_WRITE, file, line, &call);
 
 
 	offset = lseek(fd, 0, SEEK_CUR);
 	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;
 	errno = p->error;
 	return p->u.write.ret;
 	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
 static struct lock_info *WARN_UNUSED_RESULT
 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
 add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
 {
 {

+ 2 - 0
ccan/failtest/failtest.h

@@ -76,6 +76,7 @@ struct read_call {
 	int fd;
 	int fd;
 	void *buf;
 	void *buf;
 	size_t count;
 	size_t count;
+	off_t off;
 };
 };
 
 
 struct write_call {
 struct write_call {
@@ -83,6 +84,7 @@ struct write_call {
 	int fd;
 	int fd;
 	const void *buf;
 	const void *buf;
 	size_t count;
 	size_t count;
+	off_t off;
 };
 };
 
 
 struct fcntl_call {
 struct fcntl_call {

+ 8 - 0
ccan/failtest/failtest_override.h

@@ -39,6 +39,14 @@
 #define write(fd, buf, count) \
 #define write(fd, buf, count) \
 	failtest_write((fd), (buf), (count), __FILE__, __LINE__)
 	failtest_write((fd), (buf), (count), __FILE__, __LINE__)
 
 
+#undef pread
+#define pread(fd, buf, count, off)				\
+	failtest_pread((fd), (buf), (count), (off), __FILE__, __LINE__)
+
+#undef pwrite
+#define pwrite(fd, buf, count, off)					\
+	failtest_pwrite((fd), (buf), (count), (off), __FILE__, __LINE__)
+
 #undef close
 #undef close
 #define close(fd) failtest_close(fd)
 #define close(fd) failtest_close(fd)
 
 

+ 4 - 0
ccan/failtest/failtest_proto.h

@@ -15,6 +15,10 @@ ssize_t failtest_read(int fd, void *buf, size_t count,
 		      const char *file, unsigned line);
 		      const char *file, unsigned line);
 ssize_t failtest_write(int fd, const void *buf, size_t count,
 ssize_t failtest_write(int fd, const void *buf, size_t count,
 		       const char *file, unsigned line);
 		       const char *file, unsigned line);
+ssize_t failtest_pread(int fd, void *buf, size_t count, off_t offset,
+		       const char *file, unsigned line);
+ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t offset,
+			const char *file, unsigned line);
 int failtest_close(int fd);
 int failtest_close(int fd);
 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...);
 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...);
 #endif /* CCAN_FAILTEST_PROTO_H */
 #endif /* CCAN_FAILTEST_PROTO_H */