file.c revision 1.1 1 1.1 agc /*-
2 1.1 agc * Copyright (c) 1998-2004 Dag-Erling Codan Smrgrav
3 1.1 agc * All rights reserved.
4 1.1 agc *
5 1.1 agc * Redistribution and use in source and binary forms, with or without
6 1.1 agc * modification, are permitted provided that the following conditions
7 1.1 agc * are met:
8 1.1 agc * 1. Redistributions of source code must retain the above copyright
9 1.1 agc * notice, this list of conditions and the following disclaimer
10 1.1 agc * in this position and unchanged.
11 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 agc * notice, this list of conditions and the following disclaimer in the
13 1.1 agc * documentation and/or other materials provided with the distribution.
14 1.1 agc * 3. The name of the author may not be used to endorse or promote products
15 1.1 agc * derived from this software without specific prior written permission
16 1.1 agc *
17 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 agc * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 agc * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 agc * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 agc * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 1.1 agc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 1.1 agc * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 1.1 agc * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 1.1 agc * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 1.1 agc * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 agc */
28 1.1 agc
29 1.1 agc #include "free2net.h"
30 1.1 agc
31 1.1 agc #include <sys/cdefs.h>
32 1.1 agc __FBSDID("$FreeBSD: src/lib/libfetch/file.c,v 1.16.6.1 2006/11/11 00:16:07 des Exp $");
33 1.1 agc
34 1.1 agc #include <sys/param.h>
35 1.1 agc #include <sys/stat.h>
36 1.1 agc
37 1.1 agc #include <dirent.h>
38 1.1 agc #include <stdio.h>
39 1.1 agc #include <string.h>
40 1.1 agc
41 1.1 agc #include "fetch.h"
42 1.1 agc #include "common.h"
43 1.1 agc
44 1.1 agc FILE *
45 1.1 agc fetchXGetFile(struct url *u, struct url_stat *us, const char *flags)
46 1.1 agc {
47 1.1 agc FILE *f;
48 1.1 agc
49 1.1 agc if (us && fetchStatFile(u, us, flags) == -1)
50 1.1 agc return (NULL);
51 1.1 agc
52 1.1 agc f = fopen(u->doc, "r");
53 1.1 agc
54 1.1 agc if (f == NULL)
55 1.1 agc _fetch_syserr();
56 1.1 agc
57 1.1 agc if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
58 1.1 agc fclose(f);
59 1.1 agc _fetch_syserr();
60 1.1 agc }
61 1.1 agc
62 1.1 agc return (f);
63 1.1 agc }
64 1.1 agc
65 1.1 agc FILE *
66 1.1 agc fetchGetFile(struct url *u, const char *flags)
67 1.1 agc {
68 1.1 agc return (fetchXGetFile(u, NULL, flags));
69 1.1 agc }
70 1.1 agc
71 1.1 agc FILE *
72 1.1 agc fetchPutFile(struct url *u, const char *flags)
73 1.1 agc {
74 1.1 agc FILE *f;
75 1.1 agc
76 1.1 agc if (CHECK_FLAG('a'))
77 1.1 agc f = fopen(u->doc, "a");
78 1.1 agc else
79 1.1 agc f = fopen(u->doc, "w+");
80 1.1 agc
81 1.1 agc if (f == NULL)
82 1.1 agc _fetch_syserr();
83 1.1 agc
84 1.1 agc if (u->offset && fseeko(f, u->offset, SEEK_SET) == -1) {
85 1.1 agc fclose(f);
86 1.1 agc _fetch_syserr();
87 1.1 agc }
88 1.1 agc
89 1.1 agc return (f);
90 1.1 agc }
91 1.1 agc
92 1.1 agc static int
93 1.1 agc _fetch_stat_file(const char *fn, struct url_stat *us)
94 1.1 agc {
95 1.1 agc struct stat sb;
96 1.1 agc
97 1.1 agc us->size = -1;
98 1.1 agc us->atime = us->mtime = 0;
99 1.1 agc if (stat(fn, &sb) == -1) {
100 1.1 agc _fetch_syserr();
101 1.1 agc return (-1);
102 1.1 agc }
103 1.1 agc us->size = sb.st_size;
104 1.1 agc us->atime = sb.st_atime;
105 1.1 agc us->mtime = sb.st_mtime;
106 1.1 agc return (0);
107 1.1 agc }
108 1.1 agc
109 1.1 agc /* ARGSUSED2 */
110 1.1 agc int
111 1.1 agc fetchStatFile(struct url *u, struct url_stat *us, const char *flags __unused)
112 1.1 agc {
113 1.1 agc return (_fetch_stat_file(u->doc, us));
114 1.1 agc }
115 1.1 agc
116 1.1 agc /* ARGSUSED1 */
117 1.1 agc struct url_ent *
118 1.1 agc fetchListFile(struct url *u, const char *flags __unused)
119 1.1 agc {
120 1.1 agc struct dirent *de;
121 1.1 agc struct url_stat us;
122 1.1 agc struct url_ent *ue;
123 1.1 agc int size, len;
124 1.1 agc char fn[PATH_MAX], *p;
125 1.1 agc DIR *dir;
126 1.1 agc int l;
127 1.1 agc
128 1.1 agc if ((dir = opendir(u->doc)) == NULL) {
129 1.1 agc _fetch_syserr();
130 1.1 agc return (NULL);
131 1.1 agc }
132 1.1 agc
133 1.1 agc ue = NULL;
134 1.1 agc strncpy(fn, u->doc, sizeof(fn) - 2);
135 1.1 agc fn[sizeof(fn) - 2] = 0;
136 1.1 agc strcat(fn, "/");
137 1.1 agc p = strchr(fn, 0);
138 1.1 agc l = sizeof(fn) - strlen(fn) - 1;
139 1.1 agc
140 1.1 agc while ((de = readdir(dir)) != NULL) {
141 1.1 agc strncpy(p, de->d_name, (unsigned)(l - 1));
142 1.1 agc p[l - 1] = 0;
143 1.1 agc if (_fetch_stat_file(fn, &us) == -1)
144 1.1 agc /* should I return a partial result, or abort? */
145 1.1 agc break;
146 1.1 agc _fetch_add_entry(&ue, &size, &len, de->d_name, &us);
147 1.1 agc }
148 1.1 agc
149 1.1 agc return (ue);
150 1.1 agc }
151