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 https://www.gnu.org/licenses/.
29 #include "wimlib/error.h"
30 #include "wimlib/file_io.h"
31 #include "wimlib/util.h"
34 # include "wimlib/win32.h"
35 # define read win32_read
36 # define write win32_write
37 # define pread win32_pread
38 # define pwrite win32_pwrite
42 * Wrapper around read() that checks for errors and keeps retrying until all
43 * requested bytes have been 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 EINVAL)
51 full_read(struct filedes *fd, void *buf, size_t count)
54 ssize_t ret = read(fd->fd, buf, count);
55 if (unlikely(ret <= 0)) {
58 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
62 return WIMLIB_ERR_READ;
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);
101 * Wrapper around pread() that checks for errors and keeps retrying until all
102 * requested bytes have been read or until end-of file has occurred. This also
103 * transparently handle reading from pipe files, but the caller needs to be sure
104 * the requested offset is greater than or equal to the current offset, or else
105 * WIMLIB_ERR_RESOURCE_ORDER will be returned.
108 * WIMLIB_ERR_SUCCESS (0)
109 * WIMLIB_ERR_READ (errno set)
110 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to EINVAL)
111 * WIMLIB_ERR_RESOURCE_ORDER (errno set to ESPIPE)
114 full_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
120 ssize_t ret = pread(fd->fd, buf, count, offset);
121 if (unlikely(ret <= 0)) {
124 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
128 if (errno == ESPIPE) {
132 return WIMLIB_ERR_READ;
141 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)
156 ssize_t ret = write(fd->fd, buf, count);
157 if (unlikely(ret < 0)) {
160 return WIMLIB_ERR_WRITE;
171 * Wrapper around pwrite() that checks for errors and keeps retrying until all
172 * requested bytes have been written.
175 * WIMLIB_ERR_SUCCESS (0)
176 * WIMLIB_ERR_WRITE (errno set)
179 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
182 ssize_t ret = pwrite(fd->fd, buf, count, offset);
183 if (unlikely(ret < 0)) {
186 return WIMLIB_ERR_WRITE;
195 off_t filedes_seek(struct filedes *fd, off_t offset)
201 if (fd->offset != offset) {
202 if (lseek(fd->fd, offset, SEEK_SET) == -1)
209 bool filedes_is_seekable(struct filedes *fd)
211 return !fd->is_pipe && lseek(fd->fd, 0, SEEK_CUR) != -1;