]> wimlib.net Git - wimlib/blob - src/file_io.c
NTFS-3g apply: Open extracted files by MFT reference
[wimlib] / src / file_io.c
1 /*
2  * file_io.c - Helper functions for reading and writing to file descriptors.
3  */
4
5 /*
6  * Copyright (C) 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
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)
13  * any later version.
14  *
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
18  * details.
19  *
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/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/assert.h"
29 #include "wimlib/error.h"
30 #include "wimlib/file_io.h"
31 #include "wimlib/util.h"
32 #ifdef __WIN32__
33 #  include "wimlib/win32.h" /* For pread(), pwrite() replacements */
34 #else
35 #  include <sys/uio.h> /* for writev() and `struct iovec' */
36 #endif
37
38 #include <errno.h>
39 #include <unistd.h>
40
41
42 /* Wrapper around read() that keeps retrying until all requested bytes have been
43  * read or until end-of file has occurred.
44  *
45  * Return values:
46  *      WIMLIB_ERR_SUCCESS                      (0)
47  *      WIMLIB_ERR_READ                         (errno set)
48  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE       (errno set to 0)
49  */
50 int
51 full_read(struct filedes *fd, void *buf, size_t count)
52 {
53         ssize_t bytes_read;
54         size_t bytes_remaining;
55
56         for (bytes_remaining = count;
57              bytes_remaining != 0;
58              bytes_remaining -= bytes_read, buf += bytes_read)
59         {
60                 bytes_read = read(fd->fd, buf, bytes_remaining);
61                 if (unlikely(bytes_read <= 0)) {
62                         if (bytes_read == 0) {
63                                 errno = 0;
64                                 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
65                         } else if (errno == EINTR) {
66                                 continue;
67                         } else {
68                                 return WIMLIB_ERR_READ;
69                         }
70                 }
71         }
72         count -= bytes_remaining;
73         fd->offset += count;
74         return 0;
75 }
76
77 static int
78 pipe_read(struct filedes *fd, void *buf, size_t count, off_t offset)
79 {
80         int ret;
81
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 "
86                       "pipable.",
87                         fd->offset, offset);
88                 errno = ESPIPE;
89                 return WIMLIB_ERR_RESOURCE_ORDER;
90         }
91         while (fd->offset != offset) {
92                 size_t bytes_to_read = min(offset - fd->offset, BUFFER_SIZE);
93                 u8 dummy[bytes_to_read];
94
95                 ret = full_read(fd, dummy, bytes_to_read);
96                 if (ret)
97                         return ret;
98         }
99         return full_read(fd, buf, count);
100 }
101
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.
107  *
108  * Return values:
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)
113  * */
114 int
115 full_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
116 {
117         ssize_t bytes_read;
118         size_t bytes_remaining;
119
120         if (fd->is_pipe)
121                 goto is_pipe;
122
123         for (bytes_remaining = count;
124              bytes_remaining != 0;
125              bytes_remaining -= bytes_read, buf += bytes_read,
126                 offset += bytes_read)
127         {
128                 bytes_read = pread(fd->fd, buf, bytes_remaining, offset);
129                 if (unlikely(bytes_read <= 0)) {
130                         if (bytes_read == 0) {
131                                 errno = 0;
132                                 return WIMLIB_ERR_UNEXPECTED_END_OF_FILE;
133                         } else if (errno == EINTR) {
134                                 continue;
135                         } else if (errno == ESPIPE) {
136                                 wimlib_assert(count == bytes_remaining);
137                                 fd->is_pipe = 1;
138                                 goto is_pipe;
139                         } else {
140                                 return WIMLIB_ERR_READ;
141                         }
142                 }
143         }
144         return 0;
145
146 is_pipe:
147         return pipe_read(fd, buf, count, offset);
148 }
149
150 /* Wrapper around write() that keeps retrying until all requested bytes have
151  * been written.
152  *
153  * Return values:
154  *      WIMLIB_ERR_SUCCESS                      (0)
155  *      WIMLIB_ERR_WRITE                        (errno set)
156  */
157 int
158 full_write(struct filedes *fd, const void *buf, size_t count)
159 {
160         ssize_t bytes_written;
161         size_t bytes_remaining;
162
163         for (bytes_remaining = count;
164              bytes_remaining != 0;
165              bytes_remaining -= bytes_written, buf += bytes_written)
166         {
167                 bytes_written = write(fd->fd, buf, bytes_remaining);
168                 if (unlikely(bytes_written < 0)) {
169                         if (errno == EINTR)
170                                 continue;
171                         return WIMLIB_ERR_WRITE;
172                 }
173         }
174         fd->offset += count;
175         return 0;
176 }
177
178
179 /* Wrapper around pwrite() that keep retrying until all requested bytes have been
180  * written.
181  *
182  * Return values:
183  *      WIMLIB_ERR_SUCCESS      (0)
184  *      WIMLIB_ERR_WRITE        (errno set)
185  * */
186 int
187 full_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
188 {
189         ssize_t bytes_written;
190         size_t bytes_remaining;
191
192         for (bytes_remaining = count;
193              bytes_remaining != 0;
194              bytes_remaining -= bytes_written, buf += bytes_written,
195                 offset += bytes_written)
196         {
197                 bytes_written = pwrite(fd->fd, buf, bytes_remaining, offset);
198                 if (unlikely(bytes_written < 0)) {
199                         if (errno == EINTR)
200                                 continue;
201                         return WIMLIB_ERR_WRITE;
202                 }
203         }
204         return 0;
205 }
206
207 /* Wrapper around writev() that keep retrying until all requested bytes have been
208  * written.
209  *
210  * Return values:
211  *      WIMLIB_ERR_SUCCESS      (0)
212  *      WIMLIB_ERR_WRITE        (errno set)
213  * */
214 int
215 full_writev(struct filedes *fd, struct iovec *iov, int iovcnt)
216 {
217         size_t total_bytes_written = 0;
218         while (iovcnt > 0) {
219                 ssize_t bytes_written;
220
221                 bytes_written = writev(fd->fd, iov, iovcnt);
222                 if (bytes_written < 0) {
223                         if (errno == EINTR)
224                                 continue;
225                         return WIMLIB_ERR_WRITE;
226                 }
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;
231                                 iov++;
232                                 iovcnt--;
233                         } else {
234                                 iov[0].iov_base += bytes_written;
235                                 iov[0].iov_len -= bytes_written;
236                                 bytes_written = 0;
237                         }
238                 }
239         }
240         fd->offset += total_bytes_written;
241         return 0;
242 }
243
244 ssize_t
245 raw_pread(struct filedes *fd, void *buf, size_t count, off_t offset)
246 {
247         return pread(fd->fd, buf, count, offset);
248 }
249
250 ssize_t
251 raw_pwrite(struct filedes *fd, const void *buf, size_t count, off_t offset)
252 {
253         return pwrite(fd->fd, buf, count, offset);
254 }
255
256 off_t filedes_seek(struct filedes *fd, off_t offset)
257 {
258         if (fd->is_pipe) {
259                 errno = ESPIPE;
260                 return -1;
261         }
262         if (fd->offset != offset) {
263                 if (lseek(fd->fd, offset, SEEK_SET) == -1)
264                         return -1;
265                 fd->offset = offset;
266         }
267         return offset;
268 }
269
270 bool filedes_is_seekable(struct filedes *fd)
271 {
272         return !fd->is_pipe && lseek(fd->fd, 0, SEEK_CUR) != -1;
273 }