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