xref: /freebsd/lib/libfetch/file.c (revision 63dab8eed99114670445270e26cf7193fe55e0fa)
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 
43 FILE *
44 fetchXGetFile(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, "r");
52 
53 	if (f == NULL)
54 		fetch_syserr();
55 
56 	if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
57 		fclose(f);
58 		fetch_syserr();
59 	}
60 
61 	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
62 	return (f);
63 }
64 
65 FILE *
66 fetchGetFile(struct url *u, const char *flags)
67 {
68 	return (fetchXGetFile(u, NULL, flags));
69 }
70 
71 FILE *
72 fetchPutFile(struct url *u, const char *flags)
73 {
74 	FILE *f;
75 
76 	if (CHECK_FLAG('a'))
77 		f = fopen(u->doc, "a");
78 	else
79 		f = fopen(u->doc, "w+");
80 
81 	if (f == NULL)
82 		fetch_syserr();
83 
84 	if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
85 		fclose(f);
86 		fetch_syserr();
87 	}
88 
89 	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
90 	return (f);
91 }
92 
93 static int
94 fetch_stat_file(const char *fn, struct url_stat *us)
95 {
96 	struct stat sb;
97 
98 	us->size = -1;
99 	us->atime = us->mtime = 0;
100 	if (stat(fn, &sb) == -1) {
101 		fetch_syserr();
102 		return (-1);
103 	}
104 	us->size = sb.st_size;
105 	us->atime = sb.st_atime;
106 	us->mtime = sb.st_mtime;
107 	return (0);
108 }
109 
110 int
111 fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
112 {
113 	return (fetch_stat_file(u->doc, us));
114 }
115 
116 struct url_ent *
117 fetchListFile(struct url *u, const char *flags __unused)
118 {
119 	struct dirent *de;
120 	struct url_stat us;
121 	struct url_ent *ue;
122 	int size, len;
123 	char fn[PATH_MAX], *p;
124 	DIR *dir;
125 	int l;
126 
127 	if ((dir = opendir(u->doc)) == NULL) {
128 		fetch_syserr();
129 		return (NULL);
130 	}
131 
132 	ue = NULL;
133 	strncpy(fn, u->doc, sizeof(fn) - 2);
134 	fn[sizeof(fn) - 2] = 0;
135 	strcat(fn, "/");
136 	p = strchr(fn, 0);
137 	l = sizeof(fn) - strlen(fn) - 1;
138 
139 	while ((de = readdir(dir)) != NULL) {
140 		strncpy(p, de->d_name, l - 1);
141 		p[l - 1] = 0;
142 		if (fetch_stat_file(fn, &us) == -1)
143 			/* should I return a partial result, or abort? */
144 			break;
145 		fetch_add_entry(&ue, &size, &len, de->d_name, &us);
146 	}
147 
148 	return (ue);
149 }
150