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