File: storage.c

package info (click to toggle)
libfprint 1%3A1.94.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 27,716 kB
  • sloc: ansic: 70,852; python: 2,228; xml: 318; sh: 234; makefile: 16; cpp: 11
file content (354 lines) | stat: -rw-r--r-- 8,793 bytes parent folder | download | duplicates (2)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 * Trivial storage driver for example programs
 *
 * Copyright (C) 2019 Benjamin Berg <bberg@redhat.com>
 * Copyright (C) 2019-2020 Marco Trevisan <marco.trevisan@canonical.com>
 *
 * 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#define FP_COMPONENT "example-storage"

#include <libfprint/fprint.h>
#include <libfprint/fpi-compat.h>
#include "storage.h"

#include <errno.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define STORAGE_FILE "test-storage.variant"

static char *
get_print_data_descriptor (FpPrint *print, FpDevice *dev, FpFinger finger)
{
  const char *driver;
  const char *dev_id;

  if (print)
    {
      driver = fp_print_get_driver (print);
      dev_id = fp_print_get_device_id (print);
    }
  else
    {
      driver = fp_device_get_driver (dev);
      dev_id = fp_device_get_device_id (dev);
    }

  return g_strdup_printf ("%s/%s/%x",
                          driver,
                          dev_id,
                          finger);
}

static char *
get_print_prefix_for_device (FpDevice *dev)
{
  const char *driver;
  const char *dev_id;

  driver = fp_device_get_driver (dev);
  dev_id = fp_device_get_device_id (dev);

  return g_strdup_printf ("%s/%s/", driver, dev_id);
}

static GVariantDict *
load_data (void)
{
  GVariantDict *res;
  GVariant *var;
  gchar *contents = NULL;
  gsize length = 0;

  if (!g_file_get_contents (STORAGE_FILE, &contents, &length, NULL))
    {
      g_warning ("Error loading storage, assuming it is empty");
      return g_variant_dict_new (NULL);
    }

  var = g_variant_new_from_data (G_VARIANT_TYPE_VARDICT,
                                 contents,
                                 length,
                                 FALSE,
                                 g_free,
                                 contents);

  res = g_variant_dict_new (var);
  g_variant_unref (var);
  return res;
}

static int
save_data (GVariant *data)
{
  const gchar *contents = NULL;
  gsize length;

  length = g_variant_get_size (data);
  contents = (gchar *) g_variant_get_data (data);

  if (!g_file_set_contents (STORAGE_FILE, contents, length, NULL))
    {
      g_warning ("Error saving storage,!");
      return -1;
    }

  g_variant_ref_sink (data);
  g_variant_unref (data);

  return 0;
}

static FpPrint *
load_print_from_data (GVariant *data)
{
  const guchar *stored_data = NULL;
  gsize stored_len;
  FpPrint *print;

  g_autoptr(GError) error = NULL;
  stored_data = (const guchar *) g_variant_get_fixed_array (data, &stored_len, 1);
  print = fp_print_deserialize (stored_data, stored_len, &error);
  if (error)
    g_warning ("Error deserializing data: %s", error->message);
  return print;
}

int
print_data_save (FpPrint *print, FpFinger finger, gboolean update_fingerprint)
{
  g_autofree gchar *descr = get_print_data_descriptor (print, NULL, finger);

  g_autoptr(GError) error = NULL;
  g_autoptr(GVariantDict) dict = NULL;
  g_autofree guchar *data = NULL;
  GVariant *val;
  gsize size;
  int res;

  dict = load_data ();

  fp_print_serialize (print, &data, &size, &error);
  if (error)
    {
      g_warning ("Error serializing data: %s", error->message);
      return -1;
    }
  val = g_variant_new_fixed_array (G_VARIANT_TYPE ("y"), data, size, 1);
  g_variant_dict_insert_value (dict, descr, val);

  res = save_data (g_variant_dict_end (dict));

  return res;
}

FpPrint *
print_data_load (FpDevice *dev, FpFinger finger)
{
  g_autofree gchar *descr = get_print_data_descriptor (NULL, dev, finger);

  g_autoptr(GVariant) val = NULL;
  g_autoptr(GVariantDict) dict = NULL;

  dict = load_data ();
  val = g_variant_dict_lookup_value (dict, descr, G_VARIANT_TYPE ("ay"));

  if (val)
    return load_print_from_data (val);

  return NULL;
}

