xref: /dragonfly/lib/libfetch/file.c (revision 75a74ed8)
1 /*-
2  * Copyright (c) 1998-2011 Dag-Erling 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: head/lib/libfetch/file.c 240495 2012-09-14 12:15:13Z eadler $
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 		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 	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
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, "a");
80 	else
81 		f = fopen(u->doc, "w+");
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 	fcntl(fileno(f), F_SETFD, FD_CLOEXEC);
95 	return (f);
96 }
97 
98 static int
99 fetch_stat_file(const char *fn, struct url_stat *us)
100 {
101 	struct stat sb;
102 
103 	us->size = -1;
104 	us->atime = us->mtime = 0;
105 	if (stat(fn, &sb) == -1) {
106 		fetch_syserr();
107 		return (-1);
108 	}
109 	us->size = sb.st_size;
110 	us->atime = sb.st_atime;
111 	us->mtime = sb.st_mtime;
112 	return (0);
113 }
114 
115 int
116 fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
117 {
118 	return (fetch_stat_file(u->doc, us));
119 }
120 
121 struct url_ent *
122 fetchListFile(struct url *u, const char *flags __unused)
123 {
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;
131 
132 	if ((dir = opendir(u->doc)) == NULL) {
133 		fetch_syserr();
134 		return (NULL);
135 	}
136 
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;
143 
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);
151 	}
152 
153 	return (ue);
154 }
155