kernel - VM rework part 18 - Cleanup
[dragonfly.git] / lib / libfetch / file.c
blob43a848ebb19d0ef1c034f76e99fbb1f6a0c11e6d
1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998-2011 Dag-Erling Smørgrav
5 * All rights reserved.
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
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.
30 * $FreeBSD: head/lib/libfetch/file.c 326219 2017-11-26 02:00:33Z pfg $
33 #include <sys/param.h>
34 #include <sys/stat.h>
36 #include <dirent.h>
37 #include <fcntl.h>
38 #include <stdio.h>
39 #include <string.h>
41 #include "fetch.h"
42 #include "common.h"
44 FILE *
45 fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
47 FILE *f;
49 if (us && fetchStatFile(u, us, flags) == -1)
50 return (NULL);
52 f = fopen(u->doc, "re");
54 if (f == NULL) {
55 fetch_syserr();
56 return (NULL);
59 if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
60 fclose(f);
61 fetch_syserr();
62 return (NULL);
65 return (f);
68 FILE *
69 fetchGetFile(struct url *u, const char *flags)
71 return (fetchXGetFile(u, NULL, flags));
74 FILE *
75 fetchPutFile(struct url *u, const char *flags)
77 FILE *f;
79 if (CHECK_FLAG('a'))
80 f = fopen(u->doc, "ae");
81 else
82 f = fopen(u->doc, "w+e");
84 if (f == NULL) {
85 fetch_syserr();
86 return (NULL);
89 if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
90 fclose(f);
91 fetch_syserr();
92 return (NULL);
95 return (f);
98 static int
99 fetch_stat_file(const char *fn, struct url_stat *us)
101 struct stat sb;
103 us->size = -1;
104 us->atime = us->mtime = 0;
105 if (stat(fn, &sb) == -1) {
106 fetch_syserr();
107 return (-1);
109 us->size = sb.st_size;
110 us->atime = sb.st_atime;
111 us->mtime = sb.st_mtime;
112 return (0);
116 fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
118 return (fetch_stat_file(u->doc, us));
121 struct url_ent *
122 fetchListFile(struct url *u, const char *flags __unused)
124 struct dirent *de;
125 struct url_stat us;
126 struct url_ent *ue;
127 int size, len;
128 char fn[PATH_MAX], *p;
129 DIR *dir;
130 int l;
132 if ((dir = opendir(u->doc)) == NULL) {
133 fetch_syserr();
134 return (NULL);
137 ue = NULL;
138 strncpy(fn, u->doc, sizeof(fn) - 2);
139 fn[sizeof(fn) - 2] = 0;
140 strcat(fn, "/");
141 p = strchr(fn, 0);
142 l = sizeof(fn) - strlen(fn) - 1;
144 while ((de = readdir(dir)) != NULL) {
145 strncpy(p, de->d_name, l - 1);
146 p[l - 1] = 0;
147 if (fetch_stat_file(fn, &us) == -1)
148 /* should I return a partial result, or abort? */
149 break;
150 fetch_add_entry(&ue, &size, &len, de->d_name, &us);
153 closedir(dir);
154 return (ue);