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