GPtrArray *
gallery_data_load (FpDevice *dev)
{
  g_autoptr(GVariantDict) dict = NULL;
  g_autoptr(GVariant) dict_variant = NULL;
  g_autofree char *dev_prefix = NULL;
  GPtrArray *gallery;
  GVariantIter iter;
  GVariant *value;
  gchar *key;

  gallery = g_ptr_array_new_with_free_func (g_object_unref);
  dict = load_data ();
  dict_variant = g_variant_dict_end (dict);
  dev_prefix = get_print_prefix_for_device (dev);

  g_variant_iter_init (&iter, dict_variant);
  while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
    {
      FpPrint *print;
      const guchar *stored_data;
      g_autoptr(GError) error = NULL;
      gsize stored_len;

      if (!g_str_has_prefix (key, dev_prefix))
        continue;

      stored_data = (const guchar *) g_variant_get_fixed_array (value, &stored_len, 1);
      print = fp_print_deserialize (stored_data, stored_len, &error);

      if (error)
        {
          g_warning ("Error deserializing data: %s", error->message);
          continue;
        }

      g_ptr_array_add (gallery, print);
    }

  return gallery;
}

gboolean
clear_saved_prints (FpDevice *dev,
                    GError  **error)
{
  g_autoptr(GVariantDict) dict = NULL;
  g_autoptr(GVariantDict) updated_dict = NULL;
  g_autoptr(GVariant) dict_variant = NULL;
  g_autofree char *dev_prefix = NULL;
  GPtrArray *print_keys;
  GVariantIter iter;
  GVariant *value;
  gchar *key;

  print_keys = g_ptr_array_new_with_free_func (g_free);
  dict = load_data ();
  dict_variant = g_variant_dict_end (dict);
  dev_prefix = get_print_prefix_for_device (dev);

  g_variant_iter_init (&iter, dict_variant);
  while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
    {
      if (!g_str_has_prefix (key, dev_prefix))
        continue;

      g_ptr_array_add (print_keys, g_strdup (key));
    }

  if (!print_keys->len)
    return TRUE;

  updated_dict = load_data ();

  for (guint i = 0; i < print_keys->len; ++i)
    {
      key = g_ptr_array_index (print_keys, i);
      if (!g_variant_dict_remove (updated_dict, key))
        {
          g_warning ("Print '%s' key not found!", key);
          continue;
        }

      g_debug ("Dropping print '%s' from gallery", key);
    }

  save_data (g_variant_dict_end (updated_dict));

  return TRUE;
}

FpPrint *
print_create_template (FpDevice *dev, FpFinger finger, gboolean load_existing)
{
  g_autoptr(GVariantDict) dict = NULL;
  g_autoptr(GDateTime) datetime = NULL;
  g_autoptr(GDate) date = NULL;
  g_autoptr(GVariant) existing_val = NULL;
  g_autofree gchar *descr = get_print_data_descriptor (NULL, dev, finger);
  FpPrint *template = NULL;
  gint year, month, day;

  if (load_existing)
    {
      dict = load_data ();
      existing_val = g_variant_dict_lookup_value (dict, descr, G_VARIANT_TYPE ("ay"));
      if (existing_val != NULL)
        template = load_print_from_data (existing_val);
    }
  if (template == NULL)
    {
      template = fp_print_new (dev);
      fp_print_set_finger (template, finger);
      fp_print_set_username (template, g_get_user_name ());
    }

  datetime = g_date_time_new_now_local ();
  g_date_time_get_ymd (datetime, &year, &month, &day);
  date = g_date_new_dmy (day, month, year);
  fp_print_set_enroll_date (template, date);

  return template;
}


gboolean
save_image_to_pgm (FpImage *img, const char *path)
{
  FILE *fd = fopen (path, "w");
  size_t write_size;
  const guchar *data = fp_image_get_data (img, &write_size);
  int r;

  if (!fd)
    {
      g_warning ("could not open '%s' for writing: %d", path, errno);
      return FALSE;
    }

  r = fprintf (fd, "P5 %d %d 255\n",
               fp_image_get_width (img), fp_image_get_height (img));
  if (r < 0)
    {
      fclose (fd);
      g_critical ("pgm header write failed, error %d", r);
      return FALSE;
    }

  r = fwrite (data, 1, write_size, fd);
  if (r < write_size)
    {
      fclose (fd);
      g_critical ("short write (%d)", r);
      return FALSE;
    }

  fclose (fd);
  g_debug ("written to '%s'", path);

  return TRUE;
}

gboolean
print_image_save (FpPrint *print, const char *path)
{
  FpImage *img = NULL;

  g_return_val_if_fail (FP_IS_PRINT (print), FALSE);
  g_return_val_if_fail (path != NULL, FALSE);

  img = fp_print_get_image (print);

  if (img)
    return save_image_to_pgm (img, path);

  return FALSE;
}