File: games-scores-backend.c

package info (click to toggle)
gweled 0.9.1-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,584 kB
  • sloc: ansic: 4,170; sh: 2,083; makefile: 104
file content (336 lines) | stat: -rw-r--r-- 8,893 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* games-scores-backend.c
 *
 * Copyright (C) 2005 Callum McKenzie
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>

#include <glib.h>
#include <glib-object.h>

#include <string.h>
#include <stdlib.h>

#include "games-score.h"
#include "games-scores.h"
#include "games-scores-backend.h"


#ifdef ENABLE_SETGID
#include "games-setgid-io.h"
#endif

struct _GamesScoresBackendPrivate {
  GamesScoreStyle style;
  time_t timestamp;
  gchar *filename;
  gint fd;
};

G_DEFINE_TYPE (GamesScoresBackend, games_scores_backend, G_TYPE_OBJECT);

static void
games_scores_backend_finalize (GObject *object)
{
  GamesScoresBackend *backend = GAMES_SCORES_BACKEND (object);
  GamesScoresBackendPrivate *priv = backend->priv;

  g_free (priv->filename);
  /* FIXME: more to do? */

  G_OBJECT_CLASS (games_scores_backend_parent_class)->finalize (object);
}

static void
games_scores_backend_class_init (GamesScoresBackendClass * klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);

  g_type_class_add_private (klass, sizeof (GamesScoresBackendPrivate));
  oclass->finalize = games_scores_backend_finalize;
}

static void
games_scores_backend_init (GamesScoresBackend * backend)
{
  backend->priv = G_TYPE_INSTANCE_GET_PRIVATE (backend,
					       GAMES_TYPE_SCORES_BACKEND,
					       GamesScoresBackendPrivate);
}

GamesScoresBackend *
games_scores_backend_new (GamesScoreStyle style,
			  char *base_name,
                          char *name)
{
  GamesScoresBackend *backend;
  gchar *fullname;

  backend = GAMES_SCORES_BACKEND (g_object_new (GAMES_TYPE_SCORES_BACKEND,
						NULL));

  if (name[0] == '\0')		/* Name is "" */
    fullname = g_strjoin (".", base_name, "scores", NULL);
  else
    fullname = g_strjoin (".", base_name, name, "scores", NULL);

  backend->priv->timestamp = 0;
  backend->priv->style = style;
  backend->scores_list = NULL;
  backend->priv->filename = g_build_filename (SCOREDIR,
                                              fullname, NULL);
  g_free (fullname);

  backend->priv->fd = -1;

  return backend;
}

#ifdef ENABLE_SETGID

/* Get a lock on the scores file. Block until it is available.
 * This also supplies the file descriptor we need. The return value
 * is whether we were succesful or not. */
static gboolean
games_scores_backend_get_lock (GamesScoresBackend * self)
{
  gint error;

  if (self->priv->fd != -1) {
    /* Assume we already have the lock and rewind the file to
     * the beginning. */
    setgid_io_seek (self->priv->fd, 0, SEEK_SET);
    return TRUE;		/* Assume we already have the lock. */
  }

  self->priv->fd = setgid_io_open (self->priv->filename, O_RDWR);
  if (self->priv->fd == -1) {
    return FALSE;
  }

  error = setgid_io_lock (self->priv->fd);

  if (error == -1) {
    setgid_io_close (self->priv->fd);
    self->priv->fd = -1;
    return FALSE;
  }

  return TRUE;
}

/* Release the lock on the scores file and dispose of the fd. */
/* We ignore errors, there is nothing we can do about them. */
static void
games_scores_backend_release_lock (GamesScoresBackend * self)
{
  /* We don't have a lock, ignore this call. */
  if (self->priv->fd == -1)
    return;

  setgid_io_unlock (self->priv->fd);

  setgid_io_close (self->priv->fd);

  self->priv->fd = -1;
}

#endif /* ENABLE_SETGID */

/* You can alter the list returned by this function, but you must
 * make sure you set it again with the _set_scores method or discard it
 * with with the _discard_scores method. Otherwise deadlocks will ensue. */
