1/*-
2 * Copyright (c) 1998-2011 Dag-Erling Smørgrav
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 *    in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/param.h>
33#include <sys/stat.h>
34
35#include <dirent.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <string.h>
39
40#include "fetch.h"
41#include "common.h"
42
43FILE *
44fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
45{
46	FILE *f;
47
48	if (us && fetchStatFile(u, us, flags) == -1)
49		return (NULL);
50
51	f = fopen(u->doc, "re");
52
53	if (f == NULL) {
54		fetch_syserr();
55		return (NULL);
56	}
57
58	if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
59		fclose(f);
60		fetch_syserr();
61		return (NULL);
62	}
63
64	return (f);
65}
66
67FILE *
68fetchGetFile(struct url *u, const char *flags)
69{
70	return (fetchXGetFile(u, NULL, flags));
71}
72
73FILE *
74fetchPutFile(struct url *u, const char *flags)
75{
76	FILE *f;
77
78	if (CHECK_FLAG('a'))
79		f = fopen(u->doc, "ae");
80	else
81		f = fopen(u->doc, "w+e");
82
83	if (f == NULL) {
84		fetch_syserr();
85		return (NULL);
86	}
87
88	if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
89		fclose(f);
90		fetch_syserr();
91		return (NULL);
92	}
93
94	return (f);
95}
96
97static int
98fetch_stat_file(const char *fn, struct url_stat *us)
99{
100	struct stat sb;
101
102	us->size = -1;
103	us->atime = us->mtime = 0;
104	if (stat(fn, &sb) == -1) {
105		fetch_syserr();
106		return (-1);
107	}
108	us->size = sb.st_size;
109	us->atime = sb.st_atime;
110	us->mtime = sb.st_mtime;
111	return (0);
112}
113
114int
115fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
116{
117	return (fetch_stat_file(u->doc, us));
118}
119
120struct url_ent *
121fetchListFile(struct url *u, const char *flags __unused)
122{
123	struct dirent *de;
124	struct url_stat us;
125	struct url_ent *ue;
126	int size, len;
127	char fn[PATH_MAX], *p;
128	DIR *dir;
129	int l;
130
131	if ((dir = opendir(u->doc)) == NULL) {
132		fetch_syserr();
133		return (NULL);
134	}
135
136	ue = NULL;
137	strncpy(fn, u->doc, sizeof(fn) - 2);
138	fn[sizeof(fn) - 2] = 0;
139	strcat(fn, "/");
140	p = strchr(fn, 0);
141	l = sizeof(fn) - strlen(fn) - 1;
142
143	while ((de = readdir(dir)) != NULL) {
144		strncpy(p, de->d_name, l - 1);
145		p[l - 1] = 0;
146		if (fetch_stat_file(fn, &us) == -1)
147			/* should I return a partial result, or abort? */
148			break;
149		fetch_add_entry(&ue, &size, &len, de->d_name, &us);
150	}
151
152	closedir(dir);
153	return (ue);
154}
155