2 * file_io.c - Helper functions for reading and writing to file descriptors.
6 * Copyright (C) 2013 Eric Biggers
8 * This file is part of wimlib, a library for working with WIM files.
10 * wimlib is free software; you can redistribute it and/or modify it under the
11 * terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 3 of the License, or (at your option)
15 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17 * A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib/assert.h"
29 #include "wimlib/error.h"
30 #include "wimlib/file_io.h"
31 #include "wimlib/util.h"
33 # include "wimlib/win32.h" /* For pread(), pwrite() replacements */
35 # include <sys/uio.h> /* for writev() and `struct iovec' */
42 /* Wrapper around read() that keeps retrying until all requested bytes have been
43 * read or until end-of file has occurred.
46 * WIMLIB_ERR_SUCCESS (0)
47 * WIMLIB_ERR_READ (errno set)
48 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
51 full_read(struct filedes *fd, void *buf, size_t count)
54 size_t bytes_remaining;
56 for (bytes_remaining = count;
58 bytes_remaining -= bytes_read, buf += bytes_read)
60 bytes_read = read(fd->fd, buf, bytes_remaining);
61 if (unlikely(bytes_read <= 0)) {
62 if (bytes_read == 0) {
64 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
65 } else if (errno == EINTR) {
68 return WIMLIB_ERR_READ;
72 count -= bytes_remaining;
78 pipe_read(struct filedes *fd, void *buf, size_t count, off_t offset)
82 if (offset < fd->offset) {
83 ERROR("Can't seek backwards in pipe "
84 "(offset %"PRIu64" => %"PRIu64").\n"
85 " Make sure the WIM was captured as "
89 return WIMLIB_ERR_RESOURCE_ORDER;
91 while (fd->offset != offset) {
92 size_t bytes_to_read = min(offset - fd->offset, BUFFER_SIZE);
93 u8 dummy[bytes_to_read];
95 ret = full_read(fd, dummy, bytes_to_read);
99 return full_read(fd, buf, count);
102 /* Wrapper around pread() that keep retrying until all requested bytes have been
103 * read or until end-of file has occurred. This also transparently handle
104 * reading from pipe files, but the caller needs to be sure the requested offset
105 * is greater than or equal to the current offset, or else
106 * WIMLIB_ERR_RESOURCE_ORDER will be returned.
109 * WIMLIB_ERR_SUCCESS (0)
110 * WIMLIB_ERR_READ (errno set)
111 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
112 * WIMLIB_ERR_RESOURCE_ORDER (errno set to ESPIPE)
115 full_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
118 size_t bytes_remaining;
123 for (bytes_remaining = count;
124 bytes_remaining != 0;
125 bytes_remaining -= bytes_read, buf += bytes_read,
126 offset += bytes_read)
128 bytes_read = pread(fd->fd, buf, bytes_remaining, offset);
129 if (unlikely(bytes_read <= 0)) {
130 if (bytes_read == 0) {
132 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
133 } else if (errno == EINTR) {
135 } else if (errno == ESPIPE) {
136 wimlib_assert(count == bytes_remaining);
140 return WIMLIB_ERR_READ;
147 return pipe_read(fd, buf, count, offset);
150 /* Wrapper around write() that keeps retrying until all requested bytes have
154 * WIMLIB_ERR_SUCCESS (0)
155 * WIMLIB_ERR_WRITE (errno set)
158 full_write(struct filedes *fd, const void *buf, size_t count)
160 ssize_t bytes_written;
161 size_t bytes_remaining;
163 for (bytes_remaining = count;
164 bytes_remaining != 0;
165 bytes_remaining -= bytes_written, buf += bytes_written)
167 bytes_written = write(fd->fd, buf, bytes_remaining);
168 if (unlikely(bytes_written < 0)) {
171 return WIMLIB_ERR_WRITE;
179 /* Wrapper around pwrite() that keep retrying until all requested bytes have been
183 * WIMLIB_ERR_SUCCESS (0)
184 * WIMLIB_ERR_WRITE (errno set)
187 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
189 ssize_t bytes_written;
190 size_t bytes_remaining;
192 for (bytes_remaining = count;
193 bytes_remaining != 0;
194 bytes_remaining -= bytes_written, buf += bytes_written,
195 offset += bytes_written)
197 bytes_written = pwrite(fd->fd, buf, bytes_remaining, offset);
198 if (unlikely(bytes_written < 0)) {
201 return WIMLIB_ERR_WRITE;
207 /* Wrapper around writev() that keep retrying until all requested bytes have been
211 * WIMLIB_ERR_SUCCESS (0)
212 * WIMLIB_ERR_WRITE (errno set)
215 full_writev(struct filedes *fd, struct iovec *iov, int iovcnt)
217 size_t total_bytes_written = 0;
219 ssize_t bytes_written;
221 bytes_written = writev(fd->fd, iov, iovcnt);
222 if (bytes_written < 0) {
225 return WIMLIB_ERR_WRITE;
227 total_bytes_written += bytes_written;
228 while (bytes_written) {
229 if (bytes_written >= iov[0].iov_len) {
230 bytes_written -= iov[0].iov_len;
234 iov[0].iov_base += bytes_written;
235 iov[0].iov_len -= bytes_written;
240 fd->offset += total_bytes_written;
245 raw_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
247 return pread(fd->fd, buf, count, offset);
251 raw_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
253 return pwrite(fd->fd, buf, count, offset);
256 off_t filedes_seek(struct filedes *fd, off_t offset)
262 if (fd->offset != offset) {
263 if (lseek(fd->fd, offset, SEEK_SET) == -1)
270 bool filedes_is_seekable(struct filedes *fd)
272 return !fd->is_pipe && lseek(fd->fd, 0, SEEK_CUR) != -1;