GList *
games_scores_backend_get_scores (GamesScoresBackend * self)
{
#ifdef ENABLE_SETGID
  gchar *buffer;
  gchar *eol;
  gchar *scorestr;
  gchar *timestr;
  gchar *namestr;
  GamesScore *newscore;
  struct stat info;
  int error;
  ssize_t length, target;
  GList *t;

  /* Check for a change in the scores file and update if necessary. */
  error = setgid_io_stat (self->priv->filename, &info);

  /* If an error occurs then we give up on the file and return NULL. */
  if (error != 0)
    return NULL;

  if ((info.st_mtime > self->priv->timestamp) || (self->scores_list == NULL)) {
    self->priv->timestamp = info.st_mtime;

    /* Dump the old list of scores. */
    t = self->scores_list;
    while (t != NULL) {
      games_score_destroy ((GamesScore *) t->data);
      t = g_list_next (t);
    }
    g_list_free (self->scores_list);
    self->scores_list = NULL;

    /* Lock the file and get the list. */
    if (!games_scores_backend_get_lock (self))
      return NULL;

    buffer = g_malloc (info.st_size + 1);
    if (buffer == NULL) {
      games_scores_backend_release_lock (self);
      return NULL;
    }

    target = info.st_size;
    length = 0;
    do {
      target -= length;
      length = setgid_io_read (self->priv->fd, buffer, info.st_size);
      if (length == -1) {
	games_scores_backend_release_lock (self);
	g_free (buffer);
	return NULL;
      }
    } while (length < target);

    buffer[info.st_size] = '\0';

    /* FIXME: These details should be in a sub-class. */

    /* Parse the list. We start by breaking it into lines. */
    /* Since the buffer is null-terminated
     * we can do the string stuff reasonably safely. */
    eol = strchr (buffer, '\n');
    scorestr = buffer;
    while (eol != NULL) {
      *eol++ = '\0';
      timestr = strchr (scorestr, ' ');
      if (timestr == NULL)
	break;
      *timestr++ = '\0';
      namestr = strchr (timestr, ' ');
      if (namestr == NULL)
	break;
      *namestr++ = '\0';
      /* At this point we have three strings, all null terminated. All
       * part of the original buffer. */
      newscore = games_score_new ();
      newscore->name = g_strdup (namestr);
      newscore->time = g_ascii_strtoull (timestr, NULL, 10);
      switch (self->priv->style) {
      case GAMES_SCORES_STYLE_PLAIN_DESCENDING:
      case GAMES_SCORES_STYLE_PLAIN_ASCENDING:
	newscore->value.plain = g_ascii_strtod (scorestr, NULL);
	break;
      case GAMES_SCORES_STYLE_TIME_DESCENDING:
      case GAMES_SCORES_STYLE_TIME_ASCENDING:
	newscore->value.time_double = g_ascii_strtod (scorestr, NULL);
	break;
      default:
        g_assert_not_reached ();
      }
      self->scores_list = g_list_append (self->scores_list, newscore);
      /* Setup again for the next time around. */
      scorestr = eol;
      eol = strchr (eol, '\n');
    }

    g_free (buffer);
  }

  /* FIXME: Sort the scores! We shouldn't rely on the file being sorted. */

  return self->scores_list;
#else
  return NULL;
#endif /* ENABLE_SETGID */
}

gboolean
games_scores_backend_set_scores (GamesScoresBackend * self, GList * list)
{
#ifdef ENABLE_SETGID
  GList *s;
  GamesScore *d;
  gchar *buffer;
  gint output_length = 0;
  gchar dtostrbuf[G_ASCII_DTOSTR_BUF_SIZE];

  if (!games_scores_backend_get_lock (self))
    return FALSE;

  self->scores_list = list;

  s = list;
  while (s != NULL) {
    gdouble rscore;
    guint64 rtime;
    gchar *rname;

    d = (GamesScore *) s->data;
    rscore = 0.0;
    switch (self->priv->style) {
    case GAMES_SCORES_STYLE_PLAIN_DESCENDING:
    case GAMES_SCORES_STYLE_PLAIN_ASCENDING:
      rscore = d->value.plain;
      break;
    case GAMES_SCORES_STYLE_TIME_DESCENDING:
    case GAMES_SCORES_STYLE_TIME_ASCENDING:
      rscore = d->value.time_double;
      break;
    default:
      g_assert_not_reached ();
    }
    rtime = d->time;
    rname = d->name;

    buffer = g_strdup_printf ("%s %lld %s\n",
			      g_ascii_dtostr (dtostrbuf, sizeof (dtostrbuf),
					      rscore), rtime, rname);
    setgid_io_write (self->priv->fd, buffer, strlen (buffer));
    output_length += strlen (buffer);
    /* Ignore any errors and blunder on. */
    g_free (buffer);

    s = g_list_next (s);
  }

  /* Remove any content in the file that hasn't yet been overwritten. */
  setgid_io_truncate (self->priv->fd, output_length--);

  /* Update the timestamp so we don't reread the scores unnecessarily. */
  self->priv->timestamp = time (NULL);

  games_scores_backend_release_lock (self);

  return TRUE;
#else
  return FALSE;
#endif /* ENABLE_SETGID */
}

void
games_scores_backend_discard_scores (GamesScoresBackend * self)
{
#ifdef ENABLE_SETGID
  games_scores_backend_release_lock (self);
#endif
}