1
|
1 /*****************************************************************************/
|
|
2 /* cache.c - contains the cache routines */
|
885
|
3 /* Copyright (C) 1998-2007 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
|
765
|
39 gftp_parse_cache_line (gftp_request * request, /*@out@*/ gftp_cache_entry * centry,
|
114
|
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
|
765
|
91 gftp_restore_cache_line (gftp_cache_entry * centry)
|
114
|
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
|
765
|
105 gftp_generate_cache_description (gftp_request * request,
|
|
106 char *description,
|
182
|
107 size_t len, int ignore_directory)
|
|
108 {
|
|
109 g_snprintf (description, len, "%s://%s@%s:%d%s",
|
|
110 request->url_prefix,
|
|
111 request->username == NULL ? "" : request->username,
|
|
112 request->hostname == NULL ? "" : request->hostname,
|
|
113 request->port,
|
|
114 ignore_directory || request->directory == NULL ? "" : request->directory);
|
|
115 }
|
|
116
|
|
117
|
58
|
118 int
|
1
|
119 gftp_new_cache_entry (gftp_request * request)
|
|
120 {
|
58
|
121 char *cachedir, *tempstr, *temp1str;
|
325
|
122 int cache_fd, fd;
|
|
123 intptr_t cache_ttl;
|
58
|
124 ssize_t ret;
|
222
|
125 time_t t;
|
|
126
|
|
127 gftp_lookup_request_option (request, "cache_ttl", &cache_ttl);
|
|
128 time (&t);
|
|
129 t += cache_ttl;
|
1
|
130
|
555
|
131 cachedir = gftp_expand_path (NULL, BASE_CONF_DIR "/cache");
|
1
|
132 if (access (cachedir, F_OK) == -1)
|
|
133 {
|
58
|
134 if (mkdir (cachedir, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
|
|
135 {
|
|
136 if (request != NULL)
|
186
|
137 request->logging_function (gftp_logging_error, request,
|
58
|
138 _("Error: Could not make directory %s: %s\n"),
|
|
139 cachedir, g_strerror (errno));
|
|
140
|
765
|
141 g_free (cachedir);
|
58
|
142 return (-1);
|
|
143 }
|
1
|
144 }
|
|
145
|
58
|
146 tempstr = g_strdup_printf ("%s/index.db", cachedir);
|
182
|
147 if ((fd = gftp_fd_open (request, tempstr, O_WRONLY | O_APPEND | O_CREAT,
|
|
148 S_IRUSR | S_IWUSR)) == -1)
|
1
|
149 {
|
58
|
150 g_free (tempstr);
|
1
|
151 g_free (cachedir);
|
58
|
152 return (-1);
|
1
|
153 }
|
182
|
154
|
58
|
155 g_free (tempstr);
|
1
|
156
|
58
|
157 tempstr = g_strdup_printf ("%s/cache.XXXXXX", cachedir);
|
1
|
158 if ((cache_fd = mkstemp (tempstr)) < 0)
|
58
|
159 {
|
|
160 g_free (tempstr);
|
765
|
161 g_free (cachedir);
|
58
|
162 if (request != NULL)
|
186
|
163 request->logging_function (gftp_logging_error, request,
|
58
|
164 _("Error: Cannot create temporary file: %s\n"),
|
|
165 g_strerror (errno));
|
|
166 return (-1);
|
|
167 }
|
1
|
168 g_free (cachedir);
|
|
169
|
58
|
170 lseek (fd, 0, SEEK_END);
|
222
|
171 temp1str = g_strdup_printf ("%s://%s@%s:%d%s\t%s\t%d\t%ld\n",
|
87
|
172 request->url_prefix,
|
58
|
173 request->username == NULL ? "" : request->username,
|
|
174 request->hostname == NULL ? "" : request->hostname,
|
|
175 request->port,
|
|
176 request->directory == NULL ? "" : request->directory,
|
222
|
177 tempstr, request->server_type, t);
|
58
|
178 g_free (tempstr);
|
168
|
179 ret = gftp_fd_write (NULL, temp1str, strlen (temp1str), fd);
|
58
|
180 g_free (temp1str);
|
1
|
181
|
58
|
182 if (close (fd) != 0 || ret < 0)
|
1
|
183 {
|
58
|
184 if (request != NULL)
|
186
|
185 request->logging_function (gftp_logging_error, request,
|
58
|
186 _("Error closing file descriptor: %s\n"),
|
|
187 g_strerror (errno));
|
|
188
|
1
|
189 close (cache_fd);
|
58
|
190 return (-1);
|
1
|
191 }
|
|
192
|
58
|
193 return (cache_fd);
|
1
|
194 }
|
|
195
|
|
196
|
58
|
197 int
|
1
|
198 gftp_find_cache_entry (gftp_request * request)
|
|
199 {
|
114
|
200 char *indexfile, buf[BUFSIZ], description[BUFSIZ];
|
58
|
201 gftp_getline_buffer * rbuf;
|
114
|
202 gftp_cache_entry centry;
|
58
|
203 int indexfd, cachefd;
|
222
|
204 time_t now;
|
|
205
|
|
206 time (&now);
|
1
|
207
|
765
|
208 *description = '\0';
|
182
|
209 gftp_generate_cache_description (request, description, sizeof (description),
|
|
210 0);
|
1
|
211
|
555
|
212 indexfile = gftp_expand_path (NULL, BASE_CONF_DIR "/cache/index.db");
|
190
|
213 if ((indexfd = gftp_fd_open (NULL, indexfile, O_RDONLY, 0)) == -1)
|
1
|
214 {
|
|
215 g_free (indexfile);
|
58
|
216 return (-1);
|
1
|
217 }
|
|
218 g_free (indexfile);
|
|
219
|
58
|
220 rbuf = NULL;
|
60
|
221 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf), indexfd) > 0)
|
1
|
222 {
|
114
|
223 if (gftp_parse_cache_line (request, ¢ry, buf) < 0)
|
1
|
224 continue;
|
|
225
|
222
|
226 /* See if this entry is still valid... */
|
|
227 if (centry.expiration_date < now)
|
|
228 continue;
|
|
229
|
114
|
230 if (strcmp (description, centry.url) == 0)
|
1
|
231 {
|
58
|
232 if (close (indexfd) != 0)
|
|
233 {
|
|
234 if (request != NULL)
|
186
|
235 request->logging_function (gftp_logging_error, request,
|
58
|
236 _("Error closing file descriptor: %s\n"),
|
|
237 g_strerror (errno));
|
|
238 return (-1);
|
|
239 }
|
1
|
240
|
182
|
241 if ((cachefd = gftp_fd_open (request, centry.file, O_RDONLY, 0)) == -1)
|
|
242 return (-1);
|
58
|
243
|
|
244 if (lseek (cachefd, 0, SEEK_END) == 0)
|
1
|
245 {
|
58
|
246 close (cachefd);
|
|
247 return (-1);
|
1
|
248 }
|
58
|
249
|
|
250 if (lseek (cachefd, 0, SEEK_SET) == -1)
|
|
251 {
|
|
252 if (request != NULL)
|
186
|
253 request->logging_function (gftp_logging_error, request,
|
58
|
254 _("Error: Cannot seek on file %s: %s\n"),
|
114
|
255 centry.file, g_strerror (errno));
|
58
|
256
|
|
257 }
|
|
258
|
114
|
259 request->server_type = centry.server_type;
|
1
|
260 return (cachefd);
|
|
261 }
|
|
262 }
|
58
|
263 close (indexfd);
|
|
264 return (-1);
|
1
|
265 }
|
|
266
|
|
267
|
|
268 void
|
|
269 gftp_clear_cache_files (void)
|
|
270 {
|
114
|
271 char *indexfile, buf[BUFSIZ];
|
58
|
272 gftp_getline_buffer * rbuf;
|
114
|
273 gftp_cache_entry centry;
|
58
|
274 int indexfd;
|
1
|
275
|
555
|
276 indexfile = gftp_expand_path (NULL, BASE_CONF_DIR "/cache/index.db");
|
182
|
277 if ((indexfd = gftp_fd_open (NULL, indexfile, O_RDONLY, 0)) == -1)
|
1
|
278 {
|
|
279 g_free (indexfile);
|
|
280 return;
|
|
281 }
|
|
282
|
765
|
283 *buf = '\0';
|
58
|
284 rbuf = NULL;
|
60
|
285 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf), indexfd) > 0)
|
1
|
286 {
|
114
|
287 if (gftp_parse_cache_line (NULL, ¢ry, buf) < 0)
|
|
288 continue;
|
1
|
289
|
114
|
290 unlink (centry.file);
|
1
|
291 }
|
|
292
|
58
|
293 close (indexfd);
|
1
|
294 unlink (indexfile);
|
|
295 g_free (indexfile);
|
|
296 }
|
|
297
|
|
298
|
|
299 void
|
253
|
300 gftp_delete_cache_entry (gftp_request * request, char *descr,
|
|
301 int ignore_directory)
|
1
|
302 {
|
114
|
303 char *oldindexfile, *newindexfile, buf[BUFSIZ], description[BUFSIZ];
|
516
|
304 int indexfd, newfd, del_entry;
|
58
|
305 gftp_getline_buffer * rbuf;
|
114
|
306 gftp_cache_entry centry;
|
460
|
307 size_t len;
|
222
|
308 time_t now;
|
|
309
|
253
|
310 g_return_if_fail (request != NULL || descr != NULL);
|
|
311
|
222
|
312 time (&now);
|
182
|
313 if (request != NULL)
|
|
314 {
|
765
|
315 *description = '\0';
|
182
|
316 gftp_generate_cache_description (request, description, sizeof (description),
|
|
317 ignore_directory);
|
|
318 }
|
|
319 else if (descr != NULL)
|
|
320 {
|
|
321 strncpy (description, descr, sizeof (description));
|
249
|
322 description[sizeof (description) - 1] = '\0';
|
182
|
323 }
|
|
324 else
|
|
325 return;
|
1
|
326
|
555
|
327 oldindexfile = gftp_expand_path (NULL, BASE_CONF_DIR "/cache/index.db");
|
190
|
328 if ((indexfd = gftp_fd_open (NULL, oldindexfile, O_RDONLY, 0)) == -1)
|
1
|
329 {
|
|
330 g_free (oldindexfile);
|
|
331 return;
|
|
332 }
|
|
333
|
555
|
334 newindexfile = gftp_expand_path (NULL, BASE_CONF_DIR "/cache/index.db.new");
|
182
|
335 if ((newfd = gftp_fd_open (request, newindexfile, O_WRONLY | O_CREAT,
|
|
336 S_IRUSR | S_IWUSR)) == -1)
|
1
|
337 {
|
|
338 g_free (oldindexfile);
|
|
339 g_free (newindexfile);
|
|
340 return;
|
|
341 }
|
|
342
|
765
|
343 *buf = '\0';
|
58
|
344 rbuf = NULL;
|
114
|
345 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf) - 1, indexfd) > 0)
|
1
|
346 {
|
114
|
347 if (gftp_parse_cache_line (request, ¢ry, buf) < 0)
|
|
348 continue;
|
1
|
349
|
516
|
350 del_entry = 0;
|
222
|
351 if (centry.expiration_date < now)
|
516
|
352 del_entry = 1;
|
222
|
353 else if (ignore_directory)
|
47
|
354 {
|
114
|
355 if (strncmp (centry.url, description, strlen (description)) == 0)
|
516
|
356 del_entry = 1;
|
47
|
357 }
|
|
358 else
|
|
359 {
|
114
|
360 if (strcmp (centry.url, description) == 0)
|
516
|
361 del_entry = 1;
|
47
|
362 }
|
|
363
|
|
364
|
516
|
365 if (del_entry)
|
114
|
366 unlink (centry.file);
|
1
|
367 else
|
|
368 {
|
114
|
369 /* Make sure we put the tabs back in the line. I do it this way
|
|
370 so that I don't have to allocate memory again for each line
|
|
371 as we read it */
|
765
|
372 gftp_restore_cache_line (¢ry);
|
114
|
373
|
179
|
374 /* Make sure when we call gftp_get_line() that we pass the read size
|
|
375 as sizeof(buf) - 1 so that we'll have room to put the newline */
|
460
|
376 len = strlen (buf);
|
|
377 buf[len--] = '\n';
|
179
|
378
|
460
|
379 if (gftp_fd_write (NULL, buf, len, newfd) < 0)
|
58
|
380 break;
|
1
|
381 }
|
|
382 }
|
|
383
|
58
|
384 close (indexfd);
|
|
385 close (newfd);
|
1
|
386
|
|
387 unlink (oldindexfile);
|
|
388 rename (newindexfile, oldindexfile);
|
|
389
|
|
390 g_free (oldindexfile);
|
|
391 g_free (newindexfile);
|
|
392 }
|
|
393
|