+
+static void restore_contents(struct failtest_call *opener,
+ struct contents_saved *s,
+ bool restore_offset,
+ const char *caller)
+{
+ int fd;
+
+ /* The top parent doesn't need to restore. */
+ if (control_fd == -1)
+ return;
+
+ /* Has the fd been closed? */
+ if (opener->u.open.closed) {
+ /* Reopen, replace fd, close silently as we clean up. */
+ fd = open(opener->u.open.pathname, O_RDWR);
+ if (fd < 0) {
+ fwarn("failtest: could not reopen %s to clean up %s!",
+ opener->u.open.pathname, caller);
+ return;
+ }
+ /* Make it clearly distinguisable from a "normal" fd. */
+ fd = move_fd_to_high(fd);
+ trace("Reopening %s to restore it (was fd %i, now %i)\n",
+ opener->u.open.pathname, opener->u.open.ret, fd);
+ opener->u.open.ret = fd;
+ opener->u.open.closed = false;
+ }
+ fd = opener->u.open.ret;
+
+ trace("Restoring %p %s %zu@%llu after %s (filelength %llu) via fd %i\n",
+ s, opener->u.open.pathname, s->count, (long long)s->off, caller,
+ (long long)s->old_len, fd);
+ if (pwrite(fd, s->contents, s->count, s->off) != s->count) {
+ fwarn("failtest: write failed cleaning up %s for %s!",
+ opener->u.open.pathname, caller);
+ }
+
+ if (ftruncate(fd, s->old_len) != 0) {
+ fwarn("failtest_write: truncate failed cleaning up %s for %s!",
+ opener->u.open.pathname, caller);
+ }
+
+ if (restore_offset) {
+ trace("Restoring offset of fd %i to %llu\n",
+ fd, (long long)s->off);
+ lseek(fd, s->off, SEEK_SET);
+ }
+}
+
+/* We save/restore most things on demand, but always do mmaped files. */
+static void save_mmapped_files(void)