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 * $FreeBSD: head/lib/libfetch/file.c 326219 2017-11-26 02:00:33Z pfg $
31 */
32
33 #include <sys/cdefs.h>
34
35 #include <sys/param.h>
36 #include <sys/stat.h>
37
38 #include <dirent.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <string.h>
42
43 #include "fetch.h"
44 #include "common.h"
45
46 FILE *
fetchXGetFile(struct url * u,struct url_stat * us,const char * flags)47 fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
48 {
49 FILE *f;
50
51 if (us && fetchStatFile(u, us, flags) == -1)
52 return (NULL);
53
54 f = fopen(u->doc, "re");
55
56 if (f == NULL) {
57 fetch_syserr();
58 return (NULL);
59 }
60
61 if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
62 fclose(f);
63 fetch_syserr();
64 return (NULL);
65 }
66
67 return (f);
68 }
69
70 FILE *
fetchGetFile(struct url * u,const char * flags)71 fetchGetFile(struct url *u, const char *flags)
72 {
73 return (fetchXGetFile(u, NULL, flags));
74 }
75
76 FILE *
fetchPutFile(struct url * u,const char * flags)77 fetchPutFile(struct url *u, const char *flags)
78 {
79 FILE *f;
80
81 if (CHECK_FLAG('a'))
82 f = fopen(u->doc, "ae");
83 else
84 f = fopen(u->doc, "w+e");
85
86 if (f == NULL) {
87 fetch_syserr();
88 return (NULL);
89 }
90
91 if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
92 fclose(f);
93 fetch_syserr();
94 return (NULL);
95 }
96
97 return (f);
98 }
99
100 static int
fetch_stat_file(const char * fn,struct url_stat * us)101 fetch_stat_file(const char *fn, struct url_stat *us)
102 {
103 struct stat sb;
104
105 us->size = -1;
106 us->atime = us->mtime = 0;
107 if (stat(fn, &sb) == -1) {
108 fetch_syserr();
109 return (-1);
110 }
111 us->size = sb.st_size;
112 us->atime = sb.st_atime;
113 us->mtime = sb.st_mtime;
114 return (0);
115 }
116
117 int
fetchStatFile(struct url * u,struct url_stat * us,const char * flags __unused)118 fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
119 {
120 return (fetch_stat_file(u->doc, us));
121 }
122
123 struct url_ent *
fetchListFile(struct url * u,const char * flags __unused)124 fetchListFile(struct url *u, const char *flags __unused)
125 {
126 struct dirent *de;
127 struct url_stat us;
128 struct url_ent *ue;
129 int size, len;
130 char fn[PATH_MAX], *p;
131 DIR *dir;
132 int l;
133
134 if ((dir = opendir(u->doc)) == NULL) {
135 fetch_syserr();
136 return (NULL);
137 }
138
139 ue = NULL;
140 strncpy(fn, u->doc, sizeof(fn) - 2);
141 fn[sizeof(fn) - 2] = 0;
142 strcat(fn, "/");
143 p = strchr(fn, 0);
144 l = sizeof(fn) - strlen(fn) - 1;
145
146 while ((de = readdir(dir)) != NULL) {
147 strncpy(p, de->d_name, l - 1);
148 p[l - 1] = 0;
149 if (fetch_stat_file(fn, &us) == -1)
150 /* should I return a partial result, or abort? */
151 break;
152 fetch_add_entry(&ue, &size, &len, de->d_name, &us);
153 }
154
155 closedir(dir);
156 return (ue);
157 }
158