-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfprintd-load-store-persistent-data-from-device.patch
More file actions
227 lines (211 loc) · 7.35 KB
/
Copy pathfprintd-load-store-persistent-data-from-device.patch
File metadata and controls
227 lines (211 loc) · 7.35 KB
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
diff --git a/src/device.c b/src/device.c
index b9de3ee..63b4fef 100644
--- a/src/device.c
+++ b/src/device.c
@@ -22,6 +22,7 @@
#include <glib/gi18n.h>
#include <gio/gio.h>
+#include <glib.h>
#include <polkit/polkit.h>
#include <fprint.h>
@@ -232,17 +233,23 @@ G_DEFINE_AUTOPTR_CLEANUP_FUNC (FprintDeviceActionUnset, auto_device_action_unset
static void
fprint_device_dispose (GObject *object)
{
+
+ g_autoptr(GError) error = NULL;
FprintDevice *self = (FprintDevice *) object;
FprintDevicePrivate *priv = fprint_device_get_instance_private (self);
g_hash_table_remove_all (priv->clients);
+ if (!store.persistent_data_save (priv->dev, &error))
+ g_warning ("Failed to save persistent data: %s", error->message);
+
G_OBJECT_CLASS (fprint_device_parent_class)->dispose (object);
}
static void
fprint_device_finalize (GObject *object)
{
+
FprintDevice *self = (FprintDevice *) object;
FprintDevicePrivate *priv = fprint_device_get_instance_private (self);
@@ -374,6 +381,8 @@ on_temperature_changed (FprintDevice *rdev,
static void
fprint_device_constructed (GObject *object)
{
+
+ g_autoptr(GError) error = NULL;
FprintDevice *rdev = FPRINT_DEVICE (object);
FprintDBusDevice *dbus_dev = FPRINT_DBUS_DEVICE (rdev);
FprintDevicePrivate *priv = fprint_device_get_instance_private (rdev);
@@ -400,6 +409,9 @@ fprint_device_constructed (GObject *object)
rdev, G_CONNECT_SWAPPED);
on_temperature_changed (rdev, NULL, priv->dev);
+ if (!store.persistent_data_load (priv->dev, &error))
+ g_warning ("Failed to load persistent data: %s", error->message);
+
G_OBJECT_CLASS (fprint_device_parent_class)->constructed (object);
}
diff --git a/src/file_storage.c b/src/file_storage.c
index e12fe58..87f1f3b 100644
--- a/src/file_storage.c
+++ b/src/file_storage.c
@@ -39,7 +39,10 @@
#include "file_storage.h"
-#define FILE_STORAGE_PATH "/var/lib/fprint"
+// #define FILE_STORAGE_PATH "/var/lib/fprint"
+#define FILE_STORAGE_PATH "/home/vojtapl/test"
+/* Starts with a number as that is not valid in a username */
+#define PERSISTENT_DATA_DIR "0-persistent"
#define DIR_PERMS 0700
static char *storage_path = NULL;
@@ -353,9 +356,80 @@ file_storage_discover_users (void)
return list;
}
+gboolean
+file_storage_persistent_data_save (FpDevice *dev, GError **error)
+{
+ g_critical("Save called!");
+ g_autofree gchar *dir = NULL;
+ g_autofree gchar *path = NULL;
+ g_autofree guint8 *cur_contents = NULL;
+ g_autofree guint8 *new_contents = NULL;
+ gsize cur_length = 0;
+ gsize new_length = 0;
+
+ if (!fp_device_get_persistent_data (dev, &new_contents, &new_length, error))
+ return FALSE;
+
+ dir = g_build_filename (get_storage_path (), PERSISTENT_DATA_DIR, fp_device_get_driver (dev), NULL);
+ path = g_build_filename (dir, fp_device_get_device_id (dev), NULL);
+
+ if (new_length == 0)
+ {
+ if (g_unlink (path) < 0)
+ {
+ if (errno == ENOENT)
+ return TRUE;
+
+ g_set_error (error,
+ G_IO_ERROR,
+ g_io_error_from_errno (errno),
+ "Failed to delete persistent storage for driver/device %s/%s", fp_device_get_driver (dev), fp_device_get_device_id (dev));
+ return FALSE;
+ }
+ return TRUE;
+ }
+
+ if (g_mkdir_with_parents (dir, DIR_PERMS) < 0)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ g_io_error_from_errno (errno),
+ "Failed to create directory for persistent data storage");
+ return FALSE;
+ }
+
+ /* Try to load to avoid writing if that is nothing changed (ignore errors) */
+ g_file_get_contents (path, (char **) &cur_contents, &cur_length, NULL);
+
+ /* Nothing needs to be written. */
+ if (new_length == cur_length && memcmp (new_contents, cur_contents, cur_length) == 0)
+ return TRUE;
+
+ /* Pass over to device */
+ return g_file_set_contents (path, new_contents, new_length, error);
+}
+
+gboolean
+file_storage_persistent_data_load (FpDevice *dev, GError **error)
+{
+ g_critical("Load called!");
+ g_autofree gchar *path = NULL;
+ g_autofree gchar *contents;
+ gsize length;
+
+ path = g_build_filename (get_storage_path (), PERSISTENT_DATA_DIR, fp_device_get_driver (dev), fp_device_get_device_id (dev), NULL);
+
+ if (!g_file_get_contents (path, &contents, &length, error))
+ return FALSE;
+
+ /* Pass over to device */
+ return fp_device_set_persistent_data (dev, contents, length, error);
+}
+
int
file_storage_init (void)
{
+ g_critical("Init called!");
/* Nothing to do */
return 0;
}
@@ -363,6 +437,7 @@ file_storage_init (void)
int
file_storage_deinit (void)
{
+ g_critical("Deinit called!");
g_clear_pointer (&storage_path, g_free);
return 0;
}
diff --git a/src/file_storage.h b/src/file_storage.h
index 29e70df..37a5881 100644
--- a/src/file_storage.h
+++ b/src/file_storage.h
@@ -31,6 +31,11 @@ int file_storage_print_data_delete (FpDevice *dev,
FpFinger finger,
const char *username);
+gboolean file_storage_persistent_data_save (FpDevice *dev,
+ GError **error);
+gboolean file_storage_persistent_data_load (FpDevice *dev,
+ GError **error);
+
int file_storage_init (void);
int file_storage_deinit (void);
diff --git a/src/main.c b/src/main.c
index eba2f4b..4778c5f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -49,6 +49,8 @@ set_storage_file (void)
store.print_data_save = &file_storage_print_data_save;
store.print_data_load = &file_storage_print_data_load;
store.print_data_delete = &file_storage_print_data_delete;
+ store.persistent_data_save = &file_storage_persistent_data_save;
+ store.persistent_data_load = &file_storage_persistent_data_load;
store.discover_prints = &file_storage_discover_prints;
store.discover_users = &file_storage_discover_users;
}
diff --git a/src/storage.h b/src/storage.h
index ef25a84..6dc11ae 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -31,18 +31,26 @@ typedef int (*storage_print_data_delete)(FpDevice *dev,
typedef GSList *(*storage_discover_prints)(FpDevice *dev,
const char *username);
typedef GSList *(*storage_discover_users)(void);
+
+typedef int (*storage_persistent_data_save)(FpDevice *dev,
+ GError **error);
+typedef int (*storage_persistent_data_load)(FpDevice *dev,
+ GError **error);
+
typedef int (*storage_init)(void);
typedef int (*storage_deinit)(void);
struct storage
{
- storage_init init;
- storage_deinit deinit;
- storage_print_data_save print_data_save;
- storage_print_data_load print_data_load;
- storage_print_data_delete print_data_delete;
- storage_discover_prints discover_prints;
- storage_discover_users discover_users;
+ storage_init init;
+ storage_deinit deinit;
+ storage_print_data_save print_data_save;
+ storage_print_data_load print_data_load;
+ storage_print_data_delete print_data_delete;
+ storage_persistent_data_save persistent_data_save;
+ storage_persistent_data_load persistent_data_load;
+ storage_discover_prints discover_prints;
+ storage_discover_users discover_users;
};
typedef struct storage fp_storage;