2 * file_io.c - Helper functions for reading and writing to file descriptors.
6 * Copyright (C) 2013 Eric Biggers
8 * This file is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at your option) any
13 * This file is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this file; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib/error.h"
30 #include "wimlib/file_io.h"
31 #include "wimlib/util.h"
32 #include "wimlib/win32.h" /* For pread(), pwrite() replacements */
34 /* Wrapper around read() that checks for errors keeps retrying until all
35 * requested bytes have been read or until end-of file has occurred.
38 * WIMLIB_ERR_SUCCESS (0)
39 * WIMLIB_ERR_READ (errno set)
40 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
43 full_read(struct filedes *fd, void *buf, size_t count)
46 size_t bytes_remaining;
48 for (bytes_remaining = count;
50 bytes_remaining -= bytes_read, buf += bytes_read)
52 bytes_read = read(fd->fd, buf, bytes_remaining);
53 if (unlikely(bytes_read <= 0)) {
54 if (bytes_read == 0) {
56 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
57 } else if (errno == EINTR) {
60 return WIMLIB_ERR_READ;
64 count -= bytes_remaining;
70 pipe_read(struct filedes *fd, void *buf, size_t count, off_t offset)
74 /* Verify the offset. */
75 if (offset < fd->offset) {
76 ERROR("Can't seek backwards in pipe "
77 "(offset %"PRIu64" => %"PRIu64").\n"
78 " Make sure the WIM was captured as "
79 "pipable.", fd->offset, offset);
81 return WIMLIB_ERR_RESOURCE_ORDER;
84 /* Manually seek to the requested position. */
85 while (fd->offset != offset) {
86 size_t bytes_to_read = min(offset - fd->offset, BUFFER_SIZE);
87 u8 dummy[bytes_to_read];
89 ret = full_read(fd, dummy, bytes_to_read);
94 /* Do the actual read. */
95 return full_read(fd, buf, count);
98 /* Wrapper around pread() that checks for errors and keeps retrying until all
99 * requested bytes have been read or until end-of file has occurred. This also
100 * transparently handle reading from pipe files, but the caller needs to be sure
101 * the requested offset is greater than or equal to the current offset, or else
102 * WIMLIB_ERR_RESOURCE_ORDER will be returned.
105 * WIMLIB_ERR_SUCCESS (0)
106 * WIMLIB_ERR_READ (errno set)
107 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
108 * WIMLIB_ERR_RESOURCE_ORDER (errno set to ESPIPE)
111 full_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
114 size_t bytes_remaining;
119 for (bytes_remaining = count;
120 bytes_remaining != 0;
121 bytes_remaining -= bytes_read, buf += bytes_read,
122 offset += bytes_read)
124 bytes_read = pread(fd->fd, buf, bytes_remaining, offset);
125 if (unlikely(bytes_read <= 0)) {
126 if (bytes_read == 0) {
128 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
129 } else if (errno == EINTR) {
131 } else if (errno == ESPIPE) {
135 return WIMLIB_ERR_READ;
142 return pipe_read(fd, buf, count, offset);
145 /* Wrapper around write() that checks for errors and keeps retrying until all
146 * requested bytes have been written.
149 * WIMLIB_ERR_SUCCESS (0)
150 * WIMLIB_ERR_WRITE (errno set)
153 full_write(struct filedes *fd, const void *buf, size_t count)
155 ssize_t bytes_written;
156 size_t bytes_remaining;
158 for (bytes_remaining = count;
159 bytes_remaining != 0;
160 bytes_remaining -= bytes_written, buf += bytes_written)
162 bytes_written = write(fd->fd, buf, bytes_remaining);
163 if (unlikely(bytes_written < 0)) {
166 return WIMLIB_ERR_WRITE;
174 /* Wrapper around pwrite() that checks for errors and keeps retrying until all
175 * requested bytes have been written.
178 * WIMLIB_ERR_SUCCESS (0)
179 * WIMLIB_ERR_WRITE (errno set)
182 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
184 ssize_t bytes_written;
185 size_t bytes_remaining;
187 for (bytes_remaining = count;
188 bytes_remaining != 0;
189 bytes_remaining -= bytes_written, buf += bytes_written,
190 offset += bytes_written)
192 bytes_written = pwrite(fd->fd, buf, bytes_remaining, offset);
193 if (unlikely(bytes_written < 0)) {
196 return WIMLIB_ERR_WRITE;
203 raw_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
205 return pread(fd->fd, buf, count, offset);
209 raw_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
211 return pwrite(fd->fd, buf, count, offset);
214 off_t filedes_seek(struct filedes *fd, off_t offset)
220 if (fd->offset != offset) {
221 if (lseek(fd->fd, offset, SEEK_SET) == -1)
228 bool filedes_is_seekable(struct filedes *fd)
230 return !fd->is_pipe && lseek(fd->fd, 0, SEEK_CUR) != -1;