xref: /freebsd/contrib/libarchive/libarchive/archive_read_open_filename.c (revision 2e113ef82465598b8c26e0ca415fbe90677fbd47)
1 /*-
2  * Copyright (c) 2003-2010 Tim Kientzle
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "archive_platform.h"
27 
28 #ifdef HAVE_SYS_IOCTL_H
29 #include <sys/ioctl.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_FCNTL_H
38 #include <fcntl.h>
39 #endif
40 #ifdef HAVE_IO_H
41 #include <io.h>
42 #endif
43 #ifdef HAVE_STDLIB_H
44 #include <stdlib.h>
45 #endif
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #endif
49 #ifdef HAVE_UNISTD_H
50 #include <unistd.h>
51 #endif
52 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
53 #include <sys/disk.h>
54 #elif defined(__NetBSD__) || defined(__OpenBSD__)
55 #include <sys/disklabel.h>
56 #include <sys/dkio.h>
57 #elif defined(__DragonFly__)
58 #include <sys/diskslice.h>
59 #endif
60 
61 #include "archive.h"
62 #include "archive_private.h"
63 #include "archive_string.h"
64 
65 #ifndef O_BINARY
66 #define O_BINARY 0
67 #endif
68 #ifndef O_CLOEXEC
69 #define O_CLOEXEC	0
70 #endif
71 
72 struct read_file_data {
73 	int	 fd;
74 	size_t	 block_size;
75 	void	*buffer;
76 	mode_t	 st_mode;  /* Mode bits for opened file. */
77 	int64_t	 size;
78 	char	 use_lseek;
79 	enum fnt_e { FNT_STDIN, FNT_MBS, FNT_WCS } filename_type;
80 	union {
81 		char	 m[1];/* MBS filename. */
82 		wchar_t	 w[1];/* WCS filename. */
83 	} filename; /* Must be last! */
84 };
85 
86 static int	file_open(struct archive *, void *);
87 static int	file_close(struct archive *, void *);
88 static int file_close2(struct archive *, void *);
89 static int file_switch(struct archive *, void *, void *);
90 static ssize_t	file_read(struct archive *, void *, const void **buff);
91 static int64_t	file_seek(struct archive *, void *, int64_t request, int);
92 static int64_t	file_skip(struct archive *, void *, int64_t request);
93 static int64_t	file_skip_lseek(struct archive *, void *, int64_t request);
94 
95 int
archive_read_open_file(struct archive * a,const char * filename,size_t block_size)96 archive_read_open_file(struct archive *a, const char *filename,
97     size_t block_size)
98 {
99 	return (archive_read_open_filename(a, filename, block_size));
100 }
101 
102 int
archive_read_open_filename(struct archive * a,const char * filename,size_t block_size)103 archive_read_open_filename(struct archive *a, const char *filename,
104     size_t block_size)
105 {
106 	const char *filenames[2];
107 	filenames[0] = filename;
108 	filenames[1] = NULL;
109 	return archive_read_open_filenames(a, filenames, block_size);
110 }
111 
112 int
archive_read_open_filenames(struct archive * a,const char ** filenames,size_t block_size)113 archive_read_open_filenames(struct archive *a, const char **filenames,
114     size_t block_size)
115 {
116 	struct read_file_data *mine;
117 	const char *filename = NULL;
118 	if (filenames)
119 		filename = *(filenames++);
120 
121 	archive_clear_error(a);
122 	do
123 	{
124 		if (filename == NULL)
125 			filename = "";
126 		mine = calloc(1,
127 			sizeof(*mine) + strlen(filename));
128 		if (mine == NULL)
129 			goto no_memory;
130 		strcpy(mine->filename.m, filename);
131 		mine->block_size = block_size;
132 		mine->fd = -1;
133 		mine->buffer = NULL;
134 		mine->st_mode = mine->use_lseek = 0;
135 		if (filename == NULL || filename[0] == '\0') {
136 			mine->filename_type = FNT_STDIN;
137 		} else
138 			mine->filename_type = FNT_MBS;
139 		if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
140 			return (ARCHIVE_FATAL);
141 		if (filenames == NULL)
142 			break;
143 		filename = *(filenames++);
144 	} while (filename != NULL && filename[0] != '\0');
145 	archive_read_set_open_callback(a, file_open);
146 	archive_read_set_read_callback(a, file_read);
147 	archive_read_set_skip_callback(a, file_skip);
148 	archive_read_set_close_callback(a, file_close);
149 	archive_read_set_switch_callback(a, file_switch);
150 	archive_read_set_seek_callback(a, file_seek);
151 
152 	return (archive_read_open1(a));
153 no_memory:
154 	archive_set_error(a, ENOMEM, "No memory");
155 	return (ARCHIVE_FATAL);
156 }
157 
158 /*
159  * This function is an implementation detail of archive_read_open_filename_w,
160  * which is exposed as a separate API on Windows.
161  */
162 #if !defined(_WIN32) || defined(__CYGWIN__)
163 static
164 #endif
165 int
archive_read_open_filenames_w(struct archive * a,const wchar_t ** wfilenames,size_t block_size)166 archive_read_open_filenames_w(struct archive *a, const wchar_t **wfilenames,
167     size_t block_size)
168 {
169 	struct read_file_data *mine;
170 	const wchar_t *wfilename = NULL;
171 	if (wfilenames)
172 		wfilename = *(wfilenames++);
173 
174 	archive_clear_error(a);
175 	do
176 	{
177 		if (wfilename == NULL)
178 			wfilename = L"";
179 		mine = calloc(1,
180 			sizeof(*mine) + wcslen(wfilename) * sizeof(wchar_t));
181 		if (mine == NULL)
182 			goto no_memory;
183 		mine->block_size = block_size;
184 		mine->fd = -1;
185 
186 		if (wfilename == NULL || wfilename[0] == L'\0') {
187 			mine->filename_type = FNT_STDIN;
188 		} else {
189 #if defined(_WIN32) && !defined(__CYGWIN__)
190 			mine->filename_type = FNT_WCS;
191 			wcscpy(mine->filename.w, wfilename);
192 #else
193 			/*
194 			 * POSIX system does not support a wchar_t interface for
195 			 * open() system call, so we have to translate a wchar_t
196 			 * filename to multi-byte one and use it.
197 			 */
198 			struct archive_string fn;
199 
200 			archive_string_init(&fn);
201 			if (archive_string_append_from_wcs(&fn, wfilename,
202 			    wcslen(wfilename)) != 0) {
203 				if (errno == ENOMEM)
204 					archive_set_error(a, errno,
205 					    "Can't allocate memory");
206 				else
207 					archive_set_error(a, EINVAL,
208 					    "Failed to convert a wide-character"
209 					    " filename to a multi-byte filename");
210 				archive_string_free(&fn);
211 				free(mine);
212 				return (ARCHIVE_FATAL);
213 			}
214 			mine->filename_type = FNT_MBS;
215 			strcpy(mine->filename.m, fn.s);
216 			archive_string_free(&fn);
217 #endif
218 		}
219 		if (archive_read_append_callback_data(a, mine) != (ARCHIVE_OK))
220 			return (ARCHIVE_FATAL);
221 		if (wfilenames == NULL)
222 			break;
223 		wfilename = *(wfilenames++);
224 	} while (wfilename != NULL && wfilename[0] != '\0');
225 	archive_read_set_open_callback(a, file_open);
226 	archive_read_set_read_callback(a, file_read);
227 	archive_read_set_skip_callback(a, file_skip);
228 	archive_read_set_close_callback(a, file_close);
229 	archive_read_set_switch_callback(a, file_switch);
230 	archive_read_set_seek_callback(a, file_seek);
231 
232 	return (archive_read_open1(a));
233 no_memory:
234 	archive_set_error(a, ENOMEM, "No memory");
235 	return (ARCHIVE_FATAL);
236 }
237 
238 int
archive_read_open_filename_w(struct archive * a,const wchar_t * wfilename,size_t block_size)239 archive_read_open_filename_w(struct archive *a, const wchar_t *wfilename,
240     size_t block_size)
241 {
242 	const wchar_t *wfilenames[2];
243 	wfilenames[0] = wfilename;
244 	wfilenames[1] = NULL;
245 	return archive_read_open_filenames_w(a, wfilenames, block_size);
246 }
247 
248 static int
file_open(struct archive * a,void * client_data)249 file_open(struct archive *a, void *client_data)
250 {
251 	struct stat st;
252 	struct read_file_data *mine = (struct read_file_data *)client_data;
253 	void *buffer;
254 	const char *filename = NULL;
255 #if defined(_WIN32) && !defined(__CYGWIN__)
256 	const wchar_t *wfilename = NULL;
257 #endif
258 	int fd = -1;
259 	int is_disk_like = 0;
260 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
261 	off_t mediasize = 0; /* FreeBSD-specific, so off_t okay here. */
262 #elif defined(__NetBSD__) || defined(__OpenBSD__)
263 	struct disklabel dl;
264 #elif defined(__DragonFly__)
265 	struct partinfo pi;
266 #endif
267 
268 	archive_clear_error(a);
269 	if (mine->filename_type == FNT_STDIN) {
270 		/* We used to delegate stdin support by
271 		 * directly calling archive_read_open_fd(a,0,block_size)
272 		 * here, but that doesn't (and shouldn't) handle the
273 		 * end-of-file flush when reading stdout from a pipe.
274 		 * Basically, read_open_fd() is intended for folks who
275 		 * are willing to handle such details themselves.  This
276 		 * API is intended to be a little smarter for folks who
277 		 * want easy handling of the common case.
278 		 */
279 		fd = 0;
280 #if defined(__CYGWIN__) || defined(_WIN32)
281 		setmode(0, O_BINARY);
282 #endif
283 		filename = "";
284 	} else if (mine->filename_type == FNT_MBS) {
285 		filename = mine->filename.m;
286 		fd = open(filename, O_RDONLY | O_BINARY | O_CLOEXEC);
287 		__archive_ensure_cloexec_flag(fd);
288 		if (fd < 0) {
289 			archive_set_error(a, errno,
290 			    "Failed to open '%s'", filename);
291 			return (ARCHIVE_FATAL);
292 		}
293 	} else {
294 #if defined(_WIN32) && !defined(__CYGWIN__)
295 		wfilename = mine->filename.w;
296 		fd = _wopen(wfilename, O_RDONLY | O_BINARY);
297 		if (fd < 0 && errno == ENOENT) {
298 			wchar_t *fullpath;
299 			fullpath = __la_win_permissive_name_w(wfilename);
300 			if (fullpath != NULL) {
301 				fd = _wopen(fullpath, O_RDONLY | O_BINARY);
302 				free(fullpath);
303 			}
304 		}
305 		if (fd < 0) {
306 			archive_set_error(a, errno,
307 			    "Failed to open '%ls'", wfilename);
308 			return (ARCHIVE_FATAL);
309 		}
310 #else
311 		archive_set_error(a, ARCHIVE_ERRNO_MISC,
312 		    "Unexpedted operation in archive_read_open_filename");
313 		goto fail;
314 #endif
315 	}
316 	if (fstat(fd, &st) != 0) {
317 #if defined(_WIN32) && !defined(__CYGWIN__)
318 		if (mine->filename_type == FNT_WCS)
319 			archive_set_error(a, errno, "Can't stat '%ls'",
320 			    wfilename);
321 		else
322 #endif
323 			archive_set_error(a, errno, "Can't stat '%s'",
324 			    filename);
325 		goto fail;
326 	}
327 
328 	/*
329 	 * Determine whether the input looks like a disk device or a
330 	 * tape device.  The results are used below to select an I/O
331 	 * strategy:
332 	 *  = "disk-like" devices support arbitrary lseek() and will
333 	 *    support I/O requests of any size.  So we get easy skipping
334 	 *    and can cheat on block sizes to get better performance.
335 	 *  = "tape-like" devices require strict blocking and use
336 	 *    specialized ioctls for seeking.
337 	 *  = "socket-like" devices cannot seek at all but can improve
338 	 *    performance by using nonblocking I/O to read "whatever is
339 	 *    available right now".
340 	 *
341 	 * Right now, we only specially recognize disk-like devices,
342 	 * but it should be straightforward to add probes and strategy
343 	 * here for tape-like and socket-like devices.
344 	 */
345 	if (S_ISREG(st.st_mode)) {
346 		/* Safety:  Tell the extractor not to overwrite the input. */
347 		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
348 		/* Regular files act like disks. */
349 		is_disk_like = 1;
350 	}
351 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
352 	/* FreeBSD: if it supports DIOCGMEDIASIZE ioctl, it's disk-like. */
353 	else if (S_ISCHR(st.st_mode) &&
354 	    ioctl(fd, DIOCGMEDIASIZE, &mediasize) == 0 &&
355 	    mediasize > 0) {
356 		is_disk_like = 1;
357 	}
358 #elif defined(__NetBSD__) || defined(__OpenBSD__)
359 	/* Net/OpenBSD: if it supports DIOCGDINFO ioctl, it's disk-like. */
360 	else if ((S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) &&
361 	    ioctl(fd, DIOCGDINFO, &dl) == 0 &&
362 	    dl.d_partitions[DISKPART(st.st_rdev)].p_size > 0) {
363 		is_disk_like = 1;
364 	}
365 #elif defined(__DragonFly__)
366 	/* DragonFly BSD:  if it supports DIOCGPART ioctl, it's disk-like. */
367 	else if (S_ISCHR(st.st_mode) &&
368 	    ioctl(fd, DIOCGPART, &pi) == 0 &&
369 	    pi.media_size > 0) {
370 		is_disk_like = 1;
371 	}
372 #elif defined(__linux__)
373 	/* Linux:  All block devices are disk-like. */
374 	else if (S_ISBLK(st.st_mode) &&
375 	    lseek(fd, 0, SEEK_CUR) == 0 &&
376 	    lseek(fd, 0, SEEK_SET) == 0 &&
377 	    lseek(fd, 0, SEEK_END) > 0 &&
378 	    lseek(fd, 0, SEEK_SET) == 0) {
379 		is_disk_like = 1;
380 	}
381 #endif
382 	/* TODO: Add an "is_tape_like" variable and appropriate tests. */
383 
384 	/* Disk-like devices prefer power-of-two block sizes.  */
385 	/* Use provided block_size as a guide so users have some control. */
386 	if (is_disk_like) {
387 		size_t new_block_size = 64 * 1024;
388 		while (new_block_size < mine->block_size
389 		    && new_block_size < 64 * 1024 * 1024)
390 			new_block_size *= 2;
391 		mine->block_size = new_block_size;
392 	}
393 	buffer = malloc(mine->block_size);
394 	if (buffer == NULL) {
395 		archive_set_error(a, ENOMEM, "No memory");
396 		goto fail;
397 	}
398 	mine->buffer = buffer;
399 	mine->fd = fd;
400 	/* Remember mode so close can decide whether to flush. */
401 	mine->st_mode = st.st_mode;
402 
403 	/* Disk-like inputs can use lseek(). */
404 	if (is_disk_like) {
405 		mine->use_lseek = 1;
406 		mine->size = st.st_size;
407 	}
408 
409 	return (ARCHIVE_OK);
410 fail:
411 	/*
412 	 * Don't close file descriptors not opened or ones pointing referring
413 	 * to `FNT_STDIN`.
414 	 */
415 	if (fd != -1 && fd != 0)
416 		close(fd);
417 	return (ARCHIVE_FATAL);
418 }
419 
420 static ssize_t
file_read(struct archive * a,void * client_data,const void ** buff)421 file_read(struct archive *a, void *client_data, const void **buff)
422 {
423 	struct read_file_data *mine = (struct read_file_data *)client_data;
424 	ssize_t bytes_read;
425 
426 	/* TODO: If a recent lseek() operation has left us
427 	 * mis-aligned, read and return a short block to try to get
428 	 * us back in alignment. */
429 
430 	/* TODO: Someday, try mmap() here; if that succeeds, give
431 	 * the entire file to libarchive as a single block.  That
432 	 * could be a lot faster than block-by-block manual I/O. */
433 
434 	/* TODO: We might be able to improve performance on pipes and
435 	 * sockets by setting non-blocking I/O and just accepting
436 	 * whatever we get here instead of waiting for a full block
437 	 * worth of data. */
438 
439 	*buff = mine->buffer;
440 	for (;;) {
441 		bytes_read = read(mine->fd, mine->buffer, mine->block_size);
442 		if (bytes_read < 0) {
443 			if (errno == EINTR)
444 				continue;
445 			else if (mine->filename_type == FNT_STDIN)
446 				archive_set_error(a, errno,
447 				    "Error reading stdin");
448 			else if (mine->filename_type == FNT_MBS)
449 				archive_set_error(a, errno,
450 				    "Error reading '%s'", mine->filename.m);
451 			else
452 				archive_set_error(a, errno,
453 				    "Error reading '%ls'", mine->filename.w);
454 		}
455 		return (bytes_read);
456 	}
457 }
458 
459 /*
460  * Regular files and disk-like block devices can use simple lseek
461  * without needing to round the request to the block size.
462  *
463  * TODO: This can leave future reads mis-aligned.  Since we know the
464  * offset here, we should store it and use it in file_read() above
465  * to determine whether we should perform a short read to get back
466  * into alignment.  Long series of mis-aligned reads can negatively
467  * impact disk throughput.  (Of course, the performance impact should
468  * be carefully tested; extra code complexity is only worthwhile if
469  * it does provide measurable improvement.)
470  *
471  * TODO: Be lazy about the actual seek.  There are a few pathological
472  * cases where libarchive makes a bunch of seek requests in a row
473  * without any intervening reads.  This isn't a huge performance
474  * problem, since the kernel handles seeks lazily already, but
475  * it would be very slightly faster if we simply remembered the
476  * seek request here and then actually performed the seek at the
477  * top of the read callback above.
478  */
479 static int64_t
file_skip_lseek(struct archive * a,void * client_data,int64_t request)480 file_skip_lseek(struct archive *a, void *client_data, int64_t request)
481 {
482 	struct read_file_data *mine = (struct read_file_data *)client_data;
483 #if defined(_WIN32) && !defined(__CYGWIN__)
484 	/* We use _lseeki64() on Windows. */
485 	int64_t old_offset, new_offset, skip = request;
486 #else
487 	off_t old_offset, new_offset, skip = (off_t)request;
488 #endif
489 	int skip_bits = sizeof(skip) * 8 - 1;
490 
491 	/* We use off_t here because lseek() is declared that way. */
492 
493 	/* Reduce a request that would overflow the 'skip' variable. */
494 	if (sizeof(request) > sizeof(skip)) {
495 		const int64_t max_skip =
496 		    (((int64_t)1 << (skip_bits - 1)) - 1) * 2 + 1;
497 		if (request > max_skip)
498 			skip = max_skip;
499 	}
500 
501 	if ((old_offset = lseek(mine->fd, 0, SEEK_CUR)) >= 0) {
502 		if (old_offset >= mine->size ||
503 		    skip > mine->size - old_offset) {
504 			/* Do not seek past end of file. */
505 			errno = ESPIPE;
506 		} else if ((new_offset = lseek(mine->fd, skip, SEEK_CUR)) >= 0)
507 			return (new_offset - old_offset);
508 	}
509 
510 	/* If lseek() fails, don't bother trying again. */
511 	mine->use_lseek = 0;
512 
513 	/* Let libarchive recover with read+discard */
514 	if (errno == ESPIPE)
515 		return (0);
516 
517 	/* If the input is corrupted or truncated, fail. */
518 	if (mine->filename_type == FNT_STDIN)
519 		archive_set_error(a, errno, "Error seeking in stdin");
520 	else if (mine->filename_type == FNT_MBS)
521 		archive_set_error(a, errno, "Error seeking in '%s'",
522 		    mine->filename.m);
523 	else
524 		archive_set_error(a, errno, "Error seeking in '%ls'",
525 		    mine->filename.w);
526 	return (-1);
527 }
528 
529 
530 /*
531  * TODO: Implement another file_skip_XXXX that uses MTIO ioctls to
532  * accelerate operation on tape drives.
533  */
534 
535 static int64_t
file_skip(struct archive * a,void * client_data,int64_t request)536 file_skip(struct archive *a, void *client_data, int64_t request)
537 {
538 	struct read_file_data *mine = (struct read_file_data *)client_data;
539 
540 	/* Delegate skip requests. */
541 	if (mine->use_lseek)
542 		return (file_skip_lseek(a, client_data, request));
543 
544 	/* If we can't skip, return 0; libarchive will read+discard instead. */
545 	return (0);
546 }
547 
548 /*
549  * TODO: Store the offset and use it in the read callback.
550  */
551 static int64_t
file_seek(struct archive * a,void * client_data,int64_t request,int whence)552 file_seek(struct archive *a, void *client_data, int64_t request, int whence)
553 {
554 	struct read_file_data *mine = (struct read_file_data *)client_data;
555 	off_t seek = (off_t)request;
556 	int64_t r;
557 	int seek_bits = sizeof(seek) * 8 - 1;
558 
559 	/* We use off_t here because lseek() is declared that way. */
560 
561 	/* Reduce a request that would overflow the 'seek' variable. */
562 	if (sizeof(request) > sizeof(seek)) {
563 		const int64_t max_seek =
564 		    (((int64_t)1 << (seek_bits - 1)) - 1) * 2 + 1;
565 		const int64_t min_seek = ~max_seek;
566 		if (request > max_seek)
567 			seek = (off_t)max_seek;
568 		else if (request < min_seek)
569 			seek = (off_t)min_seek;
570 	}
571 
572 	r = lseek(mine->fd, seek, whence);
573 	if (r >= 0)
574 		return r;
575 
576 	/* If the input is corrupted or truncated, fail. */
577 	if (mine->filename_type == FNT_STDIN)
578 		archive_set_error(a, errno, "Error seeking in stdin");
579 	else if (mine->filename_type == FNT_MBS)
580 		archive_set_error(a, errno, "Error seeking in '%s'",
581 		    mine->filename.m);
582 	else
583 		archive_set_error(a, errno, "Error seeking in '%ls'",
584 		    mine->filename.w);
585 	return (ARCHIVE_FATAL);
586 }
587 
588 static int
file_close2(struct archive * a,void * client_data)589 file_close2(struct archive *a, void *client_data)
590 {
591 	struct read_file_data *mine = (struct read_file_data *)client_data;
592 
593 	(void)a; /* UNUSED */
594 
595 	/* Only flush and close if open succeeded. */
596 	if (mine->fd >= 0) {
597 		/*
598 		 * Sometimes, we should flush the input before closing.
599 		 *   Regular files: faster to just close without flush.
600 		 *   Disk-like devices:  Ditto.
601 		 *   Tapes: must not flush (user might need to
602 		 *      read the "next" item on a non-rewind device).
603 		 *   Pipes and sockets:  must flush (otherwise, the
604 		 *      program feeding the pipe or socket may complain).
605 		 * Here, I flush everything except for regular files and
606 		 * device nodes.
607 		 */
608 		if (!S_ISREG(mine->st_mode)
609 		    && !S_ISCHR(mine->st_mode)
610 		    && !S_ISBLK(mine->st_mode)) {
611 			ssize_t bytesRead;
612 			do {
613 				bytesRead = read(mine->fd, mine->buffer,
614 				    mine->block_size);
615 			} while (bytesRead > 0);
616 		}
617 		/* If a named file was opened, then it needs to be closed. */
618 		if (mine->filename_type != FNT_STDIN)
619 			close(mine->fd);
620 	}
621 	free(mine->buffer);
622 	mine->buffer = NULL;
623 	mine->fd = -1;
624 	return (ARCHIVE_OK);
625 }
626 
627 static int
file_close(struct archive * a,void * client_data)628 file_close(struct archive *a, void *client_data)
629 {
630 	struct read_file_data *mine = (struct read_file_data *)client_data;
631 	file_close2(a, client_data);
632 	free(mine);
633 	return (ARCHIVE_OK);
634 }
635 
636 static int
file_switch(struct archive * a,void * client_data1,void * client_data2)637 file_switch(struct archive *a, void *client_data1, void *client_data2)
638 {
639 	file_close2(a, client_data1);
640 	return file_open(a, client_data2);
641 }
642