annotate lib/cache.c @ 133:05cea60b5732

2003-04-16 updated ja.po. T.Aihana <aihana@gnome.gr.jp>
author aihana
date Tue, 15 Apr 2003 15:08:19 +0000
parents 76e2b58a9440
children c505d9ba9d53
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
1 /*****************************************************************************/
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
2 /* cache.c - contains the cache routines */
122
76e2b58a9440 2003-4-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 114
diff changeset
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
4 /* */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
5 /* This program is free software; you can redistribute it and/or modify */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
6 /* it under the terms of the GNU General Public License as published by */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
7 /* the Free Software Foundation; either version 2 of the License, or */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
8 /* (at your option) any later version. */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
9 /* */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
10 /* This program is distributed in the hope that it will be useful, */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
13 /* GNU General Public License for more details. */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
14 /* */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
15 /* You should have received a copy of the GNU General Public License */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
16 /* along with this program; if not, write to the Free Software */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
18 /*****************************************************************************/
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
19
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
20 #include "gftp.h"
33
c8ec7877432e 2002-10-06 Brian Masney <masneyb@gftp.org>
masneyb
parents: 1
diff changeset
21 static const char cvsid[] = "$Id$";
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
22
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
23 struct gftp_cache_entry_tag
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
24 {
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
25 char *url,
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
26 *file;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
27 int server_type;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
28
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
29 char *pos1,
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
30 *pos2;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
31 };
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
32
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
33 typedef struct gftp_cache_entry_tag gftp_cache_entry;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
34
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
35
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
36 static int
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
37 gftp_parse_cache_line (gftp_request * request, gftp_cache_entry * centry,
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
38 char *line)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
39 {
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
40 char *pos;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
41
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
42 memset (centry, 0, sizeof (*centry));
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
43
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
44 if ((pos = strchr (line, '\t')) == NULL || *(pos + 1) == '\0')
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
45 {
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
46 if (request != NULL)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
47 request->logging_function (gftp_logging_error, request->user_data,
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
48 _("Error: Invalid line %s in cache index file\n"),
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
49 line);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
50 return (-1);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
51 }
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
52
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
53 centry->pos1 = pos;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
54 *pos++ = '\0';
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
55 centry->url = line;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
56 centry->file = pos;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
57
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
58 if ((pos = strchr (pos, '\t')) == NULL || *(pos + 1) == '\0')
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
59 {
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
60 if (request != NULL)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
61 request->logging_function (gftp_logging_error, request->user_data,
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
62 _("Error: Invalid line %s in cache index file\n"),
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
63 line);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
64 return (-1);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
65 }
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
66
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
67 centry->pos2 = pos;
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
68 *pos++ = '\0';
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
69 centry->server_type = strtol (pos, NULL, 10);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
70
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
71 return (0);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
72 }
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
73
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
74
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
75 void
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
76 gftp_restore_cache_line (gftp_cache_entry * centry, char *line)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
77 {
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
78 if (centry->pos1 != NULL)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
79 *centry->pos1 = '\t';
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
80
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
81 if (centry->pos2 != NULL)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
82 *centry->pos2 = '\t';
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
83 }
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
84
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
85
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
86 int
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
87 gftp_new_cache_entry (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
88 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
89 char *cachedir, *tempstr, *temp1str;
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
90 int cache_fd, fd;
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
91 ssize_t ret;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
92
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
93 cachedir = expand_path (BASE_CONF_DIR "/cache");
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
94 if (access (cachedir, F_OK) == -1)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
95 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
96 if (mkdir (cachedir, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
97 {
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
98 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
99 request->logging_function (gftp_logging_error, request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
100 _("Error: Could not make directory %s: %s\n"),
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
101 cachedir, g_strerror (errno));
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
102
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
103 return (-1);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
104 }
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
105 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
106
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
107 tempstr = g_strdup_printf ("%s/index.db", cachedir);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
108 if ((fd = open (tempstr, O_WRONLY | O_APPEND | O_CREAT,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
109 S_IRUSR | S_IWUSR)) == -1)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
110 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
111 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
112 request->logging_function (gftp_logging_error, request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
113 _("Error: Cannot open local file %s: %s\n"),
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
114 tempstr, g_strerror (errno));
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
115
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
116 g_free (tempstr);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
117 g_free (cachedir);
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
118 return (-1);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
119 }
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
120 g_free (tempstr);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
121
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
122 tempstr = g_strdup_printf ("%s/cache.XXXXXX", cachedir);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
123 if ((cache_fd = mkstemp (tempstr)) < 0)
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
124 {
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
125 g_free (tempstr);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
126 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
127 request->logging_function (gftp_logging_error, request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
128 _("Error: Cannot create temporary file: %s\n"),
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
129 g_strerror (errno));
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
130 return (-1);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
131 }
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
132 g_free (cachedir);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
133
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
134 lseek (fd, 0, SEEK_END);
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
135 temp1str = g_strdup_printf ("%s://%s@%s:%d%s\t%s\t%d\n",
87
6df043359b41 2003-1-11 Brian Masney <masneyb@gftp.org>
masneyb
parents: 67
diff changeset
136 request->url_prefix,
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
137 request->username == NULL ? "" : request->username,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
138 request->hostname == NULL ? "" : request->hostname,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
139 request->port,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
140 request->directory == NULL ? "" : request->directory,
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
141 tempstr, request->server_type);
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
142 g_free (tempstr);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
143 ret = gftp_write (NULL, temp1str, strlen (temp1str), fd);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
144 g_free (temp1str);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
145
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
146 if (close (fd) != 0 || ret < 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
147 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
148 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
149 request->logging_function (gftp_logging_error, request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
150 _("Error closing file descriptor: %s\n"),
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
151 g_strerror (errno));
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
152
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
153 close (cache_fd);
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
154 return (-1);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
155 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
156
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
157 return (cache_fd);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
158 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
159
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
160
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
161 int
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
162 gftp_find_cache_entry (gftp_request * request)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
163 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
164 char *indexfile, buf[BUFSIZ], description[BUFSIZ];
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
165 gftp_getline_buffer * rbuf;
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
166 gftp_cache_entry centry;
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
167 int indexfd, cachefd;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
168
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
169 g_snprintf (description, sizeof (description), "%s://%s@%s:%d%s",
87
6df043359b41 2003-1-11 Brian Masney <masneyb@gftp.org>
masneyb
parents: 67
diff changeset
170 request->url_prefix,
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
171 request->username == NULL ? "" : request->username,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
172 request->hostname == NULL ? "" : request->hostname,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
173 request->port,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
174 request->directory == NULL ? "" : request->directory);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
175
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
176 indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
177 if ((indexfd = open (indexfile, O_RDONLY)) == -1)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
178 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
179 g_free (indexfile);
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
180 return (-1);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
181 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
182 g_free (indexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
183
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
184 rbuf = NULL;
60
8a9324fb63a4 2002-11-21 Brian Masney <masneyb@gftp.org>
masneyb
parents: 58
diff changeset
185 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf), indexfd) > 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
186 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
187 if (gftp_parse_cache_line (request, &centry, buf) < 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
188 continue;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
189
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
190 if (strcmp (description, centry.url) == 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
191 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
192 if (close (indexfd) != 0)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
193 {
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
194 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
195 request->logging_function (gftp_logging_error,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
196 request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
197 _("Error closing file descriptor: %s\n"),
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
198 g_strerror (errno));
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
199 return (-1);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
200 }
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
201
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
202 if ((cachefd = open (centry.file, O_RDONLY)) == -1)
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
203 {
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
204 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
205 request->logging_function (gftp_logging_error,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
206 request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
207 _("Error: Cannot open local file %s: %s\n"),
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
208 centry.file, g_strerror (errno));
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
209 return (-1);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
210 }
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
211
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
212 if (lseek (cachefd, 0, SEEK_END) == 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
213 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
214 close (cachefd);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
215 return (-1);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
216 }
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
217
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
218 if (lseek (cachefd, 0, SEEK_SET) == -1)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
219 {
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
220 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
221 request->logging_function (gftp_logging_error,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
222 request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
223 _("Error: Cannot seek on file %s: %s\n"),
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
224 centry.file, g_strerror (errno));
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
225
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
226 }
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
227
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
228 request->server_type = centry.server_type;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
229 return (cachefd);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
230 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
231 }
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
232 close (indexfd);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
233 return (-1);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
234 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
235
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
236
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
237 void
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
238 gftp_clear_cache_files (void)
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
239 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
240 char *indexfile, buf[BUFSIZ];
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
241 gftp_getline_buffer * rbuf;
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
242 gftp_cache_entry centry;
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
243 int indexfd;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
244
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
245 indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
246 if ((indexfd = open (indexfile, O_RDONLY)) == -1)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
247 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
248 g_free (indexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
249 return;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
250 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
251
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
252 rbuf = NULL;
60
8a9324fb63a4 2002-11-21 Brian Masney <masneyb@gftp.org>
masneyb
parents: 58
diff changeset
253 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf), indexfd) > 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
254 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
255 if (gftp_parse_cache_line (NULL, &centry, buf) < 0)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
256 continue;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
257
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
258 unlink (centry.file);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
259 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
260
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
261 close (indexfd);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
262 unlink (indexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
263 g_free (indexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
264 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
265
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
266
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
267 void
47
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
268 gftp_delete_cache_entry (gftp_request * request, int ignore_directory)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
269 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
270 char *oldindexfile, *newindexfile, buf[BUFSIZ], description[BUFSIZ];
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
271 gftp_getline_buffer * rbuf;
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
272 gftp_cache_entry centry;
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
273 int indexfd, newfd;
47
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
274 int remove;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
275
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
276 g_snprintf (description, sizeof (description), "%s://%s@%s:%d%s",
87
6df043359b41 2003-1-11 Brian Masney <masneyb@gftp.org>
masneyb
parents: 67
diff changeset
277 request->url_prefix,
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
278 request->username == NULL ? "" : request->username,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
279 request->hostname == NULL ? "" : request->hostname,
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
280 request->port,
47
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
281 ignore_directory || request->directory == NULL ? "" : request->directory);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
282
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
283 oldindexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
284 if ((indexfd = open (oldindexfile, O_RDONLY)) == -1)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
285 {
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
286 g_free (oldindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
287 return;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
288 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
289
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
290 newindexfile = expand_path (BASE_CONF_DIR "/cache/index.db.new");
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
291 if ((newfd = open (newindexfile, O_WRONLY | O_CREAT,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
292 S_IRUSR | S_IWUSR)) == -1)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
293 {
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
294 if (request != NULL)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
295 request->logging_function (gftp_logging_error, request->user_data,
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
296 _("Error: Cannot open local file %s: %s\n"),
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
297 newindexfile, g_strerror (errno));
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
298
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
299 g_free (oldindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
300 g_free (newindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
301 return;
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
302 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
303
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
304 rbuf = NULL;
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
305 while (gftp_get_line (NULL, &rbuf, buf, sizeof (buf) - 1, indexfd) > 0)
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
306 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
307 if (gftp_parse_cache_line (request, &centry, buf) < 0)
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
308 continue;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
309
47
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
310 remove = 0;
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
311 if (ignore_directory)
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
312 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
313 if (strncmp (centry.url, description, strlen (description)) == 0)
47
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
314 remove = 1;
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
315 }
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
316 else
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
317 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
318 if (strcmp (centry.url, description) == 0)
47
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
319 remove = 1;
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
320 }
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
321
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
322
eec25f215772 2002-11-5 Brian Masney <masneyb@gftp.org>
masneyb
parents: 33
diff changeset
323 if (remove)
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
324 unlink (centry.file);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
325 else
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
326 {
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
327 /* Make sure when we call gftp_get_line() that we pass the read size
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
328 as sizeof(buf) - 1 so that we'll have room to put the newline */
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
329 buf[strlen (buf)] = '\n';
114
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
330
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
331 /* Make sure we put the tabs back in the line. I do it this way
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
332 so that I don't have to allocate memory again for each line
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
333 as we read it */
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
334 gftp_restore_cache_line (&centry, buf);
fe36ed90aedd 2003-2-23 Brian Masney <masneyb@gftp.org>
masneyb
parents: 87
diff changeset
335
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
336 if (gftp_write (NULL, buf, strlen (buf), newfd) < 0)
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
337 break;
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
338 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
339 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
340
58
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
341 close (indexfd);
c01d91c10f6c 2002-11-20 Brian Masney <masneyb@gftp.org>
masneyb
parents: 47
diff changeset
342 close (newfd);
1
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
343
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
344 unlink (oldindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
345 rename (newindexfile, oldindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
346
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
347 g_free (oldindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
348 g_free (newindexfile);
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
349 }
8b1883341c6f Initial revision
masneyb
parents:
diff changeset
350