1228753Smm/*-
2228753Smm * Copyright (c) 2003-2007 Tim Kientzle
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1. Redistributions of source code must retain the above copyright
9228753Smm *    notice, this list of conditions and the following disclaimer.
10228753Smm * 2. Redistributions in binary form must reproduce the above copyright
11228753Smm *    notice, this list of conditions and the following disclaimer in the
12228753Smm *    documentation and/or other materials provided with the distribution.
13228753Smm *
14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24228753Smm */
25228753Smm
26228753Smm#include "archive_platform.h"
27229592Smm__FBSDID("$FreeBSD$");
28228753Smm
29228753Smm#ifdef HAVE_SYS_STAT_H
30228753Smm#include <sys/stat.h>
31228753Smm#endif
32228753Smm#ifdef HAVE_ERRNO_H
33228753Smm#include <errno.h>
34228753Smm#endif
35228753Smm#ifdef HAVE_FCNTL_H
36228753Smm#include <fcntl.h>
37228753Smm#endif
38228753Smm#ifdef HAVE_IO_H
39228753Smm#include <io.h>
40228753Smm#endif
41228753Smm#ifdef HAVE_STDLIB_H
42228753Smm#include <stdlib.h>
43228753Smm#endif
44228753Smm#ifdef HAVE_STRING_H
45228753Smm#include <string.h>
46228753Smm#endif
47228753Smm#ifdef HAVE_UNISTD_H
48228753Smm#include <unistd.h>
49228753Smm#endif
50228753Smm
51228753Smm#include "archive.h"
52228753Smm
53228753Smmstruct read_fd_data {
54228753Smm	int	 fd;
55228753Smm	size_t	 block_size;
56228753Smm	char	 can_skip;
57228753Smm	void	*buffer;
58228753Smm};
59228753Smm
60228753Smmstatic int	file_close(struct archive *, void *);
61228753Smmstatic ssize_t	file_read(struct archive *, void *, const void **buff);
62228753Smm#if ARCHIVE_API_VERSION < 2
63228753Smmstatic ssize_t	file_skip(struct archive *, void *, size_t request);
64228753Smm#else
65228753Smmstatic off_t	file_skip(struct archive *, void *, off_t request);
66228753Smm#endif
67228753Smm
68228753Smmint
69228753Smmarchive_read_open_fd(struct archive *a, int fd, size_t block_size)
70228753Smm{
71228753Smm	struct stat st;
72228753Smm	struct read_fd_data *mine;
73228753Smm	void *b;
74228753Smm
75228753Smm	archive_clear_error(a);
76228753Smm	if (fstat(fd, &st) != 0) {
77228753Smm		archive_set_error(a, errno, "Can't stat fd %d", fd);
78228753Smm		return (ARCHIVE_FATAL);
79228753Smm	}
80228753Smm
81228753Smm	mine = (struct read_fd_data *)malloc(sizeof(*mine));
82228753Smm	b = malloc(block_size);
83228753Smm	if (mine == NULL || b == NULL) {
84228753Smm		archive_set_error(a, ENOMEM, "No memory");
85228753Smm		free(mine);
86228753Smm		free(b);
87228753Smm		return (ARCHIVE_FATAL);
88228753Smm	}
89228753Smm	mine->block_size = block_size;
90228753Smm	mine->buffer = b;
91228753Smm	mine->fd = fd;
92228753Smm	/*
93228753Smm	 * Skip support is a performance optimization for anything
94228753Smm	 * that supports lseek().  On FreeBSD, only regular files and
95228753Smm	 * raw disk devices support lseek() and there's no portable
96228753Smm	 * way to determine if a device is a raw disk device, so we
97228753Smm	 * only enable this optimization for regular files.
98228753Smm	 */
99228753Smm	if (S_ISREG(st.st_mode)) {
100228753Smm		archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino);
101228753Smm		mine->can_skip = 1;
102228753Smm	} else
103228753Smm		mine->can_skip = 0;
104228753Smm#if defined(__CYGWIN__) || defined(_WIN32)
105228753Smm	setmode(mine->fd, O_BINARY);
106228753Smm#endif
107228753Smm
108228753Smm	return (archive_read_open2(a, mine,
109228753Smm		NULL, file_read, file_skip, file_close));
110228753Smm}
111228753Smm
112228753Smmstatic ssize_t
113228753Smmfile_read(struct archive *a, void *client_data, const void **buff)
114228753Smm{
115228753Smm	struct read_fd_data *mine = (struct read_fd_data *)client_data;
116228753Smm	ssize_t bytes_read;
117228753Smm
118228753Smm	*buff = mine->buffer;
119228753Smm	for (;;) {
120228753Smm		bytes_read = read(mine->fd, mine->buffer, mine->block_size);
121228753Smm		if (bytes_read < 0) {
122228753Smm			if (errno == EINTR)
123228753Smm				continue;
124228753Smm			archive_set_error(a, errno, "Error reading fd %d", mine->fd);
125228753Smm		}
126228753Smm		return (bytes_read);
127228753Smm	}
128228753Smm}
129228753Smm
130228753Smm#if ARCHIVE_API_VERSION < 2
131228753Smmstatic ssize_t
132228753Smmfile_skip(struct archive *a, void *client_data, size_t request)
133228753Smm#else
134228753Smmstatic off_t
135228753Smmfile_skip(struct archive *a, void *client_data, off_t request)
136228753Smm#endif
137228753Smm{
138228753Smm	struct read_fd_data *mine = (struct read_fd_data *)client_data;
139228753Smm	off_t old_offset, new_offset;
140228753Smm
141228753Smm	if (!mine->can_skip)
142228753Smm		return (0);
143228753Smm
144228753Smm	/* Reduce request to the next smallest multiple of block_size */
145228753Smm	request = (request / mine->block_size) * mine->block_size;
146228753Smm	if (request == 0)
147228753Smm		return (0);
148228753Smm
149228753Smm	/*
150228753Smm	 * Hurray for lazy evaluation: if the first lseek fails, the second
151228753Smm	 * one will not be executed.
152228753Smm	 */
153228753Smm	if (((old_offset = lseek(mine->fd, 0, SEEK_CUR)) < 0) ||
154228753Smm	    ((new_offset = lseek(mine->fd, request, SEEK_CUR)) < 0))
155228753Smm	{
156228753Smm		/* If seek failed once, it will probably fail again. */
157228753Smm		mine->can_skip = 0;
158228753Smm
159228753Smm		if (errno == ESPIPE)
160228753Smm		{
161228753Smm			/*
162228753Smm			 * Failure to lseek() can be caused by the file
163228753Smm			 * descriptor pointing to a pipe, socket or FIFO.
164228753Smm			 * Return 0 here, so the compression layer will use
165228753Smm			 * read()s instead to advance the file descriptor.
166228753Smm			 * It's slower of course, but works as well.
167228753Smm			 */
168228753Smm			return (0);
169228753Smm		}
170228753Smm		/*
171228753Smm		 * There's been an error other than ESPIPE. This is most
172228753Smm		 * likely caused by a programmer error (too large request)
173228753Smm		 * or a corrupted archive file.
174228753Smm		 */
175228753Smm		archive_set_error(a, errno, "Error seeking");
176228753Smm		return (-1);
177228753Smm	}
178228753Smm	return (new_offset - old_offset);
179228753Smm}
180228753Smm
181228753Smmstatic int
182228753Smmfile_close(struct archive *a, void *client_data)
183228753Smm{
184228753Smm	struct read_fd_data *mine = (struct read_fd_data *)client_data;
185228753Smm
186228753Smm	(void)a; /* UNUSED */
187228753Smm	free(mine->buffer);
188228753Smm	free(mine);
189228753Smm	return (ARCHIVE_OK);
190228753Smm}
191