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"
33 # include "wimlib/win32.h" /* For pread(), pwrite() replacements */
36 /* Wrapper around read() that checks for errors keeps retrying until all
37 * requested bytes have been read or until end-of file has occurred.
40 * WIMLIB_ERR_SUCCESS (0)
41 * WIMLIB_ERR_READ (errno set)
42 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
45 full_read(struct filedes *fd, void *buf, size_t count)
48 size_t bytes_remaining;
50 for (bytes_remaining = count;
52 bytes_remaining -= bytes_read, buf += bytes_read)
54 bytes_read = read(fd->fd, buf, bytes_remaining);
55 if (unlikely(bytes_read <= 0)) {
56 if (bytes_read == 0) {
58 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
59 } else if (errno == EINTR) {
62 return WIMLIB_ERR_READ;
66 count -= bytes_remaining;
72 pipe_read(struct filedes *fd, void *buf, size_t count, off_t offset)
76 /* Verify the offset. */
77 if (offset < fd->offset) {
78 ERROR("Can't seek backwards in pipe "
79 "(offset %"PRIu64" => %"PRIu64").\n"
80 " Make sure the WIM was captured as "
81 "pipable.", fd->offset, offset);
83 return WIMLIB_ERR_RESOURCE_ORDER;
86 /* Manually seek to the requested position. */
87 while (fd->offset != offset) {
88 size_t bytes_to_read = min(offset - fd->offset, BUFFER_SIZE);
89 u8 dummy[bytes_to_read];
91 ret = full_read(fd, dummy, bytes_to_read);
96 /* Do the actual read. */
97 return full_read(fd, buf, count);
100 /* Wrapper around pread() that checks for errors and keeps retrying until all
101 * requested bytes have been read or until end-of file has occurred. This also
102 * transparently handle reading from pipe files, but the caller needs to be sure
103 * the requested offset is greater than or equal to the current offset, or else
104 * WIMLIB_ERR_RESOURCE_ORDER will be returned.
107 * WIMLIB_ERR_SUCCESS (0)
108 * WIMLIB_ERR_READ (errno set)
109 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
110 * WIMLIB_ERR_RESOURCE_ORDER (errno set to ESPIPE)
113 full_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
116 size_t bytes_remaining;
121 for (bytes_remaining = count;
122 bytes_remaining != 0;
123 bytes_remaining -= bytes_read, buf += bytes_read,
124 offset += bytes_read)
126 bytes_read = pread(fd->fd, buf, bytes_remaining, offset);
127 if (unlikely(bytes_read <= 0)) {
128 if (bytes_read == 0) {
130 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
131 } else if (errno == EINTR) {
133 } else if (errno == ESPIPE) {
137 return WIMLIB_ERR_READ;
144 return pipe_read(fd, buf, count, offset);
147 /* Wrapper around write() that checks for errors and keeps retrying until all
148 * requested bytes have been written.
151 * WIMLIB_ERR_SUCCESS (0)
152 * WIMLIB_ERR_WRITE (errno set)
155 full_write(struct filedes *fd, const void *buf, size_t count)
157 ssize_t bytes_written;
158 size_t bytes_remaining;
160 for (bytes_remaining = count;
161 bytes_remaining != 0;
162 bytes_remaining -= bytes_written, buf += bytes_written)
164 bytes_written = write(fd->fd, buf, bytes_remaining);
165 if (unlikely(bytes_written < 0)) {
168 return WIMLIB_ERR_WRITE;
176 /* Wrapper around pwrite() that checks for errors and keeps retrying until all
177 * requested bytes have been written.
180 * WIMLIB_ERR_SUCCESS (0)
181 * WIMLIB_ERR_WRITE (errno set)
184 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
186 ssize_t bytes_written;
187 size_t bytes_remaining;
189 for (bytes_remaining = count;
190 bytes_remaining != 0;
191 bytes_remaining -= bytes_written, buf += bytes_written,
192 offset += bytes_written)
194 bytes_written = pwrite(fd->fd, buf, bytes_remaining, offset);
195 if (unlikely(bytes_written < 0)) {
198 return WIMLIB_ERR_WRITE;
205 raw_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
207 return pread(fd->fd, buf, count, offset);
211 raw_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
213 return pwrite(fd->fd, buf, count, offset);
216 off_t filedes_seek(struct filedes *fd, off_t offset)
222 if (fd->offset != offset) {
223 if (lseek(fd->fd, offset, SEEK_SET) == -1)
230 bool filedes_is_seekable(struct filedes *fd)
232 return !fd->is_pipe && lseek(fd->fd, 0, SEEK_CUR) != -1;