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