2 * Copyright (c) 1998-2011 Dag-Erling Smørgrav
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
28 * $FreeBSD: head/lib/libfetch/file.c 240495 2012-09-14 12:15:13Z eadler $
31 #include <sys/param.h>
43 fetchXGetFile(struct url
*u
, struct url_stat
*us
, const char *flags
)
47 if (us
&& fetchStatFile(u
, us
, flags
) == -1)
50 f
= fopen(u
->doc
, "r");
57 if (u
->offset
&& fseeko(f
, u
->offset
, SEEK_SET
) == -1) {
63 fcntl(fileno(f
), F_SETFD
, FD_CLOEXEC
);
68 fetchGetFile(struct url
*u
, const char *flags
)
70 return (fetchXGetFile(u
, NULL
, flags
));
74 fetchPutFile(struct url
*u
, const char *flags
)
79 f
= fopen(u
->doc
, "a");
81 f
= fopen(u
->doc
, "w+");
88 if (u
->offset
&& fseeko(f
, u
->offset
, SEEK_SET
) == -1) {
94 fcntl(fileno(f
), F_SETFD
, FD_CLOEXEC
);
99 fetch_stat_file(const char *fn
, struct url_stat
*us
)
104 us
->atime
= us
->mtime
= 0;
105 if (stat(fn
, &sb
) == -1) {
109 us
->size
= sb
.st_size
;
110 us
->atime
= sb
.st_atime
;
111 us
->mtime
= sb
.st_mtime
;
116 fetchStatFile(struct url
*u
, struct url_stat
*us
, const char *flags __unused
)
118 return (fetch_stat_file(u
->doc
, us
));
122 fetchListFile(struct url
*u
, const char *flags __unused
)
128 char fn
[PATH_MAX
], *p
;
132 if ((dir
= opendir(u
->doc
)) == NULL
) {
138 strncpy(fn
, u
->doc
, sizeof(fn
) - 2);
139 fn
[sizeof(fn
) - 2] = 0;
142 l
= sizeof(fn
) - strlen(fn
) - 1;
144 while ((de
= readdir(dir
)) != NULL
) {
145 strncpy(p
, de
->d_name
, l
- 1);
147 if (fetch_stat_file(fn
, &us
) == -1)
148 /* should I return a partial result, or abort? */
150 fetch_add_entry(&ue
, &size
, &len
, de
->d_name
, &us
);