1
|
1 /*****************************************************************************/
|
|
2 /* cache.c - contains the cache routines */
|
|
3 /* Copyright (C) 1998-2002 Brian Masney <masneyb@gftp.org> */
|
|
4 /* */
|
|
5 /* This program is free software; you can redistribute it and/or modify */
|
|
6 /* it under the terms of the GNU General Public License as published by */
|
|
7 /* the Free Software Foundation; either version 2 of the License, or */
|
|
8 /* (at your option) any later version. */
|
|
9 /* */
|
|
10 /* This program is distributed in the hope that it will be useful, */
|
|
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
|
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
|
13 /* GNU General Public License for more details. */
|
|
14 /* */
|
|
15 /* You should have received a copy of the GNU General Public License */
|
|
16 /* along with this program; if not, write to the Free Software */
|
|
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
|
|
18 /*****************************************************************************/
|
|
19
|
|
20 #include "gftp.h"
|
|
21
|
|
22
|
|
23 char *
|
|
24 gftp_cache_get_url_prefix (gftp_request * request)
|
|
25 {
|
|
26 if (strcmp (request->protocol_name, "HTTP") == 0 &&
|
|
27 strcmp (request->proxy_config, "ftp") == 0)
|
|
28 return ("ftp");
|
|
29
|
|
30 return (request->url_prefix);
|
|
31 }
|
|
32
|
|
33
|
|
34 FILE *
|
|
35 gftp_new_cache_entry (gftp_request * request)
|
|
36 {
|
|
37 char *cachedir, tempstr[BUFSIZ];
|
|
38 int cache_fd;
|
|
39 FILE *fd;
|
|
40
|
|
41 if ((fd = gftp_find_cache_entry (request)) != NULL)
|
|
42 return (fd);
|
|
43
|
|
44 cachedir = expand_path (BASE_CONF_DIR "/cache");
|
|
45 if (access (cachedir, F_OK) == -1)
|
|
46 {
|
|
47 if (mkdir (cachedir, 0x1C0) < 0)
|
|
48 return (NULL);
|
|
49 }
|
|
50
|
|
51 g_snprintf (tempstr, sizeof (tempstr), "%s/index.db", cachedir);
|
|
52 if ((fd = fopen (tempstr, "ab+")) == NULL)
|
|
53 {
|
|
54 g_free (cachedir);
|
|
55 return (NULL);
|
|
56 }
|
|
57
|
|
58 g_snprintf (tempstr, sizeof (tempstr), "%s/cache.XXXXXX", cachedir);
|
|
59 if ((cache_fd = mkstemp (tempstr)) < 0)
|
|
60 return (NULL);
|
|
61 g_free (cachedir);
|
|
62
|
|
63 fseek (fd, 0, SEEK_END);
|
|
64 fprintf (fd, "%s://%s@%s:%d%s\t%s\n",
|
|
65 gftp_cache_get_url_prefix (request),
|
|
66 request->username == NULL ? "" : request->username,
|
|
67 request->hostname == NULL ? "" : request->hostname,
|
|
68 request->port,
|
|
69 request->directory == NULL ? "" : request->directory,
|
|
70 tempstr);
|
|
71
|
|
72 if (fclose (fd) != 0)
|
|
73 {
|
|
74 close (cache_fd);
|
|
75 return (NULL);
|
|
76 }
|
|
77
|
|
78 if ((fd = fdopen (cache_fd, "wb+")) == NULL)
|
|
79 {
|
|
80 close (cache_fd);
|
|
81 return (NULL);
|
|
82 }
|
|
83
|
|
84 return (fd);
|
|
85 }
|
|
86
|
|
87
|
|
88 FILE *
|
|
89 gftp_find_cache_entry (gftp_request * request)
|
|
90 {
|
|
91 char *indexfile, *pos, buf[BUFSIZ], description[BUFSIZ];
|
|
92 FILE *indexfd, *cachefd;
|
|
93 size_t len;
|
|
94
|
|
95 g_snprintf (description, sizeof (description), "%s://%s@%s:%d%s",
|
|
96 gftp_cache_get_url_prefix (request),
|
|
97 request->username == NULL ? "" : request->username,
|
|
98 request->hostname == NULL ? "" : request->hostname,
|
|
99 request->port,
|
|
100 request->directory == NULL ? "" : request->directory);
|
|
101
|
|
102 indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
|
|
103 if ((indexfd = fopen (indexfile, "rb")) == NULL)
|
|
104 {
|
|
105 g_free (indexfile);
|
|
106 return (NULL);
|
|
107 }
|
|
108 g_free (indexfile);
|
|
109
|
|
110 while (fgets (buf, sizeof (buf), indexfd))
|
|
111 {
|
|
112 len = strlen (buf);
|
|
113 if (buf[len - 1] == '\n')
|
|
114 buf[--len] = '\0';
|
|
115 if (buf[len - 1] == '\r')
|
|
116 buf[--len] = '\0';
|
|
117
|
|
118 if (!((pos = strrchr (buf, '\t')) != NULL && *(pos + 1) != '\0'))
|
|
119 continue;
|
|
120
|
|
121 len = strlen (description);
|
|
122 if (pos - buf != len)
|
|
123 continue;
|
|
124
|
|
125 if (strncmp (buf, description, len) == 0)
|
|
126 {
|
|
127 pos++;
|
|
128 if (fclose (indexfd) != 0)
|
|
129 return (NULL);
|
|
130
|
|
131 if ((cachefd = fopen (pos, "rb+")) == NULL)
|
|
132 return (NULL);
|
|
133
|
|
134 fseek (cachefd, 0, SEEK_END);
|
|
135 if (ftell (cachefd) == 0)
|
|
136 {
|
|
137 fclose (cachefd);
|
|
138 return (NULL);
|
|
139 }
|
|
140 fseek (cachefd, 0, SEEK_SET);
|
|
141 return (cachefd);
|
|
142 }
|
|
143 }
|
|
144 fclose (indexfd);
|
|
145 return (NULL);
|
|
146 }
|
|
147
|
|
148
|
|
149 void
|
|
150 gftp_clear_cache_files (void)
|
|
151 {
|
|
152 char *indexfile, buf[BUFSIZ], *pos;
|
|
153 FILE *indexfd;
|
|
154 size_t len;
|
|
155
|
|
156 indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
|
|
157 if ((indexfd = fopen (indexfile, "rb")) == NULL)
|
|
158 {
|
|
159 g_free (indexfile);
|
|
160 return;
|
|
161 }
|
|
162
|
|
163 while (fgets (buf, sizeof (buf), indexfd))
|
|
164 {
|
|
165 len = strlen (buf);
|
|
166 if (buf[len - 1] == '\n')
|
|
167 buf[--len] = '\0';
|
|
168 if (buf[len - 1] == '\r')
|
|
169 buf[--len] = '\0';
|
|
170
|
|
171 if (!((pos = strrchr (buf, '\t')) != NULL && *(pos + 1) != '\0'))
|
|
172 {
|
|
173 printf (_("Error: Invalid line %s in cache index file\n"), buf);
|
|
174 continue;
|
|
175 }
|
|
176 unlink (pos + 1);
|
|
177 }
|
|
178
|
|
179 fclose (indexfd);
|
|
180 unlink (indexfile);
|
|
181 g_free (indexfile);
|
|
182 }
|
|
183
|
|
184
|
|
185 void
|
|
186 gftp_delete_cache_entry (gftp_request * request)
|
|
187 {
|
|
188 char *oldindexfile, *newindexfile, *pos, buf[BUFSIZ], description[BUFSIZ];
|
|
189 FILE *indexfd, *newfd;
|
|
190 size_t len, buflen;
|
|
191
|
|
192 g_snprintf (description, sizeof (description), "%s://%s@%s:%d%s",
|
|
193 gftp_cache_get_url_prefix (request),
|
|
194 request->username == NULL ? "" : request->username,
|
|
195 request->hostname == NULL ? "" : request->hostname,
|
|
196 request->port,
|
|
197 request->directory == NULL ? "" : request->directory);
|
|
198
|
|
199 oldindexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
|
|
200 if ((indexfd = fopen (oldindexfile, "rb")) == NULL)
|
|
201 {
|
|
202 g_free (oldindexfile);
|
|
203 return;
|
|
204 }
|
|
205
|
|
206 newindexfile = expand_path (BASE_CONF_DIR "/cache/index.db.new");
|
|
207 if ((newfd = fopen (newindexfile, "wb")) == NULL)
|
|
208 {
|
|
209 g_free (oldindexfile);
|
|
210 g_free (newindexfile);
|
|
211 return;
|
|
212 }
|
|
213
|
|
214 buflen = strlen (description);
|
|
215 while (fgets (buf, sizeof (buf) - 1, indexfd))
|
|
216 {
|
|
217 len = strlen (buf);
|
|
218 if (buf[len - 1] == '\n')
|
|
219 buf[--len] = '\0';
|
|
220 if (buf[len - 1] == '\r')
|
|
221 buf[--len] = '\0';
|
|
222
|
|
223 if (!((pos = strrchr (buf, '\t')) != NULL && *(pos + 1) != '\0'))
|
|
224 {
|
|
225 printf (_("Error: Invalid line %s in cache index file\n"), buf);
|
|
226 continue;
|
|
227 }
|
|
228
|
|
229 if (buflen == pos - buf && strncmp (buf, description, pos - buf) == 0)
|
|
230 unlink (pos + 1);
|
|
231 else
|
|
232 {
|
|
233 buf[strlen (buf)] = '\n';
|
|
234 fwrite (buf, 1, strlen (buf), newfd);
|
|
235 }
|
|
236 }
|
|
237
|
|
238 fclose (indexfd);
|
|
239 fclose (newfd);
|
|
240
|
|
241 unlink (oldindexfile);
|
|
242 rename (newindexfile, oldindexfile);
|
|
243
|
|
244 g_free (oldindexfile);
|
|
245 g_free (newindexfile);
|
|
246 }
|
|
247
|