source: trunk/gtk/conf.c @ 2392

Last change on this file since 2392 was 2392, checked in by joshe, 15 years ago

Replace bzero() with memset().
Define AF_LOCAL and SUN_LEN for systems that lack them.
Don't mix function and non function pointers without casting.
Fix a potential type mismatch in assignment.

  • Property svn:keywords set to Date Rev Author Id
File size: 13.5 KB
Line 
1/******************************************************************************
2 * $Id: conf.c 2392 2007-07-18 17:28:25Z joshe $
3 *
4 * Copyright (c) 2005-2007 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#include <sys/types.h>
26#include <ctype.h>
27#include <errno.h>
28#include <fcntl.h>
29#include <pwd.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sys/stat.h>
34#include <unistd.h>
35
36#include <glib.h>
37#include <glib/gi18n.h>
38
39#include "conf.h"
40#include "util.h"
41
42#define CONF_SUBDIR             "gtk"
43#define FILE_LOCK               "lock"
44#define FILE_SOCKET             "socket"
45#define FILE_PREFS              "prefs"
46#define FILE_PREFS_TMP          "prefs.tmp"
47#define FILE_STATE              "state"
48#define FILE_STATE_TMP          "state.tmp"
49#define OLD_FILE_LOCK           "gtk_lock" /* remove this after next release */
50#define OLD_FILE_PREFS          "gtk_prefs"
51#define OLD_FILE_STATE          "gtk_state"
52#define PREF_SEP_KEYVAL         '\t'
53#define PREF_SEP_LINE           '\n'
54#define STATE_SEP               '\n'
55
56static int
57lockfile(const char *file, char **errstr);
58static void
59cf_removelocks(void);
60static char *
61cf_readfile(const char *file, const char *oldfile, gsize *len,
62            gboolean *usedold, char **errstr);
63static void
64cf_benc_append(benc_val_t *val, char type, int incsize);
65static void
66cf_writebenc(const char *file, const char *tmp, benc_val_t *data,
67             char **errstr);
68static gboolean
69writefile_traverse(gpointer key, gpointer value, gpointer data);
70static char *
71getstateval(benc_val_t *state, char *line);
72
73static char *gl_confdir = NULL;
74static char *gl_old_confdir = NULL;
75static GTree *gl_prefs = NULL;
76static char *gl_lockpath = NULL;
77static char *gl_old_lockpath = NULL;
78
79/* errstr may be NULL, this might be called before GTK is initialized */
80static int
81lockfile(const char *file, char **errstr) {
82  int fd, savederr;
83  struct flock lk;
84
85  if(NULL != errstr)
86    *errstr = NULL;
87
88  if(0 > (fd = open(file, O_RDWR | O_CREAT, 0666))) {
89    savederr = errno;
90    if(NULL != errstr)
91      *errstr = g_strdup_printf(_("Failed to open the file %s for writing:\n%s"),
92                                file, strerror(errno));
93    errno = savederr;
94    return -1;
95  }
96
97  memset(&lk, 0,  sizeof(lk));
98  lk.l_start = 0;
99  lk.l_len = 0;
100  lk.l_type = F_WRLCK;
101  lk.l_whence = SEEK_SET;
102  if(-1 == fcntl(fd, F_SETLK, &lk)) {
103    savederr = errno;
104    if(NULL != errstr) {
105      if(EAGAIN == errno)
106        *errstr = g_strdup_printf(_("Another copy of %s is already running."),
107                                  g_get_application_name());
108      else
109        *errstr = g_strdup_printf(_("Failed to lock the file %s:\n%s"),
110                                  file, strerror(errno));
111    }
112    close(fd);
113    errno = savederr;
114    return -1;
115  }
116
117  return fd;
118}
119
120/* errstr may be NULL, this might be called before GTK is initialized */
121gboolean
122cf_init(const char *dir, char **errstr) {
123  if(NULL != errstr)
124    *errstr = NULL;
125  gl_old_confdir = g_strdup(dir);
126  gl_confdir = g_build_filename(dir, CONF_SUBDIR, NULL);
127
128  if(mkdir_p(gl_confdir, 0777))
129    return TRUE;
130
131  if(NULL != errstr)
132    *errstr = g_strdup_printf(_("Failed to create the directory %s:\n%s"),
133                              gl_confdir, strerror(errno));
134  return FALSE;
135}
136
137/* errstr may be NULL, this might be called before GTK is initialized */
138gboolean
139cf_lock(char **errstr) {
140  char *path = g_build_filename(gl_old_confdir, OLD_FILE_LOCK, NULL);
141  int fd = lockfile(path, errstr);
142
143  if(0 > fd)
144    g_free(path);
145  else {
146    gl_old_lockpath = path;
147    path = g_build_filename(gl_confdir, FILE_LOCK, NULL);
148    fd = lockfile(path, errstr);
149    if(0 > fd)
150      g_free(path);
151    else
152      gl_lockpath = path;
153  }
154
155  g_atexit(cf_removelocks);
156
157  return 0 <= fd;
158}
159
160static void
161cf_removelocks(void) {
162  if(NULL != gl_lockpath) {
163    unlink(gl_lockpath);
164    g_free(gl_lockpath);
165  }
166
167  if(NULL != gl_old_lockpath) {
168    unlink(gl_old_lockpath);
169    g_free(gl_old_lockpath);
170  }
171}
172
173char *
174cf_sockname(void) {
175  return g_build_filename(gl_confdir, FILE_SOCKET, NULL);
176}
177
178static char *
179cf_readfile(const char *file, const char *oldfile, gsize *len,
180            gboolean *usedold, char **errstr) {
181  char *path;
182  GIOChannel *io;
183  GError *err;
184  char *ret;
185
186  *errstr = NULL;
187  *usedold = FALSE;
188  ret = NULL;
189  err = NULL;
190  *len = 0;
191
192  path = g_build_filename(gl_confdir, file, NULL);
193  io = g_io_channel_new_file(path, "r", &err);
194  if(NULL != err && g_error_matches(err, G_FILE_ERROR, G_FILE_ERROR_NOENT)) {
195    g_error_free(err);
196    err = NULL;
197    g_free(path);
198    path = g_build_filename(gl_old_confdir, oldfile, NULL);
199    io = g_io_channel_new_file(path, "r", &err);
200    *usedold = TRUE;
201  }
202  if(NULL != err) {
203    if(!g_error_matches(err, G_FILE_ERROR, G_FILE_ERROR_NOENT))
204      *errstr = g_strdup_printf(
205        _("Failed to open the file %s for reading:\n%s"), path, err->message);
206    goto done;
207  }
208  g_io_channel_set_encoding(io, NULL, NULL);
209
210  if(G_IO_STATUS_ERROR == g_io_channel_read_to_end(io, &ret, len, &err)) {
211    *errstr = g_strdup_printf(
212      _("Error while reading from the file %s:\n%s"), path, err->message);
213    goto done;
214  }
215
216 done:
217  g_free (path);
218  if(NULL != err)
219    g_error_free(err);
220  if(NULL != io) 
221    g_io_channel_unref(io);
222  return ret;
223}
224
225void
226cf_loadprefs(char **errstr) {
227  char *data, *line, *eol, *sep, *key;
228  gsize len;
229  benc_val_t val;
230  gboolean usedold;
231  int ii;
232
233  *errstr = NULL;
234
235  if(NULL != gl_prefs)
236    g_tree_destroy(gl_prefs);
237
238  gl_prefs = g_tree_new_full((GCompareDataFunc)g_ascii_strcasecmp, NULL,
239                          g_free, g_free);
240
241  data = cf_readfile(FILE_PREFS, OLD_FILE_PREFS, &len, &usedold, errstr);
242  if(NULL != *errstr) {
243    g_assert(NULL == data);
244    return;
245  }
246
247  if(NULL == data)
248    return;
249
250  memset(&val, 0,  sizeof(val));
251  if(!usedold && !tr_bencLoad(data, len, &val, NULL)) {
252    if(TYPE_DICT == val.type) {
253      key = NULL;
254      for(ii = 0; ii < val.val.l.count; ii++) {
255        if(NULL == key) {
256          g_assert(TYPE_STR == val.val.l.vals[ii].type);
257          key = val.val.l.vals[ii].val.s.s;
258        } else {
259          if(TYPE_INT == val.val.l.vals[ii].type)
260            g_tree_insert(gl_prefs, g_strdup(key),
261                         g_strdup_printf("%"PRIu64, val.val.l.vals[ii].val.i));
262          else if(TYPE_STR == val.val.l.vals[ii].type)
263            g_tree_insert(gl_prefs, g_strdup(key),
264                          g_strdup(val.val.l.vals[ii].val.s.s));
265          key = NULL;
266        }
267      }
268    }
269
270  } else {
271    /* XXX remove this in a release or two */
272    for(line = data; NULL != (eol = strchr(line, PREF_SEP_LINE));
273        line = eol + 1) {
274      *eol = '\0';
275      if(g_utf8_validate(line, -1, NULL) &&
276         NULL != (sep = strchr(line, PREF_SEP_KEYVAL))) {
277        *sep = '\0';
278        g_tree_insert(gl_prefs, g_strcompress(line), g_strcompress(sep+1));
279      }
280    }
281    cf_saveprefs(errstr);
282  }
283
284  tr_bencFree(&val);
285  g_free(data);
286}
287
288benc_val_t *
289cf_loadstate(char **errstr) {
290  char *data, *line, *eol, *prog;
291  gsize len;
292  gboolean usedold;
293  benc_val_t *state, *torstate;
294
295  *errstr = NULL;
296
297  data = cf_readfile(FILE_STATE, OLD_FILE_STATE, &len, &usedold, errstr);
298  if(NULL != *errstr) {
299    g_assert(NULL == data);
300    return NULL;
301  }
302
303  if(NULL == data)
304    return NULL;
305
306  state = g_new0(benc_val_t, 1);
307  if(usedold || tr_bencLoad(data, len, state, NULL)) {
308    /* XXX all this evil compat code should go away at some point */
309    memset(state, 0,  sizeof(benc_val_t));
310    state->type = TYPE_LIST;
311    for(line = data; NULL != (eol = strchr(line, PREF_SEP_LINE));
312        line = eol + 1) {
313      *eol = '\0';
314      if(g_utf8_validate(line, -1, NULL)) {
315        cf_benc_append(state, TYPE_DICT, 10);
316        torstate = state->val.l.vals + state->val.l.count - 1;
317        prog = line;
318        while(NULL != (prog = getstateval(torstate, prog)))
319          ;
320      }
321    }
322  }
323
324  g_free(data);
325
326  return state;
327}
328
329static void
330cf_benc_append(benc_val_t *val, char type, int incsize) {
331  if(++val->val.l.count > val->val.l.alloc) {
332    val->val.l.alloc += incsize;
333    val->val.l.vals = g_renew(benc_val_t, val->val.l.vals, val->val.l.alloc);
334    memset(val->val.l.vals + val->val.l.alloc - incsize, 0,
335          incsize * sizeof(benc_val_t));
336  }
337  val->val.l.vals[val->val.l.count-1].type = type;
338}
339
340const char *
341cf_getpref(const char *name) {
342  g_assert(NULL != gl_prefs);
343
344  return g_tree_lookup(gl_prefs, name);
345}
346
347void
348cf_setpref(const char *name, const char *value) {
349  g_assert(NULL != gl_prefs);
350
351  g_tree_insert(gl_prefs, g_strdup(name), g_strdup(value));
352}
353
354static void
355cf_writebenc(const char *file, const char *tmp, benc_val_t *data,
356             char **errstr) {
357  char *path = g_build_filename(gl_confdir, file, NULL);
358  char *pathtmp = g_build_filename(gl_confdir, tmp, NULL);
359  GIOChannel *io = NULL;
360  GError *err;
361  char *datastr;
362  int len;
363  gsize written;
364
365  *errstr = NULL;
366  err = NULL;
367  datastr = NULL;
368
369  io = g_io_channel_new_file(pathtmp, "w", &err);
370  if(NULL != err) {
371    *errstr = g_strdup_printf(_("Failed to open the file %s for writing:\n%s"),
372                              pathtmp, err->message);
373    goto done;
374  }
375  g_io_channel_set_encoding(io, NULL, NULL);
376
377  len = 0;
378  datastr = tr_bencSaveMalloc(data, &len);
379
380  written = 0;
381  g_io_channel_write_chars(io, datastr, len, &written, &err);
382  if(NULL != err)
383    g_io_channel_flush(io, &err);
384  if(NULL != err) {
385    *errstr = g_strdup_printf(_("Error while writing to the file %s:\n%s"),
386                              pathtmp, err->message);
387    goto done;
388  }
389
390  if(0 > rename(pathtmp, path)) {
391    *errstr = g_strdup_printf(_("Failed to rename the file %s to %s:\n%s"),
392                              pathtmp, file, strerror(errno));
393    goto done;
394  }
395
396 done:
397  g_free(path);
398  g_free(pathtmp);
399  if(NULL != io)
400    g_io_channel_unref(io);
401  if(NULL != datastr)
402    free(datastr);
403}
404
405void
406cf_saveprefs(char **errstr) {
407  benc_val_t val;
408  benc_val_t *ptr;
409
410  *errstr = NULL;
411
412  memset(&val, 0,  sizeof(val));
413  val.type = TYPE_DICT;
414  val.val.l.alloc = val.val.l.count = g_tree_nnodes(gl_prefs) * 2;
415  val.val.l.vals = g_new0(benc_val_t, val.val.l.alloc);
416
417  ptr = val.val.l.vals;
418  g_tree_foreach(gl_prefs, writefile_traverse, &ptr);
419  g_assert(ptr - val.val.l.vals == val.val.l.alloc);
420
421  cf_writebenc(FILE_PREFS, FILE_PREFS_TMP, &val, errstr);
422  tr_bencFree(&val);
423}
424
425static gboolean
426writefile_traverse(gpointer key, gpointer value, gpointer data) {
427  benc_val_t **ptr = data;
428  benc_val_t *bkey = *ptr;
429  benc_val_t *bval = (*ptr) + 1;
430
431  *ptr = (*ptr) + 2;
432
433  bkey->type = TYPE_STR;
434  bkey->val.s.s = g_strdup(key);
435  bkey->val.s.i = strlen(key);
436
437  bval->type = TYPE_STR;
438  bval->val.s.s = g_strdup(value);
439  bval->val.s.i = strlen(value);
440
441  return FALSE;
442}
443
444static char *
445getstateval(benc_val_t *state, char *line) {
446  char *start, *end;
447
448  /* skip any leading whitespace */
449  while(g_ascii_isspace(*line))
450    line++;
451
452  /* walk over the key, which may be alphanumerics as well as - or _ */
453  for(start = line; g_ascii_isalnum(*start)
454        || '_' == *start || '-' == *start; start++)
455    ;
456
457  /* they key must be immediately followed by an = */
458  if('=' != *start)
459    return NULL;
460  *(start++) = '\0';
461
462  /* then the opening quote for the value */
463  if('"' != *(start++))
464    return NULL;
465
466  /* walk over the value */
467  for(end = start; '\0' != *end && '"' != *end; end++)
468    /* skip over escaped quotes */
469    if('\\' == *end && '\0' != *(end + 1))
470      end++;
471
472  /* make sure we didn't hit the end of the string */
473  if('"' != *end)
474    return NULL;
475  *end = '\0';
476
477  /* if it's a key we recognize then save the data */
478  if(0 == strcmp(line, "torrent") || 0 == strcmp(line, "dir") ||
479     0 == strcmp(line, "paused")) {
480    cf_benc_append(state, TYPE_STR, 6);
481    state->val.l.vals[state->val.l.count-1].val.s.s = g_strdup(line);
482    state->val.l.vals[state->val.l.count-1].val.s.i = strlen(line);
483    if('p' == *line) {
484      cf_benc_append(state, TYPE_INT, 6);
485      state->val.l.vals[state->val.l.count-1].val.i = strbool(start);
486    } else {
487      cf_benc_append(state, TYPE_STR, 6);
488      state->val.l.vals[state->val.l.count-1].val.s.s = g_strdup(start);
489      state->val.l.vals[state->val.l.count-1].val.s.i = strlen(start);
490    }
491  }
492
493  /* return a pointer to just past the end of the value */
494  return end + 1;
495}
496
497void
498cf_savestate(benc_val_t *state, char **errstr) {
499  *errstr = NULL;
500  cf_writebenc(FILE_STATE, FILE_STATE_TMP, state, errstr);
501}
502
503void
504cf_freestate( benc_val_t * state )
505{
506    if( NULL != state )
507    {
508        tr_bencFree( state );
509        g_free( state );
510    }
511}
Note: See TracBrowser for help on using the repository browser.