kernel debug: priv can be NULL early on
[minix.git] / lib / libfetch / file.c
blob7b2c2233b379a47ed3f1625145d9aa114ca00a45
1 /* $NetBSD: file.c,v 1.15 2009/10/15 12:36:57 joerg Exp $ */
2 /*-
3 * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
4 * Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg@NetBSD.org>
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: file.c,v 1.18 2007/12/14 10:26:58 des Exp $
33 #if HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36 #if !defined(NETBSD) && !defined(__minix)
37 #include <nbcompat.h>
38 #endif
40 #include <sys/stat.h>
42 #include <dirent.h>
43 #include <fcntl.h>
44 #include <fnmatch.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
50 #include "common.h"
52 static int fetch_stat_file(int, struct url_stat *);
54 static ssize_t
55 fetchFile_read(void *cookie, void *buf, size_t len)
57 return read(*(int *)cookie, buf, len);
60 static ssize_t
61 fetchFile_write(void *cookie, const void *buf, size_t len)
63 return write(*(int *)cookie, buf, len);
66 static void
67 fetchFile_close(void *cookie)
69 int fd = *(int *)cookie;
71 free(cookie);
73 close(fd);
76 fetchIO *
77 fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
79 char *path;
80 fetchIO *f;
81 struct url_stat local_us;
82 int if_modified_since, fd, *cookie;
84 if_modified_since = CHECK_FLAG('i');
85 if (if_modified_since && us == NULL)
86 us = &local_us;
88 if ((path = fetchUnquotePath(u)) == NULL) {
89 fetch_syserr();
90 return NULL;
93 fd = open(path, O_RDONLY);
94 free(path);
95 if (fd == -1) {
96 fetch_syserr();
97 return NULL;
100 if (us && fetch_stat_file(fd, us) == -1) {
101 close(fd);
102 fetch_syserr();
103 return NULL;
106 if (if_modified_since && u->last_modified > 0 &&
107 u->last_modified >= us->mtime) {
108 close(fd);
109 fetchLastErrCode = FETCH_UNCHANGED;
110 snprintf(fetchLastErrString, MAXERRSTRING, "Unchanged");
111 return NULL;
114 if (u->offset && lseek(fd, u->offset, SEEK_SET) == -1) {
115 close(fd);
116 fetch_syserr();
117 return NULL;
120 cookie = malloc(sizeof(int));
121 if (cookie == NULL) {
122 close(fd);
123 fetch_syserr();
124 return NULL;
127 *cookie = fd;
128 f = fetchIO_unopen(cookie, fetchFile_read, fetchFile_write, fetchFile_close);
129 if (f == NULL) {
130 close(fd);
131 free(cookie);
133 return f;
136 fetchIO *
137 fetchGetFile(struct url *u, const char *flags)
139 return (fetchXGetFile(u, NULL, flags));
142 fetchIO *
143 fetchPutFile(struct url *u, const char *flags)
145 char *path;
146 fetchIO *f;
147 int fd, *cookie;
149 if ((path = fetchUnquotePath(u)) == NULL) {
150 fetch_syserr();
151 return NULL;
154 if (CHECK_FLAG('a'))
155 fd = open(path, O_WRONLY | O_APPEND);
156 else
157 fd = open(path, O_WRONLY);
159 free(path);
161 if (fd == -1) {
162 fetch_syserr();
163 return NULL;
166 if (u->offset && lseek(fd, u->offset, SEEK_SET) == -1) {
167 close(fd);
168 fetch_syserr();
169 return NULL;
172 cookie = malloc(sizeof(int));
173 if (cookie == NULL) {
174 close(fd);
175 fetch_syserr();
176 return NULL;
179 *cookie = fd;
180 f = fetchIO_unopen(cookie, fetchFile_read, fetchFile_write, fetchFile_close);
181 if (f == NULL) {
182 close(fd);
183 free(cookie);
185 return f;
188 static int
189 fetch_stat_file(int fd, struct url_stat *us)
191 struct stat sb;
193 us->size = -1;
194 us->atime = us->mtime = 0;
195 if (fstat(fd, &sb) == -1) {
196 fetch_syserr();
197 return (-1);
199 us->size = sb.st_size;
200 us->atime = sb.st_atime;
201 us->mtime = sb.st_mtime;
202 return (0);
206 fetchStatFile(struct url *u, struct url_stat *us, const char *flags)
208 char *path;
209 int fd, rv;
211 if ((path = fetchUnquotePath(u)) == NULL) {
212 fetch_syserr();
213 return -1;
216 fd = open(path, O_RDONLY);
217 free(path);
219 if (fd == -1) {
220 fetch_syserr();
221 return -1;
224 rv = fetch_stat_file(fd, us);
225 close(fd);
227 return rv;
231 fetchListFile(struct url_list *ue, struct url *u, const char *pattern, const char *flags)
233 char *path;
234 struct dirent *de;
235 DIR *dir;
236 int ret;
238 if ((path = fetchUnquotePath(u)) == NULL) {
239 fetch_syserr();
240 return -1;
243 dir = opendir(path);
244 free(path);
246 if (dir == NULL) {
247 fetch_syserr();
248 return -1;
251 ret = 0;
253 while ((de = readdir(dir)) != NULL) {
254 if (pattern && fnmatch(pattern, de->d_name, 0) != 0)
255 continue;
256 ret = fetch_add_entry(ue, u, de->d_name, 0);
257 if (ret)
258 break;
261 closedir(dir);
263 return ret;