1 /* $NetBSD: file.c,v 1.15 2009/10/15 12:36:57 joerg Exp $ */
3 * Copyright (c) 1998-2004 Dag-Erling Coïdan Smørgrav
4 * Copyright (c) 2008, 2009 Joerg Sonnenberger <joerg@NetBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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 $
36 #if !defined(NETBSD) && !defined(__minix)
52 static int fetch_stat_file(int, struct url_stat
*);
55 fetchFile_read(void *cookie
, void *buf
, size_t len
)
57 return read(*(int *)cookie
, buf
, len
);
61 fetchFile_write(void *cookie
, const void *buf
, size_t len
)
63 return write(*(int *)cookie
, buf
, len
);
67 fetchFile_close(void *cookie
)
69 int fd
= *(int *)cookie
;
77 fetchXGetFile(struct url
*u
, struct url_stat
*us
, const char *flags
)
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
)
88 if ((path
= fetchUnquotePath(u
)) == NULL
) {
93 fd
= open(path
, O_RDONLY
);
100 if (us
&& fetch_stat_file(fd
, us
) == -1) {
106 if (if_modified_since
&& u
->last_modified
> 0 &&
107 u
->last_modified
>= us
->mtime
) {
109 fetchLastErrCode
= FETCH_UNCHANGED
;
110 snprintf(fetchLastErrString
, MAXERRSTRING
, "Unchanged");
114 if (u
->offset
&& lseek(fd
, u
->offset
, SEEK_SET
) == -1) {
120 cookie
= malloc(sizeof(int));
121 if (cookie
== NULL
) {
128 f
= fetchIO_unopen(cookie
, fetchFile_read
, fetchFile_write
, fetchFile_close
);
137 fetchGetFile(struct url
*u
, const char *flags
)
139 return (fetchXGetFile(u
, NULL
, flags
));
143 fetchPutFile(struct url
*u
, const char *flags
)
149 if ((path
= fetchUnquotePath(u
)) == NULL
) {
155 fd
= open(path
, O_WRONLY
| O_APPEND
);
157 fd
= open(path
, O_WRONLY
);
166 if (u
->offset
&& lseek(fd
, u
->offset
, SEEK_SET
) == -1) {
172 cookie
= malloc(sizeof(int));
173 if (cookie
== NULL
) {
180 f
= fetchIO_unopen(cookie
, fetchFile_read
, fetchFile_write
, fetchFile_close
);
189 fetch_stat_file(int fd
, struct url_stat
*us
)
194 us
->atime
= us
->mtime
= 0;
195 if (fstat(fd
, &sb
) == -1) {
199 us
->size
= sb
.st_size
;
200 us
->atime
= sb
.st_atime
;
201 us
->mtime
= sb
.st_mtime
;
206 fetchStatFile(struct url
*u
, struct url_stat
*us
, const char *flags
)
211 if ((path
= fetchUnquotePath(u
)) == NULL
) {
216 fd
= open(path
, O_RDONLY
);
224 rv
= fetch_stat_file(fd
, us
);
231 fetchListFile(struct url_list
*ue
, struct url
*u
, const char *pattern
, const char *flags
)
238 if ((path
= fetchUnquotePath(u
)) == NULL
) {
253 while ((de
= readdir(dir
)) != NULL
) {
254 if (pattern
&& fnmatch(pattern
, de
->d_name
, 0) != 0)
256 ret
= fetch_add_entry(ue
, u
, de
->d_name
, 0);