1
|
1 /*****************************************************************************/
|
|
2 /* cache.c - contains the cache routines */
|
122
|
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
|
1
|
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"
|
33
|
21 static const char cvsid[] = "$Id$";
|
1
|
22
|
114
|
23 struct gftp_cache_entry_tag
|
|
24 {
|
|
25 char *url,
|
|
26 *file;
|
|
27 int server_type;
|
222
|
28 time_t expiration_date;
|
114
|
29
|
|
30 char *pos1,
|
222
|
31 *pos2,
|
|
32 *pos3;
|
114
|
33 };
|
|
34
|
|
35 typedef struct gftp_cache_entry_tag gftp_cache_entry;
|
|
36
|
|
37
|
|
38 static int
|
|
39 gftp_parse_cache_line (gftp_request * request, gftp_cache_entry * centry,
|
|
40 char *line)
|
|
41 {
|
|
42 char *pos;
|
|
43
|
|
44 memset (centry, 0, sizeof (*centry));
|
|
45
|
|
46 if ((pos = strchr (line, '\t')) == NULL || *(pos + 1) == '\0')
|
|
47 {
|
|
48 if (request != NULL)
|
186
|
49 request->logging_function (gftp_logging_error, request,
|
114
|
50 _("Error: Invalid line %s in cache index file\n"),
|
|
51 line);
|
|
52 return (-1);
|
|
53 }
|
|
54
|
|
55 centry->pos1 = pos;
|
|
56 *pos++ = '\0';
|
|
57 centry->url = line;
|
|
58 centry->file = pos;
|
|
59
|
|
60 if ((pos = strchr (pos, '\t')) == NULL || *(pos + 1) == '\0')
|
|
61 {
|
|
62 if (request != NULL)
|
186
|
63 request->logging_function (gftp_logging_error, request,
|
114
|
64 _("Error: Invalid line %s in cache index file\n"),
|
|
65 line);
|
|
66 return (-1);
|
|
67 }
|
|
68
|
|
69 centry->pos2 = pos;
|
|
70 *pos++ = '\0';
|
|
71 centry->server_type = strtol (pos, NULL, 10);
|
|
72
|
222
|
73 if ((pos = strchr (pos, '\t')) == NULL || *(pos + 1) == '\0')
|
|
74 {
|
|
75 if (request != NULL)
|
|
76 request->logging_function (gftp_logging_error, request,
|
|
77 _("Error: Invalid line %s in cache index file\n"),
|
|
78 line);
|
|
79 return (-1);
|
|
80 }
|
|
81
|
|
82 centry->pos3 = pos;
|
|
83 *pos++ = '\0';
|
|
84 centry->expiration_date = strtol (pos, NULL, 10);
|
|
85
|
114
|
86 return (0);
|
|
87 }
|
|
88
|
|
89
|
182
|
90 static void
|
114
|
91 gftp_restore_cache_line (gftp_cache_entry * centry, char *line)
|
|
92 {
|
|
93 if (centry->pos1 != NULL)
|
|
94 *centry->pos1 = '\t';
|
|
95
|
|
96 if (centry->pos2 != NULL)
|
|
97 *centry->pos2 = '\t';
|
222
|
98
|
|
99 if (centry->pos3 != NULL)
|
|
100 *centry->pos3 = '\t';
|
114
|
101 }
|
|
102
|
1
|
103
|
182
|
104 void
|
|
105 gftp_generate_cache_description (gftp_request * request, char *description,
|
|
106 size_t len, int ignore_directory)
|
|
107 {
|
|
108 g_snprintf (description, len, "%s://%s@%s:%d%s",
|
|
109 request->url_prefix,
|
|
110 request->username == NULL ? "" : request->username,
|
|
111 request->hostname == NULL ? "" : request->hostname,
|
|
112 request->port,
|
|
113 ignore_directory || request->directory == NULL ? "" : request->directory);
|
|
114 }
|
|
115
|
|
116
|
58
|
117 int
|
1
|
118 gftp_new_cache_entry (gftp_request * request)
|
|
119 {
|
58
|
120 char *cachedir, *tempstr, *temp1str;
|
222
|
121 int cache_fd, fd, cache_ttl;
|
58
|
122 ssize_t ret;
|
222
|
123 time_t t;
|
|
124
|
|
125 gftp_lookup_request_option (request, "cache_ttl", &cache_ttl);
|
|
126 time (&t);
|
|
127 t += cache_ttl;
|
1
|
128
|
|
129 cachedir = expand_path (BASE_CONF_DIR "/cache");
|
|
130 if (access (cachedir, F_OK) == -1)
|
|
131 {
|
58
|
132 if (mkdir (cachedir, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
|
|
133 {
|
|
134 if (request != NULL)
|
186
|
135 request->logging_function (gftp_logging_error, request,
|
58
|
136 _("Error: Could not make directory %s: %s\n"),
|
|
137 cachedir, g_strerror (errno));
|
|
138
|
|
139 return (-1);
|
|
140 }
|
1
|
141 }
|
|
142
|
58
|
143 tempstr = g_strdup_printf ("%s/index.db", cachedir);
|
182
|
144 if ((fd = gftp_fd_open (request, tempstr, O_WRONLY | O_APPEND | O_CREAT,
|
|
145 S_IRUSR | S_IWUSR)) == -1)
|
1
|
146 {
|
58
|
147 g_free (tempstr);
|
1
|
148 g_free (cachedir);
|
58
|
149 return (-1);
|
1
|
150 }
|
182
|
151
|
58
|
152 g_free (tempstr);
|
1
|
153
|
58
|
154 tempstr = g_strdup_printf ("%s/cache.XXXXXX", cachedir);
|
1
|
155 if ((cache_fd = mkstemp (tempstr)) < 0)
|
58
|
156 {
|
|
157 g_free (tempstr);
|
|
158 if (request != NULL)
|
186
|
159 request->logging_function (gftp_logging_error, request,
|
58
|
160 _("Error: Cannot create temporary file: %s\n"),
|
|
161 g_strerror (errno));
|
|
162 return (-1);
|
|
163 }
|
1
|
164 g_free (cachedir);
|
|
165
|
58
|
166 lseek (fd, 0, SEEK_END);
|
222
|
167 temp1str = g_strdup_printf ("%s://%s@%s:%d%s\t%s\t%d\t%ld\n",
|
87
|
168 request->url_prefix,
|
58
|
169 request->username == NULL ? "" : request->username,
|
|
170 request->hostname == NULL ? "" : request->hostname,
|
|
171 request->port,
|
|
172 request->directory == NULL ? "" : request->directory,
|
222
|
173 tempstr, request->server_type, t);
|
58
|
174 g_free (tempstr);
|
168
|
175 ret = gftp_fd_write (NULL, temp1str, strlen (temp1str), fd);
|
58
|
176 g_free (temp1str);
|
1
|
177
|
58
|
178 if (close (fd) != 0 || ret < 0)
|
1
|
179 {
|
58
|
180 if (request != NULL)
|
186
|
181 request->logging_function (gftp_logging_error, request,
|
58
|
182 _("Error closing file descriptor: %s\n"),
|
|
183 g_strerror (errno));
|
|
184
|
1
|
185 close (cache_fd);
|
58
|
186 return (-1);
|
1
|
187 }
|
|
188
|
58
|
189 return (cache_fd);
|
1
|
190 }
|
|
191
|
|
192
|
58
|
193 int
|
1
|
194 gftp_find_cache_entry (gftp_request * request)
|
|
195 {
|
114
|
196 char *indexfile, buf[BUFSIZ], description[BUFSIZ];
|
58
|
197 gftp_getline_buffer * rbuf;
|
114
|
198 gftp_cache_entry centry;
|
58
|
199 int indexfd, cachefd;
|
222
|
200 time_t now;
|
|
201
|
|
202 time (&now);
|
1
|
203
|
182
|
204 gftp_generate_cache_description (request, description, sizeof (description),
|
|
205 0);
|
1
|
206
|
|
207 indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
|
190
|
208 if ((indexfd = gftp_fd_open (NULL, indexfile, O_RDONLY, 0)) == -1)
|
1
|
209 {
|
|
210 g_free (indexfile);
|
58
|
211 return (-1);
|
1
|
212 }
|
|
213 g_free (indexfile);
|
|
214
|
58
|
215 rbuf = NULL;
|
60
|
216 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf), indexfd) > 0)
|
1
|
217 {
|
114
|
218 if (gftp_parse_cache_line (request, ¢ry, buf) < 0)
|
1
|
219 continue;
|
|
220
|
222
|
221 /* See if this entry is still valid... */
|
|
222 if (centry.expiration_date < now)
|
|
223 continue;
|
|
224
|
114
|
225 if (strcmp (description, centry.url) == 0)
|
1
|
226 {
|
58
|
227 if (close (indexfd) != 0)
|
|
228 {
|
|
229 if (request != NULL)
|
186
|
230 request->logging_function (gftp_logging_error, request,
|
58
|
231 _("Error closing file descriptor: %s\n"),
|
|
232 g_strerror (errno));
|
|
233 return (-1);
|
|
234 }
|
1
|
235
|
182
|
236 if ((cachefd = gftp_fd_open (request, centry.file, O_RDONLY, 0)) == -1)
|
|
237 return (-1);
|
58
|
238
|
|
239 if (lseek (cachefd, 0, SEEK_END) == 0)
|
1
|
240 {
|
58
|
241 close (cachefd);
|
|
242 return (-1);
|
1
|
243 }
|
58
|
244
|
|
245 if (lseek (cachefd, 0, SEEK_SET) == -1)
|
|
246 {
|
|
247 if (request != NULL)
|
186
|
248 request->logging_function (gftp_logging_error, request,
|
58
|
249 _("Error: Cannot seek on file %s: %s\n"),
|
114
|
250 centry.file, g_strerror (errno));
|
58
|
251
|
|
252 }
|
|
253
|
114
|
254 request->server_type = centry.server_type;
|
1
|
255 return (cachefd);
|
|
256 }
|
|
257 }
|
58
|
258 close (indexfd);
|
|
259 return (-1);
|
1
|
260 }
|
|
261
|
|
262
|
|
263 void
|
|
264 gftp_clear_cache_files (void)
|
|
265 {
|
114
|
266 char *indexfile, buf[BUFSIZ];
|
58
|
267 gftp_getline_buffer * rbuf;
|
114
|
268 gftp_cache_entry centry;
|
58
|
269 int indexfd;
|
1
|
270
|
|
271 indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
|
182
|
272 if ((indexfd = gftp_fd_open (NULL, indexfile, O_RDONLY, 0)) == -1)
|
1
|
273 {
|
|
274 g_free (indexfile);
|
|
275 return;
|
|
276 }
|
|
277
|
58
|
278 rbuf = NULL;
|
60
|
279 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf), indexfd) > 0)
|
1
|
280 {
|
114
|
281 if (gftp_parse_cache_line (NULL, ¢ry, buf) < 0)
|
|
282 continue;
|
1
|
283
|
114
|
284 unlink (centry.file);
|
1
|
285 }
|
|
286
|
58
|
287 close (indexfd);
|
1
|
288 unlink (indexfile);
|
|
289 g_free (indexfile);
|
|
290 }
|
|
291
|
|
292
|
|
293 void
|
253
|
294 gftp_delete_cache_entry (gftp_request * request, char *descr,
|
|
295 int ignore_directory)
|
1
|
296 {
|
114
|
297 char *oldindexfile, *newindexfile, buf[BUFSIZ], description[BUFSIZ];
|
58
|
298 gftp_getline_buffer * rbuf;
|
114
|
299 gftp_cache_entry centry;
|
58
|
300 int indexfd, newfd;
|
222
|
301 time_t now;
|
47
|
302 int remove;
|
222
|
303
|
253
|
304 g_return_if_fail (request != NULL || descr != NULL);
|
|
305
|
222
|
306 time (&now);
|
182
|
307 if (request != NULL)
|
|
308 {
|
|
309 gftp_generate_cache_description (request, description, sizeof (description),
|
|
310 ignore_directory);
|
|
311 }
|
|
312 else if (descr != NULL)
|
|
313 {
|
|
314 strncpy (description, descr, sizeof (description));
|
249
|
315 description[sizeof (description) - 1] = '\0';
|
182
|
316 }
|
|
317 else
|
|
318 return;
|
1
|
319
|
|
320 oldindexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
|
190
|
321 if ((indexfd = gftp_fd_open (NULL, oldindexfile, O_RDONLY, 0)) == -1)
|
1
|
322 {
|
|
323 g_free (oldindexfile);
|
|
324 return;
|
|
325 }
|
|
326
|
|
327 newindexfile = expand_path (BASE_CONF_DIR "/cache/index.db.new");
|
182
|
328 if ((newfd = gftp_fd_open (request, newindexfile, O_WRONLY | O_CREAT,
|
|
329 S_IRUSR | S_IWUSR)) == -1)
|
1
|
330 {
|
|
331 g_free (oldindexfile);
|
|
332 g_free (newindexfile);
|
|
333 return;
|
|
334 }
|
|
335
|
58
|
336 rbuf = NULL;
|
114
|
337 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf) - 1, indexfd) > 0)
|
1
|
338 {
|
114
|
339 if (gftp_parse_cache_line (request, ¢ry, buf) < 0)
|
|
340 continue;
|
1
|
341
|
47
|
342 remove = 0;
|
222
|
343 if (centry.expiration_date < now)
|
|
344 remove = 1;
|
|
345 else if (ignore_directory)
|
47
|
346 {
|
114
|
347 if (strncmp (centry.url, description, strlen (description)) == 0)
|
47
|
348 remove = 1;
|
|
349 }
|
|
350 else
|
|
351 {
|
114
|
352 if (strcmp (centry.url, description) == 0)
|
47
|
353 remove = 1;
|
|
354 }
|
|
355
|
|
356
|
|
357 if (remove)
|
114
|
358 unlink (centry.file);
|
1
|
359 else
|
|
360 {
|
114
|
361 /* Make sure we put the tabs back in the line. I do it this way
|
|
362 so that I don't have to allocate memory again for each line
|
|
363 as we read it */
|
|
364 gftp_restore_cache_line (¢ry, buf);
|
|
365
|
179
|
366 /* Make sure when we call gftp_get_line() that we pass the read size
|
|
367 as sizeof(buf) - 1 so that we'll have room to put the newline */
|
|
368 buf[strlen (buf)] = '\n';
|
|
369
|
168
|
370 if (gftp_fd_write (NULL, buf, strlen (buf), newfd) < 0)
|
58
|
371 break;
|
1
|
372 }
|
|
373 }
|
|
374
|
58
|
375 close (indexfd);
|
|
376 close (newfd);
|
1
|
377
|
|
378 unlink (oldindexfile);
|
|
379 rename (newindexfile, oldindexfile);
|
|
380
|
|
381 g_free (oldindexfile);
|
|
382 g_free (newindexfile);
|
|
383 }
|
|
384
|