From f44233461f5c764ed8a680b38629ca1c1ab49e4f Mon Sep 17 00:00:00 2001 From: Vincent Huang Date: Wed, 10 Mar 2021 17:16:37 +0800 Subject: [PATCH] device: Clear storage before enrolling the first print Clear the device storage before we enroll the first print. At that point, we know that the storage should be completely empty and we have no way of deleting "garbage" prints later if the device does not support listing prints. --- src/device.c | 23 +++++++++++++++++++++-- tests/fprintd.py | 16 +++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/device.c b/src/device.c index 822446a..2d6723d 100644 --- a/src/device.c +++ b/src/device.c @@ -1797,8 +1797,9 @@ enroll_cb (FpDevice *dev, GAsyncResult *res, void *user_data) * enough. */ if (g_error_matches (error, FP_DEVICE_ERROR, FP_DEVICE_ERROR_DATA_FULL)) { - g_debug ("Device storage is full, trying to garbage collect old prints"); - if (try_delete_print (rdev)) + g_debug ("Device storage is full"); + if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_STORAGE_LIST) && + try_delete_print (rdev)) { /* Success? Then restart the operation */ fp_device_enroll (priv->dev, @@ -1939,6 +1940,15 @@ enroll_identify_cb (FpDevice *dev, GAsyncResult *res, void *user_data) enroll_start (rdev); } +static gboolean +is_first_enrollment (FprintDevice * rdev) +{ + g_autoptr(GPtrArray) host_prints = NULL; + host_prints = load_all_prints (rdev); + + return host_prints->len == 0; +} + static gboolean fprint_device_enroll_start (FprintDBusDevice *dbus_dev, GDBusMethodInvocation *invocation, @@ -1991,6 +2001,15 @@ fprint_device_enroll_start (FprintDBusDevice *dbus_dev, priv->enroll_data = finger; priv->current_action = ACTION_ENROLL; + if (is_first_enrollment (rdev)) + { + g_autoptr(GError) clear_err = NULL; + + if (!fp_device_clear_storage_sync (priv->dev, NULL, &clear_err)) + g_warning ("Failed to clear storage before first enrollment: %s", + clear_err->message); + } + if (fp_device_has_feature (priv->dev, FP_DEVICE_FEATURE_IDENTIFY)) { g_autoptr(GPtrArray) all_prints = load_all_prints (rdev); diff --git a/tests/fprintd.py b/tests/fprintd.py index b3366d9..2517d8e 100644 --- a/tests/fprintd.py +++ b/tests/fprintd.py @@ -850,7 +850,7 @@ class FPrintdVirtualStorageDeviceBaseTest(FPrintdVirtualDeviceBaseTest): self.assertIn(command, ['INSERT', 'REMOVE', 'SCAN', 'ERROR', 'RETRY', 'FINGER', 'UNPLUG', 'SLEEP', 'SET_ENROLL_STAGES', 'SET_SCAN_TYPE', 'SET_CANCELLATION_ENABLED', 'LIST', 'IGNORED_COMMAND', - 'SET_KEEP_ALIVE']) + 'SET_KEEP_ALIVE', 'CONT']) with Connection(self.sockaddr) as con: res = self._send_command(con, command, *args) @@ -935,6 +935,8 @@ class FPrintdVirtualStorageDeviceTests(FPrintdVirtualStorageDeviceBaseTest): 'FP1-20201231-7-ABCDEFGH-testuser' : 'right-index-finger', 'no-metadata-new' : 'left-middle-finger', } + # Explicitly fail initial cleanup (not needed, but faster) + self.send_command('ERROR', 0) for i, f in enrolled_prints.items(): self.enroll_print(i, f) @@ -973,6 +975,8 @@ class FPrintdVirtualStorageDeviceTests(FPrintdVirtualStorageDeviceBaseTest): self.device.Claim('(s)', 'testuser') self.assertEqual(self.get_stored_prints(), ['stored-print']) + # Explicitly fail initial cleanup (not needed, but faster) + self.send_command('ERROR', 0) self.device.EnrollStart('(s)', 'right-thumb') self.send_image('stored-print') # During identify self.wait_for_result('enroll-stage-passed') @@ -990,6 +994,8 @@ class FPrintdVirtualStorageDeviceTests(FPrintdVirtualStorageDeviceBaseTest): self.device.Claim('(s)', 'testuser') self.assertEqual(self.get_stored_prints(), ['stored-print']) + # Explicitly fail initial cleanup (not needed, but faster) + self.send_command('ERROR', 0) self.device.EnrollStart('(s)', 'right-thumb') self.send_image('stored-print') # During identify self.send_error(FPrint.DeviceError.PROTO) # During garbage collecting @@ -3065,6 +3071,10 @@ class FPrintdUtilsTest(FPrintdVirtualStorageDeviceBaseTest): self.set_keep_alive(True) self.device.Release() + # Open and clear storage + self.send_command('CONT') + self.send_command('CONT') + finger_name = self.get_finger_name(FPrint.Finger.LEFT_THUMB) enroll, out = self.util_start('enroll', [self.get_current_user(), '-f', finger_name]) @@ -3105,6 +3115,10 @@ class FPrintdUtilsTest(FPrintdVirtualStorageDeviceBaseTest): self.set_keep_alive(True) self.device.Release() + # Open and clear storage + self.send_command('CONT') + self.send_command('CONT') + finger_name = self.get_finger_name(FPrint.Finger.LEFT_MIDDLE) enroll, out = self.util_start('enroll', [self.get_current_user(), '-f', finger_name])