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