xref: /freebsd/lib/libfetch/file.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1998-2011 Dag-Erling Smørgrav
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
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, "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 
67 FILE *
68 fetchGetFile(struct url *u, const char *flags)
69 {
70 	return (fetchXGetFile(u, NULL, flags));
71 }
72 
73 FILE *
74 fetchPutFile(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 
97 static int
98 fetch_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 
114 int
115 fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
116 {
117 	return (fetch_stat_file(u->doc, us));
118 }
119 
120 struct url_ent *
121 fetchListFile(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