Mercurial > hg > index.fcgi > gift-gnutella > gift-gnutella-0.0.11-1pba
comparison src/gt_conf.c @ 0:d39e1d0d75b6
initial add
author | paulo@hit-nxdomain.opendns.com |
---|---|
date | Sat, 20 Feb 2010 21:18:28 -0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:b80565a50e0e |
---|---|
1 /* | |
2 * $Id: gt_conf.c,v 1.4 2004/03/24 06:27:40 hipnod Exp $ | |
3 * | |
4 * Copyright (C) 2003 giFT project (gift.sourceforge.net) | |
5 * | |
6 * This program is free software; you can redistribute it and/or modify it | |
7 * under the terms of the GNU General Public License as published by the | |
8 * Free Software Foundation; either version 2, or (at your option) any | |
9 * later version. | |
10 * | |
11 * This program is distributed in the hope that it will be useful, but | |
12 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 * General Public License for more details. | |
15 */ | |
16 | |
17 #include "gt_gnutella.h" | |
18 #include "gt_conf.h" | |
19 | |
20 /*****************************************************************************/ | |
21 | |
22 #define CHECK_CONFIG_INTERVAL (1 * MINUTES) | |
23 | |
24 /*****************************************************************************/ | |
25 | |
26 static Config *gt_conf; | |
27 | |
28 static char *conf_path; | |
29 static time_t conf_mtime; | |
30 | |
31 static Dataset *cache; | |
32 | |
33 static timer_id refresh_timer; | |
34 | |
35 /*****************************************************************************/ | |
36 | |
37 static char *get_key (const char *key_str) | |
38 { | |
39 char *str; | |
40 char *key; | |
41 | |
42 if (!(str = STRDUP (key_str))) | |
43 return NULL; | |
44 | |
45 key = string_sep (&str, "="); | |
46 return key; | |
47 } | |
48 | |
49 static char *cache_lookup (const char *key_str) | |
50 { | |
51 char *key; | |
52 char *data; | |
53 | |
54 if (!(key = get_key (key_str))) | |
55 return NULL; | |
56 | |
57 data = dataset_lookupstr (cache, key); | |
58 free (key); | |
59 | |
60 return data; | |
61 } | |
62 | |
63 static void cache_insert (const char *key_str, const char *value) | |
64 { | |
65 char *key; | |
66 | |
67 if (!(key = get_key (key_str))) | |
68 return; | |
69 | |
70 dataset_insertstr (&cache, key, value); | |
71 free (key); | |
72 } | |
73 | |
74 char *gt_config_get_str (const char *key) | |
75 { | |
76 char *str; | |
77 char *ret; | |
78 | |
79 if (!(str = cache_lookup (key))) | |
80 str = config_get_str (gt_conf, (char *)key); | |
81 | |
82 ret = str; | |
83 | |
84 /* unset keys are marked specially by the empty string so | |
85 * we don't have to call config_get_xxx for them */ | |
86 if (string_isempty (str)) | |
87 { | |
88 ret = NULL; | |
89 str = ""; | |
90 } | |
91 | |
92 /* hrm, the dataset doesn't handle inserting the same item you lookup | |
93 * yet, so we can't do the insert unconditionally here.. */ | |
94 if (str != cache_lookup (key)) | |
95 cache_insert (key, str); | |
96 | |
97 return ret; | |
98 } | |
99 | |
100 int gt_config_get_int (const char *key) | |
101 { | |
102 return ATOI (gt_config_get_str (key)); | |
103 } | |
104 | |
105 /* check the mtime on the conf file. If it has changed, reload */ | |
106 static int refresh_conf (void *udata) | |
107 { | |
108 struct stat conf_st; | |
109 char *path; | |
110 BOOL ret; | |
111 | |
112 path = STRDUP (gift_conf_path (conf_path)); | |
113 ret = file_stat (path, &conf_st); | |
114 | |
115 if (!ret || conf_st.st_mtime != conf_mtime) | |
116 { | |
117 GT->DBGFN (GT, "Gnutella.conf changed on disk. flushing cached config"); | |
118 | |
119 /* gt_config_get_xxx will reload the cache */ | |
120 dataset_clear (cache); | |
121 cache = NULL; | |
122 | |
123 conf_mtime = conf_st.st_mtime; | |
124 } | |
125 | |
126 free (path); | |
127 return TRUE; | |
128 } | |
129 | |
130 BOOL gt_config_load_file (const char *relative_path, BOOL update, BOOL force) | |
131 { | |
132 char *src_path; | |
133 char *dst_path; | |
134 BOOL src_exists; | |
135 BOOL dst_exists; | |
136 struct stat src_st; | |
137 struct stat dst_st; | |
138 BOOL ret = TRUE; | |
139 | |
140 src_path = STRDUP (stringf ("%s/%s", platform_data_dir(), relative_path)); | |
141 dst_path = STRDUP (gift_conf_path (relative_path)); | |
142 | |
143 src_exists = file_stat (src_path, &src_st); | |
144 dst_exists = file_stat (dst_path, &dst_st); | |
145 | |
146 /* | |
147 * NOTE: the user may modify the config files in ~/.giFT/Gnutella/. | |
148 * If so, the mtime there should be greater, so look for an mtime | |
149 * greater than instead of not equal to the those files. | |
150 */ | |
151 if (force || (src_exists && | |
152 (!dst_exists || src_st.st_mtime > dst_st.st_mtime))) | |
153 { | |
154 /* | |
155 * Copy the default configuration from the data dir | |
156 * (usually "/usr/local/share/giFT/Gnutella/") | |
157 */ | |
158 GT->DBGFN (GT, "reloading configuration for %s (copying %s -> %s)", | |
159 relative_path, src_path, dst_path); | |
160 ret = file_cp (src_path, dst_path); | |
161 } | |
162 | |
163 free (dst_path); | |
164 free (src_path); | |
165 | |
166 return ret; | |
167 } | |
168 | |
169 static Config *load_config (const char *relative_path) | |
170 { | |
171 Config *conf; | |
172 char *full_path; | |
173 | |
174 full_path = STRDUP (gift_conf_path (relative_path)); | |
175 | |
176 if (!(conf = config_new (full_path))) | |
177 { | |
178 /* copy the configuration from the data dir */ | |
179 gt_config_load_file (relative_path, TRUE, TRUE); | |
180 | |
181 /* retry loading the configuration */ | |
182 conf = config_new (full_path); | |
183 } | |
184 | |
185 free (full_path); | |
186 | |
187 return conf; | |
188 } | |
189 | |
190 /*****************************************************************************/ | |
191 | |
192 BOOL gt_config_init (void) | |
193 { | |
194 struct stat st; | |
195 | |
196 refresh_timer = timer_add (CHECK_CONFIG_INTERVAL, | |
197 (TimerCallback)refresh_conf, NULL); | |
198 | |
199 conf_path = STRDUP (stringf ("%s/%s.conf", GT->name, GT->name)); | |
200 | |
201 if (file_stat (gift_conf_path (conf_path), &st)) | |
202 conf_mtime = st.st_mtime; | |
203 | |
204 gt_conf = load_config (conf_path); | |
205 cache = dataset_new (DATASET_HASH); | |
206 | |
207 if (!refresh_timer || !conf_path || !gt_conf) | |
208 return FALSE; | |
209 | |
210 return TRUE; | |
211 } | |
212 | |
213 void gt_config_cleanup (void) | |
214 { | |
215 dataset_clear (cache); | |
216 cache = NULL; | |
217 | |
218 config_free (gt_conf); | |
219 gt_conf = NULL; | |
220 | |
221 free (conf_path); | |
222 conf_path = NULL; | |
223 | |
224 timer_remove_zero (&refresh_timer); | |
225 } |