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